|
| 1 | +/// <reference types="cypress" /> |
| 2 | +import { enterTodo, resetData, removeTodo } from '../../support/utils' |
| 3 | + |
| 4 | +describe('Stubbing window.track', () => { |
| 5 | + beforeEach(resetData) |
| 6 | + |
| 7 | + it('works on click', () => { |
| 8 | + cy.visit('/').then((win) => { |
| 9 | + cy.stub(win, 'track').as('track') |
| 10 | + }) |
| 11 | + enterTodo('write code') |
| 12 | + cy.get('@track').should( |
| 13 | + 'have.been.calledOnceWithExactly', |
| 14 | + 'todo.add', |
| 15 | + 'write code' |
| 16 | + ) |
| 17 | + }) |
| 18 | + |
| 19 | + it('tracks item delete', () => { |
| 20 | + cy.visit('/').then((win) => { |
| 21 | + cy.stub(win, 'track').as('track') |
| 22 | + }) |
| 23 | + enterTodo('write code') |
| 24 | + removeTodo('write code') |
| 25 | + |
| 26 | + cy.get('@track').should('have.been.calledWith', 'todo.remove', 'write code') |
| 27 | + }) |
| 28 | + |
| 29 | + it('stops working if window changes', () => { |
| 30 | + cy.visit('/').then((win) => { |
| 31 | + cy.stub(win, 'track').as('track') |
| 32 | + }) |
| 33 | + |
| 34 | + enterTodo('write code') |
| 35 | + cy.get('@track').should('be.calledOnce') |
| 36 | + |
| 37 | + cy.reload() |
| 38 | + enterTodo('write tests') |
| 39 | + // note that our stub was still called once |
| 40 | + // meaning the second todo was never counted |
| 41 | + cy.get('@track').should('be.calledOnce') |
| 42 | + }) |
| 43 | + |
| 44 | + it.only('adds stub after reload', () => { |
| 45 | + const trackStub = cy.stub().as('track') |
| 46 | + |
| 47 | + cy.visit('/').then((win) => { |
| 48 | + cy.stub(win, 'track').callsFake(trackStub) |
| 49 | + }) |
| 50 | + |
| 51 | + enterTodo('write code') |
| 52 | + cy.get('@track').should('be.calledOnce') |
| 53 | + |
| 54 | + cy.reload().then((win) => { |
| 55 | + cy.stub(win, 'track').callsFake(trackStub) |
| 56 | + }) |
| 57 | + enterTodo('write tests') |
| 58 | + // our stub is called twice: the second time |
| 59 | + // from the new window object after cy.reload() |
| 60 | + cy.get('@track').should('be.calledTwice') |
| 61 | + }) |
| 62 | + |
| 63 | + it('works on load', () => { |
| 64 | + cy.visit('/', { |
| 65 | + onBeforeLoad(win) { |
| 66 | + // there is no "window.track" yet, |
| 67 | + // thus we cannot stub just yet |
| 68 | + let track // the real track when set by the app |
| 69 | + let trackStub // our stub around the real track |
| 70 | + |
| 71 | + Object.defineProperty(win, 'track', { |
| 72 | + get() { |
| 73 | + return trackStub |
| 74 | + }, |
| 75 | + set(fn) { |
| 76 | + track = fn |
| 77 | + // give the created stub an alias so we can retrieve it later |
| 78 | + trackStub = cy.stub().callsFake(track).as('track') |
| 79 | + } |
| 80 | + }) |
| 81 | + } |
| 82 | + }) |
| 83 | + |
| 84 | + // make sure the page called the "window.track" with expected arguments |
| 85 | + cy.get('@track').should('have.been.calledOnceWith', 'window.load') |
| 86 | + }) |
| 87 | + |
| 88 | + it('works via event handler', () => { |
| 89 | + // there is no "window.track" yet, |
| 90 | + // thus we cannot stub just yet |
| 91 | + let track // the real track when set by the app |
| 92 | + let trackStub // our stub around the real track |
| 93 | + |
| 94 | + // use "cy.on" to prepare for "window.track" assignment |
| 95 | + // this code runs for every window creation, thus we |
| 96 | + // can track events from the "cy.reload()" |
| 97 | + cy.on('window:before:load', (win) => { |
| 98 | + Object.defineProperty(win, 'track', { |
| 99 | + get() { |
| 100 | + return trackStub |
| 101 | + }, |
| 102 | + set(fn) { |
| 103 | + // if the stub does not exist yet, create it |
| 104 | + if (!track) { |
| 105 | + track = fn |
| 106 | + // give the created stub an alias so we can retrieve it later |
| 107 | + trackStub = cy.stub().callsFake(track).as('track') |
| 108 | + } |
| 109 | + } |
| 110 | + }) |
| 111 | + }) |
| 112 | + |
| 113 | + cy.visit('/') |
| 114 | + |
| 115 | + // make sure the page called the "window.track" with expected arguments |
| 116 | + cy.get('@track').should('have.been.calledOnceWith', 'window.load') |
| 117 | + |
| 118 | + cy.reload() |
| 119 | + cy.reload() |
| 120 | + |
| 121 | + cy.get('@track') |
| 122 | + .should('have.been.calledThrice') |
| 123 | + // confirm every call was with "window.load" argument |
| 124 | + .invoke('getCalls') |
| 125 | + .should((calls) => { |
| 126 | + calls.forEach((trackCall, k) => { |
| 127 | + expect(trackCall.args, `call ${k + 1}`).to.deep.equal(['window.load']) |
| 128 | + }) |
| 129 | + }) |
| 130 | + }) |
| 131 | +}) |
0 commit comments