forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-spec.js
194 lines (170 loc) · 4.2 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
185
186
187
188
189
190
191
192
193
194
/// <reference types="cypress" />
/* eslint-env mocha */
/* global cy */
import { enterTodo, getTodoItems, makeTodo, resetDatabase, stubMathRandom, visit } from '../support/utils';
// testing TodoMVC server API
describe('via API', () => {
beforeEach(resetDatabase)
// used to create predictable ids
let counter = 1
beforeEach(() => {
counter = 1
})
const addTodo = title =>
cy.request('POST', '/todos', {
title,
completed: false,
id: String(counter++)
})
const fetchTodos = () => cy.request('/todos').its('body')
const deleteTodo = id => cy.request('DELETE', `/todos/${id}`)
it('adds todo', () => {
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'
}
])
})
})
// sets up spying on route "GET /todos" under name "todos"
// after visiting the page
// waits for the alias "todos" to make sure
// the application has actually made the request
it('loads todos on start', () => {
cy.server()
cy.route('/todos').as('todos')
cy.visit('/')
cy.wait('@todos')
})
// stubs expected API call with JSON response
// from a fixture file
it('loads todos from fixture file', () => {
cy.server()
// loads response from "cypress/fixtures/todos.json"
cy.route('/todos', 'fixture:todos')
cy.visit('/')
getTodoItems()
.should('have.length', 2)
.contains('li', 'mock second')
.find('.toggle')
.should('be.checked')
})
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', () => {
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')
})
})