-
Notifications
You must be signed in to change notification settings - Fork 2
/
jest_dom_test.ml
401 lines (334 loc) · 14.6 KB
/
jest_dom_test.ml
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
[@@@mel.config { flags = [| "--preamble";
{|
/**
* @jest-environment jsdom
*/
|}
|] }]
open Jest
open JestDom
open Webapi.Dom
open Webapi.Dom.Element
let render html =
let body = Document.createElement "body" document in
(body |. setInnerHTML) html;
(document |. Document.unsafeAsHtmlDocument |. HtmlDocument.setBody) body;
body
let queryByTestId (id : string) (element : Dom.element) =
match element |> querySelector {j|[data-testid="$(id)"]|j} with
| Some el -> el
| None -> raise (Failure "Element not found")
let _ =
afterEach (fun () ->
match document |. Document.unsafeAsHtmlDocument |. HtmlDocument.body with
| Some body -> (body |. setInnerHTML) ""
| None -> raise (Failure "Not document body found"))
let _ =
test "toBeDisabled" (fun () ->
render {|<button disabled data-testid="button"></button>|}
|> queryByTestId "button" |> expect |> toBeDisabled)
let _ =
test "not toBeDisabled" (fun () ->
render {|<button data-testid="button"></button>|}
|> queryByTestId "button" |> expect |> not_ |> toBeDisabled)
let _ =
test "toBeEnabled" (fun () ->
render {|<button data-testid="button"></button>|}
|> queryByTestId "button" |> expect |> toBeEnabled)
let _ =
test "not toBeEnabled" (fun () ->
render {|<button disabled data-testid="button"></button>|}
|> queryByTestId "button" |> expect |> not_ |> toBeEnabled)
let _ =
test "toBeEmptyDOMElement" (fun () ->
render {|<button data-testid="button"></button>|}
|> queryByTestId "button" |> expect |> toBeEmptyDOMElement)
let _ =
test "not toBeEmptyDOMElement" (fun () ->
render {|<button disabled data-testid="button">Click me</button>|}
|> queryByTestId "button" |> expect |> not_ |> toBeEmptyDOMElement)
let _ =
test "toBeInTheDocument" (fun () ->
render {|<button data-testid="button"></button>|}
|> queryByTestId "button" |> expect |> toBeInTheDocument)
let _ =
test "not toBeInTheDocument" (fun () ->
render {|<button></button>|} |> fun _ ->
Document.createElement "div" document
|> expect |> not_ |> toBeInTheDocument)
let _ =
test "toBeInvalid" (fun () ->
render {|<input required data-testid="input" />|}
|> queryByTestId "input" |> expect |> toBeInvalid)
let _ =
test "not toBeInvalid" (fun () ->
render {|<input data-testid="input" />|}
|> queryByTestId "input" |> expect |> not_ |> toBeInvalid)
let _ =
test "toBeRequired" (fun () ->
render {|<input required data-testid="input" />|}
|> queryByTestId "input" |> expect |> toBeRequired)
let _ =
test "not toBeRequired" (fun () ->
render {|<input data-testid="input" />|}
|> queryByTestId "input" |> expect |> not_ |> toBeRequired)
let _ =
test "toBeValid" (fun () ->
render {|<input data-testid="input" />|}
|> queryByTestId "input" |> expect |> toBeValid)
let _ =
test "not toBeValid" (fun () ->
render {|<input required data-testid="input" />|}
|> queryByTestId "input" |> expect |> not_ |> toBeValid)
let _ =
test "toBeVisible" (fun () ->
render {|<button data-testid="button"></button>|}
|> queryByTestId "button" |> expect |> toBeVisible)
let _ =
test "not toBeVisible" (fun () ->
render {|<button style="display: none" data-testid="button"></button>|}
|> queryByTestId "button" |> expect |> not_ |> toBeVisible)
let _ =
test "toContainElement" (fun () ->
let element =
render {|<span data-testid="span"><button></button></span>|}
in
element |> queryByTestId "span" |> expect
|> (("button" |. querySelector) (document |. Document.documentElement)
|. toContainElement))
let _ =
test "not toContainElement" (fun () ->
let element = render {|<span data-testid="span"></span>|} in
element |> queryByTestId "span" |> expect |> not_
|> (("div" |. Document.createElement) document |. Some |. toContainElement))
let _ =
test "toContainHTML" (fun () ->
render {|<span data-testid="span"><p></p></span>|}
|> queryByTestId "span" |> expect |> toContainHTML "<p></p>")
let _ =
test "not toContainHTML" (fun () ->
render {|<span data-testid="span"></span>|}
|> queryByTestId "span" |> expect |> not_ |> toContainHTML "<p></p>")
let _ =
test "toHaveAttribute" (fun () ->
render {|<span class="empty" data-testid="span"></span>|}
|> queryByTestId "span" |> expect |> toHaveAttribute "class")
let _ =
test "not toHaveAttribute" (fun () ->
render {|<span data-testid="span"></span>|}
|> queryByTestId "span" |> expect |> not_ |> toHaveAttribute "class")
let _ =
test "toHaveAttribute with value" (fun () ->
render {|<span class="empty" data-testid="span"></span>|}
|> queryByTestId "span" |> expect
|> toHaveAttribute "class" ~value:"empty")
let _ =
test "not toHaveAttribute with value" (fun () ->
render {|<span class="hidden" data-testid="span"></span>|}
|> queryByTestId "span" |> expect |> not_
|> toHaveAttribute "class" ~value:"empty")
let _ =
test "toHaveClass (string)" (fun () ->
render {|<span class="empty" data-testid="span"></span>|}
|> queryByTestId "span" |> expect
|> toHaveClass (`Str "empty"))
let _ =
test "not toHaveClass (string)" (fun () ->
render {|<span data-testid="span"></span>|}
|> queryByTestId "span" |> expect |> not_
|> toHaveClass (`Str "empty"))
let _ =
test "toHaveClass (list)" (fun () ->
render {|<span class="empty hidden" data-testid="span"></span>|}
|> queryByTestId "span" |> expect
|> toHaveClass (`Lst [ "empty"; "hidden" ]))
let _ =
test "not toHaveClass (list)" (fun () ->
render {|<span class="hidden" data-testid="span"></span>|}
|> queryByTestId "span" |> expect |> not_
|> toHaveClass (`Lst [ "empty"; "hidden" ]))
let _ =
test "toHaveClass (string) [exact]" (fun () ->
render {|<span class="empty hidden" data-testid="span"></span>|}
|> queryByTestId "span" |> expect
|> toHaveClass (`Str "empty hidden")
~options:(HaveClass.makeOptions ~exact:true ()))
let _ =
test "not toHaveClass (string) [exact]" (fun () ->
render {|<span class="hidden" data-testid="span"></span>|}
|> queryByTestId "span" |> expect |> not_
|> toHaveClass (`Str "empty")
~options:(HaveClass.makeOptions ~exact:true ()))
let _ =
test "toHaveFocus" (fun () ->
let element = render {|<span tabindex="1" data-testid="span"></span>|} in
("span" |. queryByTestId) element
|. Element.unsafeAsHtmlElement |. HtmlElement.focus;
element |> queryByTestId "span" |> expect |> toHaveFocus)
let _ =
test "not toHaveFocus" (fun () ->
render {|<span data-testid="span"></span>|}
|> queryByTestId "span" |> expect |> not_ |> toHaveFocus)
let _ =
test "toHaveFormValues" (fun () ->
render
{|<form data-testid="form"><label for="title">Job title</label><input type="text" id="title" name="title" value="CEO" /></form>|}
|> queryByTestId "form" |> expect
|> toHaveFormValues [%mel.obj { title = "CEO" }])
let _ =
test "not toHaveFormValues" (fun () ->
render
{|<form data-testid="form"><label for="title">Job title</label><input type="text" id="title" name="title" value="CEO" /></form>|}
|> queryByTestId "form" |> expect |> not_
|> toHaveFormValues [%mel.obj { title = "CTO" }])
let _ =
test "toHaveStyle (string)" (fun () ->
render {|<span style="color: rebeccapurple" data-testid="span"></span>|}
|> queryByTestId "span" |> expect
|> toHaveStyle (`Str "color: rebeccapurple"))
let _ =
test "not toHaveStyle (string)" (fun () ->
render {|<span style="display: none" data-testid="span"></span>|}
|> queryByTestId "span" |> expect |> not_
|> toHaveStyle (`Str "display: inline-block"))
let _ =
test "toHaveStyle (object)" (fun () ->
render {|<span style="color: rebeccapurple" data-testid="span"></span>|}
|> queryByTestId "span" |> expect
|> toHaveStyle (`Obj [%mel.obj { color = "rebeccapurple" }]))
let _ =
test "not toHaveStyle (object)" (fun () ->
render {|<span style="display: none" data-testid="span"></span>|}
|> queryByTestId "span" |> expect |> not_
|> toHaveStyle (`Obj [%mel.obj { display = "inline-block" }]))
let _ =
test "toHaveTextContent (string)" (fun () ->
render {|<span data-testid="span">Step 1 of 4</span>|}
|> queryByTestId "span" |> expect
|> toHaveTextContent (`Str "Step 1 of 4"))
let _ =
test "not toHaveTextContent (string)" (fun () ->
render {|<span data-testid="span">Step 2 of 4</span>|}
|> queryByTestId "span" |> expect |> not_
|> toHaveTextContent (`Str "Step 1 of 4"))
let _ =
test "toHaveTextContent (string) with options" (fun () ->
render {|<span data-testid="span"> Step 1 of 4</span>|}
|> queryByTestId "span" |> expect
|> toHaveTextContent (`Str " Step 1 of 4")
~options:(TextContent.makeOptions ~normalizeWhitespace:false ()))
let _ =
test "toHaveTextContent (regex)" (fun () ->
render {|<span data-testid="span">Step 1 of 4</span>|}
|> queryByTestId "span" |> expect
|> toHaveTextContent (`RegExp [%mel.re "/Step \\d of \\d/"]))
let _ =
test "not toHaveTextContent (regex)" (fun () ->
render {|<span data-testid="span">Step 2 of 4</span>|}
|> queryByTestId "span" |> expect |> not_
|> toHaveTextContent (`RegExp [%mel.re "/^\\d of 4$/"]))
let _ =
test "toHaveValue (string)" (fun () ->
render {|<input data-testid="input" value="5" />|}
|> queryByTestId "input" |> expect
|> toHaveValue (`Str "5"))
let _ =
test "not toHaveValue (string)" (fun () ->
render {|<input data-testid="input" />|}
|> queryByTestId "input" |> expect |> not_
|> toHaveValue (`Str "5"))
let _ =
test "toHaveValue (num)" (fun () ->
render {|<input type="number" data-testid="input" value="5" />|}
|> queryByTestId "input" |> expect
|> toHaveValue (`Num 5))
let _ =
test "not toHaveValue (num)" (fun () ->
render {|<input type="number" data-testid="input" />|}
|> queryByTestId "input" |> expect |> not_
|> toHaveValue (`Num 5))
let _ =
test "toHaveValue (array)" (fun () ->
render
{|<select data-testid="select" multiple><option value=""></option><option value="apple" selected>Apple</option><option value="peach">Peach</option><option value="orange" selected>Orange</option></select>|}
|> queryByTestId "select" |> expect
|> toHaveValue (`Arr [| "apple"; "orange" |]))
let _ =
test "not toHaveValue (list)" (fun () ->
render
{|<select data-testid="select" multiple><option value=""></option><option value="apple" selected>Apple</option><option value="peach">Peach</option><option value="orange" selected>Orange</option></select>|}
|> queryByTestId "select" |> expect |> not_
|> toHaveValue (`Arr [| "apple"; "peach" |]))
let _ =
test "toHaveDisplayValue (string)" (fun () ->
render {|<input data-testid="input" value="Test" />|}
|> queryByTestId "input" |> expect
|> toHaveDisplayValue (`Str "Test"))
let _ =
test "not toHaveDisplayValue (string)" (fun () ->
render {|<input data-testid="input" />|}
|> queryByTestId "input" |> expect |> not_
|> toHaveDisplayValue (`Str "Test"))
let _ =
test "toHaveDisplayValue (regex)" (fun () ->
render {|<input data-testid="input" value="Test" />|}
|> queryByTestId "input" |> expect
|> toHaveDisplayValue (`RegExp [%mel.re "/^Te/"]))
let _ =
test "not toHaveDisplayValue (regex)" (fun () ->
render {|<input data-testid="input" />|}
|> queryByTestId "input" |> expect |> not_
|> toHaveDisplayValue (`RegExp [%mel.re "/Tt/"]))
let _ =
test "toHaveDisplayValue (array)" (fun () ->
render
{|<select data-testid="select" multiple><option value=""></option><option value="apple" selected>Apple</option><option value="peach">Peach</option><option value="orange" selected>Orange</option></select>|}
|> queryByTestId "select" |> expect
|> toHaveDisplayValue (`Arr [| "Apple"; "Orange" |]))
let _ =
test "not toHaveDisplayValue (array)" (fun () ->
render
{|<select data-testid="select" multiple><option value=""></option><option value="apple" selected>Apple</option><option value="peach">Peach</option><option value="orange" selected>Orange</option></select>|}
|> queryByTestId "select" |> expect |> not_
|> toHaveDisplayValue (`Arr [| "Apple"; "Peach" |]))
let _ =
test "toBeChecked" (fun () ->
render {|<input type="checkbox" checked data-testid="input" />|}
|> queryByTestId "input" |> expect |> toBeChecked)
let _ =
test "not toBeChecked" (fun () ->
render {|<input type="checkbox" data-testid="input" />|}
|> queryByTestId "input" |> expect |> not_ |> toBeChecked)
let _ =
test "toBePartiallyChecked" (fun () ->
render
{|<input type="checkbox" aria-checked="mixed" data-testid="input" />|}
|> queryByTestId "input" |> expect |> toBePartiallyChecked)
let _ =
test "not toBePartiallyChecked" (fun () ->
render {|<input type="checkbox" checked data-testid="input" />|}
|> queryByTestId "input" |> expect |> not_ |> toBePartiallyChecked)
let _ =
test "toHaveAccessibleDescription (string)" (fun () ->
render
{|<span><button data-testid="button" aria-label="Close" aria-describedby="description-close">X</button><div id="description-close">Closing will discard any changes</div></span>|}
|> queryByTestId "button" |> expect
|> toHaveAccessibleDescription (`Str "Closing will discard any changes"))
let _ =
test "not toHaveAccessibleDescription (string)" (fun () ->
render
{|<span><button data-testid="button" aria-label="Close" aria-describedby="description-close">X</button><div id="description-close">Closing will discard any changes</div></span>|}
|> queryByTestId "button" |> expect |> not_
|> toHaveAccessibleDescription (`Str "Other description"))
let _ =
test "toHaveAccessibleDescription (regex)" (fun () ->
render
{|<span><button data-testid="button" aria-label="Close" aria-describedby="description-close">X</button><div id="description-close">Closing will discard any changes</div></span>|}
|> queryByTestId "button" |> expect
|> toHaveAccessibleDescription (`RegExp [%mel.re "/will discard/"]))
let _ =
test "not toHaveAccessibleDescription (regex)" (fun () ->
render
{|<span><button data-testid="button" aria-label="Close" aria-describedby="description-close">X</button><div id="description-close">Closing will discard any changes</div></span>|}
|> queryByTestId "button" |> expect |> not_
|> toHaveAccessibleDescription (`RegExp [%mel.re "/^Other/"]))