forked from cypress-io/cypress-example-todomvc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_spec.js
680 lines (561 loc) · 17.6 KB
/
app_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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
// type definitions for Cypress object "cy"
/// <reference types="cypress" />
// type definitions for custom commands like "createDefaultTodos"
/// <reference types="../support" />
// check this file using TypeScript if available
// @ts-check
// ***********************************************
// All of these tests are written to implement
// the official TodoMVC tests written for Selenium.
//
// The Cypress tests cover the exact same functionality,
// and match the same test names as TodoMVC.
// Please read our getting started guide
// https://on.cypress.io/introduction-to-cypress
//
// You can find the original TodoMVC tests here:
// https://github.com/tastejs/todomvc/blob/master/tests/test.js
// ***********************************************
describe('TodoMVC - React', function () {
// setup these constants to match what TodoMVC does
let TODO_ITEM_ONE = 'buy some cheese'
let TODO_ITEM_TWO = 'feed the cat'
let TODO_ITEM_THREE = 'book a doctors appointment'
beforeEach(function () {
// By default Cypress will automatically
// clear the Local Storage prior to each
// test which ensures no todos carry over
// between tests.
//
// Go out and visit our local web server
// before each test, which serves us the
// TodoMVC App we want to test against
//
// We've set our baseUrl to be http://localhost:8888
// which is automatically prepended to cy.visit
//
// https://on.cypress.io/api/visit
cy.visit('/')
})
afterEach(() => {
// In firefox, blur handlers will fire upon navigation if there is an activeElement.
// Since todos are updated on blur after editing,
// this is needed to blur activeElement after each test to prevent state leakage between tests.
cy.window().then((win) => {
// @ts-ignore
win.document.activeElement.blur()
})
})
// a very simple example helpful during presentations
it('adds 2 todos', function () {
cy.get('.new-todo')
.type('learn testing{enter}')
.type('be cool{enter}')
cy.get('.todo-list li').should('have.length', 2)
})
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
cy.get('.todo-list li').should('not.exist')
cy.get('.main').should('not.exist')
cy.get('.footer').should('not.exist')
})
})
context('New Todo', function () {
// New commands used here:
// https://on.cypress.io/type
// https://on.cypress.io/eq
// https://on.cypress.io/find
// https://on.cypress.io/contains
// https://on.cypress.io/should
// https://on.cypress.io/as
it('should allow me to add todo items', function () {
// create 1st todo
cy.get('.new-todo')
.type(TODO_ITEM_ONE)
.type('{enter}')
// make sure the 1st label contains the 1st todo text
cy.get('.todo-list li')
.eq(0)
.find('label')
.should('contain', TODO_ITEM_ONE)
// create 2nd todo
cy.get('.new-todo')
.type(TODO_ITEM_TWO)
.type('{enter}')
// make sure the 2nd label contains the 2nd todo text
cy.get('.todo-list li')
.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}')
cy.get('.todo-list li').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
// defined in cypress/support/commands.js
cy.createDefaultTodos().as('todos')
// 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')
cy.get('@todos')
.eq(0)
.find('label')
.should('contain', TODO_ITEM_ONE)
cy.get('@todos')
.eq(1)
.find('label')
.should('contain', TODO_ITEM_TWO)
cy.get('@todos')
.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.createTodo(` ${TODO_ITEM_ONE} `)
// 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
cy.get('.todo-list li')
.eq(0)
.should('have.text', TODO_ITEM_ONE)
})
it('should show #main and #footer when items added', function () {
cy.createTodo(TODO_ITEM_ONE)
cy.get('.main').should('be.visible')
cy.get('.footer').should('be.visible')
})
})
context('Mark all as completed', function () {
// New commands used here:
// - cy.check https://on.cypress.io/api/check
// - cy.uncheck https://on.cypress.io/api/uncheck
beforeEach(function () {
// This is an example of aliasing
// within a hook (beforeEach).
// Aliases will automatically persist
// between hooks and are available
// in your tests below
cy.createDefaultTodos().as('todos')
})
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'
cy.get('@todos')
.eq(0)
.should('have.class', 'completed')
cy.get('@todos')
.eq(1)
.should('have.class', 'completed')
cy.get('@todos')
.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()
cy.get('@todos')
.eq(0)
.should('not.have.class', 'completed')
cy.get('@todos')
.eq(1)
.should('not.have.class', 'completed')
cy.get('@todos')
.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')
// alias the first todo and then click it
cy.get('.todo-list li')
.eq(0)
.as('firstTodo')
.find('.toggle')
.uncheck()
// reference the .toggle-all element again
// and make sure its not checked
cy.get('@toggleAll').should('not.be.checked')
// reference the first todo again and now toggle it
cy.get('@firstTodo')
.find('.toggle')
.check()
// assert the toggle all is checked again
cy.get('@toggleAll').should('be.checked')
})
})
context('Item', function () {
// New commands used here:
// - cy.clear https://on.cypress.io/api/clear
it('should allow me to mark items as complete', function () {
// we are aliasing the return value of
// our custom command 'createTodo'
//
// the return value is the <li> in the <ul.todos-list>
cy.createTodo(TODO_ITEM_ONE).as('firstTodo')
cy.createTodo(TODO_ITEM_TWO).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 () {
cy.createTodo(TODO_ITEM_ONE).as('firstTodo')
cy.createTodo(TODO_ITEM_TWO).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 () {
cy.createDefaultTodos().as('todos')
cy.get('@todos')
.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
cy.get('@todos')
.eq(0)
.should('contain', TODO_ITEM_ONE)
cy.get('@secondTodo').should('contain', 'buy some sausages')
cy.get('@todos')
.eq(2)
.should('contain', TODO_ITEM_THREE)
})
})
context('Editing', function () {
// New commands used here:
// - cy.blur https://on.cypress.io/api/blur
beforeEach(function () {
cy.createDefaultTodos().as('todos')
})
it('should hide other controls when editing', function () {
cy.get('@todos')
.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 () {
cy.get('@todos')
.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()
cy.get('@todos')
.eq(0)
.should('contain', TODO_ITEM_ONE)
cy.get('@secondTodo').should('contain', 'buy some sausages')
cy.get('@todos')
.eq(2)
.should('contain', TODO_ITEM_THREE)
})
it('should trim entered text', function () {
cy.get('@todos')
.eq(1)
.as('secondTodo')
.find('label')
.dblclick()
cy.get('@secondTodo')
.find('.edit')
.clear()
.type(' buy some sausages ')
.type('{enter}')
cy.get('@todos')
.eq(0)
.should('contain', TODO_ITEM_ONE)
cy.get('@secondTodo').should('contain', 'buy some sausages')
cy.get('@todos')
.eq(2)
.should('contain', TODO_ITEM_THREE)
})
it('should remove the item if an empty text string was entered', function () {
cy.get('@todos')
.eq(1)
.as('secondTodo')
.find('label')
.dblclick()
cy.get('@secondTodo')
.find('.edit')
.clear()
.type('{enter}')
cy.get('@todos').should('have.length', 2)
})
it('should cancel edits on escape', function () {
cy.get('@todos')
.eq(1)
.as('secondTodo')
.find('label')
.dblclick()
cy.get('@secondTodo')
.find('.edit')
.clear()
.type('foo{esc}')
cy.get('@todos')
.eq(0)
.should('contain', TODO_ITEM_ONE)
cy.get('@todos')
.eq(1)
.should('contain', TODO_ITEM_TWO)
cy.get('@todos')
.eq(2)
.should('contain', TODO_ITEM_THREE)
})
})
context('Counter', function () {
it('should display the current number of todo items', function () {
cy.createTodo(TODO_ITEM_ONE)
cy.get('.todo-count').contains('1 item left')
cy.createTodo(TODO_ITEM_TWO)
cy.get('.todo-count').contains('2 items left')
})
})
context('Clear completed button', function () {
beforeEach(function () {
cy.createDefaultTodos().as('todos')
})
it('should display the correct text', function () {
cy.get('@todos')
.eq(0)
.find('.toggle')
.check()
cy.get('.clear-completed').contains('Clear completed')
})
it('should remove completed items when clicked', function () {
cy.get('@todos')
.eq(1)
.find('.toggle')
.check()
cy.get('.clear-completed').click()
cy.get('@todos').should('have.length', 2)
cy.get('@todos')
.eq(0)
.should('contain', TODO_ITEM_ONE)
cy.get('@todos')
.eq(1)
.should('contain', TODO_ITEM_THREE)
})
it('should be hidden when there are no items that are completed', function () {
cy.get('@todos')
.eq(1)
.find('.toggle')
.check()
cy.get('.clear-completed')
.should('be.visible')
.click()
cy.get('.clear-completed').should('not.exist')
})
})
context('Persistence', function () {
it('should persist its data', function () {
// mimicking TodoMVC tests
// by writing out this function
function testState () {
cy.get('@firstTodo')
.should('contain', TODO_ITEM_ONE)
.and('have.class', 'completed')
cy.get('@secondTodo')
.should('contain', TODO_ITEM_TWO)
.and('not.have.class', 'completed')
}
cy.createTodo(TODO_ITEM_ONE).as('firstTodo')
cy.createTodo(TODO_ITEM_TWO).as('secondTodo')
cy.get('@firstTodo')
.find('.toggle')
.check()
.then(testState)
.reload()
.then(testState)
})
})
context('Routing', function () {
// New commands used here:
// https://on.cypress.io/window
// https://on.cypress.io/its
// https://on.cypress.io/invoke
// https://on.cypress.io/within
beforeEach(function () {
cy.createDefaultTodos().as('todos')
})
it('should allow me to display active items', function () {
cy.get('@todos')
.eq(1)
.find('.toggle')
.check()
cy.get('.filters')
.contains('Active')
.click()
cy.get('@todos')
.eq(0)
.should('contain', TODO_ITEM_ONE)
cy.get('@todos')
.eq(1)
.should('contain', TODO_ITEM_THREE)
})
it('should respect the back button', function () {
cy.get('@todos')
.eq(1)
.find('.toggle')
.check()
cy.get('.filters')
.contains('Active')
.click()
cy.get('.filters')
.contains('Completed')
.click()
cy.get('@todos').should('have.length', 1)
cy.go('back')
cy.get('@todos').should('have.length', 2)
cy.go('back')
cy.get('@todos').should('have.length', 3)
})
it('should allow me to display completed items', function () {
cy.get('@todos')
.eq(1)
.find('.toggle')
.check()
cy.get('.filters')
.contains('Completed')
.click()
cy.get('@todos').should('have.length', 1)
})
it('should allow me to display all items', function () {
cy.get('@todos')
.eq(1)
.find('.toggle')
.check()
cy.get('.filters')
.contains('Active')
.click()
cy.get('.filters')
.contains('Completed')
.click()
cy.get('.filters')
.contains('All')
.click()
cy.get('@todos').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')
})
})
})
context('Contrast', () => {
it('has good contrast when empty', () => {
cy.addAxeCode()
cy.checkA11y(null, {
runOnly: ['cat.color'],
})
})
it('has good contrast with several todos', () => {
cy.addAxeCode()
cy.get('.new-todo')
.type('learn testing{enter}')
.type('be cool{enter}')
cy.get('.todo-list li').should('have.length', 2)
cy.checkA11y(null, {
runOnly: ['cat.color'],
})
// and after marking an item completed
cy.get('.todo-list li')
.first()
.find('.toggle')
.check()
cy.get('.todo-list li')
.first()
.should('have.class', 'completed')
cy.checkA11y(null, {
runOnly: ['cat.color'],
})
})
})
})