forked from rjsf-team/react-jsonschema-form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectField_test.js
649 lines (535 loc) · 17.8 KB
/
ObjectField_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
import React from "react";
import { expect } from "chai";
import { Simulate } from "react-addons-test-utils";
import { createFormComponent, createSandbox } from "./test_utils";
import validateFormData from "../src/validate";
describe("ObjectField", () => {
let sandbox;
beforeEach(() => {
sandbox = createSandbox();
});
afterEach(() => {
sandbox.restore();
});
describe("schema", () => {
const schema = {
type: "object",
title: "my object",
description: "my description",
required: ["foo"],
default: {
foo: "hey",
bar: true,
},
properties: {
foo: {
title: "Foo",
type: "string",
},
bar: {
type: "boolean",
},
},
};
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 object");
expect(legend.id).eql("root__title");
});
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 object"
);
});
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 default property label", () => {
const { node } = createFormComponent({ schema });
expect(node.querySelector(".field-boolean label").textContent).eql("bar");
});
it("should render a string property", () => {
const { node } = createFormComponent({ schema });
expect(
node.querySelectorAll(".field input[type=text]")
).to.have.length.of(1);
});
it("should render a boolean property", () => {
const { node } = createFormComponent({ schema });
expect(
node.querySelectorAll(".field input[type=checkbox]")
).to.have.length.of(1);
});
it("should handle a default object value", () => {
const { node } = createFormComponent({ schema });
expect(node.querySelector(".field input[type=text]").value).eql("hey");
expect(node.querySelector(".field input[type=checkbox]").checked).eql(
true
);
});
it("should handle required values", () => {
const { node } = createFormComponent({ schema });
// Required field is <input type="text" required="">
expect(
node.querySelector("input[type=text]").getAttribute("required")
).eql("");
expect(node.querySelector(".field-string label").textContent).eql("Foo*");
});
it("should fill fields with form data", () => {
const { node } = createFormComponent({
schema,
formData: {
foo: "hey",
bar: true,
},
});
expect(node.querySelector(".field input[type=text]").value).eql("hey");
expect(node.querySelector(".field input[type=checkbox]").checked).eql(
true
);
});
it("should handle object fields change events", () => {
const { comp, node } = createFormComponent({ schema });
Simulate.change(node.querySelector("input[type=text]"), {
target: { value: "changed" },
});
expect(comp.state.formData.foo).eql("changed");
});
it("should handle object fields with blur events", () => {
const onBlur = sandbox.spy();
const { node } = createFormComponent({ schema, onBlur });
const input = node.querySelector("input[type=text]");
Simulate.blur(input, {
target: { value: "changed" },
});
expect(onBlur.calledWith(input.id, "changed")).to.be.true;
});
it("should handle object fields with focus events", () => {
const onFocus = sandbox.spy();
const { node } = createFormComponent({ schema, onFocus });
const input = node.querySelector("input[type=text]");
Simulate.focus(input, {
target: { value: "changed" },
});
expect(onFocus.calledWith(input.id, "changed")).to.be.true;
});
it("should render the widget with the expected id", () => {
const { node } = createFormComponent({ schema });
expect(node.querySelector("input[type=text]").id).eql("root_foo");
expect(node.querySelector("input[type=checkbox]").id).eql("root_bar");
});
});
describe("fields ordering", () => {
const schema = {
type: "object",
properties: {
foo: { type: "string" },
bar: { type: "string" },
baz: { type: "string" },
qux: { type: "string" },
},
};
it("should use provided order", () => {
const { node } = createFormComponent({
schema,
uiSchema: {
"ui:order": ["baz", "qux", "bar", "foo"],
},
});
const labels = [].map.call(
node.querySelectorAll(".field > label"),
l => l.textContent
);
expect(labels).eql(["baz", "qux", "bar", "foo"]);
});
it("should insert unordered properties at wildcard position", () => {
const { node } = createFormComponent({
schema,
uiSchema: {
"ui:order": ["baz", "*", "foo"],
},
});
const labels = [].map.call(
node.querySelectorAll(".field > label"),
l => l.textContent
);
expect(labels).eql(["baz", "bar", "qux", "foo"]);
});
it("should throw when order list contains an extraneous property", () => {
const { node } = createFormComponent({
schema,
uiSchema: {
"ui:order": ["baz", "qux", "bar", "wut?", "foo", "huh?"],
},
});
expect(node.querySelector(".config-error").textContent).to.match(
/contains extraneous properties 'wut\?', 'huh\?'/
);
});
it("should throw when order list misses an existing property", () => {
const { node } = createFormComponent({
schema,
uiSchema: {
"ui:order": ["baz", "bar"],
},
});
expect(node.querySelector(".config-error").textContent).to.match(
/does not contain properties 'foo', 'qux'/
);
});
it("should throw when more than one wildcard is present", () => {
const { node } = createFormComponent({
schema,
uiSchema: {
"ui:order": ["baz", "*", "bar", "*"],
},
});
expect(node.querySelector(".config-error").textContent).to.match(
/contains more than one wildcard/
);
});
it("should order referenced schema definitions", () => {
const refSchema = {
definitions: {
testdef: { type: "string" },
},
type: "object",
properties: {
foo: { $ref: "#/definitions/testdef" },
bar: { $ref: "#/definitions/testdef" },
},
};
const { node } = createFormComponent({
schema: refSchema,
uiSchema: {
"ui:order": ["bar", "foo"],
},
});
const labels = [].map.call(
node.querySelectorAll(".field > label"),
l => l.textContent
);
expect(labels).eql(["bar", "foo"]);
});
it("should order referenced object schema definition properties", () => {
const refSchema = {
definitions: {
testdef: {
type: "object",
properties: {
foo: { type: "string" },
bar: { type: "string" },
},
},
},
type: "object",
properties: {
root: { $ref: "#/definitions/testdef" },
},
};
const { node } = createFormComponent({
schema: refSchema,
uiSchema: {
root: {
"ui:order": ["bar", "foo"],
},
},
});
const labels = [].map.call(
node.querySelectorAll(".field > label"),
l => l.textContent
);
expect(labels).eql(["bar", "foo"]);
});
it("should render the widget with the expected id", () => {
const schema = {
type: "object",
properties: {
foo: { type: "string" },
bar: { type: "string" },
},
};
const { node } = createFormComponent({
schema,
uiSchema: {
"ui:order": ["bar", "foo"],
},
});
const ids = [].map.call(
node.querySelectorAll("input[type=text]"),
node => node.id
);
expect(ids).eql(["root_bar", "root_foo"]);
});
});
describe("Title", () => {
const TitleField = props => <div id={`title-${props.title}`} />;
const fields = { TitleField };
it("should pass field name to TitleField if there is no title", () => {
const schema = {
type: "object",
properties: {
object: {
type: "object",
properties: {},
},
},
};
const { node } = createFormComponent({ schema, fields });
expect(node.querySelector("#title-object")).to.not.be.null;
});
it("should pass schema title to TitleField", () => {
const schema = {
type: "object",
properties: {},
title: "test",
};
const { node } = createFormComponent({ schema, fields });
expect(node.querySelector("#title-test")).to.not.be.null;
});
it("should pass empty schema title to TitleField", () => {
const schema = {
type: "object",
properties: {},
title: "",
};
const { node } = createFormComponent({ schema, fields });
expect(node.querySelector("#title-")).to.be.null;
});
});
describe("additionalProperties", () => {
const schema = {
type: "object",
additionalProperties: {
type: "string",
},
};
it("should automatically add a property field if in formData", () => {
const { node } = createFormComponent({
schema,
formData: { first: 1 },
});
expect(node.querySelectorAll(".field-string")).to.have.length.of(1);
});
it("should pass through non-schema properties and not throw validation errors if additionalProperties is undefined", () => {
const undefinedAPSchema = {
...schema,
properties: { second: { type: "string" } },
};
delete undefinedAPSchema.additionalProperties;
const { comp } = createFormComponent({
schema: undefinedAPSchema,
formData: { nonschema: 1 },
});
expect(comp.state.formData.nonschema).eql(1);
const result = validateFormData(comp.state.formData, comp.state.schema);
expect(result.errors).eql([]);
});
it("should pass through non-schema properties but throw a validation error if additionalProperties is false", () => {
const { comp } = createFormComponent({
schema: {
...schema,
additionalProperties: false,
properties: { second: { type: "string" } },
},
formData: { nonschema: 1 },
});
expect(comp.state.formData.nonschema).eql(1);
const result = validateFormData(comp.state.formData, comp.state.schema);
expect(result.errors[0].name).eql("additionalProperties");
});
it("should still obey properties if additionalProperties is defined", () => {
const { node } = createFormComponent({
schema: {
...schema,
properties: {
definedProperty: {
type: "string",
},
},
},
});
expect(node.querySelectorAll(".field-string")).to.have.length.of(1);
});
it("should render a label for the additional property key", () => {
const { node } = createFormComponent({
schema,
formData: { first: 1 },
});
expect(node.querySelector("[for='root_first-key']").textContent).eql(
"first Key"
);
});
it("should render a label for the additional property key if additionalProperties is true", () => {
const { node } = createFormComponent({
schema: { ...schema, additionalProperties: true },
formData: { first: 1 },
});
expect(node.querySelector("[for='root_first-key']").textContent).eql(
"first Key"
);
});
it("should not render a label for the additional property key if additionalProperties is false", () => {
const { node } = createFormComponent({
schema: { ...schema, additionalProperties: false },
formData: { first: 1 },
});
expect(node.querySelector("[for='root_first-key']")).eql(null);
});
it("should render a text input for the additional property key", () => {
const { node } = createFormComponent({
schema,
formData: { first: 1 },
});
expect(node.querySelector("#root_first-key").value).eql("first");
});
it("should render a label for the additional property value", () => {
const { node } = createFormComponent({
schema,
formData: { first: 1 },
});
expect(node.querySelector("[for='root_first']").textContent).eql("first");
});
it("should render a text input for the additional property value", () => {
const { node } = createFormComponent({
schema,
formData: { first: 1 },
});
expect(node.querySelector("#root_first").value).eql("1");
});
it("should rename formData key if key input is renamed", () => {
const { comp, node } = createFormComponent({
schema,
formData: { first: 1 },
});
const textNode = node.querySelector("#root_first-key");
Simulate.blur(textNode, {
target: { value: "newFirst" },
});
expect(comp.state.formData.newFirst).eql(1);
});
it("should attach suffix to formData key if new key already exists when key input is renamed", () => {
const formData = {
first: 1,
second: 2,
};
const { comp, node } = createFormComponent({
schema,
formData,
});
const textNode = node.querySelector("#root_first-key");
Simulate.blur(textNode, {
target: { value: "second" },
});
expect(comp.state.formData["second-1"]).eql(1);
});
it("should continue incrementing suffix to formData key until that key name is unique after a key input collision", () => {
const formData = {
first: 1,
second: 2,
"second-1": 2,
"second-2": 2,
"second-3": 2,
"second-4": 2,
"second-5": 2,
"second-6": 2,
};
const { comp, node } = createFormComponent({
schema,
formData,
});
const textNode = node.querySelector("#root_first-key");
Simulate.blur(textNode, {
target: { value: "second" },
});
expect(comp.state.formData["second-7"]).eql(1);
});
it("should have an expand button", () => {
const { node } = createFormComponent({ schema });
expect(node.querySelector(".object-property-expand button")).not.eql(
null
);
});
it("should not have an expand button if expandable is false", () => {
const { node } = createFormComponent({
schema,
uiSchema: { "ui:options": { expandable: false } },
});
expect(node.querySelector(".object-property-expand button")).to.be.null;
});
it("should add a new property when clicking the expand button", () => {
const { comp, node } = createFormComponent({ schema });
Simulate.click(node.querySelector(".object-property-expand button"));
expect(comp.state.formData.newKey).eql("New Value");
});
it("should add a new property with suffix when clicking the expand button and 'newKey' already exists", () => {
const { comp, node } = createFormComponent({
schema,
formData: { newKey: 1 },
});
Simulate.click(node.querySelector(".object-property-expand button"));
expect(comp.state.formData["newKey-1"]).eql("New Value");
});
it("should not provide an expand button if length equals maxProperties", () => {
const { node } = createFormComponent({
schema: { maxProperties: 1, ...schema },
formData: { first: 1 },
});
expect(node.querySelector(".object-property-expand button")).to.be.null;
});
it("should provide an expand button if length is less than maxProperties", () => {
const { node } = createFormComponent({
schema: { maxProperties: 2, ...schema },
formData: { first: 1 },
});
expect(node.querySelector(".object-property-expand button")).not.eql(
null
);
});
it("should not provide an expand button if expandable is expliclty false regardless of maxProperties value", () => {
const { node } = createFormComponent({
schema: { maxProperties: 2, ...schema },
formData: { first: 1 },
uiSchema: {
"ui:options": {
expandable: false,
},
},
});
expect(node.querySelector(".object-property-expand button")).to.be.null;
});
it("should ignore expandable value if maxProperties constraint is not satisfied", () => {
const { node } = createFormComponent({
schema: { maxProperties: 1, ...schema },
formData: { first: 1 },
uiSchema: {
"ui:options": {
expandable: true,
},
},
});
expect(node.querySelector(".object-property-expand button")).to.be.null;
});
});
});