/
dropdown.js
198 lines (167 loc) · 4.97 KB
/
dropdown.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
import {
frameworks,
url,
setOptions,
initSurvey,
getSurveyResult
} from "../settings";
import { Selector, ClientFunction } from "testcafe";
const assert = require("assert");
const title = `dropdown`;
const json = {
questions: [
{
type: "dropdown",
name: "car",
title: "What car are you driving?",
isRequired: true,
colCount: 0,
choices: [
"None",
"Ford",
"Vauxhall",
"Volkswagen",
"Nissan",
"Audi",
"Mercedes-Benz",
"BMW",
"Peugeot",
"Toyota",
"Citroen"
]
}
]
};
frameworks.forEach(framework => {
fixture`${framework} ${title}`.page`${url}${framework}`.beforeEach(
async t => {
await initSurvey(framework, json);
}
);
test(`choose empty`, async t => {
const getPosition = ClientFunction(() =>
document.documentElement.innerHTML.indexOf("Please answer the question")
);
let position;
let surveyResult;
await t.click(`input[value=Complete]`);
position = await getPosition();
assert.notEqual(position, -1);
surveyResult = await getSurveyResult();
assert.equal(typeof surveyResult, `undefined`);
});
test(`choose value`, async t => {
let surveyResult;
await t
.click(`select`)
.click(`option[value=Nissan]`)
.click(`input[value=Complete]`);
surveyResult = await getSurveyResult();
assert.equal(surveyResult.car, "Nissan");
});
test(`change choices order`, async t => {
const getChoicesCount = ClientFunction(
() => document.querySelectorAll(`select option`).length
);
const getFirst = Selector(`select option`, { index: 1 });
const getSecond = Selector(`select option`, { index: 2 });
let rnd_count = 0;
let first, second, first_2;
let choicesCount = await getChoicesCount();
// asc
await setOptions("car", { choicesOrder: "asc" });
first = await getFirst();
second = await getSecond();
assert.equal(first.textContent, "Audi");
assert.equal(second.textContent, "BMW");
// desc
await setOptions("car", { choicesOrder: "desc" });
first = await getFirst();
second = await getSecond();
assert.equal(first.textContent, "Volkswagen");
assert.equal(second.textContent, "Vauxhall");
// rnd
if (choicesCount === 1) {
assert(false, `need to more than one choices`);
}
for (let i = 0; i < 15; i++) {
await setOptions("car", { choicesOrder: "asc" });
await setOptions("car", { choicesOrder: "random" });
first_2 = await getFirst();
if (first.textContent !== first_2.textContent) {
rnd_count++;
}
first = first_2;
if (rnd_count >= 4) {
break;
}
}
assert(rnd_count >= 4); // beacuse of 'none', 'asc', 'desc' and if 4 it is really rnd
});
test(`check integrity`, async t => {
const choices = [
"None",
"Ford",
"Vauxhall",
"Volkswagen",
"Nissan",
"Audi",
"Mercedes-Benz",
"BMW",
"Peugeot",
"Toyota",
"Citroen"
];
let i;
const getChoicesCount = ClientFunction(
() => document.querySelectorAll("option").length
);
let choicesCount;
let checkIntegrity = async () => {
choicesCount = await getChoicesCount();
assert.equal(choicesCount, choices.length + 1); // +1 because of default "Choose..." option
for (i = 0; i < choices.length; i++) {
await t.click("select").click(`option[value=${choices[i]}]`);
}
};
await setOptions("car", { choicesOrder: "asc" });
await checkIntegrity();
await setOptions("car", { choicesOrder: "desc" });
await checkIntegrity();
await setOptions("car", { choicesOrder: "random" });
await checkIntegrity();
});
test(`show "other" choice`, async t => {
const getPosition = ClientFunction(() =>
document.documentElement.innerHTML.indexOf("Other")
);
let position;
await setOptions("car", { hasOther: true, otherText: "Other" });
position = await getPosition();
assert.notEqual(position, -1);
});
test(`check "other" choice doesn't change order`, async t => {
const getOtherChoice = Selector(
() => document.querySelectorAll(`select option`)[12]
);
let otherChoice;
await setOptions("car", { hasOther: true, otherText: "Other" });
await setOptions("car", { choicesOrder: "desc" });
otherChoice = await getOtherChoice();
assert.equal(otherChoice.textContent, "Other");
});
test(`choose other`, async t => {
const getOtherInput = Selector(
() => document.querySelectorAll("textarea")[0]
);
await setOptions("car", { hasOther: true, otherText: "Other" });
await t
.click(`select`)
.click(`option[value=other]`)
.typeText(getOtherInput, "Zaporozec")
.click(`input[value=Complete]`);
let surveyResult = await getSurveyResult();
assert.equal(surveyResult.car, "other");
assert.equal(surveyResult["car-Comment"], "Zaporozec");
});
});