forked from nadvolod/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload-via-app-spec.js
32 lines (27 loc) · 929 Bytes
/
upload-via-app-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
/// <reference types="cypress" />
/* global File */
describe('File upload', () => {
beforeEach(() => {
cy.visit('index.html')
})
it('uploads file by stubbing application code', () => {
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')
// directly stub the application method that returns the File object to upload
cy.window()
.its('app')
.then((app) => {
cy.stub(app, 'getFileToUpload')
.returns(testFile)
// give this stub an alias to confirm it was called
.as('getFileToUpload')
})
cy.get('#file1').trigger('change', { testFile })
// make sure our application stub worked
cy.get('@getFileToUpload').should('have.been.calledOnce')
// make sure upload has happened
cy.wait('@upload')
})
})