From 66d80e34c938a604d30e09575a551bf4e1471b4b Mon Sep 17 00:00:00 2001 From: Philipp Fritsche Date: Tue, 9 Mar 2021 19:48:40 +0100 Subject: [PATCH 01/43] chore: configure repo for typescript modules --- .eslintrc.js | 13 +++++++++++++ tsconfig.json | 10 ++++++++++ 2 files changed, 23 insertions(+) create mode 100644 .eslintrc.js create mode 100644 tsconfig.json diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 00000000..df4cb262 --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,13 @@ +module.exports = { + extends: './node_modules/kcd-scripts/eslint.js', + settings: { + 'import/resolver': { + node: { + extensions: ['.js', '.ts'], + }, + }, + }, + rules: { + 'testing-library/no-dom-import': 0, + }, +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 00000000..d7d83b6d --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "./node_modules/kcd-scripts/shared-tsconfig.json", + "compilerOptions": { + "allowJs": true + }, + "include": [ + "./src", + "./typings" + ] +} \ No newline at end of file From 153aca87f1795c7fbdcfd6f17bf67de1c3e1514f Mon Sep 17 00:00:00 2001 From: Philipp Fritsche Date: Tue, 9 Mar 2021 20:56:26 +0100 Subject: [PATCH 02/43] refactor: convert and restructure utils --- src/utils.js | 384 ------------------ src/utils/click/getMouseEventOptions.ts | 75 ++++ src/utils/click/index.ts | 2 + src/utils/click/isClickableInput.ts | 18 + src/utils/edit/buildTimeValue.ts | 32 ++ src/utils/edit/calculateNewValue.ts | 94 +++++ src/utils/edit/getSelectionRange.ts | 31 ++ src/utils/edit/getValue.ts | 8 + src/utils/edit/index.ts | 8 + src/utils/edit/isContentEditable.ts | 8 + src/utils/edit/isValidDateValue.ts | 14 + src/utils/edit/isValidInputTimeValue.ts | 17 + .../edit/setSelectionRangeIfNecessary.ts | 46 +++ src/utils/focus/getActiveElement.ts | 10 + src/utils/focus/index.ts | 3 + src/utils/focus/isFocusable.ts | 9 + src/utils/focus/selector.ts | 10 + src/utils/index.ts | 4 + src/utils/misc/dom-helpers.d.ts | 3 + src/utils/misc/eventWrapper.ts | 9 + src/utils/misc/index.ts | 4 + src/utils/misc/isDisabled.ts | 5 + src/utils/misc/isInstanceOfElement.ts | 40 ++ .../isLabelWithInternallyDisabledControl.ts | 16 + src/utils/misc/isVisible.ts | 18 + 25 files changed, 484 insertions(+), 384 deletions(-) delete mode 100644 src/utils.js create mode 100644 src/utils/click/getMouseEventOptions.ts create mode 100644 src/utils/click/index.ts create mode 100644 src/utils/click/isClickableInput.ts create mode 100644 src/utils/edit/buildTimeValue.ts create mode 100644 src/utils/edit/calculateNewValue.ts create mode 100644 src/utils/edit/getSelectionRange.ts create mode 100644 src/utils/edit/getValue.ts create mode 100644 src/utils/edit/index.ts create mode 100644 src/utils/edit/isContentEditable.ts create mode 100644 src/utils/edit/isValidDateValue.ts create mode 100644 src/utils/edit/isValidInputTimeValue.ts create mode 100644 src/utils/edit/setSelectionRangeIfNecessary.ts create mode 100644 src/utils/focus/getActiveElement.ts create mode 100644 src/utils/focus/index.ts create mode 100644 src/utils/focus/isFocusable.ts create mode 100644 src/utils/focus/selector.ts create mode 100644 src/utils/index.ts create mode 100644 src/utils/misc/dom-helpers.d.ts create mode 100644 src/utils/misc/eventWrapper.ts create mode 100644 src/utils/misc/index.ts create mode 100644 src/utils/misc/isDisabled.ts create mode 100644 src/utils/misc/isInstanceOfElement.ts create mode 100644 src/utils/misc/isLabelWithInternallyDisabledControl.ts create mode 100644 src/utils/misc/isVisible.ts diff --git a/src/utils.js b/src/utils.js deleted file mode 100644 index b4825948..00000000 --- a/src/utils.js +++ /dev/null @@ -1,384 +0,0 @@ -import {getConfig} from '@testing-library/dom' -import {getWindowFromNode} from '@testing-library/dom/dist/helpers' - -// isInstanceOfElement can be removed once the peerDependency for @testing-library/dom is bumped to a version that includes https://github.com/testing-library/dom-testing-library/pull/885 -/** - * Check if an element is of a given type. - * - * @param {Element} element The element to test - * @param {string} elementType Constructor name. E.g. 'HTMLSelectElement' - */ -function isInstanceOfElement(element, elementType) { - try { - const window = getWindowFromNode(element) - // Window usually has the element constructors as properties but is not required to do so per specs - if (typeof window[elementType] === 'function') { - return element instanceof window[elementType] - } - } catch (e) { - // The document might not be associated with a window - } - - // Fall back to the constructor name as workaround for test environments that - // a) not associate the document with a window - // b) not provide the constructor as property of window - if (/^HTML(\w+)Element$/.test(element.constructor.name)) { - return element.constructor.name === elementType - } - - // The user passed some node that is not created in a browser-like environment - throw new Error( - `Unable to verify if element is instance of ${elementType}. Please file an issue describing your test environment: https://github.com/testing-library/dom-testing-library/issues/new`, - ) -} - -function isMousePressEvent(event) { - return ( - event === 'mousedown' || - event === 'mouseup' || - event === 'click' || - event === 'dblclick' - ) -} - -function invert(map) { - const res = {} - for (const key of Object.keys(map)) { - res[map[key]] = key - } - - return res -} - -// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons -const BUTTONS_TO_NAMES = { - 0: 'none', - 1: 'primary', - 2: 'secondary', - 4: 'auxiliary', -} -const NAMES_TO_BUTTONS = invert(BUTTONS_TO_NAMES) - -// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button -const BUTTON_TO_NAMES = { - 0: 'primary', - 1: 'auxiliary', - 2: 'secondary', -} - -const NAMES_TO_BUTTON = invert(BUTTON_TO_NAMES) - -function convertMouseButtons(event, init, property, mapping) { - if (!isMousePressEvent(event)) { - return 0 - } - - if (init[property] != null) { - return init[property] - } - - if (init.buttons != null) { - // not sure how to test this. Feel free to try and add a test if you want. - // istanbul ignore next - return mapping[BUTTONS_TO_NAMES[init.buttons]] || 0 - } - - if (init.button != null) { - // not sure how to test this. Feel free to try and add a test if you want. - // istanbul ignore next - return mapping[BUTTON_TO_NAMES[init.button]] || 0 - } - - return property != 'button' && isMousePressEvent(event) ? 1 : 0 -} - -function getMouseEventOptions(event, init, clickCount = 0) { - init = init || {} - return { - ...init, - // https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/detail - detail: - event === 'mousedown' || event === 'mouseup' || event === 'click' - ? 1 + clickCount - : clickCount, - buttons: convertMouseButtons(event, init, 'buttons', NAMES_TO_BUTTONS), - button: convertMouseButtons(event, init, 'button', NAMES_TO_BUTTON), - } -} - -// Absolutely NO events fire on label elements that contain their control -// if that control is disabled. NUTS! -// no joke. There are NO events for: