Skip to content

Commit

Permalink
test: test special keys
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Dec 23, 2024
1 parent 59d2fd2 commit 512ffc6
Showing 1 changed file with 50 additions and 2 deletions.
52 changes: 50 additions & 2 deletions test/browser/fixtures/user-event/keyboard.test.ts
Original file line number Diff line number Diff line change
@@ -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 = `
Expand Down Expand Up @@ -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"`);
}
})

0 comments on commit 512ffc6

Please sign in to comment.