forked from bahmutov/test-todomvc-using-app-actions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspec.js
530 lines (459 loc) · 14.3 KB
/
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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
// type definitions for Cypress object "cy"
/// <reference types="cypress" />
// type definition for out TodoModel
/// <reference path='./model.d.ts' />
// @ts-check
import {
addDefaultTodos,
addTodos,
allItems,
TODO_ITEM_ONE,
TODO_ITEM_THREE,
TODO_ITEM_TWO,
toggle
} from './utils'
describe('TodoMVC', function () {
beforeEach(function () {
cy.visit('/')
})
context('When page is initially opened', function () {
it('should focus on the todo input field', function () {
// get the currently focused element and assert
// that it has class='new-todo'
//
// http://on.cypress.io/focused
cy.focused().should('have.class', 'new-todo')
})
})
context('No Todos', function () {
it('should hide #main and #footer', function () {
// Unlike the TodoMVC tests, we don't need to create
// a gazillion helper functions which are difficult to
// parse through. Instead we'll opt to use real selectors
// so as to make our testing intentions as clear as possible.
//
// http://on.cypress.io/get
allItems().should('not.exist')
cy.get('.main').should('not.exist')
cy.get('.footer').should('not.exist')
})
})
context('New Todo', function () {
// These tests confirm that add new Todo items works.
// All tests go through the DOM and events just like a real user would.
// Input element selector for typing new todo title
const NEW_TODO = '.new-todo'
it('should allow me to add todo items', function () {
cy.get(NEW_TODO)
.type(TODO_ITEM_ONE)
.type('{enter}')
allItems()
.eq(0)
.find('label')
.should('contain', TODO_ITEM_ONE)
cy.get(NEW_TODO)
.type(TODO_ITEM_TWO)
.type('{enter}')
allItems()
.eq(1)
.find('label')
.should('contain', TODO_ITEM_TWO)
})
it('adds items', function () {
// create several todos then check the number of items in the list
cy.get(NEW_TODO)
.type('todo A{enter}')
.type('todo B{enter}') // we can continue working with same element
.type('todo C{enter}') // and keep adding new items
.type('todo D{enter}')
allItems().should('have.length', 4)
})
it('should clear text input field when an item is added', function () {
cy.get(NEW_TODO)
.type(TODO_ITEM_ONE)
.type('{enter}')
cy.get(NEW_TODO).should('have.text', '')
})
it('should append new items to the bottom of the list', function () {
// this is an example of a custom command
// which is stored in tests/_support/spec_helper.js
// you should open up the spec_helper and look at
// the comments!
addDefaultTodos()
// even though the text content is split across
// multiple <span> and <strong> elements
// `cy.contains` can verify this correctly
cy.get('.todo-count').contains('3 items left')
allItems()
.eq(0)
.find('label')
.should('contain', TODO_ITEM_ONE)
allItems()
.eq(1)
.find('label')
.should('contain', TODO_ITEM_TWO)
allItems()
.eq(2)
.find('label')
.should('contain', TODO_ITEM_THREE)
})
it('should trim text input', function () {
// this is an example of another custom command
// since we repeat the todo creation over and over
// again. It's up to you to decide when to abstract
// repetitive behavior and roll that up into a custom
// command vs explicitly writing the code.
cy.get(NEW_TODO).type(` ${TODO_ITEM_ONE} {enter}`)
// we use as explicit assertion here about the text instead of
// using 'contain' so we can specify the exact text of the element
// does not have any whitespace around it
allItems()
.eq(0)
.should('have.text', TODO_ITEM_ONE)
})
it('should show #main and #footer when items added', function () {
addTodos(TODO_ITEM_ONE)
cy.get('.main').should('be.visible')
cy.get('.footer').should('be.visible')
})
})
context('Adds items (spy example)', () => {
it('calls inform', () => {
cy.window()
.its('model')
.should('be.an', 'object')
.then(model => {
cy.spy(model, 'inform').as('inform')
})
addDefaultTodos()
cy.get('@inform').should('have.been.calledOnce')
})
})
context('Mark all as completed', function () {
// These tests confirm that we can click one toggle button, and the app
// marks all items as completed or incomplete again.
// Selector for the toggle button we are going to use
const TOGGLE_ALL = '.toggle-all'
// Note that these tests do NOT create items through the DOM.
// Instead they use app action "addDefaultTodos" before each test
beforeEach(addDefaultTodos)
it('should allow me to mark all items as completed', function () {
// complete all todos
// we use 'check' instead of 'click'
// because that indicates our intention much clearer
cy.get(TOGGLE_ALL).check()
// get each todo li and ensure its class is 'completed'
allItems()
.eq(0)
.should('have.class', 'completed')
allItems()
.eq(1)
.should('have.class', 'completed')
allItems()
.eq(2)
.should('have.class', 'completed')
})
it('should allow me to clear the complete state of all items', function () {
// check and then immediately uncheck
cy.get(TOGGLE_ALL)
.check()
.uncheck()
allItems()
.eq(0)
.should('not.have.class', 'completed')
allItems()
.eq(1)
.should('not.have.class', 'completed')
allItems()
.eq(2)
.should('not.have.class', 'completed')
})
it('complete all checkbox should update state when items are completed / cleared', function () {
// alias the .toggle-all for reuse later
cy.get(TOGGLE_ALL)
.as('toggleAll')
.check()
// this assertion is silly here IMO but
// it is what TodoMVC does
.should('be.checked')
toggle(0)
// reference the .toggle-all element again
// and make sure its not checked
cy.get('@toggleAll').should('not.be.checked')
toggle(0)
// assert the toggle all is checked again
cy.get('@toggleAll').should('be.checked')
})
})
context('Item', function () {
it('should allow me to mark items as complete', function () {
addTodos(TODO_ITEM_ONE, TODO_ITEM_TWO)
allItems()
.eq(0)
.as('firstTodo')
allItems()
.eq(1)
.as('secondTodo')
cy.get('@firstTodo')
.find('.toggle')
.check()
cy.get('@firstTodo').should('have.class', 'completed')
cy.get('@secondTodo').should('not.have.class', 'completed')
cy.get('@secondTodo')
.find('.toggle')
.check()
cy.get('@firstTodo').should('have.class', 'completed')
cy.get('@secondTodo').should('have.class', 'completed')
})
it('should allow me to un-mark items as complete', function () {
addTodos(TODO_ITEM_ONE, TODO_ITEM_TWO)
allItems()
.eq(0)
.as('firstTodo')
allItems()
.eq(1)
.as('secondTodo')
cy.get('@firstTodo')
.find('.toggle')
.check()
cy.get('@firstTodo').should('have.class', 'completed')
cy.get('@secondTodo').should('not.have.class', 'completed')
cy.get('@firstTodo')
.find('.toggle')
.uncheck()
cy.get('@firstTodo').should('not.have.class', 'completed')
cy.get('@secondTodo').should('not.have.class', 'completed')
})
it('should allow me to edit an item', function () {
addDefaultTodos()
allItems()
.eq(1)
.as('secondTodo')
// TODO: fix this, dblclick should
// have been issued to label
.find('label')
.dblclick()
// clear out the inputs current value
// and type a new value
cy.get('@secondTodo')
.find('.edit')
.clear()
.type('buy some sausages')
.type('{enter}')
// explicitly assert about the text value
allItems()
.eq(0)
.should('contain', TODO_ITEM_ONE)
cy.get('@secondTodo').should('contain', 'buy some sausages')
allItems()
.eq(2)
.should('contain', TODO_ITEM_THREE)
})
})
context('Editing', function () {
beforeEach(addDefaultTodos)
it('should hide other controls when editing', function () {
allItems()
.eq(1)
.as('secondTodo')
.find('label')
.dblclick()
cy.get('@secondTodo')
.find('.toggle')
.should('not.be.visible')
cy.get('@secondTodo')
.find('label')
.should('not.be.visible')
})
it('should save edits on blur', function () {
allItems()
.eq(1)
.as('secondTodo')
.find('label')
.dblclick()
cy.get('@secondTodo')
.find('.edit')
.clear()
.type('buy some sausages')
// we can just send the blur event directly
// to the input instead of having to click
// on another button on the page. though you
// could do that its just more mental work
.blur()
allItems()
.eq(0)
.should('contain', TODO_ITEM_ONE)
cy.get('@secondTodo').should('contain', 'buy some sausages')
allItems()
.eq(2)
.should('contain', TODO_ITEM_THREE)
})
it('should trim entered text', function () {
allItems()
.eq(1)
.as('secondTodo')
.find('label')
.dblclick()
cy.get('@secondTodo')
.find('.edit')
.clear()
.type(' buy some sausages ')
.type('{enter}')
allItems()
.eq(0)
.should('contain', TODO_ITEM_ONE)
cy.get('@secondTodo').should('contain', 'buy some sausages')
allItems()
.eq(2)
.should('contain', TODO_ITEM_THREE)
})
it('should remove the item if an empty text string was entered', function () {
allItems()
.eq(1)
.as('secondTodo')
.find('label')
.dblclick()
cy.get('@secondTodo')
.find('.edit')
.clear()
.type('{enter}')
allItems().should('have.length', 2)
})
it('should cancel edits on escape', function () {
allItems()
.eq(1)
.as('secondTodo')
.find('label')
.dblclick()
cy.get('@secondTodo')
.find('.edit')
.clear()
.type('foo{esc}')
allItems()
.eq(0)
.should('contain', TODO_ITEM_ONE)
allItems()
.eq(1)
.should('contain', TODO_ITEM_TWO)
allItems()
.eq(2)
.should('contain', TODO_ITEM_THREE)
})
})
context('Counter', function () {
const COUNTER = '.todo-count'
it('should display the current number of todo items', function () {
addTodos(TODO_ITEM_ONE)
cy.get(COUNTER).contains('1 item left')
addTodos(TODO_ITEM_TWO)
cy.get(COUNTER).contains('2 items left')
})
})
context('Clear completed button', function () {
const CLEAR_COMPLETED = '.clear-completed'
beforeEach(addDefaultTodos)
it('should display the correct text', function () {
toggle(0)
cy.get(CLEAR_COMPLETED).contains('Clear completed')
})
it('should remove completed items when clicked', function () {
toggle(1)
cy.get(CLEAR_COMPLETED).click()
allItems().should('have.length', 2)
allItems()
.eq(0)
.should('contain', TODO_ITEM_ONE)
allItems()
.eq(1)
.should('contain', TODO_ITEM_THREE)
})
it('should be hidden when there are no items that are completed', function () {
toggle(1)
cy.get(CLEAR_COMPLETED)
.should('be.visible')
.click()
cy.get(CLEAR_COMPLETED).should('not.exist')
})
})
context('Persistence', function () {
// mimicking TodoMVC tests
// by writing out this function
function testState () {
allItems()
.eq(0)
.should('contain', TODO_ITEM_ONE)
.and('have.class', 'completed')
allItems()
.eq(1)
.should('contain', TODO_ITEM_TWO)
.and('not.have.class', 'completed')
}
it('should persist its data', function () {
addTodos(TODO_ITEM_ONE, TODO_ITEM_TWO)
toggle(0)
.then(testState)
.reload()
.then(testState)
})
})
context('Routing', function () {
/**
* Little utility function to click on a given filter on the page.
* We are testing routing links, so these tests go through the DOM.
*/
const clickFilter = name =>
cy
.get('.filters')
.contains(name)
.click()
// but for everything else, like created todos and toggling, these tests
// use app actions.
beforeEach(addDefaultTodos)
it('should allow me to display active items', function () {
toggle(1)
// the UI feature we are actually testing - the "Active" link
clickFilter('Active')
allItems()
.eq(0)
.should('contain', TODO_ITEM_ONE)
allItems()
.eq(1)
.should('contain', TODO_ITEM_THREE)
})
it('should respect the back button', function () {
toggle(1)
clickFilter('Active')
clickFilter('Completed')
allItems().should('have.length', 1)
cy.go('back')
allItems().should('have.length', 2)
cy.go('back')
allItems().should('have.length', 3)
})
it('should allow me to display completed items', function () {
toggle(1)
clickFilter('Completed')
allItems().should('have.length', 1)
})
it('should allow me to display all items', function () {
toggle(1)
clickFilter('Active')
clickFilter('Completed')
clickFilter('All')
allItems().should('have.length', 3)
})
it('should highlight the currently applied filter', function () {
// using a within here which will automatically scope
// nested 'cy' queries to our parent element <ul.filters>
cy.get('.filters').within(function () {
cy.contains('All').should('have.class', 'selected')
cy.contains('Active')
.click()
.should('have.class', 'selected')
cy.contains('Completed')
.click()
.should('have.class', 'selected')
})
})
})
})