This repository has been archived by the owner on Feb 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
182 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/// <reference types="cypress" /> | ||
|
||
describe('cy.on', () => { | ||
// these test handles every "window" object | ||
// by attaching the stub whenever any window is created | ||
// https://on.cypress.io/catalog-of-events | ||
it('works', () => { | ||
// there is no "window.track" yet, | ||
// thus we cannot stub just yet | ||
let track // the real track when set by the app | ||
let trackStub // our stub around the real track | ||
|
||
// use "cy.on" to prepare for "window.track" assignment | ||
// this code runs for every window creation, thus we | ||
// can track events from the "cy.reload()" | ||
cy.on('window:before:load', (win) => { | ||
Object.defineProperty(win, 'track', { | ||
get() { | ||
return trackStub | ||
}, | ||
set(fn) { | ||
// if the stub does not exist yet, create it | ||
if (!track) { | ||
track = fn | ||
// give the created stub an alias so we can retrieve it later | ||
trackStub = cy.stub().callsFake(track).as('track') | ||
} | ||
} | ||
}) | ||
}) | ||
|
||
cy.visit('/') | ||
|
||
// make sure the page called the "window.track" with expected arguments | ||
cy.get('@track').should('have.been.calledOnceWith', 'window.load') | ||
|
||
cy.reload() | ||
cy.reload() | ||
|
||
cy.get('@track') | ||
.should('have.been.calledThrice') | ||
// confirm every call was with "window.load" argument | ||
.invoke('getCalls') | ||
.should((calls) => { | ||
calls.forEach((trackCall, k) => { | ||
expect(trackCall.args, `call ${k + 1}`).to.deep.equal(['window.load']) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/// <reference types="cypress" /> | ||
|
||
// variable that will hold cy.stub created in the test | ||
let stub | ||
|
||
// use Cypress.on in its own spec to avoid every test running it | ||
// https://on.cypress.io/catalog-of-events | ||
Cypress.on('window:before:load', (win) => { | ||
// if the test has prepared a stub | ||
if (stub) { | ||
// the stub function is ready | ||
// always returns it when the application | ||
// is trying to use "window.track" | ||
Object.defineProperty(win, 'track', { | ||
get() { | ||
return stub | ||
} | ||
}) | ||
} | ||
}) | ||
|
||
describe('Cypress.on', () => { | ||
beforeEach(() => { | ||
// let the test create the stub if it needs it | ||
stub = null | ||
}) | ||
|
||
it('works', () => { | ||
// note: cy.stub returns a function | ||
stub = cy.stub().as('track') | ||
cy.visit('/') | ||
|
||
// make sure the page called the "window.track" with expected arguments | ||
cy.get('@track').should('have.been.calledOnceWith', 'window.load') | ||
|
||
cy.reload() | ||
cy.reload() | ||
|
||
cy.get('@track') | ||
.should('have.been.calledThrice') | ||
// confirm every call was with "window.load" argument | ||
.invoke('getCalls') | ||
.should((calls) => { | ||
calls.forEach((trackCall, k) => { | ||
expect(trackCall.args, `call ${k + 1}`).to.deep.equal(['window.load']) | ||
}) | ||
}) | ||
}) | ||
|
||
it('works with reset', () => { | ||
stub = cy.stub().as('track') | ||
cy.visit('/') | ||
|
||
// make sure the page called the "window.track" with expected arguments | ||
cy.get('@track') | ||
.should('have.been.calledOnceWith', 'window.load') | ||
// cy.stub().reset() brings the counts back to 0 | ||
.invoke('reset') | ||
|
||
cy.reload() | ||
cy.reload() | ||
|
||
cy.get('@track').should('have.been.calledTwice') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters