@@ -35,6 +35,18 @@ test('respects ignores', () => {
35
35
) . not . toThrowError ( )
36
36
} )
37
37
38
+ test ( 'does not suggest query that would give a different element' , ( ) => {
39
+ renderIntoDocument ( `
40
+ <div data-testid="foo"><img src="foo" /></div>
41
+ <div data-testid="bar"><a href="/foo"><div role="figure"><img src="foo" /></div></a></div>
42
+ <a data-testid="baz"><h1>link text</h1></a>
43
+ ` )
44
+
45
+ expect ( ( ) => screen . getByTestId ( 'foo' ) ) . not . toThrowError ( )
46
+ expect ( ( ) => screen . getByTestId ( 'bar' ) ) . not . toThrowError ( )
47
+ expect ( ( ) => screen . getByTestId ( 'baz' ) ) . not . toThrowError ( )
48
+ } )
49
+
38
50
test ( 'does not suggest when using getByRole' , ( ) => {
39
51
renderIntoDocument ( `<button data-testid="foo">submit</button>` )
40
52
@@ -57,6 +69,12 @@ test(`should not suggest if the suggestion would give different results`, () =>
57
69
) . not . toThrowError ( )
58
70
} )
59
71
72
+ test ( 'should suggest by label over title' , ( ) => {
73
+ renderIntoDocument ( `<label><span>bar</span><input title="foo" /></label>` )
74
+
75
+ expect ( ( ) => screen . getByTitle ( 'foo' ) ) . toThrowError ( / g e t B y L a b e l T e x t \( " b a r " \) / )
76
+ } )
77
+
60
78
test ( 'should not suggest if there would be mixed suggestions' , ( ) => {
61
79
renderIntoDocument ( `
62
80
<button data-testid="foo">submit</button>
@@ -145,6 +163,16 @@ test('should suggest img role w/ alt text', () => {
145
163
)
146
164
} )
147
165
166
+ test ( 'escapes regular expressions in suggestion' , ( ) => {
167
+ renderIntoDocument (
168
+ `<img src="foo.png" alt="The Problem (picture of a question mark)" data-testid="foo" />` ,
169
+ )
170
+
171
+ expect ( ( ) => screen . getByTestId ( 'foo' ) ) . toThrowError (
172
+ / g e t B y R o l e \( " i m g " , \{ n a m e : \/ t h e p r o b l e m \\ \( p i c t u r e o f a q u e s t i o n m a r k \\ \) \/ i \} \) / ,
173
+ )
174
+ } )
175
+
148
176
test ( 'should suggest getByLabelText when no role available' , ( ) => {
149
177
renderIntoDocument (
150
178
`<label for="foo">Username</label><input data-testid="foo" id="foo" />` ,
0 commit comments