1
1
import React from 'react' ;
2
2
import { getRole , computeAccessibleName } from 'dom-accessibility-api' ;
3
- import { messages } from '../constants' ;
3
+ import { links , messages } from '../constants' ;
4
+ import SetLike from 'dom-accessibility-api/dist/polyfills/SetLike' ;
5
+ import { useAppContext } from './Context' ;
4
6
5
7
const colors = [ 'bg-blue-600' , 'bg-yellow-600' , 'bg-orange-600' , 'bg-red-600' ] ;
6
8
@@ -23,11 +25,11 @@ function getExpression({ method, data }) {
23
25
const field = getFieldName ( method ) ;
24
26
25
27
if ( method === 'getByRole' && data . role && data . name ) {
26
- return `getByRole('${ data . role } ', { name: '${ data . name } ' })` ;
28
+ return `screen. getByRole('${ data . role } ', { name: '${ data . name } ' })` ;
27
29
}
28
30
29
31
if ( data [ field ] ) {
30
- return `${ method } ('${ data [ field ] } ')` ;
32
+ return `screen. ${ method } ('${ data [ field ] } ')` ;
31
33
}
32
34
33
35
return '' ;
@@ -67,25 +69,37 @@ function getData({ root, element }) {
67
69
} ;
68
70
}
69
71
70
- function Row ( { method , data } ) {
71
- const field = getFieldName ( method ) ;
72
- const value = data [ field ] ;
72
+ function Section ( { children } ) {
73
+ return < div className = "space-y-3" > { children } </ div > ;
74
+ }
73
75
74
- return (
75
- < tr >
76
- < td className = "px-4 text-xs" > { field } </ td >
77
- < td className = "px-4 text-xs" > { value } </ td >
78
- </ tr >
79
- ) ;
76
+ function Heading ( { children } ) {
77
+ return < h3 className = "font-bold text-xs" > { children } </ h3 > ;
80
78
}
81
79
82
- function Section ( { children } ) {
80
+ function Field ( { method, data } ) {
81
+ const { jsEditorRef } = useAppContext ( ) ;
82
+
83
+ const field = getFieldName ( method ) ;
84
+ const value = data [ field ] ;
85
+ const handleClick = value
86
+ ? ( ) => {
87
+ const expr = getExpression ( { method, data } ) ;
88
+ jsEditorRef . current . setValue ( expr ) ;
89
+ }
90
+ : undefined ;
91
+
83
92
return (
84
- < tr >
85
- < td colSpan = "2" className = "pt-4 text-xs" >
86
- < strong > { children } </ strong >
87
- </ td >
88
- </ tr >
93
+ < div
94
+ className = "text-xs field"
95
+ data-clickable = { ! ! handleClick }
96
+ onClick = { handleClick }
97
+ >
98
+ < div className = "text-gray-800 font-light" > { field } </ div >
99
+ < div className = "truncate" >
100
+ { value || < span className = "text-gray-400" > n/a</ span > }
101
+ </ div >
102
+ </ div >
89
103
) ;
90
104
}
91
105
@@ -98,47 +112,64 @@ function getQueryAdvise(data) {
98
112
...messages [ 3 ] ,
99
113
} ;
100
114
}
101
- const expression = `screen. ${ getExpression ( { method : query . method , data } ) } ` ;
115
+ const expression = getExpression ( { method : query . method , data } ) ;
102
116
return { expression, level : query . level , ...messages [ query . level ] } ;
103
117
}
104
118
105
119
// for inputs, the role will only work if there is also a type attribute
106
- function ElementInfo ( { root, element } ) {
107
- const data = getData ( { root, element } ) ;
120
+ function ElementInfo ( { element } ) {
121
+ const { htmlPreviewRef } = useAppContext ( ) ;
122
+ const data = getData ( { root : htmlPreviewRef . current , element } ) ;
108
123
const advise = getQueryAdvise ( data ) ;
109
124
110
125
return (
111
126
< div >
112
127
< div
113
- className = { [ 'border text-white p-4 rounded' , colors [ advise . level ] ] . join (
114
- ' ' ,
115
- ) }
128
+ className = { [
129
+ 'border text-white p-4 rounded mb-8' ,
130
+ colors [ advise . level ] ,
131
+ ] . join ( ' ' ) }
116
132
>
117
133
< div className = "font-bold text-xs mb-2" > suggested query:</ div >
118
134
{ advise . expression && (
119
135
< div className = "font-mono text-sm" > > { advise . expression } </ div >
120
136
) }
121
- { /*<div className="font-bold text-xs mb-2">{advise.heading}:</div>*/ }
122
- { /*{advise.description && <div className="text-sm mb-4">{advise.description}</div> }*/ }
123
137
</ div >
124
138
125
- < table className = "table-auto text-sm w-full" >
126
- < tbody >
127
- < Section > Queries Accessible to Everyone</ Section >
128
- < Row method = "getByRole" data = { data } />
129
- < Row method = "getByLabelText" data = { data } />
130
- < Row method = "getByPlaceholderText" data = { data } />
131
- < Row method = "getByText" data = { data } />
132
- < Row method = "getByDisplayValue" data = { data } />
133
-
134
- < Section > Semantic Queries</ Section >
135
- < Row method = "getByAltText" data = { data } />
136
- < Row method = "getByTitle" data = { data } />
137
-
138
- < Section > TestId</ Section >
139
- < Row method = "getByTestId" data = { data } />
140
- </ tbody >
141
- </ table >
139
+ { /*disabled for the time being*/ }
140
+ { false && advise . description && (
141
+ < blockquote className = "text-sm mb-4 italic" >
142
+ < p className = "font-bold text-xs mb-2" > { advise . heading } :</ p >
143
+ < p > { advise . description } </ p >
144
+ < cite >
145
+ < a href = { links . which_query . url } > Testing Library</ a >
146
+ </ cite >
147
+ </ blockquote >
148
+ ) }
149
+
150
+ < div className = "grid grid-cols-2 gap-4" >
151
+ < Section >
152
+ < Heading > Queries Accessible to Everyone</ Heading >
153
+ < Field method = "getByRole" data = { data } />
154
+ < Field method = "getByLabelText" data = { data } />
155
+ < Field method = "getByPlaceholderText" data = { data } />
156
+ < Field method = "getByText" data = { data } />
157
+ < Field method = "getByDisplayValue" data = { data } />
158
+ </ Section >
159
+
160
+ < div className = "space-y-8" >
161
+ < Section >
162
+ < Heading > Semantic Queries</ Heading >
163
+ < Field method = "getByAltText" data = { data } />
164
+ < Field method = "getByTitle" data = { data } />
165
+ </ Section >
166
+
167
+ < Section >
168
+ < Heading > TestId</ Heading >
169
+ < Field method = "getByTestId" data = { data } />
170
+ </ Section >
171
+ </ div >
172
+ </ div >
142
173
</ div >
143
174
) ;
144
175
}
0 commit comments