This repository has been archived by the owner on Feb 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathanswer.js
238 lines (211 loc) · 7.25 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
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
/// <reference types="cypress" />
describe('retry-ability', () => {
beforeEach(function resetData() {
cy.request('POST', '/reset', {
todos: []
})
})
beforeEach(function visitSite() {
cy.visit('/')
})
it('shows UL', function () {
cy.get('.new-todo')
.type('todo A{enter}')
.type('todo B{enter}')
.type('todo C{enter}')
.type('todo D{enter}')
cy.contains('ul', 'todo A')
// confirm that the above element
// 1. is visible
.should('be.visible')
// 2. has class "todo-list"
.and('have.class', 'todo-list')
// 3. css property "list-style-type" is equal "none"
.and('have.css', 'list-style-type', 'none')
})
it('shows UL - TDD', function () {
cy.get('.new-todo')
.type('todo A{enter}')
.type('todo B{enter}')
.type('todo C{enter}')
.type('todo D{enter}')
cy.contains('ul', 'todo A').should(($ul) => {
// use TDD assertions
// $ul is visible
// $ul has class "todo-list"
// $ul css has "list-style-type" = "none"
assert.isTrue($ul.is(':visible'), 'ul is visible')
assert.include($ul[0].className, 'todo-list')
assert.isTrue($ul.hasClass('todo-list'))
assert.equal($ul.css('list-style-type'), 'none')
})
})
it('every item starts with todo', function () {
cy.get('.new-todo')
.type('todo A{enter}')
.type('todo B{enter}')
.type('todo C{enter}')
.type('todo D{enter}')
cy.get('.todo label').should(($labels) => {
// confirm that there are 4 labels
// and that each one starts with "todo-"
expect($labels).to.have.length(4)
$labels.each((k, el) => {
expect(el.textContent).to.match(/^todo /)
})
})
})
it('has 2 items', () => {
cy.get('.new-todo') // command
.type('todo A{enter}') // command
.type('todo B{enter}') // command
cy.get('.todo-list li') // command
.should('have.length', 2) // assertion
})
it('has the right label', () => {
cy.get('.new-todo').type('todo A{enter}')
cy.get('.todo-list li') // command
.find('label') // command
.should('contain', 'todo A') // assertion
})
// flaky test - can pass or not depending on the app's speed
// to make the test flaky add the timeout
// in todomvc/app.js "addTodo({ commit, state })" method
it('has two labels', () => {
cy.get('.new-todo').type('todo A{enter}')
cy.get('.todo-list li') // command
.find('label') // command
.should('contain', 'todo A') // assertion
cy.get('.new-todo').type('todo B{enter}')
cy.get('.todo-list li') // command
.find('label') // command
.should('contain', 'todo B') // assertion
})
it('solution 1: merges queries', () => {
cy.get('.new-todo').type('todo A{enter}')
cy.get('.todo-list li label') // command
.should('contain', 'todo A') // assertion
cy.get('.new-todo').type('todo B{enter}')
cy.get('.todo-list li label') // command
.should('contain', 'todo B') // assertion
})
it('solution 2: alternate commands and assertions', () => {
cy.get('.new-todo').type('todo A{enter}')
cy.get('.todo-list li') // command
.should('have.length', 1) // assertion
.find('label') // command
.should('contain', 'todo A') // assertion
cy.get('.new-todo').type('todo B{enter}')
cy.get('.todo-list li') // command
.should('have.length', 2) // assertion
.find('label') // command
.should('contain', 'todo B') // assertion
})
it('retries reading the JSON file', () => {
// note cy.readFile retries reading the file until the should(cb) passes
// https://on.cypress.io/readfile
cy.get('.new-todo')
.type('todo A{enter}')
.type('todo B{enter}')
.type('todo C{enter}')
.type('todo D{enter}')
cy.readFile('./todomvc/data.json').should((data) => {
expect(data).to.have.property('todos')
expect(data.todos).to.have.length(4, '4 saved items')
expect(data.todos[0], 'first item').to.include({
title: 'todo A',
completed: false
})
})
})
})
describe('Careful with negative assertions', { retries: 2 }, () => {
beforeEach(function resetData() {
// cy.intercept('/todos', { body: [], delayMs: 5000 })
})
// this assertion can pass - but for the wrong reason
// the indicator initially is NOT shown, thus this assertion
// pass immediately, and probably not when the app finishes loading
it('hides the loading element', () => {
cy.visit('/')
cy.get('.loading').should('not.be.visible')
})
it('uses negative assertion and passes for the wrong reason', () => {
cy.visit('/?delay=3000')
cy.get('.loading').should('not.be.visible')
})
// NOTE: skipping because it is flakey and slowing down the request is better
it.skip('use positive then negative assertion (flakey)', () => {
cy.visit('/?delay=3000')
// first, make sure the loading indicator shows up (positive assertion)
cy.get('.loading').should('be.visible')
// then assert it goes away (negative assertion)
cy.get('.loading').should('not.be.visible')
})
it('uses cy.route to slow down network response', () => {
cy.server()
cy.route({
method: 'GET',
url: '/todos',
response: [],
delay: 2000
})
cy.visit('/?delay=3000')
// first, make sure the loading indicator shows up (positive assertion)
cy.get('.loading').should('be.visible')
// then assert it goes away (negative assertion)
cy.get('.loading').should('not.be.visible')
})
it('slows down the network response', () => {
cy.intercept('/todos', {
body: [],
delayMs: 1000
})
cy.visit('/?delay=1000')
// first, make sure the loading indicator shows up (positive assertion)
cy.get('.loading').should('be.visible')
// then assert it goes away (negative assertion)
cy.get('.loading').should('not.be.visible')
})
it('slows down the network response (programmatic)', () => {
cy.intercept('/todos', (req) => {
req.reply({
body: [],
delayMs: 1000
})
})
cy.visit('/?delay=1000')
// first, make sure the loading indicator shows up (positive assertion)
cy.get('.loading').should('be.visible')
// then assert it goes away (negative assertion)
cy.get('.loading').should('not.be.visible')
})
})
describe('aliases', () => {
context('are reset before each test', () => {
before(() => {
cy.wrap('some value').as('exampleValue')
})
it('works in the first test', () => {
cy.get('@exampleValue').should('equal', 'some value')
})
// NOTE the second test is failing because the alias is reset
it.skip('does not exist in the second test', () => {
// there is not alias because it is created once before
// the first test, and is reset before the second test
cy.get('@exampleValue').should('equal', 'some value')
})
})
context('should be created before each test', () => {
beforeEach(() => {
// we will create a new alias before each test
cy.wrap('some value').as('exampleValue')
})
it('works in the first test', () => {
cy.get('@exampleValue').should('equal', 'some value')
})
it('works in the second test', () => {
cy.get('@exampleValue').should('equal', 'some value')
})
})
})