forked from rjsf-team/react-jsonschema-form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBooleanField_test.js
230 lines (191 loc) · 5.37 KB
/
BooleanField_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
import React from "react";
import { expect } from "chai";
import { Simulate } from "react-addons-test-utils";
import { createFormComponent, createSandbox } from "./test_utils";
describe("BooleanField", () => {
let sandbox;
const CustomWidget = () => <div id="custom" />;
beforeEach(() => {
sandbox = createSandbox();
});
afterEach(() => {
sandbox.restore();
});
it("should render a boolean field", () => {
const { node } = createFormComponent({
schema: {
type: "boolean",
},
});
expect(
node.querySelectorAll(".field input[type=checkbox]")
).to.have.length.of(1);
});
it("should render a boolean field with a label", () => {
const { node } = createFormComponent({
schema: {
type: "boolean",
title: "foo",
},
});
expect(node.querySelector(".field label span").textContent).eql("foo");
});
it("should render a single label", () => {
const { node } = createFormComponent({
schema: {
type: "boolean",
title: "foo",
},
});
expect(node.querySelectorAll(".field label")).to.have.length.of(1);
});
it("should render a description", () => {
const { node } = createFormComponent({
schema: {
type: "boolean",
description: "my description",
},
});
const description = node.querySelector(".field-description");
expect(description.textContent).eql("my description");
});
it("should assign a default value", () => {
const { node } = createFormComponent({
schema: {
type: "boolean",
default: true,
},
});
expect(node.querySelector(".field input").checked).eql(true);
});
it("should default state value to undefined", () => {
const { comp } = createFormComponent({ schema: { type: "boolean" } });
expect(comp.state.formData).eql(undefined);
});
it("should handle a change event", () => {
const { comp, node } = createFormComponent({
schema: {
type: "boolean",
default: false,
},
});
Simulate.change(node.querySelector("input"), {
target: { checked: true },
});
expect(comp.state.formData).eql(true);
});
it("should fill field with data", () => {
const { node } = createFormComponent({
schema: {
type: "boolean",
},
formData: true,
});
expect(node.querySelector(".field input").checked).eql(true);
});
it("should support enumNames for radio widgets", () => {
const { node } = createFormComponent({
schema: {
type: "boolean",
enumNames: ["Yes", "No"],
},
formData: true,
uiSchema: { "ui:widget": "radio" },
});
const labels = [].map.call(
node.querySelectorAll(".field-radio-group label"),
label => label.textContent
);
expect(labels).eql(["Yes", "No"]);
});
it("should support inline radio widgets", () => {
const { node } = createFormComponent({
schema: { type: "boolean" },
formData: true,
uiSchema: {
"ui:widget": "radio",
"ui:options": {
inline: true,
},
},
});
expect(node.querySelectorAll(".radio-inline")).to.have.length.of(2);
});
it("should support enumNames for select", () => {
const { node } = createFormComponent({
schema: {
type: "boolean",
enumNames: ["Yes", "No"],
},
formData: true,
uiSchema: { "ui:widget": "select" },
});
const labels = [].map.call(
node.querySelectorAll(".field option"),
label => label.textContent
);
expect(labels).eql(["", "Yes", "No"]);
});
it("should render the widget with the expected id", () => {
const { node } = createFormComponent({
schema: {
type: "boolean",
},
});
expect(node.querySelector("input[type=checkbox]").id).eql("root");
});
it("should render customized checkbox", () => {
const { node } = createFormComponent({
schema: {
type: "boolean",
},
widgets: {
CheckboxWidget: CustomWidget,
},
});
expect(node.querySelector("#custom")).to.exist;
});
describe("Label", () => {
const Widget = props => <div id={`label-${props.label}`} />;
const widgets = { Widget };
it("should pass field name to widget if there is no title", () => {
const schema = {
type: "object",
properties: {
boolean: {
type: "boolean",
},
},
};
const uiSchema = {
boolean: {
"ui:widget": "Widget",
},
};
const { node } = createFormComponent({ schema, widgets, uiSchema });
expect(node.querySelector("#label-boolean")).to.not.be.null;
});
it("should pass schema title to widget", () => {
const schema = {
type: "boolean",
title: "test",
};
const uiSchema = {
"ui:widget": "Widget",
};
const { node } = createFormComponent({ schema, widgets, uiSchema });
expect(node.querySelector("#label-test")).to.not.be.null;
});
it("should pass empty schema title to widget", () => {
const schema = {
type: "boolean",
title: "",
};
const uiSchema = {
"ui:widget": "Widget",
};
const { node } = createFormComponent({ schema, widgets, uiSchema });
expect(node.querySelector("#label-")).to.not.be.null;
});
});
});