Skip to content

Commit

Permalink
feat: make @testing-library/dom dependency optional (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored Aug 26, 2023
1 parent a7ee93c commit 88e8bb0
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 2 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
"@testing-library/dom": "^8.0.0 || ^9.0.0",
"eslint": "^6.8.0 || ^7.0.0 || ^8.0.0"
},
"peerDependenciesMeta": {
"@testing-library/dom": {
"optional": true
}
},
"eslintConfig": {
"extends": "./node_modules/kcd-scripts/eslint.js",
"rules": {
Expand Down
146 changes: 146 additions & 0 deletions src/__tests__/queries.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
const TestingLibraryDomRef = { throwWhenRequiring: false };

const requireQueries = (throwWhenRequiring) => {
jest.resetModules();

TestingLibraryDomRef.throwWhenRequiring = throwWhenRequiring;

return require("../queries");
};

jest.mock("@testing-library/dom", () => {
if (TestingLibraryDomRef.throwWhenRequiring) {
throw new (class extends Error {
constructor(message) {
super(message);
this.code = "MODULE_NOT_FOUND";
}
})();
}

return jest.requireActual("@testing-library/dom");
});

describe("when @testing-library/dom is not available", () => {
it("uses the default queries", () => {
const { queries } = requireQueries(true);

expect([...queries].sort()).toStrictEqual([
"findAllByAltText",
"findAllByDisplayValue",
"findAllByLabelText",
"findAllByPlaceholderText",
"findAllByRole",
"findAllByTestId",
"findAllByText",
"findAllByTitle",
"findByAltText",
"findByDisplayValue",
"findByLabelText",
"findByPlaceholderText",
"findByRole",
"findByTestId",
"findByText",
"findByTitle",
"getAllByAltText",
"getAllByDisplayValue",
"getAllByLabelText",
"getAllByPlaceholderText",
"getAllByRole",
"getAllByTestId",
"getAllByText",
"getAllByTitle",
"getByAltText",
"getByDisplayValue",
"getByLabelText",
"getByPlaceholderText",
"getByRole",
"getByTestId",
"getByText",
"getByTitle",
"queryAllByAltText",
"queryAllByDisplayValue",
"queryAllByLabelText",
"queryAllByPlaceholderText",
"queryAllByRole",
"queryAllByTestId",
"queryAllByText",
"queryAllByTitle",
"queryByAltText",
"queryByDisplayValue",
"queryByLabelText",
"queryByPlaceholderText",
"queryByRole",
"queryByTestId",
"queryByText",
"queryByTitle",
]);
});
});

describe("when @testing-library/dom is available", () => {
it("returns the queries from the library", () => {
const { queries } = requireQueries(false);

expect([...queries].sort()).toStrictEqual([
"findAllByAltText",
"findAllByDisplayValue",
"findAllByLabelText",
"findAllByPlaceholderText",
"findAllByRole",
"findAllByTestId",
"findAllByText",
"findAllByTitle",
"findByAltText",
"findByDisplayValue",
"findByLabelText",
"findByPlaceholderText",
"findByRole",
"findByTestId",
"findByText",
"findByTitle",
"getAllByAltText",
"getAllByDisplayValue",
"getAllByLabelText",
"getAllByPlaceholderText",
"getAllByRole",
"getAllByTestId",
"getAllByText",
"getAllByTitle",
"getByAltText",
"getByDisplayValue",
"getByLabelText",
"getByPlaceholderText",
"getByRole",
"getByTestId",
"getByText",
"getByTitle",
"queryAllByAltText",
"queryAllByDisplayValue",
"queryAllByLabelText",
"queryAllByPlaceholderText",
"queryAllByRole",
"queryAllByTestId",
"queryAllByText",
"queryAllByTitle",
"queryByAltText",
"queryByDisplayValue",
"queryByLabelText",
"queryByPlaceholderText",
"queryByRole",
"queryByTestId",
"queryByText",
"queryByTitle",
]);
});

it("re-throws unexpected errors", () => {
jest.mock("@testing-library/dom", () => {
throw new Error("oh noes!");
});

jest.resetModules();

expect(() => require("../queries")).toThrow(/oh noes!/iu);
});
});
34 changes: 32 additions & 2 deletions src/queries.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
import { queries as allQueries } from "@testing-library/dom";
let theQueries = [
"findAllBy",
"findBy",
"getAllBy",
"getBy",
"queryAllBy",
"queryBy",
].flatMap((prefix) =>
[
"AltText",
"DisplayValue",
"LabelText",
"PlaceholderText",
"Role",
"TestId",
"Text",
"Title",
].map((element) => `${prefix}${element}`)
);

export const queries = Object.keys(allQueries);
(() => {
try {
const { queries: allQueries } = require("@testing-library/dom");

theQueries = Object.keys(allQueries);
} catch (error) {
if (error.code !== "MODULE_NOT_FOUND") {
throw error;
}
}
})();

export const queries = theQueries;

0 comments on commit 88e8bb0

Please sign in to comment.