forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matching-spec.cy.js
204 lines (171 loc) · 6.48 KB
/
matching-spec.cy.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
/// <reference types="Cypress" />
describe('intercept', () => {
context('matches order', () => {
describe('multiple matches', () => {
it('uses the last found route matcher that responds', () => {
cy.intercept('favorite-fruits').as('favorite-fruits') // does not reply
cy.intercept('favorite-*', ['Lemons 🍋']).as('favorite') // replies with a fruit
cy.intercept('*-fruits').as('fruits') // does not reply
cy.visit('/')
cy.wait('@fruits') // first route matches
cy.wait('@favorite') // second route matches
// but the third route never gets the request
// since the second route has replied
cy.contains('li', 'Lemons 🍋').should('be.visible')
})
it('using glob matches multiple interceptors', () => {
cy.visit('/')
cy.intercept('/users/*').as('users')
cy.intercept('/users/2').as('secondUser')
cy.get('#load-second-user').click()
// confirm that both interceptors are matched
cy.wait('@users')
.its('request.url')
.should('equal', 'https://jsonplaceholder.cypress.io/users/2')
cy.wait('@secondUser')
.its('request.url')
.should('equal', 'https://jsonplaceholder.cypress.io/users/2')
})
it('use regex to match exactly', () => {
// if you want to be precise, use regular expressions
cy.visit('/')
cy.intercept(/\/users$/).as('users')
cy.intercept(/\/users\/2$/).as('secondUser')
cy.get('#load-second-user').click()
// only the second interceptor should match
cy.wait('@secondUser')
.its('request.url')
.should('equal', 'https://jsonplaceholder.cypress.io/users/2')
})
it('use regex to match exactly and check if the other intercept has not fired', () => {
cy.visit('/')
let usersMatched = false
cy.intercept(/\/users$/, () => {
usersMatched = true
})
cy.intercept(/\/users\/2$/).as('secondUser')
cy.get('#load-second-user').click()
// only the second interceptor should match
cy.wait('@secondUser')
.its('request.url')
.should('equal', 'https://jsonplaceholder.cypress.io/users/2')
// but the first intercept should have never fired
.then(() => {
expect(usersMatched, 'users intercept').to.be.false
})
})
})
describe('when starts with a slash', () => {
// seems minimatch fails if the matcher URL starts with a /
// and includes * character
it('matches using minimatch', () => {
// our application issues GET "/favorite-fruits"
// which matches all these wildcards
// NOTE: we are matching full url
const URL = `${Cypress.config('baseUrl')}/favorite-fruits`
expect(Cypress.minimatch(URL, '**/*-fruits'), '**/*-fruits').to.be.true
expect(Cypress.minimatch(URL, '**/favorite-*'), '**/favorite-*').to.be.true
expect(Cypress.minimatch(URL, '**/favorite-fruits'), '**/favorite-fruits').to.be.true
})
it('matches *-fruits', () => {
cy.intercept('**/*-fruits').as('fruits')
cy.visit('/')
cy.wait('@fruits')
})
it('matches favorite-*', () => {
cy.intercept('**/favorite-*').as('favorite')
cy.visit('/')
cy.wait('@favorite')
})
it('matches favorite-fruits', () => {
cy.intercept('**/favorite-fruits').as('favorite-fruits')
cy.visit('/')
cy.wait('@favorite-fruits')
})
it('matches all routes that do not respond', () => {
cy.intercept('**/*-fruits').as('fruits')
cy.intercept('**/favorite-*').as('favorite')
cy.intercept('**/favorite-fruits').as('favorite-fruits')
cy.visit('/')
// matches all 3 routes
cy.wait('@fruits')
cy.wait('@favorite')
cy.wait('@favorite-fruits')
})
it('uses the first found route matcher (2)', () => {
cy.intercept('**/*-fruits-does-not-exist').as('fruits') // this does not match
cy.intercept('**/favorite-*').as('favorite')
cy.intercept('**/favorite-fruits').as('favorite-fruits')
cy.visit('/')
// matches last two routes
cy.wait('@favorite')
cy.wait('@favorite-fruits')
})
})
describe('without minimatch, just wildcards', () => {
it('matches *-fruits', () => {
cy.intercept('*-fruits').as('fruits')
cy.visit('/')
cy.wait('@fruits')
})
it('matches favorite-*', () => {
cy.intercept('favorite-*').as('favorite')
cy.visit('/')
cy.wait('@favorite')
})
it('matches favorite-fruits', () => {
cy.intercept('favorite-fruits').as('favorite-fruits')
cy.visit('/')
cy.wait('@favorite-fruits')
})
it('matches all routes that do not respond', () => {
cy.intercept('*-fruits').as('fruits')
cy.intercept('favorite-*').as('favorite')
cy.intercept('favorite-fruits').as('favorite-fruits')
cy.visit('/')
// all 3 routes match
cy.wait('@fruits')
cy.wait('@favorite')
cy.wait('@favorite-fruits')
})
})
})
context('using regex', () => {
beforeEach(() => {
cy.visit('/')
})
it('intercepts user #2 with exact URL', () => {
cy.intercept('/users/2').as('second')
cy.get('#load-second-user').click()
cy.contains('.user', '2 - [email protected]').should('be.visible')
cy.wait('@second').its('response.body').should('include', {
email: '[email protected]',
})
})
it('gets any user using regex', () => {
cy.intercept(/\/users\/\d+$/).as('anyUser')
cy.get('#load-second-user').click()
cy.contains('.user', '2 - [email protected]').should('be.visible')
cy.wait('@anyUser').its('response.body').should('include', {
id: 2,
email: '[email protected]',
})
})
it('mocks a user using regex', () => {
cy.intercept(/\/users\/\d+$/, {
body: {
id: 101,
email: '[email protected]',
},
headers: {
'access-control-allow-origin': Cypress.config('baseUrl'),
},
}).as('anyUser')
cy.get('#load-second-user').click()
cy.wait('@anyUser').its('request.url')
// let the browser parse the url for us
.then((url) => new URL(url)).should('have.property', 'pathname', '/users/2')
cy.contains('.user', '101 - [email protected]').should('be.visible')
})
})
})