forked from rjsf-team/react-jsonschema-form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayField_test.js
1286 lines (1084 loc) · 37.5 KB
/
ArrayField_test.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
import React from "react";
import { expect } from "chai";
import { Simulate } from "react-addons-test-utils";
import { createFormComponent, createSandbox } from "./test_utils";
describe("ArrayField", () => {
let sandbox;
const CustomComponent = props => {
return <div id="custom">{props.rawErrors}</div>;
};
beforeEach(() => {
sandbox = createSandbox();
});
afterEach(() => {
sandbox.restore();
});
describe("Unsupported array schema", () => {
it("should warn on missing items descriptor", () => {
const { node } = createFormComponent({ schema: { type: "array" } });
expect(
node.querySelector(".field-array > .unsupported-field").textContent
).to.contain("Missing items definition");
});
});
describe("List of inputs", () => {
const schema = {
type: "array",
title: "my list",
description: "my description",
items: { type: "string" },
};
it("should render a fieldset", () => {
const { node } = createFormComponent({ schema });
expect(node.querySelectorAll("fieldset")).to.have.length.of(1);
});
it("should render a fieldset legend", () => {
const { node } = createFormComponent({ schema });
const legend = node.querySelector("fieldset > legend");
expect(legend.textContent).eql("my list");
expect(legend.id).eql("root__title");
});
it("should render a description", () => {
const { node } = createFormComponent({ schema });
const description = node.querySelector("fieldset > .field-description");
expect(description.textContent).eql("my description");
expect(description.id).eql("root__description");
});
it("should render a customized title", () => {
const CustomTitleField = ({ title }) => <div id="custom">{title}</div>;
const { node } = createFormComponent({
schema,
fields: { TitleField: CustomTitleField },
});
expect(node.querySelector("fieldset > #custom").textContent).to.eql(
"my list"
);
});
it("should render a customized description", () => {
const CustomDescriptionField = ({ description }) => (
<div id="custom">{description}</div>
);
const { node } = createFormComponent({
schema,
fields: {
DescriptionField: CustomDescriptionField,
},
});
expect(node.querySelector("fieldset > #custom").textContent).to.eql(
"my description"
);
});
it("should render a customized file widget", () => {
const { node } = createFormComponent({
schema,
uiSchema: {
"ui:widget": "files",
},
widgets: { FileWidget: CustomComponent },
});
expect(node.querySelector("#custom")).to.exist;
});
it("should pass rawErrors down to custom array field templates", () => {
const schema = {
type: "array",
title: "my list",
description: "my description",
items: { type: "string" },
minItems: 2,
};
const { node } = createFormComponent({
schema,
ArrayFieldTemplate: CustomComponent,
formData: [1],
liveValidate: true,
});
const matches = node.querySelectorAll("#custom");
expect(matches).to.have.length.of(1);
expect(matches[0].textContent).to.eql(
"should NOT have less than 2 items"
);
});
it("should contain no field in the list by default", () => {
const { node } = createFormComponent({ schema });
expect(node.querySelectorAll(".field-string")).to.have.length.of(0);
});
it("should have an add button", () => {
const { node } = createFormComponent({ schema });
expect(node.querySelector(".array-item-add button")).not.eql(null);
});
it("should not have an add button if addable is false", () => {
const { node } = createFormComponent({
schema,
uiSchema: { "ui:options": { addable: false } },
});
expect(node.querySelector(".array-item-add button")).to.be.null;
});
it("should add a new field when clicking the add button", () => {
const { node } = createFormComponent({ schema });
Simulate.click(node.querySelector(".array-item-add button"));
expect(node.querySelectorAll(".field-string")).to.have.length.of(1);
});
it("should not provide an add button if length equals maxItems", () => {
const { node } = createFormComponent({
schema: { maxItems: 2, ...schema },
formData: ["foo", "bar"],
});
expect(node.querySelector(".array-item-add button")).to.be.null;
});
it("should provide an add button if length is lesser than maxItems", () => {
const { node } = createFormComponent({
schema: { maxItems: 2, ...schema },
formData: ["foo"],
});
expect(node.querySelector(".array-item-add button")).not.eql(null);
});
it("should not provide an add button if addable is expliclty false regardless maxItems value", () => {
const { node } = createFormComponent({
schema: { maxItems: 2, ...schema },
formData: ["foo"],
uiSchema: {
"ui:options": {
addable: false,
},
},
});
expect(node.querySelector(".array-item-add button")).to.be.null;
});
it("should ignore addable value if maxItems constraint is not satisfied", () => {
const { node } = createFormComponent({
schema: { maxItems: 2, ...schema },
formData: ["foo", "bar"],
uiSchema: {
"ui:options": {
addable: true,
},
},
});
expect(node.querySelector(".array-item-add button")).to.be.null;
});
it("should mark a non-null array item widget as required", () => {
const { node } = createFormComponent({ schema });
Simulate.click(node.querySelector(".array-item-add button"));
expect(node.querySelector(".field-string input[type=text]").required).eql(
true
);
});
it("should fill an array field with data", () => {
const { node } = createFormComponent({
schema,
formData: ["foo", "bar"],
});
const inputs = node.querySelectorAll(".field-string input[type=text]");
expect(inputs).to.have.length.of(2);
expect(inputs[0].value).eql("foo");
expect(inputs[1].value).eql("bar");
});
it("should't have reorder buttons when list length <= 1", () => {
const { node } = createFormComponent({ schema, formData: ["foo"] });
expect(node.querySelector(".array-item-move-up")).eql(null);
expect(node.querySelector(".array-item-move-down")).eql(null);
});
it("should have reorder buttons when list length >= 2", () => {
const { node } = createFormComponent({
schema,
formData: ["foo", "bar"],
});
expect(node.querySelector(".array-item-move-up")).not.eql(null);
expect(node.querySelector(".array-item-move-down")).not.eql(null);
});
it("should move down a field from the list", () => {
const { node } = createFormComponent({
schema,
formData: ["foo", "bar", "baz"],
});
const moveDownBtns = node.querySelectorAll(".array-item-move-down");
Simulate.click(moveDownBtns[0]);
const inputs = node.querySelectorAll(".field-string input[type=text]");
expect(inputs).to.have.length.of(3);
expect(inputs[0].value).eql("bar");
expect(inputs[1].value).eql("foo");
expect(inputs[2].value).eql("baz");
});
it("should move up a field from the list", () => {
const { node } = createFormComponent({
schema,
formData: ["foo", "bar", "baz"],
});
const moveUpBtns = node.querySelectorAll(".array-item-move-up");
Simulate.click(moveUpBtns[2]);
const inputs = node.querySelectorAll(".field-string input[type=text]");
expect(inputs).to.have.length.of(3);
expect(inputs[0].value).eql("foo");
expect(inputs[1].value).eql("baz");
expect(inputs[2].value).eql("bar");
});
it("should disable move buttons on the ends of the list", () => {
const { node } = createFormComponent({
schema,
formData: ["foo", "bar"],
});
const moveUpBtns = node.querySelectorAll(".array-item-move-up");
const moveDownBtns = node.querySelectorAll(".array-item-move-down");
expect(moveUpBtns[0].disabled).eql(true);
expect(moveDownBtns[0].disabled).eql(false);
expect(moveUpBtns[1].disabled).eql(false);
expect(moveDownBtns[1].disabled).eql(true);
});
it("should not show move up/down buttons if orderable is false", () => {
const { node } = createFormComponent({
schema,
formData: ["foo", "bar"],
uiSchema: { "ui:options": { orderable: false } },
});
const moveUpBtns = node.querySelector(".array-item-move-up");
const moveDownBtns = node.querySelector(".array-item-move-down");
expect(moveUpBtns).to.be.null;
expect(moveDownBtns).to.be.null;
});
it("should remove a field from the list", () => {
const { node } = createFormComponent({
schema,
formData: ["foo", "bar"],
});
const dropBtns = node.querySelectorAll(".array-item-remove");
Simulate.click(dropBtns[0]);
const inputs = node.querySelectorAll(".field-string input[type=text]");
expect(inputs).to.have.length.of(1);
expect(inputs[0].value).eql("bar");
});
it("should not show remove button if removable is false", () => {
const { node } = createFormComponent({
schema,
formData: ["foo", "bar"],
uiSchema: { "ui:options": { removable: false } },
});
const dropBtn = node.querySelector(".array-item-remove");
expect(dropBtn).to.be.null;
});
it("should force revalidation when a field is removed", () => {
// refs #195
const { node } = createFormComponent({
schema: {
...schema,
items: { ...schema.items, minLength: 4 },
},
formData: ["foo", "bar!"],
});
try {
Simulate.submit(node);
} catch (e) {
// Silencing error thrown as failure is expected here
}
expect(
node.querySelectorAll(".has-error .error-detail")
).to.have.length.of(1);
const dropBtns = node.querySelectorAll(".array-item-remove");
Simulate.click(dropBtns[0]);
expect(
node.querySelectorAll(".has-error .error-detail")
).to.have.length.of(0);
});
it("should handle cleared field values in the array", () => {
const schema = {
type: "array",
items: { type: "integer" },
};
const formData = [1, 2, 3];
const { comp, node } = createFormComponent({
liveValidate: true,
schema,
formData,
});
Simulate.change(node.querySelector("#root_1"), {
target: { value: "" },
});
expect(comp.state.formData).eql([1, null, 3]);
expect(comp.state.errors).to.have.length.of(1);
});
it("should render the input widgets with the expected ids", () => {
const { node } = createFormComponent({
schema,
formData: ["foo", "bar"],
});
const inputs = node.querySelectorAll("input[type=text]");
expect(inputs[0].id).eql("root_0");
expect(inputs[1].id).eql("root_1");
});
it("should render nested input widgets with the expected ids", () => {
const complexSchema = {
type: "object",
properties: {
foo: {
type: "array",
items: {
type: "object",
properties: {
bar: { type: "string" },
baz: { type: "string" },
},
},
},
},
};
const { node } = createFormComponent({
schema: complexSchema,
formData: {
foo: [{ bar: "bar1", baz: "baz1" }, { bar: "bar2", baz: "baz2" }],
},
});
const inputs = node.querySelectorAll("input[type=text]");
expect(inputs[0].id).eql("root_foo_0_bar");
expect(inputs[1].id).eql("root_foo_0_baz");
expect(inputs[2].id).eql("root_foo_1_bar");
expect(inputs[3].id).eql("root_foo_1_baz");
});
it("should render enough inputs with proper defaults to match minItems in schema when no formData is set", () => {
const complexSchema = {
type: "object",
definitions: {
Thing: {
type: "object",
properties: {
name: {
type: "string",
default: "Default name",
},
},
},
},
properties: {
foo: {
type: "array",
minItems: 2,
items: {
$ref: "#/definitions/Thing",
},
},
},
};
let form = createFormComponent({
schema: complexSchema,
formData: {},
});
let inputs = form.node.querySelectorAll("input[type=text]");
expect(inputs[0].value).eql("Default name");
expect(inputs[1].value).eql("Default name");
});
it("should render an input for each default value, even when this is greater than minItems", () => {
const schema = {
type: "object",
properties: {
turtles: {
type: "array",
minItems: 2,
default: ["Raphael", "Michaelangelo", "Donatello", "Leonardo"],
items: {
type: "string",
},
},
},
};
const { node } = createFormComponent({ schema: schema });
const inputs = node.querySelectorAll("input[type=text]");
expect(inputs.length).to.eql(4);
expect(inputs[0].value).to.eql("Raphael");
expect(inputs[1].value).to.eql("Michaelangelo");
expect(inputs[2].value).to.eql("Donatello");
expect(inputs[3].value).to.eql("Leonardo");
});
it("should render enough input to match minItems, populating the first with default values, and the rest empty", () => {
const schema = {
type: "object",
properties: {
turtles: {
type: "array",
minItems: 4,
default: ["Raphael", "Michaelangelo"],
items: {
type: "string",
},
},
},
};
const { node } = createFormComponent({ schema });
const inputs = node.querySelectorAll("input[type=text]");
expect(inputs.length).to.eql(4);
expect(inputs[0].value).to.eql("Raphael");
expect(inputs[1].value).to.eql("Michaelangelo");
expect(inputs[2].value).to.eql("");
expect(inputs[3].value).to.eql("");
});
it("should render enough input to match minItems, populating the first with default values, and the rest with the item default", () => {
const schema = {
type: "object",
properties: {
turtles: {
type: "array",
minItems: 4,
default: ["Raphael", "Michaelangelo"],
items: {
type: "string",
default: "Unknown",
},
},
},
};
const { node } = createFormComponent({ schema });
const inputs = node.querySelectorAll("input[type=text]");
expect(inputs.length).to.eql(4);
expect(inputs[0].value).to.eql("Raphael");
expect(inputs[1].value).to.eql("Michaelangelo");
expect(inputs[2].value).to.eql("Unknown");
expect(inputs[3].value).to.eql("Unknown");
});
it("should not add minItems extra formData entries when schema item is a multiselect", () => {
const schema = {
type: "object",
properties: {
multipleChoicesList: {
type: "array",
minItems: 3,
uniqueItems: true,
items: {
type: "string",
enum: ["Aramis", "Athos", "Porthos", "d'Artagnan"],
},
},
},
};
const uiSchema = {
multipleChoicesList: {
"ui:widget": "checkboxes",
},
};
const form = createFormComponent({
schema: schema,
uiSchema: uiSchema,
formData: {},
liveValidate: true,
}).comp;
expect(form.state.formData).to.have.property("multipleChoicesList");
expect(form.state.formData.multipleChoicesList).to.be.empty;
expect(form.state.errors.length).to.equal(1);
expect(form.state.errors[0].name).to.equal("minItems");
expect(form.state.errors[0].params.limit).to.equal(3);
});
it("should honor given formData, even when it does not meet ths minItems-requirement", () => {
const complexSchema = {
type: "object",
definitions: {
Thing: {
type: "object",
properties: {
name: {
type: "string",
default: "Default name",
},
},
},
},
properties: {
foo: {
type: "array",
minItems: 2,
items: {
$ref: "#/definitions/Thing",
},
},
},
};
const form = createFormComponent({
schema: complexSchema,
formData: { foo: [] },
});
const inputs = form.node.querySelectorAll("input[type=text]");
expect(inputs.length).eql(0);
});
});
describe("Multiple choices list", () => {
const schema = {
type: "array",
title: "My field",
items: {
enum: ["foo", "bar", "fuzz"],
type: "string",
},
uniqueItems: true,
};
describe("Select multiple widget", () => {
it("should render a select widget", () => {
const { node } = createFormComponent({ schema });
expect(node.querySelectorAll("select")).to.have.length.of(1);
});
it("should render a select widget with a label", () => {
const { node } = createFormComponent({ schema });
expect(node.querySelector(".field label").textContent).eql("My field");
});
it("should render a select widget with multiple attribute", () => {
const { node } = createFormComponent({ schema });
expect(node.querySelector(".field select").getAttribute("multiple")).not
.to.be.null;
});
it("should render options", () => {
const { node } = createFormComponent({ schema });
expect(node.querySelectorAll("select option")).to.have.length.of(3);
});
it("should handle a change event", () => {
const { comp, node } = createFormComponent({ schema });
Simulate.change(node.querySelector(".field select"), {
target: {
options: [
{ selected: true, value: "foo" },
{ selected: true, value: "bar" },
{ selected: false, value: "fuzz" },
],
},
});
expect(comp.state.formData).eql(["foo", "bar"]);
});
it("should handle a blur event", () => {
const onBlur = sandbox.spy();
const { node } = createFormComponent({ schema, onBlur });
const select = node.querySelector(".field select");
Simulate.blur(select, {
target: {
options: [
{ selected: true, value: "foo" },
{ selected: true, value: "bar" },
{ selected: false, value: "fuzz" },
],
},
});
expect(onBlur.calledWith(select.id, ["foo", "bar"])).to.be.true;
});
it("should handle a focus event", () => {
const onFocus = sandbox.spy();
const { node } = createFormComponent({ schema, onFocus });
const select = node.querySelector(".field select");
Simulate.focus(select, {
target: {
options: [
{ selected: true, value: "foo" },
{ selected: true, value: "bar" },
{ selected: false, value: "fuzz" },
],
},
});
expect(onFocus.calledWith(select.id, ["foo", "bar"])).to.be.true;
});
it("should fill field with data", () => {
const { node } = createFormComponent({
schema,
formData: ["foo", "bar"],
});
const options = node.querySelectorAll(".field select option");
expect(options).to.have.length.of(3);
expect(options[0].selected).eql(true); // foo
expect(options[1].selected).eql(true); // bar
expect(options[2].selected).eql(false); // fuzz
});
it("should render the select widget with the expected id", () => {
const { node } = createFormComponent({ schema });
expect(node.querySelector("select").id).eql("root");
});
it("should pass rawErrors down to custom widgets", () => {
const { node } = createFormComponent({
schema,
widgets: {
SelectWidget: CustomComponent,
},
formData: ["foo", "foo"],
liveValidate: true,
});
const matches = node.querySelectorAll("#custom");
expect(matches).to.have.length.of(1);
expect(matches[0].textContent).to.eql(
"should NOT have duplicate items (items ## 0 and 1 are identical)"
);
});
});
describe("CheckboxesWidget", () => {
const uiSchema = {
"ui:widget": "checkboxes",
};
it("should render the expected number of checkboxes", () => {
const { node } = createFormComponent({ schema, uiSchema });
expect(node.querySelectorAll("[type=checkbox]")).to.have.length.of(3);
});
it("should render the expected labels", () => {
const { node } = createFormComponent({ schema, uiSchema });
const labels = [].map.call(
node.querySelectorAll(".checkbox label"),
node => node.textContent
);
expect(labels).eql(["foo", "bar", "fuzz"]);
});
it("should handle a change event", () => {
const { comp, node } = createFormComponent({
schema,
uiSchema,
});
Simulate.change(node.querySelectorAll("[type=checkbox]")[0], {
target: { checked: true },
});
Simulate.change(node.querySelectorAll("[type=checkbox]")[2], {
target: { checked: true },
});
expect(comp.state.formData).eql(["foo", "fuzz"]);
});
it("should fill field with data", () => {
const { node } = createFormComponent({
schema,
uiSchema,
formData: ["foo", "fuzz"],
});
const labels = [].map.call(
node.querySelectorAll("[type=checkbox]"),
node => node.checked
);
expect(labels).eql([true, false, true]);
});
it("should render the widget with the expected id", () => {
const { node } = createFormComponent({ schema, uiSchema });
expect(node.querySelector(".checkboxes").id).eql("root");
});
it("should support inline checkboxes", () => {
const { node } = createFormComponent({
schema,
uiSchema: {
"ui:widget": "checkboxes",
"ui:options": {
inline: true,
},
},
});
expect(node.querySelectorAll(".checkbox-inline")).to.have.length.of(3);
});
it("should pass rawErrors down to custom widgets", () => {
const schema = {
type: "array",
title: "My field",
items: {
enum: ["foo", "bar", "fuzz"],
type: "string",
},
minItems: 3,
uniqueItems: true,
};
const { node } = createFormComponent({
schema,
widgets: {
CheckboxesWidget: CustomComponent,
},
uiSchema,
formData: [],
liveValidate: true,
});
const matches = node.querySelectorAll("#custom");
expect(matches).to.have.length.of(1);
expect(matches[0].textContent).to.eql(
"should NOT have less than 3 items"
);
});
});
});
describe("Multiple files field", () => {
const schema = {
type: "array",
title: "My field",
items: {
type: "string",
format: "data-url",
},
};
it("should render an input[type=file] widget", () => {
const { node } = createFormComponent({ schema });
expect(node.querySelectorAll("input[type=file]")).to.have.length.of(1);
});
it("should render a select widget with a label", () => {
const { node } = createFormComponent({ schema });
expect(node.querySelector(".field label").textContent).eql("My field");
});
it("should render a file widget with multiple attribute", () => {
const { node } = createFormComponent({ schema });
expect(node.querySelector(".field [type=file]").getAttribute("multiple"))
.not.to.be.null;
});
it("should handle a change event", () => {
sandbox.stub(window, "FileReader").returns({
set onload(fn) {
fn({ target: { result: "data:text/plain;base64,x=" } });
},
readAsDataUrl() {},
});
const { comp, node } = createFormComponent({ schema });
Simulate.change(node.querySelector(".field input[type=file]"), {
target: {
files: [
{ name: "file1.txt", size: 1, type: "type" },
{ name: "file2.txt", size: 2, type: "type" },
],
},
});
return new Promise(setImmediate).then(() =>
expect(comp.state.formData).eql([
"data:text/plain;name=file1.txt;base64,x=",
"data:text/plain;name=file2.txt;base64,x=",
])
);
});
it("should fill field with data", () => {
const { node } = createFormComponent({
schema,
formData: [
"data:text/plain;name=file1.txt;base64,dGVzdDE=",
"data:image/png;name=file2.png;base64,ZmFrZXBuZw==",
],
});
const li = node.querySelectorAll(".file-info li");
expect(li).to.have.length.of(2);
expect(li[0].textContent).eql("file1.txt (text/plain, 5 bytes)");
expect(li[1].textContent).eql("file2.png (image/png, 7 bytes)");
});
it("should render the file widget with the expected id", () => {
const { node } = createFormComponent({ schema });
expect(node.querySelector("input[type=file]").id).eql("root");
});
it("should pass rawErrors down to custom widgets", () => {
const schema = {
type: "array",
title: "My field",
items: {
type: "string",
format: "data-url",
},
minItems: 5,
};
const { node } = createFormComponent({
schema,
widgets: {
FileWidget: CustomComponent,
},
formData: [],
liveValidate: true,
});
const matches = node.querySelectorAll("#custom");
expect(matches).to.have.length.of(1);
expect(matches[0].textContent).to.eql(
"should NOT have less than 5 items"
);
});
});
describe("Nested lists", () => {
const schema = {
type: "array",
title: "A list of arrays",
items: {
type: "array",
title: "A list of numbers",
items: {
type: "number",
},
},
};
it("should render two lists of inputs inside of a list", () => {
const { node } = createFormComponent({
schema,
formData: [[1, 2], [3, 4]],
});
expect(node.querySelectorAll("fieldset fieldset")).to.have.length.of(2);
});
it("should add an inner list when clicking the add button", () => {
const { node } = createFormComponent({ schema });
expect(node.querySelectorAll("fieldset fieldset")).to.be.empty;
Simulate.click(node.querySelector(".array-item-add button"));
expect(node.querySelectorAll("fieldset fieldset")).to.have.length.of(1);
});
it("should pass rawErrors down to every level of custom widgets", () => {
const CustomItem = props => <div id="custom-item">{props.children}</div>;
const CustomTemplate = props => {
return (
<div id="custom">
{props.items &&
props.items.map((p, i) => <CustomItem key={i} {...p} />)}
<div id="custom-error">
{props.rawErrors && props.rawErrors.join(", ")}
</div>
</div>
);
};
const schema = {
type: "array",
title: "A list of arrays",
items: {
type: "array",
title: "A list of numbers",
items: {
type: "number",
},
minItems: 3,
},
minItems: 2,
};
const { node } = createFormComponent({
schema,
ArrayFieldTemplate: CustomTemplate,
formData: [[]],
liveValidate: true,
});
const matches = node.querySelectorAll("#custom-error");
expect(matches).to.have.length.of(2);
expect(matches[0].textContent).to.eql(
"should NOT have less than 3 items"
);
expect(matches[1].textContent).to.eql(
"should NOT have less than 2 items"
);
});
});
describe("Fixed items lists", () => {
const schema = {
type: "array",
title: "List of fixed items",
items: [
{
type: "string",