diff --git a/test/browser/fixtures/user-event/keyboard.test.ts b/test/browser/fixtures/user-event/keyboard.test.ts index 90541b433e67..997228ba255d 100644 --- a/test/browser/fixtures/user-event/keyboard.test.ts +++ b/test/browser/fixtures/user-event/keyboard.test.ts @@ -1,5 +1,5 @@ -import { expect, test } from 'vitest' -import { userEvent, page } from '@vitest/browser/context' +import { expect, test, vi } from 'vitest' +import { userEvent, page, server } from '@vitest/browser/context' test('non US keys', async () => { document.body.innerHTML = ` @@ -51,3 +51,51 @@ test('click with modifier', async () => { await userEvent.keyboard('{/Shift}') await expect.poll(() => el.textContent).toContain("[ok]") }) + +// TODO: https://github.com/vitest-dev/vitest/issues/7118 +// https://testing-library.com/docs/user-event/keyboard +// https://github.com/testing-library/user-event/blob/main/src/keyboard/keyMap.ts +// https://playwright.dev/docs/api/class-keyboard +// https://webdriver.io/docs/api/browser/keys/ +test('special keys', async () => { + async function testKeyboard(text: string) { + let data: any; + function handler(e: KeyboardEvent) { + data = `${e.key}|${e.code}|${e.location}`; + } + document.addEventListener('keydown', handler) + try { + await userEvent.keyboard(text) + } catch(e) { + return 'ERROR'; + } finally { + document.removeEventListener('keydown', handler) + } + return data + } + + if (server.provider === 'playwright') { + expect(await testKeyboard('{Shift}')).toMatchInlineSnapshot(`"Shift|ShiftLeft|1"`); + expect(await testKeyboard('{ShiftLeft}')).toMatchInlineSnapshot(`"Shift|ShiftLeft|1"`); + expect(await testKeyboard('{ShiftRight}')).toMatchInlineSnapshot(`"Shift|ShiftRight|2"`); + expect(await testKeyboard('[Shift]')).toMatchInlineSnapshot(`undefined`); + expect(await testKeyboard('[ShiftLeft]')).toMatchInlineSnapshot(`"Shift|ShiftLeft|1"`); + expect(await testKeyboard('[ShiftRight]')).toMatchInlineSnapshot(`"Shift|ShiftLeft|1"`); + } + if (server.provider === 'webdriverio') { + expect(await testKeyboard('{Shift}')).toMatchInlineSnapshot(`"Shift|ShiftLeft|1"`); + expect(await testKeyboard('{ShiftLeft}')).toMatchInlineSnapshot(`"ERROR"`); + expect(await testKeyboard('{ShiftRight}')).toMatchInlineSnapshot(`"ERROR"`); + expect(await testKeyboard('[Shift]')).toMatchInlineSnapshot(`"ERROR"`); + expect(await testKeyboard('[ShiftLeft]')).toMatchInlineSnapshot(`"Shift|ShiftLeft|1"`); + expect(await testKeyboard('[ShiftRight]')).toMatchInlineSnapshot(`"Shift|ShiftLeft|1"`); + } + if (server.provider === 'preview') { + expect(await testKeyboard('{Shift}')).toMatchInlineSnapshot(`"Shift|ShiftLeft|0"`); + expect(await testKeyboard('{ShiftLeft}')).toMatchInlineSnapshot(`"ShiftLeft|Unknown|0"`); + expect(await testKeyboard('{ShiftRight}')).toMatchInlineSnapshot(`"ShiftRight|Unknown|0"`); + expect(await testKeyboard('[Shift]')).toMatchInlineSnapshot(`"Unknown|Shift|0"`); + expect(await testKeyboard('[ShiftLeft]')).toMatchInlineSnapshot(`"Shift|ShiftLeft|0"`); + expect(await testKeyboard('[ShiftRight]')).toMatchInlineSnapshot(`"Shift|ShiftRight|0"`); + } +})