From 88e8bb0b64b0c61678cbd7de34b24c5e21766e26 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 26 Aug 2023 13:23:11 +1200 Subject: [PATCH] feat: make `@testing-library/dom` dependency optional (#319) --- package.json | 5 ++ src/__tests__/queries.test.js | 146 ++++++++++++++++++++++++++++++++++ src/queries.js | 34 +++++++- 3 files changed, 183 insertions(+), 2 deletions(-) create mode 100644 src/__tests__/queries.test.js diff --git a/package.json b/package.json index 942431d..1a1cb46 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/__tests__/queries.test.js b/src/__tests__/queries.test.js new file mode 100644 index 0000000..90d23c8 --- /dev/null +++ b/src/__tests__/queries.test.js @@ -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); + }); +}); diff --git a/src/queries.js b/src/queries.js index cd8a64f..f77a707 100644 --- a/src/queries.js +++ b/src/queries.js @@ -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;