Skip to content

Commit 8e632d5

Browse files
authored
fix: correctly type testcafe bound queries (#249)
* fix(typings): Correctly type function MatcherOptions * Remove unused import * no need for default value
1 parent f9bae3f commit 8e632d5

File tree

5 files changed

+43
-18
lines changed

5 files changed

+43
-18
lines changed

Diff for: src/index.ts

+20-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { ClientFunction, Selector } from "testcafe";
2-
import { queries } from "@testing-library/dom";
3-
import type { Options, QueryName, WithinSelectors } from "./types";
2+
import { Matcher, queries } from "@testing-library/dom";
3+
import type {
4+
Options,
5+
QueryName,
6+
QueryOptions,
7+
WithinSelectors,
8+
} from "./types";
49

510
declare global {
611
interface Window {
@@ -68,18 +73,19 @@ function isSelector(sel: any): sel is Selector {
6873

6974
const bindFunction = <T extends QueryName>(queryName: T) => {
7075
const query = queryName.replace("find", "query") as T;
71-
return Selector(
72-
(matcher, ...options) => {
73-
return window.TestingLibraryDom[query](
74-
document.body,
75-
matcher,
76-
...options
77-
) as Node | Node[] | NodeList | HTMLCollection;
78-
},
79-
{
80-
dependencies: { query },
81-
}
82-
);
76+
return (matcher: Matcher, options?: QueryOptions) => {
77+
return Selector(
78+
() =>
79+
window.TestingLibraryDom[query](document.body, matcher, options) as
80+
| Node
81+
| Node[]
82+
| NodeList
83+
| HTMLCollection,
84+
{
85+
dependencies: { query, matcher, options },
86+
}
87+
);
88+
};
8389
};
8490

8591
export const getByLabelText = bindFunction("getByLabelText");

Diff for: src/types.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { Config, BoundFunction, queries } from "@testing-library/dom";
1+
import {
2+
Config,
3+
BoundFunction,
4+
queries,
5+
Matcher,
6+
MatcherOptions,
7+
} from "@testing-library/dom";
28

39
export type Options = Pick<Config, "testIdAttribute">;
410

@@ -12,4 +18,6 @@ export type TestcafeBoundFunctions<T> = {
1218

1319
export type QueryName = keyof typeof queries;
1420

21+
export type QueryOptions = MatcherOptions;
22+
1523
export type WithinSelectors = TestcafeBoundFunctions<typeof queries>;

Diff for: test-app/index.html

+6
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ <h2>navigate</h2>
107107
document
108108
.querySelector('[data-testid="image-with-random-alt-tag"]')
109109
.setAttribute("alt", "Image Random Alt Text " + Math.random());
110+
111+
setTimeout(() => {
112+
document
113+
.querySelector("body")
114+
.appendChild(document.createTextNode("Late content!"));
115+
}, 15000);
110116
</script>
111117
</body>
112118
</html>

Diff for: tests/testcafe/selectors.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
getByAltText,
66
getByTestId,
77
getAllByText,
8+
queryByText,
89
queryAllByText,
910
findByText,
1011
} from "../../src/";
@@ -22,6 +23,10 @@ test("getByText", async (t) => {
2223
await t.click(getByText("getByText"));
2324
});
2425

26+
test("queryByText with timeout as property", async (t) => {
27+
await t.click(queryByText("Late content!").with({ timeout: 20000 }));
28+
});
29+
2530
test("getByLabelText", async (t) => {
2631
await t.typeText(
2732
getByLabelText("Label For Input Labelled By Id"),
@@ -54,7 +59,6 @@ test("queryAllByText", async (t) => {
5459

5560
test("findByText async", async (t) => {
5661
await t.click(getByText("delayed"));
57-
5862
await t.expect(findByText("updated button async")).ok();
5963
});
6064

Diff for: tests/testcafe/within.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ test("works with nested selectors", async (t) => {
4343
test('works with nested selector from "All" query with index - regex', async (t) => {
4444
const nestedDivs = getAllByTestId(/nested/);
4545
await t.expect(nestedDivs.count).eql(2);
46+
4647
const nested = within(nestedDivs.nth(1));
4748

4849
await t
@@ -61,8 +62,8 @@ test('works with nested selector from "All" query with index - exact:false', asy
6162
});
6263

6364
test('works with nested selector from "All" query with index - function', async (t) => {
64-
const nestedDivs = getAllByTestId((_content: any, element: any) =>
65-
element.getAttribute("data-testid").startsWith("nested")
65+
const nestedDivs = getAllByTestId((_content, element) =>
66+
element.getAttribute("data-testid")!.startsWith("nested")
6667
);
6768
await t.expect(nestedDivs.count).eql(2);
6869
const nested = await within(nestedDivs.nth(0));

0 commit comments

Comments
 (0)