Skip to content

Commit f9bae3f

Browse files
fix: update to typescript 4.0.2 (#248)
* chore(deps-dev): bump typescript from 3.9.7 to 4.0.2 Bumps [typescript](https://github.com/Microsoft/TypeScript) from 3.9.7 to 4.0.2. - [Release notes](https://github.com/Microsoft/TypeScript/releases) - [Commits](microsoft/TypeScript@v3.9.7...v4.0.2) Signed-off-by: dependabot-preview[bot] <[email protected]> * fix(typings): narrow within selector type Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
1 parent b469319 commit f9bae3f

File tree

3 files changed

+27
-21
lines changed

3 files changed

+27
-21
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"semantic-release": "^17.0.7",
3838
"testcafe": "^1.8.4",
3939
"ts-jest": "^26.0.0",
40-
"typescript": "^3.8.3"
40+
"typescript": "^4.0.2"
4141
},
4242
"repository": {
4343
"type": "git",

src/index.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { ClientFunction, Selector } from "testcafe";
22
import { queries } from "@testing-library/dom";
3-
import type {
4-
Options,
5-
TestcafeBoundFunction,
6-
TestcafeBoundFunctions,
7-
} from "./types";
3+
import type { Options, QueryName, WithinSelectors } from "./types";
84

95
declare global {
106
interface Window {
@@ -14,7 +10,8 @@ declare global {
1410

1511
const SELECTOR_TYPE = Selector("")().constructor.name;
1612

17-
const withinSelectors = Object.keys(queries).reduce((acc, withinQueryName) => {
13+
const queryNames = Object.keys(queries) as QueryName[];
14+
const withinSelectors = queryNames.reduce((acc, withinQueryName) => {
1815
return {
1916
...acc,
2017
[withinQueryName]: new Function(`
@@ -27,12 +24,11 @@ const withinSelectors = Object.keys(queries).reduce((acc, withinQueryName) => {
2724
return window.TestingLibraryDom.within(el).${withinQueryName}.apply(null, args);
2825
`),
2926
};
30-
}, {} as Record<keyof typeof queries, (node: Element, ...methodParams: any[]) => any>);
27+
}, {} as Record<QueryName, (node: Element, ...methodParams: any[]) => any>);
3128

3229
export async function configureOnce(options: Partial<Options>) {
3330
const { content } = configure(options);
34-
// @ts-ignore
35-
await ClientFunction(new Function(content))();
31+
await ClientFunction(new Function(content) as () => Function)();
3632
}
3733

3834
export function configure(options: Partial<Options>) {
@@ -42,17 +38,23 @@ export function configure(options: Partial<Options>) {
4238
return { content: configFunction };
4339
}
4440

45-
export function within<T>(
46-
sel: string | Selector | SelectorPromise | TestcafeBoundFunction<T>
47-
): TestcafeBoundFunctions<typeof queries> {
48-
if (sel instanceof Function) {
49-
return within(sel());
41+
const withWithinMethods = (selector: Selector) => {
42+
return (selector.addCustomMethods(withinSelectors, {
43+
returnDOMNodes: true,
44+
}) as unknown) as WithinSelectors;
45+
};
46+
47+
export function within(
48+
selector: string | Selector | SelectorPromise | (() => SelectorPromise)
49+
): WithinSelectors {
50+
if (selector instanceof Function) {
51+
return within(selector());
5052
}
51-
if (isSelector(sel)) {
52-
// @ts-ignore
53-
return sel.addCustomMethods(withinSelectors, { returnDOMNodes: true });
54-
} else if (typeof sel === "string") {
55-
return within(Selector(sel));
53+
54+
if (isSelector(selector)) {
55+
return withWithinMethods(selector);
56+
} else if (typeof selector === "string") {
57+
return within(Selector(selector));
5658
} else {
5759
throw new Error(
5860
`"within" only accepts a query (getBy, queryBy, etc), string or testcafe Selector`
@@ -64,7 +66,7 @@ function isSelector(sel: any): sel is Selector {
6466
return sel.constructor.name === SELECTOR_TYPE;
6567
}
6668

67-
const bindFunction = <T extends keyof typeof queries>(queryName: T) => {
69+
const bindFunction = <T extends QueryName>(queryName: T) => {
6870
const query = queryName.replace("find", "query") as T;
6971
return Selector(
7072
(matcher, ...options) => {

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ export type TestcafeBoundFunction<T> = (
99
export type TestcafeBoundFunctions<T> = {
1010
[P in keyof T]: TestcafeBoundFunction<T[P]>;
1111
};
12+
13+
export type QueryName = keyof typeof queries;
14+
15+
export type WithinSelectors = TestcafeBoundFunctions<typeof queries>;

0 commit comments

Comments
 (0)