-
Notifications
You must be signed in to change notification settings - Fork 0
/
webViewContent.js
221 lines (209 loc) · 9.91 KB
/
webViewContent.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
function getWebViewContent(cspSource, stylePath) {
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src ${cspSource};">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>React Testing Library Cheatsheet</title>
<link rel="stylesheet" href="${stylePath}/custom.css">
</head>
<body>
<main>
<h1>RTL Cheatsheet</h1>
<h3>Simple and complete cheat sheet</h3>
<div class="pdf-link">Find the PDF version <a href="https://github.com/testing-library/react-testing-library/raw/master/other/cheat-sheet.pdf" target="_blank" rel="noopener">here</a></div>
<div class="info-container">
<div class="info-container-header">
<p>render a component</p>
</div>
<div class="info-container-body">
<p><span class="red">import</span> { <span class="blue">render</span> } <span class="red">from</span> <span class="orange">'@testing-library/react'</span></p>
<p><span class="red">const</span> <span class="blue">result</span> = render(<MyComponent />)</p>
</div>
</div>
<div class="info-container">
<div class="info-container-header">
<p>search the DOM</p>
</div>
<div class="info-container-body">
<p><span class="red">import</span> { <span class="blue">screen</span>, <span class="blue">render</span> } <span class="red">from</span> <span class="orange">'@testing-library/react'</span></p>
<p>render(<br/>
<span class="green"><label></span><br/>
Remember Me <span class="green"><input</span> <span class="blue">type=</span><span class="orange">"checkbox"</span> <span class="green">/></span><br/>
<span class="green"></label></span>,<br/>
)
</p>
<p><span class="red">const</span> <span class="blue">checkboxInput</span> = screen.getByRole(<span class="orange">'checkbox'</span> , {<br/>
name: <span class="orange">/remember me/i</span>,<br/>
})
<p/>
</div>
</div>
<div class="info-container">
<div class="info-container-header">
<p>interact with element</p>
</div>
<div class="info-container-body">
<p><span class="red">import</span> { <span class="blue">userEvent</span> } from <span class="orange">'@testing-library/react'</span></p>
<p>// userEvent simulates advanced browser interactions like<br/>
// clicks, type, uploads, tabbing etc<br/>
// Click on a button</p>
<p>userEvent.click(screen.getByRole(<span class="orange">'button'</span>))</p>
<p>// Types HelloWorld in a text field<br/>
userEvent.type(screen.getByRole(<span class="orange">'textbox'</span>), <span class="orange">'Hello World'</span>)
</p>
</div>
</div>
<div class="info-container">
<div class="info-container-header">
<p>screen</p>
</div>
<div class="info-container-body">
<div class="info-container-body-row">
<b>debug(element)</b>
<span>Pretty print the DOM</span>
</div>
<div class="info-container-body-row">
<b>...queries</b>
<span>Functions to query the DOM</span>
</div>
</div>
</div>
<div class="info-container">
<div class="info-container-header">
<p>search variants</p>
<p> (result)</p>
</div>
<div class="info-container-body">
<div class="info-container-body-row">
<b>getBy</b>
<span>Element or Error</span>
</div>
<div class="info-container-body-row">
<b>getAllBy</b>
<span>Element[] or Error</span>
</div>
<div class="info-container-body-row">
<b>queryBy</b>
<span>Element or null</span>
</div>
<div class="info-container-body-row">
<b>queryAllBy</b>
<span>Element[] or []</span>
</div>
<div class="info-container-body-row">
<b>findBy</b>
<span>Promise<Element> or Promise<rejection></span>
</div>
<div class="info-container-body-row">
<b>findAllBy</b>
<span>Promise<Element[]> or Promise<rejection></span>
</div>
</div>
</div>
<div class="info-container">
<div class="info-container-header">
<p>search types</p>
<p> (result)</p>
</div>
<div class="info-container-body">
<div class="info-container-body-row">
<b>Role</b>
<span><div role='dialog'>...</div></span>
</div>
<div class="info-container-body-row">
<b>LabelText</b>
<span><label for="element" /></span>
</div>
<div class="info-container-body-row">
<b>PlaceholderText</b>
<span><input placeholder="username" /></span>
</div>
<div class="info-container-body-row">
<b>Text</b>
<span><a href='/about'>About</a></span>
</div>
<div class="info-container-body-row">
<b>DisplayValue</b>
<span><input value="display value" /></span>
</div>
<div class="info-container-body-row">
<b>AltText</b>
<span><img alt="movie poster" /></span>
</div>
<div class="info-container-body-row">
<b>Title</b>
<span><span title='Delete' /> or <title /></span>
</div>
<div class="info-container-body-row">
<b>TestId</b>
<span><input data-testid='username-input' /></span>
</div>
</div>
</div>
<div class="info-container">
<div class="info-container-header">
<p>text matches</p>
</div>
<div class="info-container-body">
<p>render(<span class="green"><label></span>Remember Me <span class="green"><input</span> <span class="blue">type=</span><span class="orange">"checkbox"</span> <span class="green">/></label></span>)</p>
<p>screen.getByRole(<span class="orange">'checkbox'</span>, {name: <span class="orange">/remember me/i</span>}) // ✅<br/>
screen.getByRole(<span class="orange">'checkbox'</span>, {name: <span class="orange">'remember me'</span>}) // ❌<br/>
screen.getByRole(<span class="orange">'checkbox'</span>, {name: <span class="orange">'Remember Me'</span>}) // ✅</p>
<p>// other queries accept text matches as well <br/>
// the text match argument can also be a function <br/>
screen.getByText((<span class="blue">text</span>, <span class="blue">element</span>) => {/* return true/false <br/>
*/})
</p>
</div>
</div>
<div class="info-container">
<div class="info-container-header">
<p>wait for appearance</p>
</div>
<div class="info-container-body">
<p>test(<span class="orange">'movie title appears'</span>, <span class="red">async</span> () => { <br/>
render(<span class="green"><Movie /></span>)</p>
<p> // the element isn't available yet, so wait for it:<br/>
<span class="red">const</span> <span class="blue">movieTitle</span> = <span class="red">await</span> screen.findByText( <br/>
<span class="orange">/the lion king/i,</span><br/>
)
</p>
<p> // the element is there but we want to wait for it <br/>
// to be removed <br/>
<span class="red">await</span> waitForElementToBeRemoved(() => <br/>
screen.getByLabelText(<span class="orange">/loading/i</span>), <br/>
)
</p>
<p>
// we want to wait until an assertion passes <br/>
<span class="red">await</span> waitFor(() => <br/>
expect(mockFn).toHaveBeenCalledWith(<span class="orange">'some arg'</span>), <br/>
) <br/>
})
</p>
</div>
</div>
<div class="info-container">
<div class="info-container-header">
<p>render() options</p>
</div>
<div class="info-container-body">
<div class="info-container-body-row">
<b>hydrate</b>
<span>If true, will render with ReactDOM.hydrate</span>
</div>
<div class="info-container-body-row">
<b>wrapper</b>
<span>React component which wraps the passed ui</span>
</div>
</div>
</div>
<div class="footer-link">All credit goes to <a href="https://testing-library.com/" target="_blank" rel="noopener">React Testing Library</a></div>
<div class="footer-link">Check out their <a href="https://github.com/testing-library" target="_blank" rel="noopener">Github</a></div>
</main>
</body>
</html>`;
}
module.exports = { getWebViewContent }