From b07bf6bf942e040d73f7984de018651ceacc1192 Mon Sep 17 00:00:00 2001 From: Alex Krolick Date: Tue, 6 Nov 2018 14:20:30 -0800 Subject: [PATCH] docs: add custom query example (#146) - show the much-asked-for data-test-id override as the default example - use commonjs module syntax due to a babel 6 issue with overwriting named exports (not present in v7) **What**: **Why**: **How**: **Checklist**: - [ ] Documentation - [ ] Tests - [x] Ready to be merged - [ ] Added myself to contributors table --- README.md | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/README.md b/README.md index a61eae93..4339e125 100644 --- a/README.md +++ b/README.md @@ -710,6 +710,49 @@ expect(queryByTestId(container, 'greetings')).not.toHaveTextContent('Bye bye') Check out [jest-dom's documentation](https://github.com/gnapse/jest-dom#readme) for a full list of available matchers. +## Custom Queries + +`dom-testing-library` exposes many of the helper functions that are used to implement the default queries. You can use the helpers to build custom queries. For example, the code below shows a way to override the default `testId` queries to use a different data-attribute. (Note: test files would import `test-utils.js` instead of using `dom-testing-library` directly). + +```js +// test-utils.js +const domTestingLib = require('dom-testing-library') +const {queryHelpers} = domTestingLib + +export const queryByTestId = queryHelpers.queryByAttribute.bind( + null, + 'data-test-id', +) +export const queryAllByTestId = queryHelpers.queryAllByAttribute.bind( + null, + 'data-test-id', +) + +export function getAllByTestId(container, id, ...rest) { + const els = queryAllByTestId(container, id, ...rest) + if (!els.length) { + throw queryHelpers.getElementError( + `Unable to find an element by: [data-test-id="${id}"]`, + container, + ) + } + return els +} + +export function getByTestId(...args) { + return queryHelpers.firstResultOrNull(getAllByTestId, ...args) +} + +// re-export with overrides +module.exports = { + ...domTestingLib, + getByTestId, + getAllByTestId, + queryByTestId, + queryAllByTestId, +} +``` + ### Using other assertion libraries If you're not using jest, you may be able to find a similar set of custom