Skip to content

Commit

Permalink
test(input): convert multiple tests per layout into single test with …
Browse files Browse the repository at this point in the history
…steps
  • Loading branch information
ruifigueira committed Sep 19, 2023
1 parent ee6134c commit 60d714a
Showing 1 changed file with 72 additions and 69 deletions.
141 changes: 72 additions & 69 deletions tests/page/page-keyboard-layouts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,73 +132,76 @@ const testData: Record<string, (SimpleKeyTest | AccentedKeyTest)[]> = {
'fr-CH': [['è', 'BracketLeft', 186], ['ô', 'Equal', 221, 'KeyO', 79], ['ò', 'Shift+Equal', 221, 'KeyO', 79], ['Ò', 'Shift+Equal', 221, 'Shift+KeyO', 79]],
};

for (const [locale, test] of Object.entries(testData)) {
it(`should handle ${locale} keyboard layout`, async ({ page }) => {
await page.keyboard.changeLayout(locale);

for (const [key, code, keyCode, letterCode, letterKeyCode] of test) {
const [modifiersDown, modifiersUp, modifiers, codeWithoutModifiers] = code.startsWith('Shift+') ?
[[`Keydown: Shift ShiftLeft 16 [Shift]`], [`Keyup: Shift ShiftLeft 16 []`], 'Shift', code.substring('Shift+'.length)] :
[[], [], '', code];

if (!letterCode) {

await it.step(`fire events on ${code}`, async () => {
await page.keyboard.press(code);
const charCode = key.charCodeAt(0);
expect(await page.evaluate('getResult()')).toBe(
[...modifiersDown,
`Keydown: ${key} ${codeWithoutModifiers} ${keyCode} [${modifiers}]`,
`Keypress: ${key} ${codeWithoutModifiers} ${charCode} ${charCode} [${modifiers}]`,
`Keyup: ${key} ${codeWithoutModifiers} ${keyCode} [${modifiers}]`,
...modifiersUp].join('\n'));
});

await it.step(`fire events on "${key}"`, async () => {
await page.keyboard.press(key);
const charCode = key.charCodeAt(0);
const result = await page.evaluate('getResult()');
// TODO shouldn't it send a Shift event if key is uppercase?
expect(result).toBe(
[`Keydown: ${key} ${codeWithoutModifiers} ${keyCode} []`,
`Keypress: ${key} ${codeWithoutModifiers} ${charCode} ${charCode} []`,
`Keyup: ${key} ${codeWithoutModifiers} ${keyCode} []`].join('\n'));
});
} else {
const [modifiersLetterDown, modifiersLetterUp, modifiersLetter, letterCodeWithoutModifiers] = letterCode.startsWith('Shift+') ?
[[`Keydown: Shift ShiftLeft 16 [Shift]`], [`Keyup: Shift ShiftLeft 16 []`], 'Shift', letterCode.substring('Shift+'.length)] :
[[], [], '', letterCode];

await it.step(`fire events in accented key for ${code} ${letterCode}`, async () => {
await page.keyboard.press(code);
await page.keyboard.press(letterCode);
const charCode = key.charCodeAt(0);
expect(await page.evaluate('getResult()')).toBe(
[...modifiersDown,
`Keydown: Dead ${codeWithoutModifiers} ${keyCode} [${modifiers}]`,
`Keyup: Dead ${codeWithoutModifiers} ${keyCode} [${modifiers}]`,
...modifiersUp,
...modifiersLetterDown,
`Keydown: ${key} ${letterCodeWithoutModifiers} ${letterKeyCode} [${modifiersLetter}]`,
`Keypress: ${key} ${letterCodeWithoutModifiers} ${charCode} ${charCode} [${modifiersLetter}]`,
`Keyup: ${removeAccents(key)} ${letterCodeWithoutModifiers} ${letterKeyCode} [${modifiersLetter}]`,
...modifiersLetterUp].join('\n'));
});

await it.step(`should fire events when typing accented key "${key}"`, async () => {
await page.keyboard.type(key);
const charCode = key.charCodeAt(0);
expect(await page.evaluate('getResult()')).toBe(
[...modifiersDown,
`Keydown: Dead ${codeWithoutModifiers} ${keyCode} [${modifiers}]`,
`Keyup: Dead ${codeWithoutModifiers} ${keyCode} [${modifiers}]`,
...modifiersUp,
// TODO shouldn't it send a Shift event if letter is uppercase?
`Keydown: ${key} ${letterCodeWithoutModifiers} ${letterKeyCode} []`,
`Keypress: ${key} ${letterCodeWithoutModifiers} ${charCode} ${charCode} []`,
`Keyup: ${removeAccents(key)} ${letterCodeWithoutModifiers} ${letterKeyCode} []`,].join('\n'));
});
it(`should handle all keyboard layouts`, async ({ page }) => {
for (const [layoutName, test] of Object.entries(testData)) {

await it.step(`${layoutName} layout`, async () => {
await page.keyboard.changeLayout(layoutName);

for (const [key, code, keyCode, letterCode, letterKeyCode] of test) {
const [modifiersDown, modifiersUp, modifiers, codeWithoutModifiers] = code.startsWith('Shift+') ?
[[`Keydown: Shift ShiftLeft 16 [Shift]`], [`Keyup: Shift ShiftLeft 16 []`], 'Shift', code.substring('Shift+'.length)] :
[[], [], '', code];

if (!letterCode) {

await it.step(`fire events on ${code}`, async () => {
await page.keyboard.press(code);
const charCode = key.charCodeAt(0);
expect(await page.evaluate('getResult()')).toBe(
[...modifiersDown,
`Keydown: ${key} ${codeWithoutModifiers} ${keyCode} [${modifiers}]`,
`Keypress: ${key} ${codeWithoutModifiers} ${charCode} ${charCode} [${modifiers}]`,
`Keyup: ${key} ${codeWithoutModifiers} ${keyCode} [${modifiers}]`,
...modifiersUp].join('\n'));
});

await it.step(`fire events on "${key}"`, async () => {
await page.keyboard.press(key);
const charCode = key.charCodeAt(0);
const result = await page.evaluate('getResult()');
// TODO shouldn't it send a Shift event if key is uppercase?
expect(result).toBe(
[`Keydown: ${key} ${codeWithoutModifiers} ${keyCode} []`,
`Keypress: ${key} ${codeWithoutModifiers} ${charCode} ${charCode} []`,
`Keyup: ${key} ${codeWithoutModifiers} ${keyCode} []`].join('\n'));
});
} else {
const [modifiersLetterDown, modifiersLetterUp, modifiersLetter, letterCodeWithoutModifiers] = letterCode.startsWith('Shift+') ?
[[`Keydown: Shift ShiftLeft 16 [Shift]`], [`Keyup: Shift ShiftLeft 16 []`], 'Shift', letterCode.substring('Shift+'.length)] :
[[], [], '', letterCode];

await it.step(`fire events in accented key for ${code} ${letterCode}`, async () => {
await page.keyboard.press(code);
await page.keyboard.press(letterCode);
const charCode = key.charCodeAt(0);
expect(await page.evaluate('getResult()')).toBe(
[...modifiersDown,
`Keydown: Dead ${codeWithoutModifiers} ${keyCode} [${modifiers}]`,
`Keyup: Dead ${codeWithoutModifiers} ${keyCode} [${modifiers}]`,
...modifiersUp,
...modifiersLetterDown,
`Keydown: ${key} ${letterCodeWithoutModifiers} ${letterKeyCode} [${modifiersLetter}]`,
`Keypress: ${key} ${letterCodeWithoutModifiers} ${charCode} ${charCode} [${modifiersLetter}]`,
`Keyup: ${removeAccents(key)} ${letterCodeWithoutModifiers} ${letterKeyCode} [${modifiersLetter}]`,
...modifiersLetterUp].join('\n'));
});

await it.step(`should fire events when typing accented key "${key}"`, async () => {
await page.keyboard.type(key);
const charCode = key.charCodeAt(0);
expect(await page.evaluate('getResult()')).toBe(
[...modifiersDown,
`Keydown: Dead ${codeWithoutModifiers} ${keyCode} [${modifiers}]`,
`Keyup: Dead ${codeWithoutModifiers} ${keyCode} [${modifiers}]`,
...modifiersUp,
// TODO shouldn't it send a Shift event if letter is uppercase?
`Keydown: ${key} ${letterCodeWithoutModifiers} ${letterKeyCode} []`,
`Keypress: ${key} ${letterCodeWithoutModifiers} ${charCode} ${charCode} []`,
`Keyup: ${removeAccents(key)} ${letterCodeWithoutModifiers} ${letterKeyCode} []`,].join('\n'));
});
}
}
}
});
}
});
}
});

0 comments on commit 60d714a

Please sign in to comment.