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
2 changes: 2 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"prettier"
],
"rules": {
"testing-library/no-dom-import": "off",
"babel/new-cap": "off",
"babel/quotes": "off"
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"eslint": "^7.6.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-testcafe": "^0.2.1",
"kcd-scripts": "^6.0.0",
"kcd-scripts": "^7.2.1",
"npm-run-all": "^4.1.5",
"prettier": "^2.0.5",
"semantic-release": "^17.0.7",
Expand Down
14 changes: 10 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/no-implied-eval */
/* eslint-disable no-new-func */
import { ClientFunction, Selector } from "testcafe";
import { Matcher, queries } from "@testing-library/dom";
import type { Options, QueryName, WithinSelectors } from "./types";
Expand Down Expand Up @@ -27,6 +29,7 @@ const withinSelectors = queryNames.reduce((acc, withinQueryName) => {
)}.apply(null, args);
`),
};
// eslint-disable-next-line
}, {} as Record<QueryName, (node: Element, ...methodParams: any[]) => any>);

export async function configureOnce(options: Partial<Options>) {
Expand All @@ -46,10 +49,13 @@ const withWithinMethods = (selector: Selector) => {
returnDOMNodes: true,
}) as unknown) as WithinSelectors;
};
type SelectorArg =
| string
| Selector
| SelectorPromise
| (() => SelectorPromise);

export function within(
selector: string | Selector | SelectorPromise | (() => SelectorPromise)
): WithinSelectors {
export function within(selector: SelectorArg): WithinSelectors {
if (selector instanceof Function) {
return within(selector());
}
Expand All @@ -65,7 +71,7 @@ export function within(
}
}

function isSelector(sel: any): sel is Selector {
function isSelector(sel: SelectorArg): sel is Selector {
return sel.constructor.name === SELECTOR_TYPE;
}

Expand Down
8 changes: 8 additions & 0 deletions tests/testcafe/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"plugins": ["testcafe"],
"extends": ["plugin:testcafe/recommended", "prettier"],
"rules": {
"@typescript-eslint/no-unused-expressions": "off",
"jest/no-done-callback": "off"
}
}
12 changes: 6 additions & 6 deletions tests/testcafe/configure.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { configure, configureOnce, getByTestId, getByText } from "../../src";
import { configure, configureOnce, screen } from "../../src";

fixture`configure`.clientScripts(
configure({ testIdAttribute: "data-automation-id" })
).page`../../test-app/index.html`;

test("supports alternative testIdAttribute", async (t) => {
await t.click(getByTestId("image-with-random-alt-tag"));
await t.click(screen.getByTestId("image-with-random-alt-tag"));
});

test("still works after browser page load", async (t) => {
await t
.click(getByText("Go to Page 2"))
.click(getByTestId("page2-thing"))
.expect(getByText("second page").exists)
.click(screen.getByText("Go to Page 2"))
.click(screen.getByTestId("page2-thing"))
.expect(screen.getByText("second page").exists)
.ok();
});

test("can be used standalone", async (t) => {
await configureOnce({ testIdAttribute: "data-other-test-id" });
await t.click(getByTestId("other-id"));
await t.click(screen.getByTestId("other-id"));
});
11 changes: 1 addition & 10 deletions tests/testcafe/selectors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable testing-library/prefer-screen-queries */
import {
getByText,
getByPlaceholderText,
Expand Down Expand Up @@ -73,13 +74,3 @@ test("still works after reload", async (t) => {
await t.eval(() => location.reload(true));
await t.expect(getByText("getByText").exists).ok();
});

test.skip("getByTestId only throws the error message", async (t) => {
const testId = "Some random id";
const errorMessage = `Unable to find an element by: [data-testid="${testId}"]`;
try {
await t.click(getByText(testId));
} catch (e) {
await t.expect(e).contains(errorMessage);
}
});
8 changes: 7 additions & 1 deletion tests/testcafe/within.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable testing-library/prefer-screen-queries */
/* eslint-disable @typescript-eslint/await-thenable */
import { Selector } from "testcafe";
import { within, screen } from "../../src";

Expand Down Expand Up @@ -65,6 +67,7 @@ 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 = screen.getAllByTestId((_content, element) =>
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
element.getAttribute("data-testid")!.startsWith("nested")
);
await t.expect(nestedDivs.count).eql(2);
Expand All @@ -82,8 +85,10 @@ test("works on a standard testcafe nested selector", async (t) => {
test("should throw if invalid param", async (t) => {
let didThrow = false;
try {
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
await t.expect(within({ foo: "bar" }).getByText("baz").exists).ok();
// eslint-disable-next-line @typescript-eslint/no-implicit-any-catch
} catch (e) {
didThrow = true;
}
Expand All @@ -97,6 +102,7 @@ test("should throw error if count > 1", async (t) => {
let didThrow = false;
try {
await t.expect(within(nestedDivs).getByText("blah").exists);
// eslint-disable-next-line @typescript-eslint/no-implicit-any-catch
} catch (e) {
didThrow = true;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/selectors.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as allExports from "../../src";
import { queries } from "@testing-library/dom";
import * as allExports from "../../src";

it("exports expected exports", () => {
expect(allExports).toMatchObject(expect.any(Object));
Expand All @@ -20,7 +20,7 @@ it("exports expected exports", () => {
});

it("exports all dom-testing-library queries", () => {
let {
const {
configureOnce,
configure,
within,
Expand Down