forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec.js
37 lines (32 loc) · 1.17 KB
/
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
/// <reference types="cypress" />
/* global File */
describe('File upload', () => {
beforeEach(() => {
cy.visit('index.html')
})
it('uploads mock file and stubs the server', () => {
// mock the network call using https://on.cypress.io/intercept
cy.intercept('POST', 'https://some-server.com/upload', {}).as('upload')
// load mock data from a fixture or construct here
const testFile = new File(['data to upload'], 'upload.txt')
cy.get('#file1').trigger('change', { testFile })
// make sure upload has happened
cy.wait('@upload')
})
it('uploads mock file by stubbing axios.post', () => {
// see how to stub an object in the application environment
// https://on.cypress.io/stub
cy.window()
.its('axios')
.then((axios) => {
// save stub under an alias
cy.stub(axios, 'post').as('file-upload')
})
// load mock data from a fixture or construct here
const testFile = new File(['data to upload'], 'upload.txt')
cy.get('#file1').trigger('change', { testFile })
// check stub has been used
// https://on.cypress.io/assertions#Sinon-Chai
cy.get('@file-upload').should('have.been.calledOnce')
})
})