This repository was archived by the owner on Feb 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathanswer.js
98 lines (81 loc) · 2.75 KB
/
answer.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
/// <reference types="cypress" />
// IMPORTANT ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️
// remember to manually delete all items before running the test
// IMPORTANT ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️
beforeEach(() => {
cy.visit('localhost:3000')
})
it('loads', () => {
cy.contains('h1', 'todos')
})
it('adds two items', () => {
cy.get('.new-todo').type('first item{enter}')
cy.contains('li.todo', 'first item').should('be.visible')
cy.get('.new-todo').type('second item{enter}')
cy.contains('li.todo', 'second item').should('be.visible')
})
it('can mark an item as completed', () => {
// adds a few items
addItem('simple')
addItem('hard')
// marks the first item as completed
cy.contains('li.todo', 'simple').should('exist').find('.toggle').check()
// confirms the first item has the expected completed class
cy.contains('li.todo', 'simple').should('have.class', 'completed')
// confirms the other items are still incomplete
cy.contains('li.todo', 'hard').should('not.have.class', 'completed')
})
it('can delete an item', () => {
// adds a few items
addItem('simple')
addItem('hard')
// deletes the first item
cy.contains('li.todo', 'simple')
.should('exist')
.find('.destroy')
// use force: true because we don't wsnt to hover
.click({ force: true })
// confirm the deleted item is gone from the dom
cy.contains('li.todo', 'simple').should('not.exist')
// confirm the other item still exists
cy.contains('li.todo', 'hard').should('exist')
})
/**
* Adds a todo item
* @param {string} text
*/
const addItem = (text) => {
cy.get('.new-todo').type(`${text}{enter}`)
}
it('can add many items', () => {
// assumes there are no items at the beginning
const N = 5
for (let k = 0; k < N; k += 1) {
addItem(`item ${k}`)
}
// check number of items
cy.get('li.todo').should('have.length', 5)
})
it('adds item with random text', () => {
const randomLabel = `Item ${Math.random().toString().slice(2, 14)}`
addItem(randomLabel)
cy.contains('li.todo', randomLabel)
.should('be.visible')
.and('not.have.class', 'completed')
})
it('starts with zero items', () => {
cy.get('li.todo').should('have.length', 0)
})
it('does not allow adding blank todos', () => {
cy.on('uncaught:exception', (e) => {
// what will happen if this assertion fails?
// will the test fail?
// expect(e.message).to.include('Cannot add a blank todo')
// return false
// a better shortcut
return !e.message.includes('Cannot add a blank todo')
})
addItem(' ')
})
// what a challenge?
// test more UI at http://todomvc.com/examples/vue/