-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(sbb-tooltip): migrate tooltip and toolti-trigger
- Loading branch information
1 parent
dc981b5
commit e1a97c0
Showing
12 changed files
with
570 additions
and
543 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './sbb-tooltip-trigger'; |
124 changes: 67 additions & 57 deletions
124
src/components/sbb-tooltip-trigger/sbb-tooltip-trigger.e2e.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,112 +1,122 @@ | ||
import events from '../sbb-tooltip/sbb-tooltip.events'; | ||
import { E2EElement, E2EPage, newE2EPage } from '@stencil/core/testing'; | ||
import { waitForCondition } from '../../global/testing'; | ||
|
||
import { assert, expect, fixture } from '@open-wc/testing'; | ||
import { sendKeys } from '@web/test-runner-commands'; | ||
import { html } from 'lit/static-html.js'; | ||
import { waitForCondition, waitForLitRender } from '../../global/testing'; | ||
import { EventSpy } from '../../global/testing/event-spy'; | ||
import '../sbb-tooltip/sbb-tooltip'; | ||
import { SbbTooltipTrigger } from './sbb-tooltip-trigger'; | ||
|
||
describe('sbb-tooltip-trigger', () => { | ||
let element: E2EElement, page: E2EPage; | ||
let element: SbbTooltipTrigger; | ||
|
||
beforeEach(async () => { | ||
page = await newE2EPage(); | ||
await page.setContent(` | ||
await fixture(html` | ||
<sbb-tooltip-trigger id="tooltip-trigger"></sbb-tooltip-trigger> | ||
<sbb-tooltip id="tooltip" trigger="tooltip-trigger" disable-animation> | ||
Tooltip content. <sbb-link id="tooltip-link" variant="inline" sbb-tooltip-close>Link</sbb-link> | ||
Tooltip content. | ||
<sbb-link id="tooltip-link" variant="inline" sbb-tooltip-close>Link</sbb-link> | ||
</sbb-tooltip> | ||
`); | ||
element = await page.find('sbb-tooltip-trigger'); | ||
await page.waitForChanges(); | ||
element = document.querySelector('sbb-tooltip-trigger'); | ||
await element.updateComplete; | ||
}); | ||
|
||
it('renders', () => { | ||
expect(element).toHaveClass('hydrated'); | ||
assert.instanceOf(element, SbbTooltipTrigger); | ||
}); | ||
|
||
it('shows tooltip on tooltip-trigger click', async () => { | ||
const dialog = await page.find('sbb-tooltip >>> dialog'); | ||
const willOpenEventSpy = await page.spyOnEvent(events.willOpen); | ||
const didOpenEventSpy = await page.spyOnEvent(events.didOpen); | ||
// NOTE: the ">>>" operator is not supported outside stencil. (convert it to something like "element.shadowRoot.querySelector(...)") | ||
const dialog = document.querySelector('sbb-tooltip').shadowRoot.querySelector('dialog'); | ||
const willOpenEventSpy = new EventSpy(events.willOpen); | ||
const didOpenEventSpy = new EventSpy(events.didOpen); | ||
|
||
await page.waitForChanges(); | ||
await element.updateComplete; | ||
await element.click(); | ||
|
||
await page.waitForChanges(); | ||
await element.updateComplete; | ||
await waitForCondition(() => willOpenEventSpy.events.length === 1); | ||
expect(willOpenEventSpy).toHaveReceivedEventTimes(1); | ||
expect(willOpenEventSpy.count).to.be.equal(1); | ||
|
||
await page.waitForChanges(); | ||
await element.updateComplete; | ||
await waitForCondition(() => didOpenEventSpy.events.length === 1); | ||
expect(didOpenEventSpy).toHaveReceivedEventTimes(1); | ||
expect(didOpenEventSpy.count).to.be.equal(1); | ||
|
||
await page.waitForChanges(); | ||
expect(dialog).toHaveAttribute('open'); | ||
await element.updateComplete; | ||
expect(dialog).to.have.attribute('open'); | ||
}); | ||
|
||
it("doesn't show tooltip on disabled tooltip-trigger click", async () => { | ||
const dialog = await page.find('sbb-tooltip >>> dialog'); | ||
const willOpenEventSpy = await page.spyOnEvent(events.willOpen); | ||
element.setProperty('disabled', true); | ||
// NOTE: the ">>>" operator is not supported outside stencil. (convert it to something like "element.shadowRoot.querySelector(...)") | ||
const dialog = document.querySelector('sbb-tooltip').shadowRoot.querySelector('dialog'); | ||
const willOpenEventSpy = new EventSpy(events.willOpen); | ||
element.disabled = true; | ||
|
||
await page.waitForChanges(); | ||
await element.updateComplete; | ||
await element.click(); | ||
|
||
await page.waitForChanges(); | ||
await element.updateComplete; | ||
await waitForCondition(() => willOpenEventSpy.events.length === 0); | ||
expect(willOpenEventSpy).toHaveReceivedEventTimes(0); | ||
expect(willOpenEventSpy.count).to.be.equal(0); | ||
|
||
await page.waitForChanges(); | ||
expect(dialog).not.toHaveAttribute('open'); | ||
await element.updateComplete; | ||
expect(dialog).not.to.have.attribute('open'); | ||
}); | ||
|
||
it('shows tooltip on keyboard event', async () => { | ||
const tooltipTrigger = await page.find('sbb-tooltip-trigger'); | ||
const dialog = await page.find('sbb-tooltip >>> dialog'); | ||
const changeSpy = await tooltipTrigger.spyOnEvent('focus'); | ||
const tooltipTrigger = document.querySelector('sbb-tooltip-trigger'); | ||
// NOTE: the ">>>" operator is not supported outside stencil. (convert it to something like "element.shadowRoot.querySelector(...)") | ||
const dialog = document.querySelector('sbb-tooltip').shadowRoot.querySelector('dialog'); | ||
const changeSpy = new EventSpy('focus', tooltipTrigger); | ||
|
||
await tooltipTrigger.focus(); | ||
await page.waitForChanges(); | ||
await element.updateComplete; | ||
await waitForCondition(() => changeSpy.events.length === 1); | ||
expect(changeSpy).toHaveReceivedEventTimes(1); | ||
expect(changeSpy.count).to.be.equal(1); | ||
|
||
await page.keyboard.down('Enter'); | ||
await page.waitForChanges(); | ||
await sendKeys({ down: 'Enter' }); | ||
await element.updateComplete; | ||
|
||
expect(dialog).toHaveAttribute('open'); | ||
expect(dialog).to.have.attribute('open'); | ||
}); | ||
|
||
it('shows tooltip on keyboard event with hover-trigger', async () => { | ||
const tooltipTrigger = await page.find('sbb-tooltip-trigger'); | ||
const tooltip = await page.find('sbb-tooltip'); | ||
const dialog = await page.find('sbb-tooltip >>> dialog'); | ||
const changeSpy = await tooltipTrigger.spyOnEvent('focus'); | ||
const tooltipTrigger = document.querySelector('sbb-tooltip-trigger'); | ||
const tooltip = document.querySelector('sbb-tooltip'); | ||
// NOTE: the ">>>" operator is not supported outside stencil. (convert it to something like "element.shadowRoot.querySelector(...)") | ||
const dialog = document.querySelector('sbb-tooltip').shadowRoot.querySelector('dialog'); | ||
const changeSpy = new EventSpy('focus', tooltipTrigger); | ||
|
||
tooltip.setProperty('hoverTrigger', true); | ||
await page.waitForChanges(); | ||
tooltip.hoverTrigger = true; | ||
await element.updateComplete; | ||
|
||
await tooltipTrigger.focus(); | ||
await page.waitForChanges(); | ||
await element.updateComplete; | ||
await waitForCondition(() => changeSpy.events.length === 1); | ||
expect(changeSpy).toHaveReceivedEventTimes(1); | ||
expect(changeSpy.count).to.be.equal(1); | ||
|
||
await page.keyboard.down('Enter'); | ||
await page.waitForChanges(); | ||
await sendKeys({ down: 'Enter' }); | ||
await element.updateComplete; | ||
|
||
expect(dialog).toHaveAttribute('open'); | ||
expect(dialog).to.have.attribute('open'); | ||
}); | ||
|
||
it("doesn't focus tooltip-trigger on keyboard event when disabled", async () => { | ||
const tooltipTrigger = await page.find('sbb-tooltip-trigger'); | ||
const tooltip = await page.find('sbb-tooltip'); | ||
const dialog = await page.find('sbb-tooltip >>> dialog'); | ||
const changeSpy = await tooltipTrigger.spyOnEvent('focus'); | ||
const tooltipTrigger = document.querySelector('sbb-tooltip-trigger'); | ||
const tooltip = document.querySelector('sbb-tooltip'); | ||
const dialog = document.querySelector('sbb-tooltip').shadowRoot.querySelector('dialog'); | ||
const changeSpy = new EventSpy('focus', tooltipTrigger); | ||
|
||
element.setProperty('disabled', true); | ||
tooltip.setProperty('hoverTrigger', true); | ||
await page.waitForChanges(); | ||
tooltipTrigger.disabled = true; | ||
tooltip.hoverTrigger = true; | ||
await waitForLitRender(element); | ||
|
||
await tooltipTrigger.focus(); | ||
await page.waitForChanges(); | ||
await sendKeys({ down: 'Tab' }); | ||
await waitForLitRender(element); | ||
|
||
expect(changeSpy).not.toHaveReceivedEvent(); | ||
expect(dialog).not.toHaveAttribute('open'); | ||
expect(changeSpy.count).not.to.be.greaterThan(0); | ||
expect(dialog).not.to.have.attribute('open'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 40 additions & 33 deletions
73
src/components/sbb-tooltip-trigger/sbb-tooltip-trigger.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,50 @@ | ||
import { SbbTooltipTrigger } from './sbb-tooltip-trigger'; | ||
import { newSpecPage } from '@stencil/core/testing'; | ||
import '../sbb-icon'; | ||
import './sbb-tooltip-trigger'; | ||
|
||
import { expect, fixture } from '@open-wc/testing'; | ||
import { html } from 'lit/static-html.js'; | ||
|
||
describe('sbb-tooltip-trigger', () => { | ||
it('renders', async () => { | ||
const { root } = await newSpecPage({ | ||
components: [SbbTooltipTrigger], | ||
html: '<sbb-tooltip-trigger />', | ||
}); | ||
const root = await fixture(html`<sbb-tooltip-trigger />`); | ||
|
||
expect(root).toEqualHtml(` | ||
<sbb-tooltip-trigger role="button" tabindex="0" dir="ltr"> | ||
<mock:shadow-root> | ||
<span class="sbb-tooltip-trigger"> | ||
<slot> | ||
<sbb-icon name="circle-information-small"></sbb-icon> | ||
</slot> | ||
</span> | ||
</mock:shadow-root> | ||
</sbb-tooltip-trigger> | ||
`); | ||
expect(root).dom.to.be.equal( | ||
`<sbb-tooltip-trigger role="button" tabindex="0" dir="ltr"></sbb-tooltip-trigger>`, | ||
); | ||
expect(root).shadowDom.to.be.equal( | ||
`<span class="sbb-tooltip-trigger"> | ||
<slot> | ||
<sbb-icon | ||
aria-hidden="true" | ||
data-namespace="default" | ||
name="circle-information-small" | ||
role="img"> | ||
</sbb-icon> | ||
</slot> | ||
</span> | ||
`, | ||
); | ||
}); | ||
|
||
it('renders with custom content', async () => { | ||
const { root } = await newSpecPage({ | ||
components: [SbbTooltipTrigger], | ||
html: '<sbb-tooltip-trigger>Custom Content</sbb-tooltip-trigger>', | ||
}); | ||
const root = await fixture(html`<sbb-tooltip-trigger>Custom Content</sbb-tooltip-trigger>`); | ||
|
||
expect(root).toEqualHtml(` | ||
<sbb-tooltip-trigger role="button" tabindex="0" dir="ltr"> | ||
<mock:shadow-root> | ||
<span class="sbb-tooltip-trigger"> | ||
<slot> | ||
<sbb-icon name="circle-information-small"></sbb-icon> | ||
</slot> | ||
</span> | ||
</mock:shadow-root> | ||
Custom Content | ||
</sbb-tooltip-trigger> | ||
`); | ||
expect(root).dom.to.be.equal( | ||
`<sbb-tooltip-trigger role="button" tabindex="0" dir="ltr"> | ||
Custom Content | ||
</sbb-tooltip-trigger>`, | ||
); | ||
expect(root).shadowDom.to.be.equal( | ||
`<span class="sbb-tooltip-trigger"> | ||
<slot> | ||
<sbb-icon | ||
aria-hidden="true" | ||
data-namespace="default" | ||
name="circle-information-small" | ||
role="img"> | ||
</sbb-icon> | ||
</slot> | ||
</span>`, | ||
); | ||
}); | ||
}); |
5 changes: 4 additions & 1 deletion
5
src/components/sbb-tooltip-trigger/sbb-tooltip-trigger.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.