forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cy-on-spec.js
36 lines (32 loc) · 1.14 KB
/
cy-on-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
/// <reference types="cypress" />
describe('cy.on window:before:load', () => {
// @see https://on.cypress.io/catalog-of-events
// let's use "window:before:load" to automatically
// attach mock Analytics method to the application's "window" object
// we will use "cy.on" which requires a test or a hook to work
beforeEach(() => {
cy.on('window:before:load', (win) => {
win.Analytics = {
sendEvent: cy.stub().as('sendEvent'),
}
})
})
it('sends events', () => {
cy.visit('index.html')
cy.get('button#click-me').click().click()
cy.get('@sendEvent').should('be.calledTwice')
.invoke('reset') // reset the stub counter
cy.get('button#click-me').click()
cy.get('@sendEvent').should('be.calledOnceWithExactly', 'click', 'button#click-me')
})
it('sends more events', () => {
cy.visit('index.html')
cy.get('button#click-me').click()
// if we do not precise value for one of the arguments
// we can "skip" it but make sure it is still a string
// using Sinon matchers
//
cy.get('@sendEvent').should('be.calledOnceWith',
Cypress.sinon.match.string, 'button#click-me')
})
})