Skip to content
This repository was archived by the owner on Dec 12, 2020. It is now read-only.

Commit 547e0c2

Browse files
authored
add window.fetch example (#368)
1 parent f6812ad commit 547e0c2

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@ Spec | Description
530530
[access-component](cypress/component/advanced/access-component) | Access the mounted component directly from test
531531
[i18n](cypress/component/advanced/i18n) | Testing component that uses [Vue I18n](https://kazupon.github.io/vue-i18n/) plugin
532532
[mocking-axios](cypress/component/advanced/mocking-axios) | Mocking 3rd party CommonJS modules like `axios`
533+
[mocking-fetch](cypress/component/advanced/mocking-fetch) | Mocking `window.fetch` to stub responses and test the UI
533534
[mocking-components](cypress/component/advanced/mocking-components) | Mocking locally registered child components during tests
534535
[mocking-imports](cypress/component/advanced/mocking-imports) | Stub ES6 imports from the tests
535536
[render-functions](cypress/component/advanced/render-functions) | Mounting components with a [render function](https://www.tutorialandexample.com/vue-js-render-functions/)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# mocking fetch method
2+
3+
We can directly access the `window` object and mock the `window.fetch` method. See [Users.vue](Users.vue) and [Users.spec.js](Users.spec.js)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/// <reference types="cypress" />
2+
import { mount } from 'cypress-vue-unit-test'
3+
import Users from './Users.vue'
4+
5+
describe('Fetching users', () => {
6+
let mockUsers
7+
8+
before(() => {
9+
// load the mock user list once from cypress/fixtures/users.json
10+
cy.fixture('users').then((list) => {
11+
expect(list).to.have.length(2)
12+
mockUsers = list
13+
})
14+
})
15+
16+
it('renders real data', () => {
17+
mount(Users)
18+
cy.get('.user').should('have.length', 3)
19+
})
20+
21+
it('can mock window.fetch method', () => {
22+
cy.stub(window, 'fetch').resolves({
23+
json: cy.stub().resolves(mockUsers),
24+
})
25+
mount(Users)
26+
cy.get('.user').should('have.length', mockUsers.length)
27+
cy.get('.user')
28+
.first()
29+
.should('have.text', `${mockUsers[0].id} - ${mockUsers[0].name}`)
30+
})
31+
32+
it('shows loading UI while fetch is happening', () => {
33+
const mockResponse = {
34+
json: cy.stub().resolves(mockUsers),
35+
}
36+
cy.stub(window, 'fetch').resolves(
37+
// resolve promise after a delay
38+
Cypress.Promise.resolve(mockResponse).delay(1000),
39+
)
40+
mount(Users)
41+
cy.get('.loading').should('be.visible')
42+
cy.get('.loading').should('not.exist')
43+
44+
cy.get('.user').should('have.length', mockUsers.length)
45+
cy.get('.user')
46+
.first()
47+
.should('have.text', `${mockUsers[0].id} - ${mockUsers[0].name}`)
48+
})
49+
})
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<template>
2+
<div>
3+
<h1>Users</h1>
4+
<ul v-if="users && users.length">
5+
<li class="user" v-for="user of users" v-bind:key="user.id">
6+
<p><strong>{{user.id}}</strong> - {{user.name}}</p>
7+
</li>
8+
</ul>
9+
<p class="loading" v-else>Loading...</p>
10+
</div>
11+
</template>
12+
13+
<script>
14+
export default {
15+
data() {
16+
return {
17+
users: []
18+
}
19+
},
20+
21+
created() {
22+
fetch('https://jsonplaceholder.cypress.io/users?_limit=3')
23+
.then(response => response.json())
24+
.then(list => {
25+
this.users = list
26+
})
27+
}
28+
}
29+
</script>

cypress/fixtures/users.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[{
2+
"id": "101",
3+
"name": "Cypress User"
4+
}, {
5+
"id": "102",
6+
"name": "Svelte User"
7+
}]

0 commit comments

Comments
 (0)