File tree Expand file tree Collapse file tree 3 files changed +30
-7
lines changed Expand file tree Collapse file tree 3 files changed +30
-7
lines changed Original file line number Diff line number Diff line change @@ -88,7 +88,7 @@ test('get throws a useful error message', () => {
88
88
expect ( ( ) => getByRole ( 'LucyRicardo' ) ) . toThrowErrorMatchingInlineSnapshot ( `
89
89
"Unable to find an element with the role "LucyRicardo"
90
90
91
- Here are the available roles:
91
+ There are no available roles.
92
92
93
93
<div>
94
94
<div />
Original file line number Diff line number Diff line change @@ -20,3 +20,16 @@ Here are the available roles:
20
20
</div>"
21
21
` )
22
22
} )
23
+
24
+ test ( 'logs error when there are no available roles' , ( ) => {
25
+ const { getByRole} = render ( '<div />' )
26
+ expect ( ( ) => getByRole ( 'article' ) ) . toThrowErrorMatchingInlineSnapshot ( `
27
+ "Unable to find an element with the role "article"
28
+
29
+ There are no available roles.
30
+
31
+ <div>
32
+ <div />
33
+ </div>"
34
+ ` )
35
+ } )
Original file line number Diff line number Diff line change @@ -26,16 +26,26 @@ function queryAllByRole(
26
26
27
27
const getMultipleError = ( c , role ) =>
28
28
`Found multiple elements with the role "${ role } "`
29
- const getMissingError = ( container , role ) =>
30
- `
31
- Unable to find an element with the role "${ role } "
32
29
30
+ const getMissingError = ( container , role ) => {
31
+ const roles = prettyRoles ( container )
32
+ let roleMessage
33
+
34
+ if ( roles . length === 0 ) {
35
+ roleMessage = 'There are no available roles.'
36
+ } else {
37
+ roleMessage = `
33
38
Here are the available roles:
34
39
35
- ${ prettyRoles ( container )
36
- . replace ( / \n / g, '\n ' )
37
- . replace ( / \n \s \s \n / g, '\n\n' ) }
40
+ ${ roles . replace ( / \n / g, '\n ' ) . replace ( / \n \s \s \n / g, '\n\n' ) }
38
41
` . trim ( )
42
+ }
43
+
44
+ return `
45
+ Unable to find an element with the role "${ role } "
46
+
47
+ ${ roleMessage } `. trim ( )
48
+ }
39
49
40
50
const [
41
51
queryByRole ,
You can’t perform that action at this time.
0 commit comments