import { expect, test } from '../../test/playwright/index.js'; import type { Tooltip } from './tooltip.js'; import { tagName } from './tooltip.options.js'; test.describe('Tooltip', () => { test.use({ tagName, innerHTML: 'This is a tooltip', }); test('should set default host attributes when connected', async ({ fastPage }) => { await fastPage.setTemplate(); await expect(fastPage.element).toHaveAttribute('popover', 'auto'); await expect(fastPage.element).toHaveAttribute('aria-hidden', 'true'); }); test('should preserve authored host attributes when connected', async ({ fastPage }) => { await fastPage.setTemplate({ attributes: { popover: 'manual', 'aria-hidden': 'false' } }); await expect(fastPage.element).toHaveAttribute('popover', 'manual'); await expect(fastPage.element).toHaveAttribute('aria-hidden', 'false'); }); test('should create with document.createElement()', async ({ page, fastPage }) => { await fastPage.setTemplate(); let hasError = false; page.on('pageerror', () => { hasError = true; }); await page.evaluate(tagName => { document.createElement(tagName); }, tagName); expect(hasError).toBe(false); }); test('escape key should hide the tooltip', async ({ fastPage, page }) => { const { element } = fastPage; const button = page.locator('button'); await fastPage.setTemplate(/* html */ `
<${tagName} anchor="target">This is a tooltip
`); await button.focus(); await expect(element).toBeVisible(); await page.keyboard.press('Escape'); await expect(element).toBeHidden(); }); test('should have the role set to `tooltip`', async ({ fastPage }) => { const { element } = fastPage; await fastPage.setTemplate(); await expect(element).toHaveJSProperty('elementInternals.role', 'tooltip'); }); test('should have the `aria-describedby` attribute set to the tooltip id', async ({ fastPage, page }) => { const { element } = fastPage; const button = page.locator('button'); await fastPage.setTemplate(/* html */ `
<${tagName} anchor="target">This is a tooltip
`); await expect(element).toHaveAttribute('id'); const id = await element.evaluate((node: Tooltip) => node.id); await expect(button).toHaveAttribute('aria-describedby', id); }); test('should remove the tooltip id from `aria-describedby` attribute when tooltip is removed', async ({ fastPage, }) => { const { element, page } = fastPage; const button = page.locator('button'); await fastPage.setTemplate(/* html */ `
<${tagName} anchor="target">This is a tooltip <${tagName} anchor="target">This is another tooltip
`); const id1 = await element.nth(0).evaluate((node: Tooltip) => node.id); const id2 = await element.nth(1).evaluate((node: Tooltip) => node.id); await expect(button).toHaveAttribute('aria-describedby', [id1, id2].join(' ')); await element.nth(0).evaluate(node => { node.remove(); }); await expect(button).toHaveAttribute('aria-describedby', id2); await button.evaluate( (node, [tagName, id]) => { const tooltip = document.createElement(tagName); tooltip.id = id; tooltip.setAttribute('anchor', 'target'); tooltip.textContent = 'New tooltip'; node.after(tooltip); }, [tagName, id1], ); await expect(button).toHaveAttribute('aria-describedby', [id2, id1].join(' ')); }); test('should not be visible by default', async ({ fastPage }) => { const { element } = fastPage; await fastPage.setTemplate(); await expect(element).toBeHidden(); }); test('should show the tooltip on hover', async ({ fastPage, page }) => { const { element } = fastPage; const button = page.locator('button'); await fastPage.setTemplate(/* html */ `
<${tagName} anchor="target">This is a tooltip
`); await expect(element).toBeHidden(); await button.hover(); await expect(element).toBeVisible(); }); test('should show the tooltip on focus', async ({ fastPage, page }) => { const { element } = fastPage; const button = page.locator('button'); await fastPage.setTemplate(/* html */ `
<${tagName} anchor="target">This is a tooltip
`); await expect(element).toBeHidden(); await button.focus(); await expect(element).toBeVisible(); await button.blur(); await expect(element).toBeHidden(); }); test('default placement should be set to `above`', async ({ fastPage, page }) => { const { element } = fastPage; const button = page.locator('button'); await fastPage.setTemplate(/* html */ `
<${tagName} anchor="target">This is a tooltip
`); await expect(element).not.toHaveAttribute('positioning', 'above'); await button.focus(); await expect(element).toBeVisible(); const buttonTop = await button.evaluate((node: HTMLElement) => node.getBoundingClientRect().top); const elementBottom = await element.evaluate((node: HTMLElement) => node.getBoundingClientRect().bottom); expect(buttonTop).toBeGreaterThan(elementBottom); }); test('position should be set to `above` when `positioning` is set to `above`', async ({ fastPage, page }) => { const { element } = fastPage; const button = page.locator('button'); await fastPage.setTemplate(/* html */ `
<${tagName} anchor="target">This is a tooltip
`); await element.evaluate((node: Tooltip) => { node.positioning = 'above'; }); await expect(element).toHaveAttribute('positioning', 'above'); await button.focus(); const buttonTop = await button.evaluate((node: HTMLElement) => node.getBoundingClientRect().top); const elementBottom = await element.evaluate((node: HTMLElement) => node.getBoundingClientRect().bottom); expect(buttonTop).toBeGreaterThan(elementBottom); }); test('position should be set to `below` when `positioning` is set to `below`', async ({ fastPage, page }) => { const { element } = fastPage; const button = page.locator('button'); await fastPage.setTemplate(/* html */ `
<${tagName} anchor="target" positioning="below">This is a tooltip
`); await expect(element).toHaveJSProperty('positioning', 'below'); await button.focus(); const buttonBottom = await button.evaluate((node: HTMLElement) => node.getBoundingClientRect().bottom); const elementTop = await element.evaluate((node: HTMLElement) => node.getBoundingClientRect().top); expect(buttonBottom).toBeLessThan(elementTop); }); test('position should be set to `before` when `positioning` is set to `before`', async ({ fastPage, page }) => { const { element } = fastPage; const button = page.locator('button'); await fastPage.setTemplate(/* html */ `
<${tagName} anchor="target" positioning="before">This is a tooltip
`); await expect(element).toHaveJSProperty('positioning', 'before'); await button.focus(); const buttonLeft = await button.evaluate((node: HTMLElement) => node.getBoundingClientRect().left); const elementRight = await element.evaluate((node: HTMLElement) => node.getBoundingClientRect().right); expect(buttonLeft).toBeGreaterThan(elementRight); }); test('position should be set to `after` when `positioning` is set to `after`', async ({ fastPage, page }) => { const { element } = fastPage; const button = page.locator('button'); await fastPage.setTemplate(/* html */ `
<${tagName} anchor="target" positioning="after">This is a tooltip
`); await expect(element).toHaveJSProperty('positioning', 'after'); await button.focus(); const buttonRight = await button.evaluate((node: HTMLElement) => node.getBoundingClientRect().right); const elementLeft = await element.evaluate((node: HTMLElement) => node.getBoundingClientRect().left); expect(buttonRight).toBeLessThan(elementLeft); }); });