Files
shoelace/src/components/tooltip/tooltip.test.ts
Konnor Rogers 7891dbef93 Add missing extensions (#1770)
* fix(typescript): add missing extension to imports in typescript

This is required for the types to work with the new
`--module-resolution=node16`.

The list of places to fix was obtained by a crude script:

```sh
rg -g'**/*.ts' -g'!**/*.test.ts' ' from\s+.\.' | rg -v '\.js'
```

References #1765

* add missing extensions

* revert tsconfig

* prettier

* fix test files for NodeNext

* prettier

* changelog entry

* prettier

* prettier

* prettier

---------

Co-authored-by: Andrey Lushnikov <aslushnikov@gmail.com>
2023-12-08 12:30:31 -05:00

163 lines
5.6 KiB
TypeScript

import '../../../dist/shoelace.js';
import { expect, fixture, html, waitUntil } from '@open-wc/testing';
import sinon from 'sinon';
import type SlTooltip from './tooltip.js';
describe('<sl-tooltip>', () => {
it('should be visible with the open attribute', async () => {
const el = await fixture<SlTooltip>(html`
<sl-tooltip content="This is a tooltip" open>
<sl-button>Hover Me</sl-button>
</sl-tooltip>
`);
const body = el.shadowRoot!.querySelector<HTMLElement>('[part~="body"]')!;
expect(body.hidden).to.be.false;
});
it('should not be visible without the open attribute', async () => {
const el = await fixture<SlTooltip>(html`
<sl-tooltip content="This is a tooltip">
<sl-button>Hover Me</sl-button>
</sl-tooltip>
`);
const body = el.shadowRoot!.querySelector<HTMLElement>('[part~="body"]')!;
expect(body.hidden).to.be.true;
});
it('should emit sl-show and sl-after-show when calling show()', async () => {
const el = await fixture<SlTooltip>(html`
<sl-tooltip content="This is a tooltip">
<sl-button>Hover Me</sl-button>
</sl-tooltip>
`);
const body = el.shadowRoot!.querySelector<HTMLElement>('[part~="body"]')!;
const showHandler = sinon.spy();
const afterShowHandler = sinon.spy();
el.addEventListener('sl-show', showHandler);
el.addEventListener('sl-after-show', afterShowHandler);
el.show();
await waitUntil(() => showHandler.calledOnce);
await waitUntil(() => afterShowHandler.calledOnce);
expect(showHandler).to.have.been.calledOnce;
expect(afterShowHandler).to.have.been.calledOnce;
expect(body.hidden).to.be.false;
});
it('should emit sl-hide and sl-after-hide when calling hide()', async () => {
const el = await fixture<SlTooltip>(html`
<sl-tooltip content="This is a tooltip" open>
<sl-button>Hover Me</sl-button>
</sl-tooltip>
`);
const body = el.shadowRoot!.querySelector<HTMLElement>('[part~="body"]')!;
const hideHandler = sinon.spy();
const afterHideHandler = sinon.spy();
el.addEventListener('sl-hide', hideHandler);
el.addEventListener('sl-after-hide', afterHideHandler);
el.hide();
await waitUntil(() => hideHandler.calledOnce);
await waitUntil(() => afterHideHandler.calledOnce);
expect(hideHandler).to.have.been.calledOnce;
expect(afterHideHandler).to.have.been.calledOnce;
expect(body.hidden).to.be.true;
});
it('should emit sl-show and sl-after-show when setting open = true', async () => {
const el = await fixture<SlTooltip>(html`
<sl-tooltip content="This is a tooltip">
<sl-button>Hover Me</sl-button>
</sl-tooltip>
`);
const body = el.shadowRoot!.querySelector<HTMLElement>('[part~="body"]')!;
const showHandler = sinon.spy();
const afterShowHandler = sinon.spy();
el.addEventListener('sl-show', showHandler);
el.addEventListener('sl-after-show', afterShowHandler);
el.open = true;
await waitUntil(() => showHandler.calledOnce);
await waitUntil(() => afterShowHandler.calledOnce);
expect(showHandler).to.have.been.calledOnce;
expect(afterShowHandler).to.have.been.calledOnce;
expect(body.hidden).to.be.false;
});
it('should emit sl-hide and sl-after-hide when setting open = false', async () => {
const el = await fixture<SlTooltip>(html`
<sl-tooltip content="This is a tooltip" open>
<sl-button>Hover Me</sl-button>
</sl-tooltip>
`);
const body = el.shadowRoot!.querySelector<HTMLElement>('[part~="body"]')!;
const hideHandler = sinon.spy();
const afterHideHandler = sinon.spy();
el.addEventListener('sl-hide', hideHandler);
el.addEventListener('sl-after-hide', afterHideHandler);
el.open = false;
await waitUntil(() => hideHandler.calledOnce);
await waitUntil(() => afterHideHandler.calledOnce);
expect(hideHandler).to.have.been.calledOnce;
expect(afterHideHandler).to.have.been.calledOnce;
expect(body.hidden).to.be.true;
});
it('should hide the tooltip when tooltip is visible and disabled becomes true', async () => {
const el = await fixture<SlTooltip>(html`
<sl-tooltip content="This is a tooltip" open>
<sl-button>Hover Me</sl-button>
</sl-tooltip>
`);
const body = el.shadowRoot!.querySelector<HTMLElement>('[part~="body"]')!;
const hideHandler = sinon.spy();
const afterHideHandler = sinon.spy();
el.addEventListener('sl-hide', hideHandler);
el.addEventListener('sl-after-hide', afterHideHandler);
el.disabled = true;
await waitUntil(() => hideHandler.calledOnce);
await waitUntil(() => afterHideHandler.calledOnce);
expect(hideHandler).to.have.been.calledOnce;
expect(afterHideHandler).to.have.been.calledOnce;
expect(body.hidden).to.be.true;
});
it('should show when open initially', async () => {
const el = await fixture<SlTooltip>(html`
<sl-tooltip content="This is a tooltip" open>
<sl-button>Hover Me</sl-button>
</sl-tooltip>
`);
const body = el.shadowRoot!.querySelector<HTMLElement>('[part~="body"]')!;
await el.updateComplete;
expect(body.hidden).to.be.false;
});
it('should not accept user selection on the tooltip', async () => {
const el = await fixture<SlTooltip>(html`
<sl-tooltip content="This is a tooltip" open>
<sl-button>Hover Me</sl-button>
</sl-tooltip>
`);
const tooltipBody = el.shadowRoot!.querySelector('.tooltip__body')!;
const userSelect = getComputedStyle(tooltipBody).userSelect || getComputedStyle(tooltipBody).webkitUserSelect;
expect(userSelect).to.equal('none');
});
});