Skip to content

Commit

Permalink
feat: updating exported methods to be async
Browse files Browse the repository at this point in the history
Updating many things to be async after userEvent 14 announced all its exported methods would be
marked async

BREAKING CHANGE: All exported methods now return Promises
  • Loading branch information
Grunet committed Jan 27, 2022
1 parent e7d6b3a commit 3bc117c
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 70 deletions.
35 changes: 18 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
},
"homepage": "https://github.com/Grunet/keyboard-testing-library#readme",
"peerDependencies": {
"@testing-library/dom": "7.x",
"@testing-library/user-event": "13.x"
"@testing-library/dom": "^7.x",
"@testing-library/user-event": "^14.x"
},
"peerDependenciesMeta": {
"@testing-library/dom": {
Expand All @@ -70,7 +70,7 @@
},
"devDependencies": {
"@testing-library/dom": "^7.30.3",
"@testing-library/user-event": "^13.1.1",
"@testing-library/user-event": "14.0.0-beta.8",
"@typescript-eslint/eslint-plugin": "^4.15.0",
"@typescript-eslint/parser": "^4.15.0",
"commitizen": "^4.2.3",
Expand All @@ -90,7 +90,7 @@
"semantic-release": "^17.3.9",
"ts-jest": "^26.5.1",
"ts-node": "^9.1.1",
"typescript": "^4.1.5"
"typescript": "^4.5.5"
},
"config": {
"commitizen": {
Expand Down
40 changes: 20 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,28 @@ function __createKeyboardOnlyUserEvent() {
}
}
},
navigateTo(element: Element) {
__navigateToAndThrowIfNotFound(element, navigationActions, logger);
async navigateTo(element: Element) {
await __navigateToAndThrowIfNotFound(element, navigationActions, logger);
},
navigateToAndPressEnter(element: Element) {
__navigateToAndThrowIfNotFound(element, navigationActions, logger);
async navigateToAndPressEnter(element: Element) {
await __navigateToAndThrowIfNotFound(element, navigationActions, logger);

activationActions.enter(element);
await activationActions.enter(element);
},
navigateToAndPressSpacebar(element: Element) {
__navigateToAndThrowIfNotFound(element, navigationActions, logger);
async navigateToAndPressSpacebar(element: Element) {
await __navigateToAndThrowIfNotFound(element, navigationActions, logger);

activationActions.spacebar(element);
await activationActions.spacebar(element);
},
};
}

function __navigateToAndThrowIfNotFound(
async function __navigateToAndThrowIfNotFound(
element: Element,
navigationActions: INavigationActions,
logger: ILogger
) {
const foundElement = navigateTo(element, navigationActions, logger);
const foundElement = await navigateTo(element, navigationActions, logger);

if (!foundElement) {
throw new Error(
Expand Down Expand Up @@ -130,17 +130,17 @@ const testingLibShims: IKeyboardActions = {
navigation: {
tab:
userEvent &&
(() => {
userEvent.tab();
(async () => {
await userEvent.tab();
}),
shiftTab:
userEvent &&
(() => {
userEvent.tab({ shift: true });
(async () => {
await userEvent.tab({ shift: true });
}),
arrowUp:
fireEvent &&
((element) => {
(async (element) => {
fireEvent.keyDown(element, {
key: "ArrowUp",
code: "ArrowUp",
Expand All @@ -149,7 +149,7 @@ const testingLibShims: IKeyboardActions = {
}),
arrowRight:
fireEvent &&
((element) => {
(async (element) => {
fireEvent.keyDown(element, {
key: "ArrowRight",
code: "ArrowRight",
Expand All @@ -158,7 +158,7 @@ const testingLibShims: IKeyboardActions = {
}),
arrowDown:
fireEvent &&
((element) => {
(async (element) => {
fireEvent.keyDown(element, {
key: "ArrowDown",
code: "ArrowDown",
Expand All @@ -167,7 +167,7 @@ const testingLibShims: IKeyboardActions = {
}),
arrowLeft:
fireEvent &&
((element) => {
(async (element) => {
fireEvent.keyDown(element, {
key: "ArrowLeft",
code: "ArrowLeft",
Expand All @@ -178,7 +178,7 @@ const testingLibShims: IKeyboardActions = {
activation: {
enter:
fireEvent &&
((element) => {
(async (element) => {
fireEvent.keyDown(element, {
key: "Enter",
code: "Enter",
Expand All @@ -187,7 +187,7 @@ const testingLibShims: IKeyboardActions = {
}),
spacebar:
fireEvent &&
((element) => {
(async (element) => {
fireEvent.keyDown(element, {
key: " ",
code: "Space",
Expand Down
18 changes: 9 additions & 9 deletions src/navigateTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import {
ILogger,
} from "./shared/interfaces";

function navigateTo(
async function navigateTo(
element: Element,
navigationActions: INavigationActions,
logger: ILogger
): boolean {
const foundElement = findTarget(
): Promise<boolean> {
const foundElement = await findTarget(
element,
new KeyboardNavigationGraphAdapter(),
navigationActions,
Expand All @@ -19,12 +19,12 @@ function navigateTo(
return foundElement;
}

function findTarget(
async function findTarget(
targetEl: Element,
kngService: KeyboardNavigationGraphAdapter,
navigationActions: INavigationActions,
logger: ILogger
): boolean {
): Promise<boolean> {
let curEl = getCurrentlyFocusedEl();
/*eslint no-constant-condition: ["error", { "checkLoops": false }] -- to allow for the infinite while loop */
while (true) {
Expand All @@ -39,7 +39,7 @@ function findTarget(
}
logger?.capturePath(unexploredPath);

const newCurEl = followPath(
const newCurEl = await followPath(
curEl,
unexploredPath,
kngService,
Expand All @@ -51,12 +51,12 @@ function findTarget(
}
}

function followPath(
async function followPath(
startEl: Element,
pathOfActions: Array<keyof INavigationActions>,
kngService: KeyboardNavigationGraphAdapter,
navigationActions: INavigationActions
): Element {
): Promise<Element> {
const elsOnPath: Array<Element> = [startEl];
const remainingPath = [...pathOfActions];

Expand All @@ -65,7 +65,7 @@ function followPath(

const navActionToPerform = navigationActions[nextAction];

navActionToPerform(elsOnPath[0]);
await navActionToPerform(elsOnPath[0]);
const nextEl = getCurrentlyFocusedEl();

elsOnPath.unshift(nextEl);
Expand Down
16 changes: 8 additions & 8 deletions src/shared/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ interface IKeyboardActions {
}

interface INavigationActions {
tab: () => void;
shiftTab: () => void;
arrowUp: (element: Element) => void;
arrowRight: (element: Element) => void;
arrowDown: (element: Element) => void;
arrowLeft: (element: Element) => void;
tab: () => Promise<void>;
shiftTab: () => Promise<void>;
arrowUp: (element: Element) => Promise<void>;
arrowRight: (element: Element) => Promise<void>;
arrowDown: (element: Element) => Promise<void>;
arrowLeft: (element: Element) => Promise<void>;
}

/* Hack to export an iterable list of the interface's property names */
Expand Down Expand Up @@ -39,8 +39,8 @@ const unused1: areDuplicateNavigationActionNamesTheSame = true; //Should cause a
/* End hack */

interface IActivationActions {
enter: (element: Element) => void;
spacebar: (element: Element) => void;
enter: (element: Element) => Promise<void>;
spacebar: (element: Element) => Promise<void>;
}

/* Hack to export an iterable list of the interface's property names */
Expand Down
Loading

0 comments on commit 3bc117c

Please sign in to comment.