Skip to content

Meemaw/typescript 4.0.2 #248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"semantic-release": "^17.0.7",
"testcafe": "^1.8.4",
"ts-jest": "^26.0.0",
"typescript": "^3.8.3"
"typescript": "^4.0.2"
},
"repository": {
"type": "git",
Expand Down
42 changes: 22 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { ClientFunction, Selector } from "testcafe";
import { queries } from "@testing-library/dom";
import type {
Options,
TestcafeBoundFunction,
TestcafeBoundFunctions,
} from "./types";
import type { Options, QueryName, WithinSelectors } from "./types";

declare global {
interface Window {
Expand All @@ -14,7 +10,8 @@ declare global {

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

const withinSelectors = Object.keys(queries).reduce((acc, withinQueryName) => {
const queryNames = Object.keys(queries) as QueryName[];
const withinSelectors = queryNames.reduce((acc, withinQueryName) => {
return {
...acc,
[withinQueryName]: new Function(`
Expand All @@ -27,12 +24,11 @@ const withinSelectors = Object.keys(queries).reduce((acc, withinQueryName) => {
return window.TestingLibraryDom.within(el).${withinQueryName}.apply(null, args);
`),
};
}, {} as Record<keyof typeof queries, (node: Element, ...methodParams: any[]) => any>);
}, {} as Record<QueryName, (node: Element, ...methodParams: any[]) => any>);

export async function configureOnce(options: Partial<Options>) {
const { content } = configure(options);
// @ts-ignore
await ClientFunction(new Function(content))();
await ClientFunction(new Function(content) as () => Function)();
}

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

export function within<T>(
sel: string | Selector | SelectorPromise | TestcafeBoundFunction<T>
): TestcafeBoundFunctions<typeof queries> {
if (sel instanceof Function) {
return within(sel());
const withWithinMethods = (selector: Selector) => {
return (selector.addCustomMethods(withinSelectors, {
returnDOMNodes: true,
}) as unknown) as WithinSelectors;
};

export function within(
selector: string | Selector | SelectorPromise | (() => SelectorPromise)
): WithinSelectors {
if (selector instanceof Function) {
return within(selector());
}
if (isSelector(sel)) {
// @ts-ignore
return sel.addCustomMethods(withinSelectors, { returnDOMNodes: true });
} else if (typeof sel === "string") {
return within(Selector(sel));

if (isSelector(selector)) {
return withWithinMethods(selector);
} else if (typeof selector === "string") {
return within(Selector(selector));
} else {
throw new Error(
`"within" only accepts a query (getBy, queryBy, etc), string or testcafe Selector`
Expand All @@ -64,7 +66,7 @@ function isSelector(sel: any): sel is Selector {
return sel.constructor.name === SELECTOR_TYPE;
}

const bindFunction = <T extends keyof typeof queries>(queryName: T) => {
const bindFunction = <T extends QueryName>(queryName: T) => {
const query = queryName.replace("find", "query") as T;
return Selector(
(matcher, ...options) => {
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ export type TestcafeBoundFunction<T> = (
export type TestcafeBoundFunctions<T> = {
[P in keyof T]: TestcafeBoundFunction<T[P]>;
};

export type QueryName = keyof typeof queries;

export type WithinSelectors = TestcafeBoundFunctions<typeof queries>;