-
Notifications
You must be signed in to change notification settings - Fork 531
/
fieldSelect.spec.js
316 lines (262 loc) · 7.88 KB
/
fieldSelect.spec.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
import { mount, createLocalVue } from "@vue/test-utils";
import FieldSelect from "src/fields/core/fieldSelect.vue";
const localVue = createLocalVue();
let wrapper;
function createField2(data, methods) {
const _wrapper = mount(FieldSelect, {
localVue,
propsData: data,
methods: methods
});
wrapper = _wrapper;
return _wrapper;
}
describe("fieldSelect.vue", () => {
describe("check template", () => {
let schema = {
type: "select",
label: "Cities",
model: "city",
required: false,
disabled: false,
inputName: "",
values: ["London", "Paris", "Rome", "Berlin"],
fieldClasses: ["applied-class", "another-class"]
};
let model = { city: "Paris" };
let input;
before(() => {
createField2({ schema, model, disabled: false });
input = wrapper.find("select");
});
it("should contain a select element", () => {
expect(wrapper.exists()).to.be.true;
expect(input.is("select")).to.be.true;
expect(input.classes()).to.include("form-control");
});
it("should contain option elements", () => {
let options = input.findAll("option");
expect(options.length).to.be.equal(4 + 1); // +1 for <non selected>
expect(options.at(2).element.value).to.be.equal("Paris");
expect(options.at(2).text()).to.be.equal("Paris");
expect(options.at(2).element.selected).to.be.true;
});
it("should contain a <non selected> element", () => {
let options = input.findAll("option");
expect(options.at(0).attributes().disabled).to.be.undefined;
expect(options.at(0).text()).to.be.equal("<Nothing selected>");
});
it("should contain the value", () => {
expect(input.element.value).to.be.equal("Paris");
});
describe("check optional attribute", () => {
let attributes = ["disabled", "inputName"];
attributes.forEach(name => {
it("should set " + name, () => {
checkAttribute(name, wrapper, schema, "select");
});
});
});
it("input value should be the model value after changed", () => {
model.city = "Rome";
wrapper.update();
expect(input.element.value).to.be.equal("Rome");
});
it("model value should be the input value if changed", () => {
input.element.value = "London";
input.trigger("change");
expect(model.city).to.be.equal("London");
});
it("should contain a disabled <non selected> element if required", () => {
schema.required = true;
wrapper.update();
let options = input.findAll("option");
expect(options.at(0).attributes().disabled).to.be.equal("disabled");
expect(options.at(0).text()).to.be.equal("<Nothing selected>");
});
it("should show the customized <non selected> text", () => {
schema.selectOptions = {
noneSelectedText: "Empty list"
};
wrapper.update();
let options = input.findAll("option");
expect(options.at(0).attributes().disabled).to.be.equal("disabled");
expect(options.at(0).text()).to.be.equal("Empty list");
schema.selectOptions = null;
wrapper.update();
});
it("should hide the customized <non selected> text", () => {
schema.selectOptions = {
noneSelectedText: "Empty list",
hideNoneSelectedText: true
};
wrapper.update();
let options = input.findAll("option");
expect(options.at(0).attributes().disabled).to.be.equal("disabled");
expect(options.at(0).text()).to.not.be.equal("Empty list");
schema.selectOptions = null;
wrapper.update();
});
});
describe("check static values with { id, name } objects", () => {
let schema = {
type: "select",
label: "Cities",
model: "city",
values: [
{ id: 1, name: "London" },
{ id: 2, name: "Paris" },
{ id: 3, name: "Rome" },
{ id: 4, name: "Berlin" },
{ id: 5, name: "Budapest", group: "HUN" },
{ id: 6, name: "Paks", group: "HUN" }
]
};
let model = { city: 2 };
let input;
before(() => {
createField2({ schema, model, disabled: false });
input = wrapper.find("select");
wrapper.update();
});
it("should contain option elements", () => {
let options = input.findAll("option");
expect(options.length).to.be.equal(6 + 1); // +1 for <non selected>
expect(options.at(2).element.value).to.be.equal("2");
expect(options.at(2).text()).to.be.equal("Paris");
expect(options.at(2).element.selected).to.be.true;
expect(options.at(1).element.selected).to.be.false;
});
it("should contain optgroup elements", () => {
let optgroups = input.findAll("optgroup");
expect(optgroups.length).to.be.equal(1);
expect(optgroups.at(0).element.label).to.be.equal("HUN");
});
it("should contain option elements in optgroup", () => {
let og = input.find("optgroup");
let options = og.findAll("option");
expect(options.length).to.be.equal(2);
expect(options.at(0).element.selected).to.be.false;
expect(options.at(1).element.selected).to.be.false;
expect(options.at(1).text()).to.be.equal("Paks");
expect(options.at(1).element.value).to.be.equal("6");
});
it("should contain the value", () => {
expect(input.element.value).to.be.equal("2");
});
it("input value should be the model value after changed", () => {
model.city = 3;
wrapper.update();
expect(input.element.value).to.be.equal("3");
});
it("model value should be the input value if changed", () => {
input.element.value = "4";
input.trigger("change");
expect(model.city).to.be.equal(4);
});
});
describe("check function values", () => {
let schema = {
type: "select",
label: "Cities",
model: "city",
values() {
return [
{ id: 1, name: "London" },
{ id: 2, name: "Paris" },
{ id: 3, name: "Rome" },
{ id: 4, name: "Berlin" }
];
},
fieldClasses: ["applied-class", "another-class"]
};
let model = { city: 2 };
let input;
before(() => {
createField2({ schema, model, disabled: false });
input = wrapper.find("select");
});
it("should contain the value", () => {
expect(input.element.value).to.be.equal("2");
});
it("input value should be the model value after changed", () => {
model.city = 3;
wrapper.update();
expect(input.element.value).to.be.equal("3");
});
it("model value should be the input value if changed", () => {
input.element.value = "4";
input.trigger("change");
expect(model.city).to.be.equal(4);
});
it("should have 2 classes", () => {
expect(input.classes()).to.include("applied-class");
expect(input.classes()).to.include("another-class");
});
});
describe("check dynamic html attributes", () => {
describe("check input/wrapper attributes", () => {
let schema = {
type: "select",
label: "First Name",
model: "user__model",
inputName: "input_name",
fieldClasses: ["applied-class", "another-class"],
values() {
return [
{ id: 1, name: "London" },
{ id: 2, name: "Paris" },
{ id: 3, name: "Rome" },
{ id: 4, name: "Berlin" }
];
},
attributes: {
wrapper: {
"data-toggle": "collapse"
},
input: {
"data-toggle": "tooltip"
}
}
};
let model = {};
let input;
before(() => {
createField2({ schema, model });
input = wrapper.find("select");
});
it("input should have data-toggle attribute", () => {
expect(input.attributes()["data-toggle"]).to.be.equal("tooltip");
});
});
describe("check non-specific attributes", () => {
let schema = {
type: "select",
label: "First Name",
model: "user__model",
inputName: "input_name",
fieldClasses: ["applied-class", "another-class"],
values() {
return [
{ id: 1, name: "London" },
{ id: 2, name: "Paris" },
{ id: 3, name: "Rome" },
{ id: 4, name: "Berlin" }
];
},
attributes: {
"data-toggle": "tooltip"
}
};
let model = {};
let input;
before(() => {
createField2({ schema, model });
input = wrapper.find("select");
});
it("input should have data-toggle attribute", () => {
expect(input.attributes()["data-toggle"]).to.be.equal("tooltip");
});
});
});
});