-
Notifications
You must be signed in to change notification settings - Fork 776
/
ranking.js
336 lines (298 loc) · 8.89 KB
/
ranking.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
import { frameworks, url, initSurvey, getData, setData } from "../helper";
import { Selector, fixture, test, ClientFunction } from "testcafe";
const title = "ranking";
const json = {
pages: [
{
name: "page1",
elements: [
{
type: "ranking",
name: "smartphone-features",
title:
"Please rank the following smartphone features in order of importance:",
isRequired: true,
choices: [
"Battery life",
"Screen size",
"Storage space",
"Camera quality",
"Durability",
"Processor power",
"Price",
],
},
],
},
{
name: "page2",
elements: [
{
type: "checkbox",
name: "car",
isRequired: true,
title: "What cars have you being drived?",
colCount: 4,
choicesOrder: "asc",
choices: [
"Ford",
"Vauxhall",
"Volkswagen",
"Nissan",
"Audi",
"Mercedes-Benz",
"BMW",
"Peugeot",
"Toyota",
"Citroen",
"Tesla",
],
},
{
type: "ranking",
name: "bestcar",
isRequired: true,
visibleIf: "{car.length} > 1",
title: "What car did you enjoy the most?",
choicesFromQuestion: "car",
choicesFromQuestionMode: "selected",
},
],
},
],
};
// for (let index = 0; index < 100; index++) {
// frameworks.push("knockout");
// }
const setSjsFramework = ClientFunction((framework) => {
window["sjsFramework"] = framework;
});
frameworks.forEach((framework) => {
fixture`${framework} ${title}`.page`${url}${framework}`.beforeEach(
async (t) => {
await t.resizeWindow(1920, 1080);
await setSjsFramework(framework);
await initSurvey(framework, json);
}
);
const PriceItem = Selector("span")
.withText("Please rank the following smartphone features in order of importance:")
.parent("[data-name]")
.find("span")
.withText("Price");
const BatteryItem = Selector("span")
.withText("Please rank the following smartphone features in order of importance:")
.parent("[data-name]")
.find("span")
.withText("Battery life");
test("ranking: simple using", async (t) => {
await t.dragToElement(PriceItem, BatteryItem);
await t.wait(300);
let data = await getData();
await t.expect(data["smartphone-features"]).eql([
"Price",
"Battery life",
"Screen size",
"Storage space",
"Camera quality",
"Durability",
"Processor power",
]);
});
test("ranking: predeficed data", async (t) => {
await setData({
"smartphone-features": [
"Price",
"Battery life",
"Screen size",
"Storage space",
"Camera quality",
"Durability",
"Processor power",
],
});
await t.dragToElement(PriceItem, BatteryItem, {
destinationOffsetY: 0,
offsetY: 0,
speed: 0.1
});
await t.wait(300);
let data = await getData();
await t.expect(data["smartphone-features"]).eql([
"Battery life",
"Price",
"Screen size",
"Storage space",
"Camera quality",
"Durability",
"Processor power",
]);
await setData(null);
await t.dragToElement(PriceItem, BatteryItem);
await t.wait(300);
data = await getData();
await t.expect(data["smartphone-features"]).eql([
"Price",
"Battery life",
"Screen size",
"Storage space",
"Camera quality",
"Durability",
"Processor power",
]);
});
test("ranking: carry forward", async (t) => {
await t.hover(PriceItem);
await t.drag(PriceItem, 0, -300, {
offsetX: 7,
offsetY: 8
});
const rankAudiItem = Selector("span")
.withText("What car did you enjoy the most?")
.parent("[data-name]")
.find("span")
.withText("Audi");
const rankMercedesBenzItem = Selector("span")
.withText("What car did you enjoy the most?")
.parent("[data-name]")
.find("span")
.withText("Mercedes-Benz");
const checkboxAudiItem = Selector("span")
.withText("What cars have you being drived?")
.parent("[data-name]")
.find("span")
.withText("Audi");
const checkboxMerscedesItem = Selector("span")
.withText("What cars have you being drived?")
.parent("[data-name]")
.find("span")
.withText("Mercedes-Benz");
const checkboxToyotaItem = Selector("span")
.withText("What cars have you being drived?")
.parent("[data-name]")
.find("span")
.withText("Toyota");
await t.click(Selector("input[value='Next']"));
await t
.click(checkboxAudiItem)
.click(checkboxMerscedesItem)
.click(checkboxToyotaItem);
let data = await getData();
await t.expect(typeof data.bestcar).ok();
await t.hover(rankAudiItem);
await t.dragToElement(rankAudiItem, rankMercedesBenzItem, { offsetY: 0, destinationOffsetY: 0, speed: 0.1 });
data = await getData();
await t.expect(data.bestcar).eql(["Mercedes-Benz", "Audi", "Toyota"]);
//TODO click doesn't work after the d&d above without the "click('body')" hack
await t.click("body");
await t.click(checkboxMerscedesItem);
data = await getData();
await t.expect(data.bestcar).eql(["Audi", "Toyota"]);
await t.click(checkboxMerscedesItem);
data = await getData();
await t.expect(data.bestcar).eql(["Audi", "Toyota", "Mercedes-Benz"]);
await t
.click(checkboxAudiItem)
.click(checkboxMerscedesItem)
.click(checkboxToyotaItem);
data = await getData();
await t.expect(typeof data.bestcar).eql("undefined");
});
test("ranking: keyborad", async (t) => {
await t.pressKey("tab").pressKey("tab").pressKey("up");
let data = await getData();
await t.expect(data["smartphone-features"]).eql([
"Screen size",
"Battery life",
"Storage space",
"Camera quality",
"Durability",
"Processor power",
"Price",
]);
await t.pressKey("down");
data = await getData();
await t.expect(data["smartphone-features"]).eql([
"Battery life",
"Screen size",
"Storage space",
"Camera quality",
"Durability",
"Processor power",
"Price",
]);
});
test("ranking: run-time creation ", async (t) => {
const newName = "ranking-new";
const addNewRankingQuestion = ClientFunction((newName) => {
let qr;
if (window["sjsFramework"] === "knockout") { //see https://github.com/surveyjs/survey-library/issues/6396
qr = new window["Survey"].QuestionRanking(newName);
} else {
qr = new window["Survey"].QuestionRankingModel(newName);
}
qr.choices = ["one", "two"];
window["survey"].currentPage.addQuestion(qr);
});
await addNewRankingQuestion(newName);
const FirstItem = Selector("span")
.withText(newName)
.parent("[data-name]")
.find("span")
.withText("one");
const SecondItem = Selector("span")
.withText(newName)
.parent("[data-name]")
.find("span")
.withText("two");
await t.dragToElement(FirstItem, SecondItem, { speed: 0.1 });
let data = await getData();
await t.expect(data[newName]).eql([
"two",
"one"
]);
});
test("ranking: work with flexbox layout", async (t) => {
const addFlexboxLayout = ClientFunction(() => {
const stylesheet = document.styleSheets[0];
stylesheet.addRule(".sv-ranking.sv-ranking.sv-ranking.sv-ranking.sv-ranking", "display:flex;flex-direction: column", 0);
});
const removeFlexboxLayout = ClientFunction(() => {
const stylesheet = document.styleSheets[0];
stylesheet.removeRule(0);
});
await addFlexboxLayout();
await t.dragToElement(PriceItem, BatteryItem, { destinationOffsetY: 0 });
await t.wait(300);
let data = await getData();
await t.expect(data["smartphone-features"]).eql([
"Price",
"Battery life",
"Screen size",
"Storage space",
"Camera quality",
"Durability",
"Processor power",
]);
await removeFlexboxLayout();
});
test("ranking: selectToRank: click to add", async (t) => {
const setSelectToRankEnabled = ClientFunction(() => {
const rankingQ = window["survey"].getAllQuestions()[0];
rankingQ.selectToRankEnabled = true;
});
await setSelectToRankEnabled();
await t.click(PriceItem);
await t.click(BatteryItem);
let data = await getData();
await t.expect(data["smartphone-features"]).eql([
"Price",
"Battery life"
]);
const setSelectToRankDisabled = ClientFunction(() => {
const rankingQ = window["survey"].getAllQuestions()[0];
rankingQ.selectToRankEnabled = false;
});
await setSelectToRankDisabled();
});
});