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 pathapi-spec.js
184 lines (162 loc) · 3.95 KB
/
api-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
import {
enterTodo,
getTodoItems,
makeTodo,
resetDatabase,
stubMathRandom,
visit
} from '../../support/utils'
// testing TodoMVC server API
// because json-server can fail sometimes, let the tests retry
// https://on.cypress.io/test-retries
describe('via API', { retries: 2 }, () => {
beforeEach(resetDatabase)
// used to create predictable ids
let counter = 1
beforeEach(() => {
counter = 1
})
const addTodo = (title) =>
// the way to print newly created id in the command log
cy
.request('POST', '/todos', {
title,
completed: false,
id: String(counter++)
})
.its('body.id')
.then(cy.log)
/**
* Fetches TODO items, returns just the body of the XHR request.
*/
const fetchTodos = () =>
cy
.request('/todos')
.its('body')
.then((list) => {
cy.log(JSON.stringify(list, null, 2))
cy.wrap(list, { log: false })
})
const deleteTodo = (id) => cy.request('DELETE', `/todos/${id}`)
it('adds 2 todos', () => {
addTodo('first todo')
addTodo('second todo')
fetchTodos().should('have.length', 2)
})
it('adds todo deep', () => {
addTodo('first todo')
addTodo('second todo')
fetchTodos().should('deep.equal', [
{
title: 'first todo',
completed: false,
id: '1'
},
{
title: 'second todo',
completed: false,
id: '2'
}
])
})
it('adds and deletes a todo', () => {
addTodo('first todo') // id "1"
addTodo('second todo') // id "2"
deleteTodo('2')
fetchTodos().should('deep.equal', [
{
title: 'first todo',
completed: false,
id: '1'
}
])
})
})
describe('stub network', () => {
it('initial todos', () => {
cy.server()
cy.route('/todos', [
{
title: 'mock first',
completed: false,
id: '1'
},
{
title: 'mock second',
completed: true,
id: '2'
}
])
visit(true)
getTodoItems()
.should('have.length', 2)
.contains('li', 'mock second')
.find('.toggle')
.should('be.checked')
})
})
describe('API', { retries: 2 }, () => {
beforeEach(resetDatabase)
beforeEach(() => visit(true))
beforeEach(stubMathRandom)
it('receives empty list of items', () => {
cy.request('todos').its('body').should('deep.equal', [])
})
it('adds two items', () => {
const first = makeTodo()
const second = makeTodo()
cy.request('POST', 'todos', first)
cy.request('POST', 'todos', second)
cy.request('todos')
.its('body')
.should('have.length', 2)
.and('deep.equal', [first, second])
})
it('adds two items and deletes one', () => {
const first = makeTodo()
const second = makeTodo()
cy.request('POST', 'todos', first)
cy.request('POST', 'todos', second)
cy.request('DELETE', `todos/${first.id}`)
cy.request('todos')
.its('body')
.should('have.length', 1)
.and('deep.equal', [second])
})
it('does not delete non-existent item', () => {
cy.request({
method: 'DELETE',
url: 'todos/aaa111bbb',
failOnStatusCode: false
})
.its('status')
.should('equal', 404)
})
it('is adding todo item', () => {
cy.server()
cy.route({
method: 'POST',
url: '/todos'
}).as('postTodo')
// go through the UI
enterTodo('first item') // id "1"
// thanks to stubbed random id generator
// we can "predict" what the TODO object is going to look like
cy.wait('@postTodo').its('request.body').should('deep.equal', {
title: 'first item',
completed: false,
id: '1'
})
})
it('is deleting a todo item', () => {
cy.server()
cy.route({
method: 'DELETE',
url: '/todos/1'
}).as('deleteTodo')
// go through the UI
enterTodo('first item') // id "1"
getTodoItems().first().find('.destroy').click({ force: true })
cy.wait('@deleteTodo')
})
})