Skip to content

Commit 0bdea33

Browse files
author
Kent C. Dodds
committed
use buildQueries
1 parent 7cdb55d commit 0bdea33

File tree

9 files changed

+83
-107
lines changed

9 files changed

+83
-107
lines changed

src/queries/alt-text.js

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import {
2-
makeFindQuery,
3-
matches,
4-
fuzzyMatches,
5-
makeNormalizer,
6-
makeSingleQuery,
7-
makeGetAllQuery,
8-
} from './all-utils'
1+
import {matches, fuzzyMatches, makeNormalizer, buildQueries} from './all-utils'
92

103
function queryAllByAltText(
114
container,
@@ -21,15 +14,15 @@ function queryAllByAltText(
2114

2215
const getMultipleError = (c, alt) =>
2316
`Found multiple elements with the alt text: ${alt}`
24-
const queryByAltText = makeSingleQuery(queryAllByAltText, getMultipleError)
25-
const getAllByAltText = makeGetAllQuery(
26-
queryAllByAltText,
27-
(c, alt) => `Unable to find an element with the alt text: ${alt}`,
28-
)
29-
const getByAltText = makeSingleQuery(getAllByAltText, getMultipleError)
30-
31-
const findAllByAltText = makeFindQuery(getAllByAltText)
32-
const findByAltText = makeFindQuery(getByAltText)
17+
const getMissingError = (c, alt) =>
18+
`Unable to find an element with the alt text: ${alt}`
19+
const [
20+
queryByAltText,
21+
getAllByAltText,
22+
getByAltText,
23+
findAllByAltText,
24+
findByAltText,
25+
] = buildQueries(queryAllByAltText, getMultipleError, getMissingError)
3326

3427
export {
3528
queryByAltText,

src/queries/display-value.js

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import {
2-
makeFindQuery,
32
getNodeText,
43
matches,
54
fuzzyMatches,
65
makeNormalizer,
7-
makeSingleQuery,
8-
makeGetAllQuery,
6+
buildQueries,
97
} from './all-utils'
108

119
function queryAllByDisplayValue(
@@ -33,21 +31,16 @@ function queryAllByDisplayValue(
3331

3432
const getMultipleError = (c, value) =>
3533
`Found multiple elements with the value: ${value}.`
36-
const queryByDisplayValue = makeSingleQuery(
37-
queryAllByDisplayValue,
38-
getMultipleError,
39-
)
40-
const getAllByDisplayValue = makeGetAllQuery(
41-
queryAllByDisplayValue,
42-
(c, value) => `Unable to find an element with the value: ${value}.`,
43-
)
44-
const getByDisplayValue = makeSingleQuery(
45-
getAllByDisplayValue,
46-
getMultipleError,
47-
)
34+
const getMissingError = (c, value) =>
35+
`Unable to find an element with the value: ${value}.`
4836

49-
const findAllByDisplayValue = makeFindQuery(getAllByDisplayValue)
50-
const findByDisplayValue = makeFindQuery(getByDisplayValue)
37+
const [
38+
queryByDisplayValue,
39+
getAllByDisplayValue,
40+
getByDisplayValue,
41+
findAllByDisplayValue,
42+
findByDisplayValue,
43+
] = buildQueries(queryAllByDisplayValue, getMultipleError, getMissingError)
5144

5245
export {
5346
queryByDisplayValue,

src/queries/label-text.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ function getAllByLabelText(container, text, ...rest) {
110110
return els
111111
}
112112

113+
// the reason mentioned above is the same reason we're not using buildQueries
113114
const getMultipleError = (c, text) =>
114115
`Found multiple elements with the text of: ${text}`
115116
const queryByLabelText = makeSingleQuery(queryAllByLabelText, getMultipleError)

src/queries/placeholder-text.js

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,19 @@
1-
import {
2-
queryAllByAttribute,
3-
makeFindQuery,
4-
makeSingleQuery,
5-
makeGetAllQuery,
6-
} from './all-utils'
1+
import {queryAllByAttribute, buildQueries} from './all-utils'
72

83
const queryAllByPlaceholderText = queryAllByAttribute.bind(null, 'placeholder')
94

105
const getMultipleError = (c, text) =>
116
`Found multiple elements with the placeholder text of: ${text}`
12-
const queryByPlaceholderText = makeSingleQuery(
13-
queryAllByPlaceholderText,
14-
getMultipleError,
15-
)
16-
const getAllByPlaceholderText = makeGetAllQuery(
17-
queryAllByPlaceholderText,
18-
(c, text) =>
19-
`Unable to find an element with the placeholder text of: ${text}`,
20-
)
21-
const getByPlaceholderText = makeSingleQuery(
22-
getAllByPlaceholderText,
23-
getMultipleError,
24-
)
7+
const getMissingError = (c, text) =>
8+
`Unable to find an element with the placeholder text of: ${text}`
259

26-
const findByPlaceholderText = makeFindQuery(getByPlaceholderText)
27-
const findAllByPlaceholderText = makeFindQuery(getAllByPlaceholderText)
10+
const [
11+
queryByPlaceholderText,
12+
getAllByPlaceholderText,
13+
getByPlaceholderText,
14+
findAllByPlaceholderText,
15+
findByPlaceholderText,
16+
] = buildQueries(queryAllByPlaceholderText, getMultipleError, getMissingError)
2817

2918
export {
3019
queryByPlaceholderText,

src/queries/role.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
1-
import {
2-
queryAllByAttribute,
3-
makeSingleQuery,
4-
makeGetAllQuery,
5-
makeFindQuery,
6-
} from './all-utils'
1+
import {queryAllByAttribute, buildQueries} from './all-utils'
72

83
const queryAllByRole = queryAllByAttribute.bind(null, 'role')
94

105
const getMultipleError = (c, id) => `Found multiple elements by [role=${id}]`
11-
const queryByRole = makeSingleQuery(queryAllByRole, getMultipleError)
12-
const getAllByRole = makeGetAllQuery(
13-
queryAllByRole,
14-
(c, id) => `Unable to find an element by [role=${id}]`,
15-
)
16-
const getByRole = makeSingleQuery(getAllByRole, getMultipleError)
6+
const getMissingError = (c, id) => `Unable to find an element by [role=${id}]`
177

18-
const findAllByRole = makeFindQuery(getAllByRole)
19-
const findByRole = makeFindQuery(getByRole)
8+
const [
9+
queryByRole,
10+
getAllByRole,
11+
getByRole,
12+
findAllByRole,
13+
findByRole,
14+
] = buildQueries(queryAllByRole, getMultipleError, getMissingError)
2015

2116
export {
2217
queryByRole,

src/queries/test-id.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import {
2-
queryAllByAttribute,
3-
makeFindQuery,
4-
getConfig,
5-
makeSingleQuery,
6-
makeGetAllQuery,
7-
} from './all-utils'
1+
import {queryAllByAttribute, getConfig, buildQueries} from './all-utils'
82

93
const getTestIdAttribute = () => getConfig().testIdAttribute
104

@@ -13,15 +7,16 @@ const queryAllByTestId = (...args) =>
137

148
const getMultipleError = (c, id) =>
159
`Found multiple elements by: [${getTestIdAttribute()}="${id}"]`
16-
const queryByTestId = makeSingleQuery(queryAllByTestId, getMultipleError)
17-
const getAllByTestId = makeGetAllQuery(
18-
queryAllByTestId,
19-
(c, id) => `Unable to find an element by: [${getTestIdAttribute()}="${id}"]`,
20-
)
21-
const getByTestId = makeSingleQuery(getAllByTestId, getMultipleError)
10+
const getMissingError = (c, id) =>
11+
`Unable to find an element by: [${getTestIdAttribute()}="${id}"]`
2212

23-
const findByTestId = makeFindQuery(getByTestId)
24-
const findAllByTestId = makeFindQuery(getAllByTestId)
13+
const [
14+
queryByTestId,
15+
getAllByTestId,
16+
getByTestId,
17+
findAllByTestId,
18+
findByTestId,
19+
] = buildQueries(queryAllByTestId, getMultipleError, getMissingError)
2520

2621
export {
2722
queryByTestId,

src/queries/text.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ import {
33
matches,
44
makeNormalizer,
55
getNodeText,
6-
makeSingleQuery,
7-
makeGetAllQuery,
8-
makeFindQuery,
6+
buildQueries,
97
} from './all-utils'
108

119
function queryAllByText(
@@ -33,15 +31,16 @@ function queryAllByText(
3331

3432
const getMultipleError = (c, text) =>
3533
`Found multiple elements with the text: ${text}`
36-
const queryByText = makeSingleQuery(queryAllByText, getMultipleError)
37-
const getAllByText = makeGetAllQuery(
38-
queryAllByText,
39-
(c, text) =>
40-
`Unable to find an element with the text: ${text}. 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.`,
41-
)
42-
const getByText = makeSingleQuery(getAllByText, getMultipleError)
43-
const findByText = makeFindQuery(getByText)
44-
const findAllByText = makeFindQuery(getAllByText)
34+
const getMissingError = (c, text) =>
35+
`Unable to find an element with the text: ${text}. 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.`
36+
37+
const [
38+
queryByText,
39+
getAllByText,
40+
getByText,
41+
findAllByText,
42+
findByText,
43+
] = buildQueries(queryAllByText, getMultipleError, getMissingError)
4544

4645
export {
4746
queryByText,

src/queries/title.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ import {
33
matches,
44
makeNormalizer,
55
getNodeText,
6-
makeFindQuery,
7-
makeSingleQuery,
8-
makeGetAllQuery,
6+
buildQueries,
97
} from './all-utils'
108

119
function queryAllByTitle(
@@ -24,14 +22,16 @@ function queryAllByTitle(
2422

2523
const getMultipleError = (c, title) =>
2624
`Found multiple elements with the title: ${title}.`
27-
const queryByTitle = makeSingleQuery(queryAllByTitle, getMultipleError)
28-
const getAllByTitle = makeGetAllQuery(
29-
queryAllByTitle,
30-
(c, title) => `Unable to find an element with the title: ${title}.`,
31-
)
32-
const getByTitle = makeSingleQuery(getAllByTitle, getMultipleError)
33-
const findByTitle = makeFindQuery(getByTitle)
34-
const findAllByTitle = makeFindQuery(getAllByTitle)
25+
const getMissingError = (c, title) =>
26+
`Unable to find an element with the title: ${title}.`
27+
28+
const [
29+
queryByTitle,
30+
getAllByTitle,
31+
getByTitle,
32+
findAllByTitle,
33+
findByTitle,
34+
] = buildQueries(queryAllByTitle, getMultipleError, getMissingError)
3535

3636
export {
3737
queryByTitle,

src/query-helpers.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ function makeFindQuery(getter) {
100100
)
101101
}
102102

103+
function buildQueries(queryAllBy, getMultipleError, getMissingError) {
104+
const queryBy = makeSingleQuery(queryAllBy, getMultipleError)
105+
const getAllBy = makeGetAllQuery(queryAllBy, getMissingError)
106+
const getBy = makeSingleQuery(getAllBy, getMultipleError)
107+
const findAllBy = makeFindQuery(getAllBy)
108+
const findBy = makeFindQuery(getBy)
109+
110+
return [queryBy, getAllBy, getBy, findAllBy, findBy]
111+
}
112+
103113
export {
104114
debugDOM,
105115
getElementError,
@@ -109,4 +119,5 @@ export {
109119
makeSingleQuery,
110120
makeGetAllQuery,
111121
makeFindQuery,
122+
buildQueries,
112123
}

0 commit comments

Comments
 (0)