forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspec.js
39 lines (34 loc) · 1.19 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
38
39
/// <reference types="cypress" />
/* global File */
describe('File upload', () => {
beforeEach(() => {
cy.visit('index.html')
})
it('uploads mock file and stubs the server', () => {
// see how to mock a remote server
// https://on.cypress.io/route
cy.server()
cy.route('POST', 'https://some-server.com/upload', 200).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')
})
})