Skip to content

Commit 6dda2da

Browse files
committed
Add assertions for real and fake runtime
1 parent ab64018 commit 6dda2da

11 files changed

+77
-90
lines changed

src/__tests__/ariaAttributes.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ test('`selected: true` matches `aria-selected="true"` on supported roles', () =>
142142
'selected-listbox-option',
143143
])
144144

145-
expect(
146-
getAllByRole('rowheader', {selected: true}).map(({id}) => id),
147-
).toEqual(['selected-rowheader', 'selected-native-rowheader'])
145+
expect(getAllByRole('rowheader', {selected: true}).map(({id}) => id)).toEqual(
146+
['selected-rowheader', 'selected-native-rowheader'],
147+
)
148148

149149
expect(getAllByRole('treeitem', {selected: true}).map(({id}) => id)).toEqual([
150150
'selected-treeitem',

src/__tests__/element-queries.js

+6-15
Original file line numberDiff line numberDiff line change
@@ -1099,11 +1099,8 @@ test('getByText ignores script tags by default', () => {
10991099
})
11001100

11011101
test('get/query input element by current value', () => {
1102-
const {
1103-
getByDisplayValue,
1104-
queryByDisplayValue,
1105-
getByTestId,
1106-
} = renderIntoDocument(`
1102+
const {getByDisplayValue, queryByDisplayValue, getByTestId} =
1103+
renderIntoDocument(`
11071104
<div>
11081105
<input placeholder="name" type="text" data-testid="name" value="Mercury" />
11091106
</div>
@@ -1119,11 +1116,8 @@ test('get/query input element by current value', () => {
11191116
})
11201117

11211118
test('get/query select element by current value', () => {
1122-
const {
1123-
getByDisplayValue,
1124-
queryByDisplayValue,
1125-
getByTestId,
1126-
} = renderIntoDocument(`
1119+
const {getByDisplayValue, queryByDisplayValue, getByTestId} =
1120+
renderIntoDocument(`
11271121
<select id="state-select" data-testid="state">
11281122
<option value="">State</option>
11291123
<option value="AL">Alabama</option>
@@ -1141,11 +1135,8 @@ test('get/query select element by current value', () => {
11411135
})
11421136

11431137
test('get/query textarea element by current value', () => {
1144-
const {
1145-
getByDisplayValue,
1146-
queryByDisplayValue,
1147-
getByTestId,
1148-
} = renderIntoDocument(`
1138+
const {getByDisplayValue, queryByDisplayValue, getByTestId} =
1139+
renderIntoDocument(`
11491140
<textarea id="content-textarea" data-testid="content">
11501141
Hello
11511142
</textarea>

src/__tests__/fake-timers.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ async function runWaitFor({time = 300} = {}, options) {
1111
await waitFor(() => expect(result).toBe(response), options)
1212
}
1313

14+
afterEach(() => {
15+
jest.useRealTimers()
16+
})
17+
1418
test('real timers', async () => {
1519
// the only difference when not using fake timers is this test will
1620
// have to wait the full length of the timeout
@@ -40,9 +44,10 @@ test('fake timer timeout', async () => {
4044
})
4145

4246
test('times out after 1000ms by default', async () => {
47+
const startReal = performance.now()
4348
jest.useFakeTimers()
4449
const {container} = render(`<div></div>`)
45-
const start = performance.now()
50+
const startFake = performance.now()
4651
// there's a bug with this rule here...
4752
// eslint-disable-next-line jest/valid-expect
4853
await expect(
@@ -51,7 +56,13 @@ test('times out after 1000ms by default', async () => {
5156
`Timed out in waitForElementToBeRemoved.`,
5257
)
5358
// NOTE: this assertion ensures that the timeout runs in the declared (fake) clock.
54-
expect(performance.now() - start).toBeGreaterThanOrEqual(1000)
59+
expect(performance.now() - startFake).toBeGreaterThanOrEqual(1000)
60+
jest.useRealTimers()
61+
// NOTE: this assertion ensures that the timeout runs in the declared (fake) clock
62+
// while in real time the time was only a fraction since the real clock is only bound by the CPU.
63+
// So 20ms is really just an approximation on how long the CPU needs to execute our code.
64+
// If people want to timeout in real time they should rely on their test runners.
65+
expect(performance.now() - startReal).toBeLessThanOrEqual(20)
5566
})
5667

5768
test('recursive timers do not cause issues', async () => {

src/get-user-code-frame.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ try {
77
const nodeRequire = module && module.require
88

99
readFileSync = nodeRequire.call(module, 'fs').readFileSync
10-
codeFrameColumns = nodeRequire.call(module, '@babel/code-frame')
11-
.codeFrameColumns
10+
codeFrameColumns = nodeRequire.call(
11+
module,
12+
'@babel/code-frame',
13+
).codeFrameColumns
1214
chalk = nodeRequire.call(module, 'chalk')
1315
} catch {
1416
// We're in a browser environment

src/queries/role.js

+4-9
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ function queryAllByRole(
148148

149149
return matches(
150150
computeAccessibleName(element, {
151-
computedStyleSupportsPseudoElements: getConfig()
152-
.computedStyleSupportsPseudoElements,
151+
computedStyleSupportsPseudoElements:
152+
getConfig().computedStyleSupportsPseudoElements,
153153
}),
154154
element,
155155
name,
@@ -227,13 +227,8 @@ const queryAllByRoleWithSuggestions = wrapAllByQueryWithSuggestion(
227227
queryAllByRole.name,
228228
'queryAll',
229229
)
230-
const [
231-
queryByRole,
232-
getAllByRole,
233-
getByRole,
234-
findAllByRole,
235-
findByRole,
236-
] = buildQueries(queryAllByRole, getMultipleError, getMissingError)
230+
const [queryByRole, getAllByRole, getByRole, findAllByRole, findByRole] =
231+
buildQueries(queryAllByRole, getMultipleError, getMissingError)
237232

238233
export {
239234
queryByRole,

src/queries/text.ts

+2-7
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,8 @@ const queryAllByTextWithSuggestions = wrapAllByQueryWithSuggestion(
5151
'queryAll',
5252
)
5353

54-
const [
55-
queryByText,
56-
getAllByText,
57-
getByText,
58-
findAllByText,
59-
findByText,
60-
] = buildQueries(queryAllByText, getMultipleError, getMissingError)
54+
const [queryByText, getAllByText, getByText, findAllByText, findByText] =
55+
buildQueries(queryAllByText, getMultipleError, getMissingError)
6156

6257
export {
6358
queryByText,

src/queries/title.ts

+2-7
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,8 @@ const queryAllByTitleWithSuggestions = wrapAllByQueryWithSuggestion(
4242
'queryAll',
4343
)
4444

45-
const [
46-
queryByTitle,
47-
getAllByTitle,
48-
getByTitle,
49-
findAllByTitle,
50-
findByTitle,
51-
] = buildQueries(queryAllByTitle, getMultipleError, getMissingError)
45+
const [queryByTitle, getAllByTitle, getByTitle, findAllByTitle, findByTitle] =
46+
buildQueries(queryAllByTitle, getMultipleError, getMissingError)
5247

5348
export {
5449
queryByTitle,

src/query-helpers.js

+36-38
Original file line numberDiff line numberDiff line change
@@ -100,49 +100,47 @@ function makeFindQuery(getter) {
100100
}
101101
}
102102

103-
const wrapSingleQueryWithSuggestion = (query, queryAllByName, variant) => (
104-
container,
105-
...args
106-
) => {
107-
const element = query(container, ...args)
108-
const [{suggest = getConfig().throwSuggestions} = {}] = args.slice(-1)
109-
if (element && suggest) {
110-
const suggestion = getSuggestedQuery(element, variant)
111-
if (suggestion && !queryAllByName.endsWith(suggestion.queryName)) {
112-
throw getSuggestionError(suggestion.toString(), container)
103+
const wrapSingleQueryWithSuggestion =
104+
(query, queryAllByName, variant) =>
105+
(container, ...args) => {
106+
const element = query(container, ...args)
107+
const [{suggest = getConfig().throwSuggestions} = {}] = args.slice(-1)
108+
if (element && suggest) {
109+
const suggestion = getSuggestedQuery(element, variant)
110+
if (suggestion && !queryAllByName.endsWith(suggestion.queryName)) {
111+
throw getSuggestionError(suggestion.toString(), container)
112+
}
113113
}
114-
}
115114

116-
return element
117-
}
115+
return element
116+
}
118117

119-
const wrapAllByQueryWithSuggestion = (query, queryAllByName, variant) => (
120-
container,
121-
...args
122-
) => {
123-
const els = query(container, ...args)
124-
125-
const [{suggest = getConfig().throwSuggestions} = {}] = args.slice(-1)
126-
if (els.length && suggest) {
127-
// get a unique list of all suggestion messages. We are only going to make a suggestion if
128-
// all the suggestions are the same
129-
const uniqueSuggestionMessages = [
130-
...new Set(
131-
els.map(element => getSuggestedQuery(element, variant)?.toString()),
132-
),
133-
]
134-
135-
if (
136-
// only want to suggest if all the els have the same suggestion.
137-
uniqueSuggestionMessages.length === 1 &&
138-
!queryAllByName.endsWith(getSuggestedQuery(els[0], variant).queryName)
139-
) {
140-
throw getSuggestionError(uniqueSuggestionMessages[0], container)
118+
const wrapAllByQueryWithSuggestion =
119+
(query, queryAllByName, variant) =>
120+
(container, ...args) => {
121+
const els = query(container, ...args)
122+
123+
const [{suggest = getConfig().throwSuggestions} = {}] = args.slice(-1)
124+
if (els.length && suggest) {
125+
// get a unique list of all suggestion messages. We are only going to make a suggestion if
126+
// all the suggestions are the same
127+
const uniqueSuggestionMessages = [
128+
...new Set(
129+
els.map(element => getSuggestedQuery(element, variant)?.toString()),
130+
),
131+
]
132+
133+
if (
134+
// only want to suggest if all the els have the same suggestion.
135+
uniqueSuggestionMessages.length === 1 &&
136+
!queryAllByName.endsWith(getSuggestedQuery(els[0], variant).queryName)
137+
) {
138+
throw getSuggestionError(uniqueSuggestionMessages[0], container)
139+
}
141140
}
142-
}
143141

144-
return els
145-
}
142+
return els
143+
}
146144

147145
function buildQueries(queryAllBy, getMultipleError, getMissingError) {
148146
const queryBy = wrapSingleQueryWithSuggestion(

src/role-helpers.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ function prettyRoles(dom, {hidden}) {
188188
const elementsString = elements
189189
.map(el => {
190190
const nameString = `Name "${computeAccessibleName(el, {
191-
computedStyleSupportsPseudoElements: getConfig()
192-
.computedStyleSupportsPseudoElements,
191+
computedStyleSupportsPseudoElements:
192+
getConfig().computedStyleSupportsPseudoElements,
193193
})}":\n`
194194
const domString = prettyDOM(el.cloneNode(false))
195195
return `${nameString}${domString}`

src/suggestions.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ export function getSuggestedQuery(element, variant = 'get', method) {
8686
return makeSuggestion('Role', element, role, {
8787
variant,
8888
name: computeAccessibleName(element, {
89-
computedStyleSupportsPseudoElements: getConfig()
90-
.computedStyleSupportsPseudoElements,
89+
computedStyleSupportsPseudoElements:
90+
getConfig().computedStyleSupportsPseudoElements,
9191
}),
9292
})
9393
}

types/role-helpers.d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export function logRoles(container: HTMLElement): string
2-
export function getRoles(
3-
container: HTMLElement,
4-
): {[index: string]: HTMLElement[]}
2+
export function getRoles(container: HTMLElement): {
3+
[index: string]: HTMLElement[]
4+
}
55
/**
66
* https://testing-library.com/docs/dom-testing-library/api-helpers#isinaccessible
77
*/

0 commit comments

Comments
 (0)