Skip to content

Commit 31040e5

Browse files
committed
move config around a bit
1 parent 8b8307a commit 31040e5

File tree

5 files changed

+21
-22
lines changed

5 files changed

+21
-22
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
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"`;
3+
exports[`getByLabelText query will throw the custom error returned by config.getElementError 1`] = `"My custom error: Unable to find a label with the text of: TEST QUERY"`;
44

5-
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."`;
5+
exports[`getByText query will throw the custom error returned by config.getElementError 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."`;

src/__tests__/get-by-errors.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ cases(
4747
)
4848

4949
test.each([['getByText'], ['getByLabelText']])(
50-
'%s query will throw the custom error returned by config.customGetElementError',
50+
'%s query will throw the custom error returned by config.getElementError',
5151
query => {
52-
const customGetElementError = jest.fn(
52+
const getElementError = jest.fn(
5353
(message, _container) => new Error(`My custom error: ${message}`),
5454
)
55-
configure({customGetElementError})
55+
configure({getElementError})
5656
document.body.innerHTML = '<div>Hello</div>'
5757
expect(() => screen[query]('TEST QUERY')).toThrowErrorMatchingSnapshot()
58-
expect(customGetElementError).toBeCalledTimes(1)
58+
expect(getElementError).toBeCalledTimes(1)
5959
},
6060
)
6161

src/config.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {prettyDOM} from './pretty-dom'
2+
13
// It would be cleaner for this to live inside './queries', but
24
// other parts of the code assume that all exports from
35
// './queries' are query functions.
@@ -16,7 +18,11 @@ let config = {
1618
defaultHidden: false,
1719

1820
// called when getBy* queries fail. (message, container) => Error
19-
customGetElementError: null,
21+
getElementError(message, container) {
22+
return new Error(
23+
[message, prettyDOM(container)].filter(Boolean).join('\n\n'),
24+
)
25+
},
2026
}
2127

2228
export function configure(newConfig) {

src/queries/label-text.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import {getConfig} from '../config'
12
import {
23
fuzzyMatches,
34
matches,
45
makeNormalizer,
5-
getElementError,
66
queryAllByAttribute,
77
makeFindQuery,
88
makeSingleQuery,
@@ -104,12 +104,12 @@ function getAllByLabelText(container, text, ...rest) {
104104
if (!els.length) {
105105
const labels = queryAllLabelsByText(container, text, ...rest)
106106
if (labels.length) {
107-
throw getElementError(
107+
throw getConfig().getElementError(
108108
`Found a label with the text of: ${text}, however no form control was found associated to that label. Make sure you're using the "for" attribute or "aria-labelledby" attribute correctly.`,
109109
container,
110110
)
111111
} else {
112-
throw getElementError(
112+
throw getConfig().getElementError(
113113
`Unable to find a label with the text of: ${text}`,
114114
container,
115115
)

src/query-helpers.js

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
1-
import {prettyDOM} from './pretty-dom'
21
import {fuzzyMatches, matches, makeNormalizer} from './matches'
32
import {waitForElement} from './wait-for-element'
43
import {getConfig} from './config'
54

6-
function getElementError(message, container) {
7-
const customGetElementError = getConfig().customGetElementError
8-
if (customGetElementError) {
9-
return customGetElementError(message, container)
10-
}
11-
return new Error([message, prettyDOM(container)].filter(Boolean).join('\n\n'))
12-
}
13-
145
function getMultipleElementsFoundError(message, container) {
15-
return getElementError(
6+
return getConfig().getElementError(
167
`${message}\n\n(If this is intentional, then use the \`*AllBy*\` variant of the query (like \`queryAllByText\`, \`getAllByText\`, or \`findAllByText\`)).`,
178
container,
189
)
@@ -64,7 +55,10 @@ function makeGetAllQuery(allQuery, getMissingError) {
6455
return (container, ...args) => {
6556
const els = allQuery(container, ...args)
6657
if (!els.length) {
67-
throw getElementError(getMissingError(container, ...args), container)
58+
throw getConfig().getElementError(
59+
getMissingError(container, ...args),
60+
container,
61+
)
6862
}
6963
return els
7064
}
@@ -91,7 +85,6 @@ function buildQueries(queryAllBy, getMultipleError, getMissingError) {
9185
}
9286

9387
export {
94-
getElementError,
9588
getMultipleElementsFoundError,
9689
queryAllByAttribute,
9790
queryByAttribute,

0 commit comments

Comments
 (0)