forked from cypress-io/testing-workshop-cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanswer-route-spec.js
162 lines (149 loc) · 4.39 KB
/
answer-route-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
/// <reference types="cypress" />
// note, we are not resetting the server before each test
it('starts with zero items (waits)', () => {
cy.visit('/')
/* eslint-disable-next-line cypress/no-unnecessary-waiting */
cy.wait(1000)
cy.get('li.todo').should('have.length', 0)
})
it('starts with zero items', () => {
// start Cypress network server
// spy on route `GET /todos`
// THEN visit the page
cy.server()
cy.route('GET', '/todos').as('todos')
cy.visit('/')
cy.wait('@todos') // wait for `GET /todos` response
// inspect the server's response
.its('response.body')
.should('have.length', 0)
// then check the DOM
// note that we don't have to use "cy.wait(...).then(...)"
// because all Cypress commands are flattened into a single chain
// automatically. Thus just write "cy.wait(); cy.get();" naturally
cy.get('li.todo').should('have.length', 0)
})
it('starts with zero items (stubbed response)', () => {
// start Cypress network server
// spy on route `GET /todos`
// THEN visit the page
cy.server()
cy.route('GET', '/todos', []).as('todos')
cy.visit('/')
cy.wait('@todos') // wait for `GET /todos` response
// inspect the server's response
.its('response.body')
.should('have.length', 0)
// then check the DOM
cy.get('li.todo').should('have.length', 0)
})
it('starts with zero items (fixture)', () => {
// start Cypress network server
// stub route `GET /todos`, return data from fixture file
// THEN visit the page
cy.server()
cy.route('GET', '/todos', 'fixture:empty-list').as('todos')
cy.visit('/')
cy.wait('@todos') // wait for `GET /todos` response
// inspect the server's response
.its('response.body')
.should('have.length', 0)
// then check the DOM
cy.get('li.todo').should('have.length', 0)
})
it('posts new item to the server', () => {
cy.server()
cy.route('POST', '/todos').as('new-item')
cy.visit('/')
cy.get('.new-todo').type('test api{enter}')
cy.wait('@new-item').its('request.body').should('have.contain', {
title: 'test api',
completed: false
})
})
it('posts new item to the server response', () => {
cy.server()
cy.route('POST', '/todos').as('new-item')
cy.visit('/')
cy.get('.new-todo').type('test api{enter}')
cy.wait('@new-item').its('response.body').should('have.contain', {
title: 'test api',
completed: false
})
})
it('loads several items from a fixture', () => {
// start Cypress network server
// stub route `GET /todos` with data from a fixture file
// THEN visit the page
cy.server()
cy.route('GET', '/todos', 'fx:two-items')
cy.visit('/')
// then check the DOM: some items should be marked completed
// we can do this in a variety of ways
cy.get('li.todo').should('have.length', 2)
cy.get('li.todo.completed').should('have.length', 1)
cy.contains('.todo', 'first item from fixture')
.should('not.have.class', 'completed')
.find('.toggle')
.should('not.be.checked')
cy.contains('.todo.completed', 'second item from fixture')
.find('.toggle')
.should('be.checked')
})
it('handles 404 when loading todos', () => {
// when the app tries to load items
// set it up to fail
cy.server()
cy.route({
url: '/todos',
response: 'test does not allow it',
status: 404,
delay: 2000
})
cy.visit('/', {
// spy on console.error because we expect app would
// print the error message there
onBeforeLoad: (win) => {
cy.spy(win.console, 'error').as('console-error')
}
})
// observe external effect from the app - console.error(...)
cy.get('@console-error').should(
'have.been.calledWithExactly',
'test does not allow it'
)
})
it('shows loading element', () => {
// delay XHR to "/todos" by a few seconds
// and respond with an empty list
cy.server()
cy.route({
url: '/todos',
delay: 2000,
response: []
}).as('loading')
cy.visit('/')
// shows Loading element
cy.get('.loading').should('be.visible')
// wait for the network call to complete
cy.wait('@loading')
// now the Loading element should go away
cy.get('.loading').should('not.be.visible')
})
it('handles todos with blank title', () => {
cy.server()
cy.route('/todos', [
{
id: '123',
title: ' ',
completed: false
}
])
cy.visit('/')
cy.get('li.todo')
.should('have.length', 1)
.first()
.should('not.have.class', 'completed')
.find('label')
.should('have.text', ' ')
})