forked from franciscop/react-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
1177 lines (1033 loc) · 35.2 KB
/
index.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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react-dom/test-utils'), require('react'), require('react-dom')) :
typeof define === 'function' && define.amd ? define(['exports', 'react-dom/test-utils', 'react', 'react-dom'], factory) :
(global = global || self, factory(global.$ = {}, global.testUtils, global.React, global.ReactDOM));
}(this, (function (exports, testUtils, React, reactDom) { 'use strict';
React = React && Object.prototype.hasOwnProperty.call(React, 'default') ? React['default'] : React;
reactDom = reactDom && Object.prototype.hasOwnProperty.call(reactDom, 'default') ? reactDom['default'] : reactDom;
const delay = (time) => new Promise((done) => setTimeout(done, time));
const untilCallback = async (cb) => {
let value = await cb();
while (!value) {
await testUtils.act(async () => {
await delay(50);
value = await cb();
});
}
return value;
};
const execute = (obj, chain) => {
let newObj = obj;
for (let [key, args] of chain) {
newObj = newObj[key](...args);
}
return newObj;
};
// Store the action chain in an object, and execute it when we find '.then'
const untilObject = (obj) => {
const chain = [];
const getter = (target, key) => {
if (key === "then") {
return async (cb) => {
let res;
while (!res) {
await testUtils.act(async () => {
await delay(50);
res = execute(obj, chain);
// If it's an object that looks like an instance, we want to ignore
// the cases where there are no matched nodes and keep looping then
if (res && res.nodes && !res.nodes.length) {
res = false;
}
});
}
return cb(res);
};
} else {
return (...args) => {
chain.push([key, args]);
return new Proxy(obj, { get: getter });
};
}
};
return new Proxy(obj, { get: getter });
};
/**
* Wait until the specified condition is fulfilled. There are multiple ways of specifying the conditions:
*
* ```js
* await until(() => new Date() - init > 1000);
* await until(button).is(".active");
* await until(list).find("li");
* ```
*
* **[→ Full until() Docs](https://react-test.dev/documentation#until)**
*/
function until(arg) {
if (typeof arg === "function") {
return untilCallback(arg);
}
if (typeof arg === "object") {
return untilObject(arg);
}
}
function createCommonjsModule(fn, module) {
return module = { exports: {} }, fn(module, module.exports), module.exports;
}
var client = createCommonjsModule(function (module, exports) {
if (process.env.NODE_ENV === 'production') {
exports.createRoot = reactDom.createRoot;
exports.hydrateRoot = reactDom.hydrateRoot;
} else {
var i = reactDom.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
exports.createRoot = function(c, o) {
i.usingClientEntryPoint = true;
try {
return reactDom.createRoot(c, o);
} finally {
i.usingClientEntryPoint = false;
}
};
exports.hydrateRoot = function(c, h, o) {
i.usingClientEntryPoint = true;
try {
return reactDom.hydrateRoot(c, h, o);
} finally {
i.usingClientEntryPoint = false;
}
};
}
});
var client_1 = client.createRoot;
var client_2 = client.hydrateRoot;
global.IS_REACT_ACT_ENVIRONMENT = true;
const createBoundaries = () => {
const handler = { error: false };
class Catcher extends React.Component {
constructor(props) {
super(props);
this.state = { isError: false };
window.addEventListener("error", this.onError.bind(this));
}
static getDerivedStateFromError() {
return { isError: true };
}
componentWillUnmount() {
window.removeEventListener("error", this.onError);
}
onError(event) {
// This elevates the errors from local in the render tree
// to global in the test level
event.preventDefault();
this.componentDidCatch(event.error);
}
componentDidCatch(error) {
handler.error = error;
}
render() {
if (this.state && this.state.isError) return null;
return this.props.children;
}
}
return [handler, Catcher];
};
const renderRoot = (component) => {
const [handler, Catcher] = createBoundaries();
const container = window.document.createElement("div");
container.id = "root";
container.handler = handler;
container.catcher = Catcher;
container.component = component;
window.document.body.appendChild(container);
const root = client_1(container);
container.root = root;
testUtils.act(() => root.render(React.createElement(Catcher, null, component)));
if (handler.error) {
testUtils.act(() => root.unmount());
throw handler.error;
}
return [...container.childNodes];
};
// This takes a react object like <Button /> and returns the DOM tree
var render = (obj) => {
if (!obj) return [];
if (["string", "number", "boolean"].includes(typeof obj)) {
return renderRoot(obj);
}
// A react instance, so render it to jsdom:
if (obj.$$typeof) {
return renderRoot(obj);
}
// It's already parsed
return (Array.isArray(obj) ? obj : [obj]).filter(
(obj) => typeof obj === "object"
);
};
// [INTERNAL USE ONLY]
var normalize = (frag) => {
if (!frag) {
throw new Error(
"expect() should receive an HTMLElement or React Test instance"
);
}
// Convert a raw element to
if (frag.$$typeof) {
const parts = render(frag);
frag = parts[0];
}
// For now get the first one, consider looping later
if (frag.array) frag = frag.array();
// It's a single node
if (!Array.isArray(frag)) frag = [frag];
frag.forEach((node) => {
// Make sure it's an HTML node
if (!node.nodeName) {
throw new Error(
"expect() should receive an HTMLElement or React Test instance"
);
}
});
return frag;
};
// [INTERNAL USE ONLY
// Retrieves a clear name for the passed element
var getPlainTag = (el) => {
// Get the full HTML tag WITHOUT its contents
const html = el.cloneNode(false).outerHTML;
// Regex should NOT be used generally for HTML. We make an exception here
// because it's a very strict regex out of a very well defined output string
return html.replace(/<\/[a-zA-Z0-9-]+>$/, "");
};
function toBeEnabled (frag) {
// To avoid double negations ¯\_(ツ)_/¯
this.affirmative = !this.isNot;
// Convert it into a plain array of nodes
frag = normalize(frag);
for (let el of frag) {
// Prepare the message if there's an error. It needs to build this string:
// <input disabled="">
const base = getPlainTag(el);
// Boolean indicating if any of the received nodes have the attribute "disabled"
const isEnabled = !el.disabled;
// expect(<input />).toBeEnabled();
if (this.affirmative) {
if (!isEnabled) {
const msg = `Expected ${base} not to include the attribute "disabled"`;
return { pass: false, message: () => msg };
}
}
// expect(<input type="text" disabled />).not.toBeEnabled();
if (this.isNot) {
if (isEnabled) {
const msg = `Expected ${base} to include the attribute "disabled"`;
return { pass: true, message: () => msg };
}
}
}
return { pass: !this.isNot };
}
function toHaveAttribute (frag, attr, val) {
// To avoid double negations ¯\_(ツ)_/¯
this.affirmative = !this.isNot;
// Convert it into a plain array of nodes
frag = normalize(frag);
for (let el of frag) {
const attributes = [...el.attributes];
const base = getPlainTag(el);
// Find attribute that matches passed in attr and val
const found = attributes.some(({ name, value }) => {
if (attr !== name) return false;
if (val instanceof RegExp) return val.test(value);
if (typeof val === "boolean") return value === "";
return val ? value === val : true;
});
// Prepare val error message
let valErrMessage = "";
if (val instanceof RegExp) valErrMessage = ` that matches ${val}`;
else if (val) valErrMessage = `="${val}"`;
if (this.affirmative && !found) {
const msg = `Expected ${base} to have attribute \`${attr}\`${valErrMessage}`;
return { pass: false, message: () => msg };
}
if (this.isNot && found) {
const msg = `Expected ${base} not to have attribute \`${attr}\`${valErrMessage}`;
return { pass: true, message: () => msg };
}
}
return { pass: !this.isNot };
}
const toStr = (list) => {
return `class${list.length > 1 ? "es" : ""} "${list.join('", "')}"`;
};
function toHaveClass (frag, ...expectedClasses) {
// To avoid double negations ¯\_(ツ)_/¯
this.affirmative = !this.isNot;
// Convert it into a plain array of nodes
frag = normalize(frag);
// All of the expected classes
const expected = expectedClasses.flat();
for (let el of frag) {
// Prepare the message if there's an error. It needs to build this string:
// <button class="primary button">
const received = [...el.classList];
const base = getPlainTag(el);
// All the expected classes that have been received
const found = expected.filter((name) => received.includes(name));
// All of the expected classes that have NOT been received
const notfound = expected.filter((name) => !received.includes(name));
// expect(<div className="banana" />).toHaveClass('banana');
if (this.affirmative) {
if (found.length < expected.length) {
const msg = `Expected ${base} to include ${toStr(notfound)}`;
return { pass: false, message: () => msg };
}
}
// expect(<div className="orange" />).not.toHaveClass('banana');
if (this.isNot) {
if (found.length) {
const msg = `Expected ${base} not to include ${toStr(found)}`;
return { pass: true, message: () => msg };
}
}
}
return { pass: !this.isNot };
}
function toHaveHtml (frag, html) {
this.affirmative = !this.isNot;
frag = normalize(frag);
if (typeof html !== "string") {
const msg = `Second argument of .toHaveHtml() needs to be a string`;
return { pass: false, message: () => msg };
}
for (let el of frag) {
const hasHTML = el.outerHTML.includes(html.trim());
if (this.affirmative && !hasHTML) {
const msg = `Expected ${el.outerHTML} to include ${html}`;
return { pass: false, message: () => msg };
}
if (this.isNot && hasHTML) {
const msg = `Expected ${el.outerHTML} not to include ${html}`;
return { pass: true, message: () => msg };
}
}
return { pass: !this.isNot };
}
const whitespace = (str) => str.replace(/\s+/g, " ");
function toHaveText (frag, expected) {
// To avoid double negations ¯\_(ツ)_/¯
this.affirmative = !this.isNot;
// Convert it into a plain array of nodes
frag = normalize(frag);
for (let el of frag) {
// Prepare the message if there's an error. It needs to build this string:
// <button class="primary button">
const received = el.textContent;
const base = getPlainTag(el);
// expect(<div>banana</div>).toHaveText('banana');
if (this.affirmative) {
if (whitespace(received) !== whitespace(expected)) {
const msg = `Expected ${base} to have text "${expected}" but it received "${received}"`;
return { pass: false, message: () => msg };
}
}
// expect(<div>orange</div>).not.toHaveText('banana');
else {
if (whitespace(received) === whitespace(expected)) {
const msg = `Expected ${base} not to have the text "${received}"`;
return { pass: true, message: () => msg };
}
}
}
return { pass: !this.isNot };
}
function toHaveValue (frag, value = true) {
// To avoid double negations ¯\_(ツ)_/¯
this.affirmative = !this.isNot;
// Convert it into a plain array of nodes
frag = normalize(frag);
//Should only handle one element
if (frag.length > 1)
throw new Error(
"Cannot check multiple elements for values. Please pass only one element."
);
const el = frag[0];
const tagName = el.tagName.toLowerCase();
if (tagName === "input" && ["checkbox", "radio"].includes(el.type)) {
throw new Error(
'Cannot check .toHaveValue() for input type="checkbox" or type="radio".'
);
}
const base = getPlainTag(el);
let matches = false;
if (tagName === "input") {
matches =
el.type === "number" ? Number(el.value) === value : el.value === value;
} else if (tagName === "textarea") {
matches = el.value === value;
} else if (tagName === "select") {
const selected = [...el.options].find((option) => option.selected);
if (selected) {
if (value === true) {
matches = true;
} else {
matches = selected.value === value;
}
} else {
if (value) {
matches = false;
} else {
const msg = `Expected an option to be selected in ${base} (but none was)`;
return { pass: true, message: () => msg };
}
}
} else {
throw new Error(
"Not a valid element that has a value attribute. Please insert an element that has a value."
);
}
if (this.affirmative && !matches) {
const msg = `Expected ${base} to have value="${value}"`;
return { pass: false, message: () => msg };
}
if (this.isNot && matches) {
const msg = `Expected ${base} not to have value=${value}`;
return { pass: true, message: () => msg };
}
return { pass: !this.isNot };
}
function toMatchSelector (frag, selectorStr) {
// To avoid double negations ¯\_(ツ)_/¯
this.affirmative = !this.isNot;
// Convert it into a plain array of nodes
frag = normalize(frag);
for (let el of frag) {
const base = getPlainTag(el);
const matches = el.matches(selectorStr);
if (this.affirmative && !matches) {
const msg = `Expected ${base} to match selector, ${selectorStr}`;
return { pass: false, message: () => msg };
}
if (this.isNot && matches) {
const msg = `Expected ${base} not to match selector, ${selectorStr}`;
return { pass: true, message: () => msg };
}
}
return { pass: !this.isNot };
}
// Parse JS camelCase style properties to lowercase hyphenated strings
const parseCamelCase = (styleToParse) =>
styleToParse
.replace(/([a-z\d])([A-Z])/g, "$1" + "-" + "$2")
.replace(/([A-Z]+)([A-Z][a-z\d]+)/g, "$1" + "-" + "$2")
.toLowerCase();
// Clean styles object to return string array of individual styles styles
const cleanStylesObject = (styles) =>
Object.entries(styles).map(
([key, value]) => `${parseCamelCase(key)}: ${value}`
);
// Split style string into array with all semicolons and spaces removed
const cleanStylesStr = (stylesStr) => {
let styles = stylesStr.split("; ");
styles[styles.length - 1] = styles[styles.length - 1].replace(/;/g, "");
return styles;
};
// Get correct error msg string depending on number of incorrect styles
const getErrorStr = (incorrectStyles) =>
`style${incorrectStyles.length > 1 ? "s" : ""} [${incorrectStyles.join(
", "
)}]`;
function toHaveStyle (frag, styles) {
// To avoid double negations ¯\_(ツ)_/¯
this.affirmative = !this.isNot;
// Convert it into a plain array of nodes
frag = normalize(frag);
for (let el of frag) {
// Get the element string for use in error message if test fails
const base = getPlainTag(el);
// Get an array of style strings present on the HTML element
const elStyles = Object.entries(el.style["_values"]).map(
([key, value]) => `${key}: ${value}`
);
// Get an array of style strings to search for in the element styles. Has to handle styles argument of either type string or type object
let stylesArray =
typeof styles === "string"
? cleanStylesStr(styles)
: cleanStylesObject(styles);
// expect(<div style={{display: "none"}} />).toHaveStyle({ display: "none" });
if (this.affirmative) {
// Check each of the search styles to see if they're present on the HTML element and isolate missing styles
const missingStyles = stylesArray.filter(
(styleToBeChecked) => !elStyles.includes(styleToBeChecked)
);
if (missingStyles.length) {
const msg = `Expected ${base} to include ${getErrorStr(missingStyles)}`;
return { pass: false, message: () => msg };
}
}
// expect(<div style={{display: "none"}} />).not.toHaveStyle({ backgroundColor: "red" });
if (this.isNot) {
// Check each of the search styles to see if they're incorrectly present on the HTML element and isolate those that are
const incorrectStyles = stylesArray.filter((styleToBeChecked) =>
elStyles.includes(styleToBeChecked)
);
if (incorrectStyles.length) {
const msg = `Expected ${base} not to include ${getErrorStr(
incorrectStyles
)}`;
return { pass: true, message: () => msg };
}
}
}
return { pass: !this.isNot };
}
expect.extend({
toBeEnabled,
toHaveAttribute,
toHaveClass,
toHaveHtml,
toHaveText,
toHaveValue,
toMatchSelector,
toHaveStyle,
});
function ReactTest(obj, ctx = {}) {
if (!(this instanceof ReactTest)) return new ReactTest(obj, ctx);
this.events = ctx.events || {};
window.addEventListener = (event, callback) => {
this.events[event] = this.events[event] || [];
this.events[event].push(callback);
};
document.addEventListener = (event, callback) => {
this.events[event] = this.events[event] || [];
this.events[event].push(callback);
};
this.nodes = render(obj);
// Add a .length that goes to measure the nodes
Object.defineProperty(this, "length", { get: () => this.nodes.length });
return this;
}
// Allow to iterate with for...of and destructure it like [...$list.find('li')]
ReactTest.prototype[Symbol.iterator] = function* () {
for (let node of this.nodes) {
yield node;
}
};
/**
* Read the attribute value of the first node and return its value, or null if there's no node or attribute:
*
* ```js
* const input = $(<input name="email" disabled />);
* expect(input.attr("name")).toBe("email");
* expect(input.attr("disabled")).toBe("");
* expect(input.attr("placeholder")).toBe(null);
* ```
*
* **[→ Full .attr() Docs](https://react-test.dev/documentation#attr)**
*/
ReactTest.prototype.attr = function (name) {
const node = this.get(0);
return node && node.getAttribute(name);
};
/**
* Get all of the matched nodes as a plain array. Optionally extract data of each node either with a key used as an prop, or with a callback:
*
* ```js
* const list = $(<List />).children();
* list.array(); // [<li>A</li>, <li>B</li>, <li>C</li>]
* list.array("nodeName"); // ["LI", "LI", "LI"]
* list.array(node => node.innerText); // ["A", "B", "C"]
* ```
*
* **[→ Full .array() Docs](https://react-test.dev/documentation#array)**
*/
ReactTest.prototype.array = function (callback = (node) => node) {
if (typeof callback === "string") {
const key = callback;
callback = (node) => node[key];
}
return this.nodes.map(callback);
};
/**
* Trigger a change in all of the matched elements. It should be awaited for the side effects to run and the component to re-rendered:
*
* ```js
* const input = $(<input defaultValue="hello" />);
* expect(input).toHaveValue("hello");
* await input.change("world");
* expect(input).toHaveValue("world");
* ```
*
* **[→ Full .change() Docs](https://react-test.dev/documentation#change)**
*/
ReactTest.prototype.change = function (value) {
// This is needed for uncontrolled inputs
this.map((node) => {
if (
node.nodeName === "INPUT" &&
["checkbox", "radio"].includes(node.type)
) {
node.checked = value;
} else {
node.value = value;
}
});
return this.trigger("change", { target: { value } });
};
/**
* Get the children nodes of all of the matched elements, optionally filtering them with a CSS selector:
*
* ```js
* const list = $(<List />);
* expect(list.children()).toHaveLength(3)
* expect(list.children(".active")).toHaveLength(1);
* ```
*
* **[→ Full .children() Docs](https://react-test.dev/documentation#children)**
*/
ReactTest.prototype.children = function (selector = "*") {
return this.map((node) => [...node.children]).filter(selector);
};
/**
* Trigger a click on all the matched elements. It should be awaited for the side effects to run and the component to re-rendered:
*
* ```js
* const counter = $(<Counter />);
* expect(counter.text()).toEqual("0");
* await counter.click();
* expect(counter.text()).toEqual("1");
* ```
*
* **[→ Full .click() Docs](https://react-test.dev/documentation#click)**
*/
ReactTest.prototype.click = function () {
return this.trigger("click");
};
/**
* Find the first ancestor that matches the selector for each element (deduplicated):
*
* ```js
* const list = $(<List />);
* const item = list.find("a").closest("li");
* expect(item.html()).toBe("<li><a>A</a></li>");
* ```
*
* **[→ Full .closest() Docs](https://react-test.dev/documentation#closest)**
*/
ReactTest.prototype.closest = function (selector = "*") {
return this.map((node) => node.closest(selector));
};
/**
* Read the data-attribute value of the first node and return its value:
*
* ```js
* const card = $(<div data-id="25" data-selected />);
* expect(card.data("id")).toBe("25");
* expect(card.data("selected")).toBe("true");
* expect(card.data("name")).toBe(null);
* ```
*
* **[→ Full .data() Docs](https://react-test.dev/documentation#data)**
*/
ReactTest.prototype.data = function (name) {
return this.attr(`data-${name}`);
};
/**
* Makes the component to wait for the specified period of time in milliseconds:
*
* ```js
* const down = $(<CountDown />);
* expect(down).toHaveText("3");
* await down.delay(4000); // 4 seconds
* expect(down).toHaveText("Done!");
* ```
*
* **[→ Full .delay() Docs](https://react-test.dev/documentation#delay)**
*/
ReactTest.prototype.delay = async function (time) {
await testUtils.act(() => new Promise((done) => setTimeout(done, time)));
};
/**
* Iterates over each of the nodes and returns the same collection of nodes as there was before:
*
* ```js
* const items = $(<List />).find("li");
* const texts = [];
* items.each((node) => texts.push(node.innerText));
* expect(texts).toEqual(["A", "B", "C"]);
* ```
*
* **[→ Full .each() Docs](https://react-test.dev/documentation#each)**
*/
ReactTest.prototype.each = function (callback = () => {}) {
this.array(callback);
return this;
};
/**
* Keep only the nodes that match the selector, removing the others:
*
* ```js
* const items = $(<ChatRooms />).children();
* const people = items.filter(".user").array("innerText");
* expect(people).toEqual(["John", "Sarah"]);
* ```
*
* **[→ Full .filter() Docs](https://react-test.dev/documentation#filter)**
*/
ReactTest.prototype.filter = function (selector = "*") {
// An plain string
if (typeof selector === "string") {
const sel = selector;
selector = (node) => node.matches(sel);
}
// An instance of ReactTest
if (selector.nodes) {
const sel = selector;
selector = (node) => sel.nodes.includes(node);
}
return ReactTest(this.array().filter(selector), this);
};
/**
* Get all of the descendants of the nodes with an optional filter:
*
* ```js
* const links = $(<ChatRooms />).find("a");
* expect(links).toHaveAttribute("src");
* ```
*
* **[→ Full .find() Docs](https://react-test.dev/documentation#find)**
*/
ReactTest.prototype.find = function (selector) {
if (!selector) return this;
return this.map((node) => [...node.querySelectorAll(selector)]);
};
/**
* Get a native DOM Node given its index. Defaults to the first element:
*
* ```js
* const item = $(<List />).children().get(0);
* expect(item.innerText).toBe("First Item");
* ```
*
* **[→ Full .get() Docs](https://react-test.dev/documentation#get)**
*/
ReactTest.prototype.get = function (index = 0) {
// Convert it to a plain array
const nodes = this.array();
// No elements at all; cannot match
if (!nodes.length) return null;
// Wrap around overflowing indexes
index = index % nodes.length;
// Ensure the index is positive
index = (nodes.length + index) % nodes.length;
// Return the correct node
return nodes[index];
};
/**
* Retrieve the OuterHTML of the first element matched, with the whitespace normalized:
*
* ```js
* const items = $(<List />).children();
* expect(items.html()).toBe("<li>First Item</li>");
* ```
*
* **[→ Full .html() Docs](https://react-test.dev/documentation#html)**
*/
ReactTest.prototype.html = function () {
const node = this.get(0);
return node ? node.outerHTML : "";
};
/**
* Check whether all of the nodes match the selector:
*
* ```js
* const items = $(<List />).children();
* expect(items.is("li")).toBe(true);
* ```
*
* **[→ Full .is() Docs](https://react-test.dev/documentation#is)**
*/
ReactTest.prototype.is = function (selector = "*") {
return this.filter(selector).length === this.length;
};
/**
* Iterates over each of the nodes and returns a new collection with the nodes that were returned from the callback:
*
* ```js
* const items = $(<List />).map(node => {
* return node.querySelectorAll("li");
* }).array("nodeName");
* expect(items).toBe(["LI", "LI"]);
* ```
*
* **[→ Full .map() Docs](https://react-test.dev/documentation#map)**
*/
ReactTest.prototype.map = function (callback) {
// We don't want to select repeated nodes
const nodes = [];
this.array(callback)
// Convert any potential NodeList into an array of plain nodes
.map((ret) => (ret && ret.forEach ? [...ret] : ret))
.flat()
.forEach((node) => {
if (!node) return;
if (nodes.includes(node)) return;
nodes.push(node);
});
return ReactTest(nodes, this);
};
/**
* Remove the matched nodes from the collection. It's the opposite of .filter():
*
* ```js
* const items = $(<ChatRooms />).children();
* const groups = items.not(".user").array("innerText");
* expect(groups).toEqual(["Summer", "Birthday"]);
* ```
*
* **[→ Full .not() Docs](https://react-test.dev/documentation#not)**
*/
ReactTest.prototype.not = function (filter = "*") {
if (typeof filter === "function") {
throw new Error("A callback is not allowed for .not()");
}
return this.filter((node) => !ReactTest(node).is(filter));
};
/**
* Return a new collection with the direct parent of the current nodes with an optional filter:
*
* ```js
* const list = $(<List />);
* const items = list.find("li > a").parent();
* expect(items.array("nodeName")).toEqual(["LI", "LI"]);
* ```
*
* **[→ Full .parent() Docs](https://react-test.dev/documentation#parent)**
*/
ReactTest.prototype.parent = function () {
return this.map((node) => node.parentNode);
};
/**
* Re-render a component with the new props specified as a plain object:
*
* ```js
* const demo = $(<Demo className="hello" />);
* expect(demo).toHaveHtml(`<div class="hello">world</div>`);
* demo.props({ className: "bye" });
* expect(demo).toHaveHtml(`<div class="bye">world</div>`);
* ```
*
* **[→ Full .props() Docs](https://react-test.dev/documentation#props)**
*/
ReactTest.prototype.props = function (props) {
const container = this.nodes[0].closest("#root");
const component = container.component;
const root = container.root;
const handler = container.handler;
const Catcher = container.catcher;
if (typeof props === "function") {
props = props(component.props);
}
testUtils.act(() =>
root.render(React.createElement(Catcher, null, { ...component, props }))
);
if (handler.error) {
testUtils.act(() => root.unmount());
throw handler.error;
}
this.nodes = [...container.childNodes];
return this;
};
/**
* Rerender the component as specified with the new value:
*
* ```js
* const demo = $(<Demo className="hello" />);
* expect(demo).toHaveHtml(`<div class="hello">world</div>`);
* demo.render(<Demo className="bye" />);
* expect(demo).toHaveHtml(`<div class="bye">world</div>`);
* ```
*
* **[→ Full .render() Docs](https://react-test.dev/documentation#render)**
*/
ReactTest.prototype.render = function (component) {
const container = this.nodes[0].closest("#root");
const root = container.root;
const Catcher = container.catcher;
const handler = container.handler;
testUtils.act(() => root.render(React.createElement(Catcher, null, component)));
if (handler.error) {
testUtils.act(() => root.unmount());
throw handler.error;
}
this.nodes = [...container.childNodes];
return this;
};
/**
* Find all of the sibling nodes to the current one:
*
* ```js
* const list = $(<List />);
* const items = list.find("li.active").siblings();
* expect(items.array("className")).toEqual(["", ""]);
* ```