@@ -6,6 +6,10 @@ beforeAll(() => {
6
6
configure ( { throwSuggestions : true } )
7
7
} )
8
8
9
+ afterEach ( ( ) => {
10
+ configure ( { testIdAttribute : 'data-testid' } )
11
+ } )
12
+
9
13
afterAll ( ( ) => {
10
14
configure ( { throwSuggestions : false } )
11
15
} )
@@ -100,18 +104,18 @@ test('should suggest getByRole when used with getBy', () => {
100
104
renderIntoDocument ( `<button data-testid="foo">submit</button>` )
101
105
102
106
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 })
105
109
106
110
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
+ `)
115
119
} )
116
120
117
121
test ( 'should suggest getAllByRole when used with getAllByTestId' , ( ) => {
@@ -121,27 +125,27 @@ test('should suggest getAllByRole when used with getAllByTestId', () => {
121
125
122
126
expect ( ( ) => screen . getAllByTestId ( 'foo' ) )
123
127
. 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
+ `)
145
149
} )
146
150
test ( 'should suggest findByRole when used with findByTestId' , async ( ) => {
147
151
renderIntoDocument ( `
@@ -435,6 +439,34 @@ test('getSuggestedQuery can return specified methods in addition to the best', (
435
439
expect ( getSuggestedQuery ( button , 'get' , 'TestId' ) ) . toBeUndefined ( )
436
440
} )
437
441
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
+
438
470
test ( 'getSuggestedQuery does not create suggestions for script and style elements' , ( ) => {
439
471
const { container} = render ( `
440
472
<script data-testid="script"></script>
0 commit comments