@@ -26,6 +26,25 @@ describe('Stubbing window.track', () => {
2626 cy . get ( '@track' ) . should ( 'have.been.calledWith' , 'todo.remove' , 'write code' )
2727 } )
2828
29+ it ( 'resets the count' , ( ) => {
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+ enterTodo ( 'write tests' )
38+ cy . get ( '@track' )
39+ . should ( 'be.calledTwice' )
40+ // reset the stub
41+ . invoke ( 'reset' )
42+
43+ cy . get ( '@track' ) . should ( 'not.be.called' )
44+ enterTodo ( 'control the state' )
45+ cy . get ( '@track' ) . should ( 'be.calledOnce' )
46+ } )
47+
2948 it ( 'stops working if window changes' , ( ) => {
3049 cy . visit ( '/' ) . then ( ( win ) => {
3150 cy . stub ( win , 'track' ) . as ( 'track' )
@@ -36,12 +55,16 @@ describe('Stubbing window.track', () => {
3655
3756 cy . reload ( )
3857 enterTodo ( 'write tests' )
58+
59+ /* eslint-disable-next-line cypress/no-unnecessary-waiting */
60+ cy . wait ( 500 ) // wait just in case the call happens late
61+
3962 // note that our stub was still called once
4063 // meaning the second todo was never counted
4164 cy . get ( '@track' ) . should ( 'be.calledOnce' )
4265 } )
4366
44- it . only ( 'adds stub after reload' , ( ) => {
67+ it ( 'adds stub after reload' , ( ) => {
4568 const trackStub = cy . stub ( ) . as ( 'track' )
4669
4770 cy . visit ( '/' ) . then ( ( win ) => {
@@ -84,48 +107,4 @@ describe('Stubbing window.track', () => {
84107 // make sure the page called the "window.track" with expected arguments
85108 cy . get ( '@track' ) . should ( 'have.been.calledOnceWith' , 'window.load' )
86109 } )
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- } )
131110} )
0 commit comments