Skip to content

Commit

Permalink
feat(config): Add customGetElementError config option
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsa committed Mar 4, 2020
1 parent cf57dcd commit 8b8307a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/__tests__/__snapshots__/get-by-errors.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`getByLabelText query will throw the custom error returned by config.customGetElementError 1`] = `"My custom error: Unable to find a label with the text of: TEST QUERY"`;

exports[`getByText query will throw the custom error returned by config.customGetElementError 1`] = `"My custom error: Unable to find an element with the text: TEST QUERY. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible."`;
20 changes: 20 additions & 0 deletions src/__tests__/get-by-errors.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import cases from 'jest-in-case'
import {screen} from '../'
import {configure, getConfig} from '../config'
import {render} from './helpers/test-utils'

const originalConfig = getConfig()
beforeEach(() => {
configure(originalConfig)
})

cases(
'getBy* queries throw an error when there are multiple elements returned',
({name, query, html}) => {
Expand Down Expand Up @@ -39,6 +46,19 @@ cases(
},
)

test.each([['getByText'], ['getByLabelText']])(
'%s query will throw the custom error returned by config.customGetElementError',
query => {
const customGetElementError = jest.fn(
(message, _container) => new Error(`My custom error: ${message}`),
)
configure({customGetElementError})
document.body.innerHTML = '<div>Hello</div>'
expect(() => screen[query]('TEST QUERY')).toThrowErrorMatchingSnapshot()
expect(customGetElementError).toBeCalledTimes(1)
},
)

cases(
'queryBy* queries throw an error when there are multiple elements returned',
({name, query, html}) => {
Expand Down
3 changes: 3 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ let config = {
asyncWrapper: cb => cb(),
// default value for the `hidden` option in `ByRole` queries
defaultHidden: false,

// called when getBy* queries fail. (message, container) => Error
customGetElementError: null,
}

export function configure(newConfig) {
Expand Down
5 changes: 5 additions & 0 deletions src/query-helpers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import {prettyDOM} from './pretty-dom'
import {fuzzyMatches, matches, makeNormalizer} from './matches'
import {waitForElement} from './wait-for-element'
import {getConfig} from './config'

function getElementError(message, container) {
const customGetElementError = getConfig().customGetElementError
if (customGetElementError) {
return customGetElementError(message, container)
}
return new Error([message, prettyDOM(container)].filter(Boolean).join('\n\n'))
}

Expand Down

0 comments on commit 8b8307a

Please sign in to comment.