import 'jest-puppeteer'; import { type ElementHandle, type Page } from 'puppeteer'; import { gotoStory } from '../../../../helpers/test'; async function expectInputValue(page: Page, value: string): Promise { await page.waitForFunction( (v) => (document.querySelector('[data-ods="combobox-control"] input') as HTMLInputElement | null)?.value === v, {}, value, ); } async function getItems(page: Page): Promise { return await page.$$('[role="option"]'); } async function getTags(page: Page): Promise { return page.$$('[data-ods="combobox-control"] [data-ods="tag"]'); } async function waitForItemCount(page: Page, count: number): Promise { await page.waitForFunction((n) => document.querySelectorAll('[role="option"]').length === n, {}, count); } async function waitForTagCount(page: Page, count: number): Promise { await page.waitForFunction( (n) => document.querySelectorAll('[data-ods="combobox-control"] [data-ods="tag"]').length === n, {}, count, ); } async function isContentOpened(page: Page): Promise { return page.$eval('[data-ods="combobox-content"]', (el) => !!el && !el.hasAttribute('hidden')); } async function openContent(page: Page): Promise { await page.click('[data-ods="combobox-control"]'); await page.waitForSelector('[data-ods="combobox-content"]:not([hidden])'); } async function typeOnInput(page: Page, text: string): Promise { await page.click('[data-ods="combobox-control"] input'); await page.type('[data-ods="combobox-control"] input', text); } describe('Combobox behaviour', () => { describe('clearable', () => { describe('uncontrolled', () => { beforeEach(async() => { await gotoStory(page, 'behavior/clearable'); await page.waitForSelector('[data-ods="combobox"]'); }); it('should clear the input value', async() => { await typeOnInput(page, 'Value'); await expectInputValue(page, 'Value'); await page.keyboard.press('Tab'); await page.keyboard.press('Enter'); await expectInputValue(page, ''); await page.click('body'); await expectInputValue(page, ''); }); }); describe('controlled', () => { beforeEach(async() => { await gotoStory(page, 'behavior/clearable-controlled'); await page.waitForSelector('[data-ods="combobox"]'); }); it('should clear the input value', async() => { await typeOnInput(page, 'Value'); await expectInputValue(page, 'Value'); await page.keyboard.press('Tab'); await page.keyboard.press('Enter'); await expectInputValue(page, ''); await page.click('body'); await expectInputValue(page, ''); }); }); }); describe('controlled value', () => { beforeEach(async() => { await gotoStory(page, 'behavior/controlled-value'); await page.waitForSelector('[data-ods="combobox"]'); }); it('should reflect external change to value', async() => { const changeValueButton = await page.$('#force-value'); await expectInputValue(page, 'Apple'); await changeValueButton?.click(); await expectInputValue(page, 'Banana'); }); it('should clear value when set to []', async() => { const clearValueButton = await page.$('#clear-value'); await expectInputValue(page, 'Apple'); await clearValueButton?.click(); await expectInputValue(page, ''); }); }); describe('on blur', () => { beforeEach(async() => { await gotoStory(page, 'behavior/on-blur'); await page.waitForSelector('[data-ods="combobox"]'); }); it('should reset the input value to an empty value', async() => { await typeOnInput(page, 'Dummy'); await page.keyboard.press('Tab'); await expectInputValue(page, ''); }); it('should reset the input value to the current selection', async() => { await openContent(page); await page.keyboard.press('ArrowDown'); await page.keyboard.press('Enter'); await expectInputValue(page, 'Apple'); await typeOnInput(page, 'Dummy'); await page.keyboard.press('Tab'); await expectInputValue(page, 'Apple'); }); }); describe('keyboard navigation', () => { beforeEach(async() => { await gotoStory(page, 'behavior/keyboard-navigation'); await page.waitForSelector('[data-ods="combobox"]'); }); it('should handle arrow down and enter to select item', async() => { await openContent(page); await page.keyboard.press('ArrowDown'); await page.keyboard.press('Enter'); await expectInputValue(page, 'Apple'); }); }); describe('scrolling', () => { beforeEach(async() => { await gotoStory(page, 'behavior/page-bottom'); await page.waitForSelector('[data-ods="combobox"]'); }); it('should scroll the page to keep the highlighted option visible on keyboard navigation', async() => { // The pointer rests on the control, so the page scroll brings options under it. await openContent(page); const itemCount = (await getItems(page)).length; await Array.from({ length: itemCount }).reduce>( (previous, _, idx) => previous.then(async() => { await page.keyboard.press('ArrowDown'); await page.evaluate(() => new Promise((resolve) => requestAnimationFrame(resolve))); // An option sliding under the pointer must not take the highlight. expect(await page.$$eval('[role="option"]', (els) => els.findIndex((el) => el.getAttribute('aria-selected') === 'true'))).toBe(idx); }), Promise.resolve(), ); await page.evaluate(() => new Promise((resolve) => requestAnimationFrame(resolve))); const bottom = await page.$eval('[role="option"]:last-child', (el) => el.getBoundingClientRect().bottom); expect(bottom).toBeLessThanOrEqual(await page.evaluate(() => window.innerHeight)); }); it('should not scroll the page when hovering a partly visible option', async() => { await openContent(page); const scrollY = await page.evaluate(() => window.scrollY); const top = await page.evaluate(() => { const options = Array.from(document.querySelectorAll('[role="option"]')); const partlyVisible = options.find((el) => el.getBoundingClientRect().bottom > window.innerHeight); return partlyVisible?.getBoundingClientRect().top; }); expect(top).toBeLessThan(await page.evaluate(() => window.innerHeight)); await page.mouse.move(20, (top! + await page.evaluate(() => window.innerHeight)) / 2); await page.waitForSelector('[role="option"][aria-selected="true"]'); await page.evaluate(() => new Promise((resolve) => requestAnimationFrame(resolve))); expect(await page.evaluate(() => window.scrollY)).toBe(scrollY); }); it('should scroll a hovered option into view once reached back by keyboard', async() => { await openContent(page); const { index, top } = await page.evaluate(() => { const options = Array.from(document.querySelectorAll('[role="option"]')); const partlyVisible = options.findIndex((el) => el.getBoundingClientRect().bottom > window.innerHeight); return { index: partlyVisible, top: options[partlyVisible]?.getBoundingClientRect().top }; }); expect(top).toBeLessThan(await page.evaluate(() => window.innerHeight)); // The pointer stays still on the partly visible option from here on. await page.mouse.move(20, (top! + await page.evaluate(() => window.innerHeight)) / 2); await page.waitForFunction((idx) => document.querySelectorAll('[role="option"]')[idx]?.getAttribute('aria-selected') === 'true', {}, index); await page.keyboard.press('ArrowUp'); await page.keyboard.press('ArrowDown'); await page.evaluate(() => new Promise((resolve) => requestAnimationFrame(resolve))); const bottom = await page.evaluate((idx) => document.querySelectorAll('[role="option"]')[idx].getBoundingClientRect().bottom, index); expect(bottom).toBeLessThanOrEqual(await page.evaluate(() => window.innerHeight)); }); }); describe('filtering', () => { describe('default', () => { beforeEach(async() => { await gotoStory(page, 'behavior/filtering'); await page.waitForSelector('[data-ods="combobox"]'); }); it('should filter items based on input value', async() => { await openContent(page); expect((await getItems(page)).length).toBe(4); await typeOnInput(page, 'ba'); await waitForItemCount(page, 1); expect((await getItems(page)).length).toBe(1); }); it('should show all items when input is cleared', async() => { await typeOnInput(page, 'ba'); await waitForItemCount(page, 1); expect((await getItems(page)).length).toBe(1); await page.keyboard.press('Tab'); await page.keyboard.press('Enter'); await openContent(page); await waitForItemCount(page, 4); expect((await getItems(page)).length).toBe(4); }); }); describe('custom', () => { beforeEach(async() => { await gotoStory(page, 'behavior/filtering-custom'); await page.waitForSelector('[data-ods="combobox"]'); }); it('should filter items based on input value', async() => { await openContent(page); expect((await getItems(page)).length).toBe(4); await typeOnInput(page, 'yrr'); await waitForItemCount(page, 1); expect((await getItems(page)).length).toBe(1); }); it('should show all items when input is cleared', async() => { await typeOnInput(page, 'yrr'); await waitForItemCount(page, 1); expect((await getItems(page)).length).toBe(1); await page.keyboard.press('Tab'); await page.keyboard.press('Enter'); await openContent(page); await waitForItemCount(page, 4); expect((await getItems(page)).length).toBe(4); }); }); }); describe('interaction', () => { it('should not open dropdown when disabled', async() => { await gotoStory(page, 'behavior/disabled'); await page.waitForSelector('[data-ods="combobox"]'); await page.click('[data-ods="combobox-control"]'); expect(await isContentOpened(page)).toBe(false); }); it('should not open dropdown when readonly', async() => { await gotoStory(page, 'behavior/readonly'); await page.waitForSelector('[data-ods="combobox"]'); await page.click('[data-ods="combobox-control"]'); expect(await isContentOpened(page)).toBe(false); }); }); describe('group', () => { beforeEach(async() => { await gotoStory(page, 'behavior/group'); await page.waitForSelector('[data-ods="combobox"]'); }); it('should select items from groups using mouse', async() => { await openContent(page); const items = await getItems(page); expect(items.length).toBe(7); await items[1].click(); expect(await page.$eval('[data-testid="group-value"]', (el) => el.textContent || '')).toBe('the-banana'); }); it('should select items from groups using keyboard', async() => { await openContent(page); await page.keyboard.press('ArrowDown'); await page.keyboard.press('Enter'); expect(await page.$eval('[data-testid="group-value"]', (el) => el.textContent || '')).toBe('the-apple'); await expectInputValue(page, 'Apple'); }); }); describe('multiple selection', () => { describe('controlled', () => { beforeEach(async() => { await gotoStory(page, 'behavior/multiple-controlled'); await page.waitForSelector('[data-ods="combobox"]'); }); it('should update display values when multiple items are selected', async() => { await openContent(page); await page.locator('[role="option"]:nth-child(1)').click(); await waitForTagCount(page, 1); expect((await getTags(page)).length).toBe(1); await page.locator('[role="option"]:nth-child(1)').click(); await waitForTagCount(page, 2); expect((await getTags(page)).length).toBe(2); }); }); describe('uncontrolled', () => { beforeEach(async() => { await gotoStory(page, 'behavior/multiple-uncontrolled'); await page.waitForSelector('[data-ods="combobox"]'); }); it('should update display values when multiple items are selected', async() => { await openContent(page); await page.locator('[role="option"]:nth-child(1)').click(); await waitForTagCount(page, 1); expect((await getTags(page)).length).toBe(1); await page.locator('[role="option"]:nth-child(1)').click(); await waitForTagCount(page, 2); expect((await getTags(page)).length).toBe(2); }); }); }); describe('multiple combobox', () => { async function isContentOpened(element: ElementHandle | null): Promise { if (!element) { return false; } return element?.$eval('[data-ods="combobox-content"]', (el) => !!el && !el.hasAttribute('hidden')); } async function typeOnInput(element: ElementHandle | null, text: string): Promise { const input = await element?.$('[data-ods="combobox-control"] input'); await input?.click(); await input?.type(text); await element?.waitForSelector('[data-ods="combobox-content"]:not([hidden])'); } beforeEach(async() => { await gotoStory(page, 'behavior/multiple-combobox'); await page.waitForSelector('#combobox1'); await page.waitForSelector('#combobox2'); }); it('should blur current one when focusing the second', async() => { const combobox1 = await page.waitForSelector('#combobox1'); const combobox2 = await page.waitForSelector('#combobox2'); await typeOnInput(combobox1, '1'); expect(await isContentOpened(combobox1)).toBe(true); expect(await isContentOpened(combobox2)).toBe(false); await typeOnInput(combobox2, '2'); await combobox1?.waitForSelector('[data-ods="combobox-content"][hidden]'); expect(await isContentOpened(combobox1)).toBe(false); expect(await isContentOpened(combobox2)).toBe(true); }); }); });