forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstub-fetch-spec.js
92 lines (78 loc) · 2.51 KB
/
stub-fetch-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
/// <reference types="Cypress" />
// Here, we completely stub out window.fetch, allowing
// us to more finely control the server responses
//
// This allows us to test various data responses like errors
const deferred = require('./deferred')
describe('stubbing', function () {
beforeEach(function () {
// We use a deferred object to make it easy to test
// different scenarios
this.fetchFavoritesDeferred = deferred()
// We use cy.visit({onBeforeLoad: ...}) to stub
// window.fetch before any app code runs
cy.visit('/', {
onBeforeLoad (win) {
cy.stub(win, 'fetch')
.withArgs('/favorite-fruits')
.as('fetchFavorites')
.returns(this.fetchFavoritesDeferred.promise)
},
})
})
it('requests favorite fruits', function () {
// aliasing allows us to easily get access to our stub
cy.get('@fetchFavorites').should('be.calledWith', '/favorite-fruits')
})
// A big advantage of controlling the response is we can test
// how our app handles a slow response, which normally might be
// difficult against a fast development server
it('shows loader while fetching fruits', function () {
cy.get('.loader')
})
describe('when favorite fruits are returned', function () {
beforeEach(function () {
this.fetchFavoritesDeferred.resolve({
json () {
return ['Apple', 'Banana', 'Cantaloupe']
},
ok: true,
})
})
it('displays the list of fruits', function () {
cy.get('.favorite-fruits li').as('favoriteFruits')
.should('have.length', 3)
cy.get('@favoriteFruits').first()
.should('have.text', 'Apple')
cy.get('@favoriteFruits').eq(1)
.should('have.text', 'Banana')
cy.get('@favoriteFruits').eq(2)
.should('have.text', 'Cantaloupe')
})
})
describe('when no favorite fruits are returned', function () {
beforeEach(function () {
this.fetchFavoritesDeferred.resolve({
json () {
return []
},
ok: true,
})
})
it('displays empty message', function () {
cy.get('.favorite-fruits').should('have.text', 'No favorites')
})
})
describe('when request fails', function () {
beforeEach(function () {
this.fetchFavoritesDeferred.resolve({
ok: false,
statusText: 'Orchard under maintenance',
})
})
it('displays error', function () {
cy.get('.favorite-fruits')
.should('have.text', 'Failed loading favorite fruits: Orchard under maintenance')
})
})
})