forked from rjsf-team/react-jsonschema-form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSchemaField_test.js
357 lines (296 loc) · 9.62 KB
/
SchemaField_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
import React from "react";
import { expect } from "chai";
import { Simulate } from "react-addons-test-utils";
import SchemaField from "../src/components/fields/SchemaField";
import TitleField from "../src/components/fields/TitleField";
import DescriptionField from "../src/components/fields/DescriptionField";
import { createFormComponent, createSandbox } from "./test_utils";
import { getDefaultRegistry } from "../src/utils";
describe("SchemaField", () => {
let sandbox;
beforeEach(() => {
sandbox = createSandbox();
});
afterEach(() => {
sandbox.restore();
});
describe("Unsupported field", () => {
it("should warn on invalid field type", () => {
const { node } = createFormComponent({
schema: { type: "invalid" },
});
expect(node.querySelector(".unsupported-field").textContent).to.contain(
"Unknown field type invalid"
);
});
});
describe("Custom SchemaField component", () => {
const CustomSchemaField = function(props) {
return (
<div id="custom">
<SchemaField {...props} />
</div>
);
};
it("should use the specified custom SchemaType property", () => {
const fields = { SchemaField: CustomSchemaField };
const { node } = createFormComponent({
schema: { type: "string" },
fields,
});
expect(
node.querySelectorAll("#custom > .field input[type=text]")
).to.have.length.of(1);
});
});
describe("ui:field support", () => {
class MyObject extends React.Component {
constructor(props) {
super(props);
}
render() {
return <div id="custom" />;
}
}
const schema = {
type: "object",
properties: {
foo: { type: "string" },
bar: { type: "string" },
},
};
it("should use provided direct custom component for object", () => {
const uiSchema = { "ui:field": MyObject };
const { node } = createFormComponent({ schema, uiSchema });
expect(node.querySelectorAll("#custom")).to.have.length.of(1);
expect(node.querySelectorAll("label")).to.have.length.of(0);
});
it("should use provided direct custom component for specific property", () => {
const uiSchema = {
foo: { "ui:field": MyObject },
};
const { node } = createFormComponent({ schema, uiSchema });
expect(node.querySelectorAll("#custom")).to.have.length.of(1);
expect(node.querySelectorAll("input")).to.have.length.of(1);
expect(node.querySelectorAll("label")).to.have.length.of(1);
});
it("should provide custom field the expected fields", () => {
let receivedProps;
createFormComponent({
schema,
uiSchema: {
"ui:field": class extends React.Component {
constructor(props) {
super(props);
receivedProps = props;
}
render() {
return <div />;
}
},
},
});
const { registry } = receivedProps;
expect(registry.widgets).eql(getDefaultRegistry().widgets);
expect(registry.definitions).eql({});
expect(registry.fields).to.be.an("object");
expect(registry.fields.SchemaField).eql(SchemaField);
expect(registry.fields.TitleField).eql(TitleField);
expect(registry.fields.DescriptionField).eql(DescriptionField);
});
it("should use registered custom component for object", () => {
const uiSchema = { "ui:field": "myobject" };
const fields = { myobject: MyObject };
const { node } = createFormComponent({ schema, uiSchema, fields });
expect(node.querySelectorAll("#custom")).to.have.length.of(1);
});
it("should handle referenced schema definitions", () => {
const schema = {
definitions: {
foobar: {
type: "object",
properties: {
foo: { type: "string" },
bar: { type: "string" },
},
},
},
$ref: "#/definitions/foobar",
};
const uiSchema = { "ui:field": "myobject" };
const fields = { myobject: MyObject };
const { node } = createFormComponent({ schema, uiSchema, fields });
expect(node.querySelectorAll("#custom")).to.have.length.of(1);
});
it("should not pass classNames to child component", () => {
const CustomSchemaField = function(props) {
return (
<SchemaField
{...props}
uiSchema={{ ...props.uiSchema, "ui:field": undefined }}
/>
);
};
const schema = {
type: "string",
};
const uiSchema = {
"ui:field": "customSchemaField",
classNames: "foo",
};
const fields = { customSchemaField: CustomSchemaField };
const { node } = createFormComponent({ schema, uiSchema, fields });
expect(node.querySelectorAll(".foo")).to.have.length.of(1);
});
});
describe("label support", () => {
const schema = {
type: "object",
properties: {
foo: { type: "string" },
},
};
it("should render label by default", () => {
const { node } = createFormComponent({ schema });
expect(node.querySelectorAll("label")).to.have.length.of(1);
});
it("should render label if ui:options label is set to true", () => {
const uiSchema = {
foo: { "ui:options": { label: true } },
};
const { node } = createFormComponent({ schema, uiSchema });
expect(node.querySelectorAll("label")).to.have.length.of(1);
});
it("should not render label if ui:options label is set to false", () => {
const uiSchema = {
foo: { "ui:options": { label: false } },
};
const { node } = createFormComponent({ schema, uiSchema });
expect(node.querySelectorAll("label")).to.have.length.of(0);
});
});
describe("description support", () => {
const schema = {
type: "object",
properties: {
foo: { type: "string", description: "A Foo field" },
bar: { type: "string" },
},
};
it("should render description if available from the schema", () => {
const { node } = createFormComponent({ schema });
expect(node.querySelectorAll("#root_foo__description")).to.have.length.of(
1
);
});
it("should render description if available from a referenced schema", () => {
// Overriding.
const schemaWithReference = {
type: "object",
properties: {
foo: { $ref: "#/definitions/foo" },
bar: { type: "string" },
},
definitions: {
foo: {
type: "string",
description: "A Foo field",
},
},
};
const { node } = createFormComponent({
schema: schemaWithReference,
});
const matches = node.querySelectorAll("#root_foo__description");
expect(matches).to.have.length.of(1);
expect(matches[0].textContent).to.equal("A Foo field");
});
it("should not render description if not available from schema", () => {
const { node } = createFormComponent({ schema });
expect(node.querySelectorAll("#root_bar__description")).to.have.length.of(
0
);
});
it("should render a customized description field", () => {
const CustomDescriptionField = ({ description }) => (
<div id="custom">{description}</div>
);
const { node } = createFormComponent({
schema,
fields: {
DescriptionField: CustomDescriptionField,
},
});
expect(node.querySelector("#custom").textContent).to.eql("A Foo field");
});
});
describe("errors", () => {
const schema = {
type: "object",
properties: {
foo: { type: "string" },
},
};
const uiSchema = {
"ui:field": props => {
const { uiSchema, ...fieldProps } = props; //eslint-disable-line
return <SchemaField {...fieldProps} />;
},
};
function validate(formData, errors) {
errors.addError("container");
errors.foo.addError("test");
return errors;
}
function submit(node) {
try {
Simulate.submit(node);
} catch (e) {
// Silencing error thrown as failure is expected here
}
}
it("should render it's own errors", () => {
const { node } = createFormComponent({
schema,
uiSchema,
validate,
});
submit(node);
const matches = node.querySelectorAll(
"form > .form-group > div > .error-detail .text-danger"
);
expect(matches).to.have.length.of(1);
expect(matches[0].textContent).to.eql("container");
});
it("should pass errors to child component", () => {
const { node } = createFormComponent({
schema,
uiSchema,
validate,
});
submit(node);
const matches = node.querySelectorAll(
"form .form-group .form-group .text-danger"
);
expect(matches).to.have.length.of(1);
expect(matches[0].textContent).to.contain("test");
});
describe("Custom error rendering", () => {
const customStringWidget = props => {
return <div className="custom-text-widget">{props.rawErrors}</div>;
};
it("should pass rawErrors down to custom widgets", () => {
const { node } = createFormComponent({
schema,
uiSchema,
validate,
widgets: { BaseInput: customStringWidget },
});
submit(node);
const matches = node.querySelectorAll(".custom-text-widget");
expect(matches).to.have.length.of(1);
expect(matches[0].textContent).to.eql("test");
});
});
});
});