Skip to content

Commit

Permalink
JSDoc example for translatePlatformKeys, rename translatePlatformKey
Browse files Browse the repository at this point in the history
  • Loading branch information
stalgiag committed Dec 11, 2024
1 parent 60d40b1 commit aa905f6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 31 deletions.
12 changes: 7 additions & 5 deletions test/tests/toolbar_toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const assertAttributeValues = require('../util/assertAttributeValues');
const assertRovingTabindex = require('../util/assertRovingTabindex');
const assertHasFocus = require('../util/assertHasFocus');
const assertAttributeCanBeToggled = require('../util/assertAttributeCanBeToggled');
const translatePlatformKey = require('../util/translatePlatformKey');
const translatePlatformKey = require('../util/translatePlatformKeys');

const exampleFile = 'content/patterns/toolbar/examples/toolbar.html';

Expand Down Expand Up @@ -1116,8 +1116,9 @@ ariaTest(
'toolbar-button-enter-or-space',
async (t) => {
let textarea = await t.context.session.findElement(By.css('textarea'));
let modifierKey = translatePlatformKey(Key.CONTROL);
await textarea.sendKeys(Key.chord(...modifierKey, 'a'));
let selectAllKeys = translatePlatformKey([Key.CONTROL, 'a']);
let selectAllChord = Key.chord(...selectAllKeys);
await textarea.sendKeys(selectAllChord);
let originalText = await textarea.getAttribute('value');

const buttons = await t.context.queryElements(
Expand Down Expand Up @@ -1208,8 +1209,9 @@ ariaTest(
'toolbar-button-enter-or-space',
async (t) => {
let textarea = await t.context.session.findElement(By.css('textarea'));
let modifierKey = translatePlatformKey(Key.CONTROL);
await textarea.sendKeys(Key.chord(...modifierKey, 'a'));
let selectAllKeys = translatePlatformKey([Key.CONTROL, 'a']);
let selectAllChord = Key.chord(...selectAllKeys);
await textarea.sendKeys(selectAllChord);
let originalText = await textarea.getAttribute('value');

const buttons = await t.context.queryElements(
Expand Down
26 changes: 0 additions & 26 deletions test/util/translatePlatformKey.js

This file was deleted.

45 changes: 45 additions & 0 deletions test/util/translatePlatformKeys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const { Key } = require('selenium-webdriver');
const isMacOS = require('./isMacOS');

const MAC_KEY_MAPPINGS = {
[Key.CONTROL]: Key.META,
};

/**
* Translates a key or key combination for the current OS
*
* @param {string|string[]} keys - The key(s) to translate
* @returns {string[]} - The translated key(s) as a flat array ready for spreading
*
* @example
* // On macOS, translates CONTROL to META (Command key)
* translatePlatformKey(Key.CONTROL)
* // Returns: [Key.META]
*
* // On non-macOS systems, returns key unchanged
* translatePlatformKey(Key.CONTROL)
* // Returns: [Key.CONTROL]
*
* // Works with arrays of keys for key combinations
* translatePlatformKey([Key.CONTROL, 'a'])
* // Returns on macOS: [Key.META, 'a']
* // Returns on Windows/Linux: [Key.CONTROL, 'a']
*
* // Usage with Selenium WebDriver:
* const selectAllKeys = translatePlatformKey([Key.CONTROL, 'a']);
* const selectAllChord = Key.chord(...selectAllKeys);
* await element.sendKeys(selectAllChord);
*/
function translatePlatformKeys(keys) {
const keyArray = Array.isArray(keys) ? keys : [keys];
if (!isMacOS()) {
return keyArray;
}

return keyArray.reduce((acc, key) => {
const mappedKey = MAC_KEY_MAPPINGS[key] || key;
return acc.concat(mappedKey);
}, []);
}

module.exports = translatePlatformKeys;

0 comments on commit aa905f6

Please sign in to comment.