-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make
@testing-library/dom
dependency optional (#319)
- Loading branch information
Showing
3 changed files
with
183 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |