forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wait-vs-get.js
44 lines (36 loc) · 1.22 KB
/
wait-vs-get.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
/// <reference types="cypress" />
describe('XHR', () => {
// NOTE: this is a demo of failing test
it.skip('cy.get yields null if the network request has not happened yet', () => {
cy.visit('index.html')
cy.server()
cy.route('POST', '/posts').as('post')
cy.get('#delayed-load').click()
// cy.get does NOT work
// because it immediately returns null object,
// since the request has not happened yet
cy.get('@post').should('have.property', 'status', 201)
})
it('cy.wait waits for the network request to happen', () => {
cy.visit('index.html')
cy.server()
cy.route('POST', '/posts').as('post')
cy.get('#delayed-load').click()
cy.wait('@post').should('have.property', 'status', 201)
})
it('cy.wait then cy.get to retrieve the same XHR', () => {
cy.visit('index.html')
cy.server()
cy.route('POST', '/posts').as('post')
cy.get('#delayed-load').click()
// there is only 1 POST request
cy.wait('@post').then((xhr1) => {
// ask for the XHR again using cy.get
// by now it has happened for sure,
// and cy.get should yield same XHR object
cy.get('@post').then((xhr2) => {
expect(xhr1, 'same XHR').to.equal(xhr2)
})
})
})
})