Skip to content

Commit 82c64f5

Browse files
committed
Use custom testIdAttribute in getSuggestedQuery
Currently `getSuggestedQuery` always uses `data-testid` to find relevant elements. This change updates it to use the configured `testIdAttribute` if one has been set in the configuration.
1 parent df57d7b commit 82c64f5

File tree

2 files changed

+65
-33
lines changed

2 files changed

+65
-33
lines changed

src/__tests__/suggestions.js

Lines changed: 63 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ beforeAll(() => {
66
configure({throwSuggestions: true})
77
})
88

9+
afterEach(() => {
10+
configure({testIdAttribute: 'data-testid'})
11+
})
12+
913
afterAll(() => {
1014
configure({throwSuggestions: false})
1115
})
@@ -100,18 +104,18 @@ test('should suggest getByRole when used with getBy', () => {
100104
renderIntoDocument(`<button data-testid="foo">submit</button>`)
101105

102106
expect(() => screen.getByTestId('foo')).toThrowErrorMatchingInlineSnapshot(`
103-
"A better query is available, try this:
104-
getByRole('button', { name: /submit/i })
107+
"A better query is available, try this:
108+
getByRole('button', { name: /submit/i })
105109
106110
107-
<body>
108-
<button
109-
data-testid="foo"
110-
>
111-
submit
112-
</button>
113-
</body>"
114-
`)
111+
<body>
112+
<button
113+
data-testid="foo"
114+
>
115+
submit
116+
</button>
117+
</body>"
118+
`)
115119
})
116120

117121
test('should suggest getAllByRole when used with getAllByTestId', () => {
@@ -121,27 +125,27 @@ test('should suggest getAllByRole when used with getAllByTestId', () => {
121125

122126
expect(() => screen.getAllByTestId('foo'))
123127
.toThrowErrorMatchingInlineSnapshot(`
124-
"A better query is available, try this:
125-
getAllByRole('button', { name: /submit/i })
126-
127-
128-
<body>
129-
130-
131-
<button
132-
data-testid="foo"
133-
>
134-
submit
135-
</button>
136-
137-
138-
<button
139-
data-testid="foo"
140-
>
141-
submit
142-
</button>
143-
</body>"
144-
`)
128+
"A better query is available, try this:
129+
getAllByRole('button', { name: /submit/i })
130+
131+
132+
<body>
133+
134+
135+
<button
136+
data-testid="foo"
137+
>
138+
submit
139+
</button>
140+
141+
142+
<button
143+
data-testid="foo"
144+
>
145+
submit
146+
</button>
147+
</body>"
148+
`)
145149
})
146150
test('should suggest findByRole when used with findByTestId', async () => {
147151
renderIntoDocument(`
@@ -435,6 +439,34 @@ test('getSuggestedQuery can return specified methods in addition to the best', (
435439
expect(getSuggestedQuery(button, 'get', 'TestId')).toBeUndefined()
436440
})
437441

442+
test('getSuggestedQuery works with custom testIdAttribute', () => {
443+
configure({testIdAttribute: 'data-test'})
444+
445+
const {container} = render(`
446+
<label for="username">label</label>
447+
<input
448+
id="username"
449+
name="name"
450+
placeholder="placeholder"
451+
data-test="testid"
452+
title="title"
453+
alt="alt"
454+
value="value"
455+
type="text"
456+
/>
457+
<button>button</button>
458+
`)
459+
460+
const input = container.querySelector('input')
461+
462+
expect(getSuggestedQuery(input, 'get', 'TestId')).toMatchObject({
463+
queryName: 'TestId',
464+
queryMethod: 'getByTestId',
465+
queryArgs: ['testid'],
466+
variant: 'get',
467+
})
468+
})
469+
438470
test('getSuggestedQuery does not create suggestions for script and style elements', () => {
439471
const {container} = render(`
440472
<script data-testid="script"></script>

src/suggestions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {computeAccessibleName} from 'dom-accessibility-api'
22
import {getDefaultNormalizer} from './matches'
33
import {getNodeText} from './get-node-text'
4-
import {DEFAULT_IGNORE_TAGS} from './config'
4+
import {DEFAULT_IGNORE_TAGS, getConfig} from './config'
55
import {getImplicitAriaRoles} from './role-helpers'
66

77
const normalize = getDefaultNormalizer()
@@ -120,7 +120,7 @@ export function getSuggestedQuery(element, variant = 'get', method) {
120120
return makeSuggestion('Title', title, {variant})
121121
}
122122

123-
const testId = element.getAttribute('data-testid')
123+
const testId = element.getAttribute(getConfig().testIdAttribute)
124124
if (canSuggest('TestId', method, testId)) {
125125
return makeSuggestion('TestId', testId, {variant})
126126
}

0 commit comments

Comments
 (0)