Skip to content
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
34 changes: 20 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { ClientFunction, Selector } from "testcafe";
import { queries } from "@testing-library/dom";
import type { Options, QueryName, WithinSelectors } from "./types";
import { Matcher, queries } from "@testing-library/dom";
import type {
Options,
QueryName,
QueryOptions,
WithinSelectors,
} from "./types";

declare global {
interface Window {
Expand Down Expand Up @@ -68,18 +73,19 @@ function isSelector(sel: any): sel is Selector {

const bindFunction = <T extends QueryName>(queryName: T) => {
const query = queryName.replace("find", "query") as T;
return Selector(
(matcher, ...options) => {
return window.TestingLibraryDom[query](
document.body,
matcher,
...options
) as Node | Node[] | NodeList | HTMLCollection;
},
{
dependencies: { query },
}
);
return (matcher: Matcher, options?: QueryOptions) => {
return Selector(
() =>
window.TestingLibraryDom[query](document.body, matcher, options) as
| Node
| Node[]
| NodeList
| HTMLCollection,
{
dependencies: { query, matcher, options },
}
);
};
};

export const getByLabelText = bindFunction("getByLabelText");
Expand Down
10 changes: 9 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { Config, BoundFunction, queries } from "@testing-library/dom";
import {
Config,
BoundFunction,
queries,
Matcher,
MatcherOptions,
} from "@testing-library/dom";

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

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

export type QueryName = keyof typeof queries;

export type QueryOptions = MatcherOptions;

export type WithinSelectors = TestcafeBoundFunctions<typeof queries>;
6 changes: 6 additions & 0 deletions test-app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ <h2>navigate</h2>
document
.querySelector('[data-testid="image-with-random-alt-tag"]')
.setAttribute("alt", "Image Random Alt Text " + Math.random());

setTimeout(() => {
document
.querySelector("body")
.appendChild(document.createTextNode("Late content!"));
}, 15000);
</script>
</body>
</html>
6 changes: 5 additions & 1 deletion tests/testcafe/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getByAltText,
getByTestId,
getAllByText,
queryByText,
queryAllByText,
findByText,
} from "../../src/";
Expand All @@ -22,6 +23,10 @@ test("getByText", async (t) => {
await t.click(getByText("getByText"));
});

test("queryByText with timeout as property", async (t) => {
await t.click(queryByText("Late content!").with({ timeout: 20000 }));
});

test("getByLabelText", async (t) => {
await t.typeText(
getByLabelText("Label For Input Labelled By Id"),
Expand Down Expand Up @@ -54,7 +59,6 @@ test("queryAllByText", async (t) => {

test("findByText async", async (t) => {
await t.click(getByText("delayed"));

await t.expect(findByText("updated button async")).ok();
});

Expand Down
5 changes: 3 additions & 2 deletions tests/testcafe/within.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ test("works with nested selectors", async (t) => {
test('works with nested selector from "All" query with index - regex', async (t) => {
const nestedDivs = getAllByTestId(/nested/);
await t.expect(nestedDivs.count).eql(2);

const nested = within(nestedDivs.nth(1));

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

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