forked from tastejs/todomvc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
382 lines (301 loc) · 10.9 KB
/
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
'use strict';
var webdriver = require('selenium-webdriver');
var chrome = require('selenium-webdriver/chrome');
var test = require('selenium-webdriver/testing');
var Page = require('./page');
var PageLaxMode = require('./pageLaxMode');
var TestOperations = require('./testOperations');
module.exports.todoMVCTest = function (frameworkName, baseUrl, speedMode, laxMode, browserName) {
test.describe('TodoMVC - ' + frameworkName, function () {
var TODO_ITEM_ONE = 'buy some cheese';
var TODO_ITEM_TWO = 'feed the cat';
var TODO_ITEM_THREE = 'book a doctors appointment';
var browser, testOps, page;
// a number of tests use this set of ToDo items.
function createStandardItems() {
page.enterItem(TODO_ITEM_ONE);
page.enterItem(TODO_ITEM_TWO);
page.enterItem(TODO_ITEM_THREE);
}
function launchBrowser() {
var chromeOptions = new chrome.Options();
chromeOptions.addArguments('no-sandbox');
if (process.env.CHROME_PATH !== undefined) {
chromeOptions.setChromeBinaryPath(process.env.CHROME_PATH);
}
browser = new webdriver.Builder()
.withCapabilities({
browserName: browserName
})
.setChromeOptions(chromeOptions)
.build();
browser.get(baseUrl);
page = laxMode ? new PageLaxMode(browser) : new Page(browser);
testOps = new TestOperations(page);
// for apps that use require, we have to wait a while for the dependencies to
// be loaded. There must be a more elegant solution than this!
browser.sleep(200);
}
function closeBrowser() {
browser.quit();
}
if (speedMode) {
test.before(function () {
launchBrowser();
});
test.after(function () {
closeBrowser();
});
test.beforeEach(function () {
page.getItemElements().then(function (items) {
if (items.length > 0) {
// find any items that are not complete
page.getNonCompletedItemElements().then(function (nonCompleteItems) {
if (nonCompleteItems.length > 0) {
page.clickMarkAllCompletedCheckBox();
}
page.clickClearCompleteButton();
});
}
});
});
} else {
test.beforeEach(function () {
launchBrowser();
page.ensureAppIsVisible();
});
test.afterEach(function () {
closeBrowser();
});
}
test.describe('When page is initially opened', function () {
test.it('should focus on the todo input field', function () {
testOps.assertFocussedElementId('new-todo');
});
});
test.describe('No Todos', function () {
test.it('should hide #main and #footer', function () {
testOps.assertItemCount(0);
testOps.assertMainSectionIsHidden();
testOps.assertFooterIsHidden();
});
});
test.describe('New Todo', function () {
test.it('should allow me to add todo items', function () {
page.enterItem(TODO_ITEM_ONE);
testOps.assertItems([TODO_ITEM_ONE]);
page.enterItem(TODO_ITEM_TWO);
testOps.assertItems([TODO_ITEM_ONE, TODO_ITEM_TWO]);
});
test.it('should clear text input field when an item is added', function () {
page.enterItem(TODO_ITEM_ONE);
testOps.assertItemInputFieldText('');
});
test.it('should append new items to the bottom of the list', function () {
createStandardItems();
testOps.assertItemCount(3);
testOps.assertItemText(0, TODO_ITEM_ONE);
testOps.assertItemText(1, TODO_ITEM_TWO);
testOps.assertItemText(2, TODO_ITEM_THREE);
});
test.it('should trim text input', function () {
page.enterItem(' ' + TODO_ITEM_ONE + ' ');
testOps.assertItemText(0, TODO_ITEM_ONE);
});
test.it('should show #main and #footer when items added', function () {
page.enterItem(TODO_ITEM_ONE);
testOps.assertMainSectionIsVisible();
testOps.assertFooterIsVisible();
});
});
test.describe('Mark all as completed', function () {
test.beforeEach(function () {
createStandardItems();
});
test.it('should allow me to mark all items as completed', function () {
page.clickMarkAllCompletedCheckBox();
testOps.assertItemAtIndexIsCompleted(0);
testOps.assertItemAtIndexIsCompleted(1);
testOps.assertItemAtIndexIsCompleted(2);
});
test.it('should allow me to clear the completion state of all items', function () {
page.clickMarkAllCompletedCheckBox();
page.clickMarkAllCompletedCheckBox();
testOps.assertItemAtIndexIsNotCompleted(0);
testOps.assertItemAtIndexIsNotCompleted(1);
testOps.assertItemAtIndexIsNotCompleted(2);
});
test.it('complete all checkbox should update state when items are completed / cleared', function () {
page.clickMarkAllCompletedCheckBox();
testOps.assertCompleteAllIsChecked();
// all items are complete, now mark one as not-complete
page.toggleItemAtIndex(0);
testOps.assertCompleteAllIsClear();
// now mark as complete, so that once again all items are completed
page.toggleItemAtIndex(0);
testOps.assertCompleteAllIsChecked();
});
});
test.describe('Item', function () {
test.it('should allow me to mark items as complete', function () {
page.enterItem(TODO_ITEM_ONE);
page.enterItem(TODO_ITEM_TWO);
page.toggleItemAtIndex(0);
testOps.assertItemAtIndexIsCompleted(0);
testOps.assertItemAtIndexIsNotCompleted(1);
page.toggleItemAtIndex(1);
testOps.assertItemAtIndexIsCompleted(0);
testOps.assertItemAtIndexIsCompleted(1);
});
test.it('should allow me to un-mark items as complete', function () {
page.enterItem(TODO_ITEM_ONE);
page.enterItem(TODO_ITEM_TWO);
page.toggleItemAtIndex(0);
testOps.assertItemAtIndexIsCompleted(0);
testOps.assertItemAtIndexIsNotCompleted(1);
page.toggleItemAtIndex(0);
testOps.assertItemAtIndexIsNotCompleted(0);
testOps.assertItemAtIndexIsNotCompleted(1);
});
test.it('should allow me to edit an item', function () {
createStandardItems();
page.doubleClickItemAtIndex(1);
page.editItemAtIndex(1, 'buy some sausages' + webdriver.Key.ENTER);
testOps.assertItems([TODO_ITEM_ONE, 'buy some sausages', TODO_ITEM_THREE]);
});
});
test.describe('Editing', function () {
test.it('should hide other controls when editing', function () {
createStandardItems();
page.doubleClickItemAtIndex(1);
testOps.assertItemToggleIsHidden(1);
testOps.assertItemLabelIsHidden(1);
});
test.it('should save edits on enter', function () {
createStandardItems();
page.doubleClickItemAtIndex(1);
page.editItemAtIndex(1, 'buy some sausages' + webdriver.Key.ENTER);
testOps.assertItems([TODO_ITEM_ONE, 'buy some sausages', TODO_ITEM_THREE]);
});
test.it('should save edits on blur', function () {
createStandardItems();
page.doubleClickItemAtIndex(1);
page.editItemAtIndex(1, 'buy some sausages');
// click a toggle button so that the blur() event is fired
page.toggleItemAtIndex(0);
testOps.assertItems([TODO_ITEM_ONE, 'buy some sausages', TODO_ITEM_THREE]);
});
test.it('should trim entered text', function () {
createStandardItems();
page.doubleClickItemAtIndex(1);
page.editItemAtIndex(1, ' buy some sausages ' + webdriver.Key.ENTER);
testOps.assertItems([TODO_ITEM_ONE, 'buy some sausages', TODO_ITEM_THREE]);
});
test.it('should remove the item if an empty text string was entered', function () {
createStandardItems();
page.doubleClickItemAtIndex(1);
page.editItemAtIndex(1, webdriver.Key.ENTER);
testOps.assertItems([TODO_ITEM_ONE, TODO_ITEM_THREE]);
});
test.it('should cancel edits on escape', function () {
createStandardItems();
page.doubleClickItemAtIndex(1);
page.editItemAtIndex(1, 'foo' + webdriver.Key.ESCAPE);
testOps.assertItems([TODO_ITEM_ONE, TODO_ITEM_TWO, TODO_ITEM_THREE]);
});
});
test.describe('Counter', function () {
test.it('should display the current number of todo items', function () {
page.enterItem(TODO_ITEM_ONE);
testOps.assertItemCountText('1 item left');
page.enterItem(TODO_ITEM_TWO);
testOps.assertItemCountText('2 items left');
});
});
test.describe('Clear completed button', function () {
test.beforeEach(function () {
createStandardItems();
});
test.it('should display the correct text', function () {
page.toggleItemAtIndex(1);
testOps.assertClearCompleteButtonText('Clear completed');
});
test.it('should remove completed items when clicked', function () {
page.toggleItemAtIndex(1);
page.clickClearCompleteButton();
testOps.assertItemCount(2);
testOps.assertItems([TODO_ITEM_ONE, TODO_ITEM_THREE]);
});
test.it('should be hidden when there are no items that are completed', function () {
page.toggleItemAtIndex(1);
testOps.assertClearCompleteButtonIsVisible();
page.clickClearCompleteButton();
testOps.assertClearCompleteButtonIsHidden();
});
});
test.describe('Persistence', function () {
test.it('should persist its data', function () {
// set up state
page.enterItem(TODO_ITEM_ONE);
page.enterItem(TODO_ITEM_TWO);
page.toggleItemAtIndex(1);
function stateTest() {
testOps.assertItems([TODO_ITEM_ONE, TODO_ITEM_TWO]);
testOps.assertItemAtIndexIsCompleted(1);
testOps.assertItemAtIndexIsNotCompleted(0);
}
// test it
stateTest();
// navigate away and back again
browser.get('about:blank');
browser.get(baseUrl);
// repeat the state test
stateTest();
});
});
test.describe('Routing', function () {
test.beforeEach(function () {
createStandardItems();
});
test.it('should allow me to display active items', function () {
page.toggleItemAtIndex(1);
page.filterByActiveItems();
testOps.assertItems([TODO_ITEM_ONE, TODO_ITEM_THREE]);
});
test.it('should respect the back button', function () {
page.toggleItemAtIndex(1);
page.filterByActiveItems();
page.filterByCompletedItems();
// should show completed items
testOps.assertItems([TODO_ITEM_TWO]);
// then active items
page.back();
testOps.assertItems([TODO_ITEM_ONE, TODO_ITEM_THREE]);
// then all items
page.back();
testOps.assertItems([TODO_ITEM_ONE, TODO_ITEM_TWO, TODO_ITEM_THREE]);
});
test.it('should allow me to display completed items', function () {
page.toggleItemAtIndex(1);
page.filterByCompletedItems();
testOps.assertItems([TODO_ITEM_TWO]);
});
test.it('should allow me to display all items', function () {
page.toggleItemAtIndex(1);
// apply the other filters first, before returning to the 'all' state
page.filterByActiveItems();
page.filterByCompletedItems();
page.filterByAllItems();
testOps.assertItems([TODO_ITEM_ONE, TODO_ITEM_TWO, TODO_ITEM_THREE]);
});
test.it('should highlight the currently applied filter', function () {
// initially 'all' should be selected
testOps.assertFilterAtIndexIsSelected(0);
page.filterByActiveItems();
testOps.assertFilterAtIndexIsSelected(1);
page.filterByCompletedItems();
testOps.assertFilterAtIndexIsSelected(2);
});
});
});
};