@@ -49,7 +49,7 @@ describe("trackerHoc", () => {
4949 it ( "should render component with trackedPromiseInProgress equals false when counter is 0" , ( ) => {
5050 // Arrange
5151 const TestSpinnerComponent = props => < span > test</ span > ;
52- trackPromiseAPI . getCounter = jest . fn ( ) . mockImplementation ( ( ) => 0 ) ;
52+ trackPromiseAPI . getCounter = jest . fn ( ) . mockReturnValue ( 0 ) ;
5353
5454 // Act
5555 const TrackedComponent = promiseTrackerHoc ( TestSpinnerComponent ) ;
@@ -64,7 +64,7 @@ describe("trackerHoc", () => {
6464 it ( "should render component with trackedPromiseInProgress equals false when counter is 0 and emit event with progress equals false" , ( ) => {
6565 // Arrange
6666 const TestSpinnerComponent = props => < span > test</ span > ;
67- trackPromiseAPI . getCounter = jest . fn ( ) . mockImplementation ( ( ) => 0 ) ;
67+ trackPromiseAPI . getCounter = jest . fn ( ) . mockReturnValue ( 0 ) ;
6868
6969 const progress = false ;
7070 const area = defaultArea ;
@@ -85,7 +85,7 @@ describe("trackerHoc", () => {
8585 it ( "should render component with trackedPromiseInProgress equals false when counter is 0 and emit event with progress equals false to different area" , ( ) => {
8686 // Arrange
8787 const TestSpinnerComponent = props => < span > test</ span > ;
88- trackPromiseAPI . getCounter = jest . fn ( ) . mockImplementation ( ( ) => 0 ) ;
88+ trackPromiseAPI . getCounter = jest . fn ( ) . mockReturnValue ( 0 ) ;
8989
9090 const progress = false ;
9191 const area = "otherArea" ;
@@ -106,7 +106,7 @@ describe("trackerHoc", () => {
106106 it ( "should render component with trackedPromiseInProgress equals true when counter is 0 and emit event with progress equals true" , ( ) => {
107107 // Arrange
108108 const TestSpinnerComponent = props => < span > test</ span > ;
109- trackPromiseAPI . getCounter = jest . fn ( ) . mockImplementation ( ( ) => 0 ) ;
109+ trackPromiseAPI . getCounter = jest . fn ( ) . mockReturnValue ( 0 ) ;
110110
111111 const progress = true ;
112112 const area = defaultArea ;
@@ -127,7 +127,7 @@ describe("trackerHoc", () => {
127127 it ( "should render component with trackedPromiseInProgress equals false when counter is 0 and emit event with progress equals true to different area" , ( ) => {
128128 // Arrange
129129 const TestSpinnerComponent = props => < span > test</ span > ;
130- trackPromiseAPI . getCounter = jest . fn ( ) . mockImplementation ( ( ) => 0 ) ;
130+ trackPromiseAPI . getCounter = jest . fn ( ) . mockReturnValue ( 0 ) ;
131131
132132 const progress = true ;
133133 const area = "otherArea" ;
@@ -148,7 +148,7 @@ describe("trackerHoc", () => {
148148 it ( "should render component with trackedPromiseInProgress equals true when counter is 1" , ( ) => {
149149 // Arrange
150150 const TestSpinnerComponent = props => < span > test</ span > ;
151- trackPromiseAPI . getCounter = jest . fn ( ) . mockImplementation ( ( ) => 1 ) ;
151+ trackPromiseAPI . getCounter = jest . fn ( ) . mockReturnValue ( 1 ) ;
152152
153153 // Act
154154 const TrackedComponent = promiseTrackerHoc ( TestSpinnerComponent ) ;
@@ -163,7 +163,7 @@ describe("trackerHoc", () => {
163163 it ( "should render component with trackedPromiseInProgress equals true when counter is 1 and emit event with progress equals true" , ( ) => {
164164 // Arrange
165165 const TestSpinnerComponent = props => < span > test</ span > ;
166- trackPromiseAPI . getCounter = jest . fn ( ) . mockImplementation ( ( ) => 1 ) ;
166+ trackPromiseAPI . getCounter = jest . fn ( ) . mockReturnValue ( 1 ) ;
167167
168168 const progress = true ;
169169 const area = defaultArea ;
@@ -184,7 +184,7 @@ describe("trackerHoc", () => {
184184 it ( "should render component with trackedPromiseInProgress equals true when counter is 1 and emit event with progress equals true to different area" , ( ) => {
185185 // Arrange
186186 const TestSpinnerComponent = props => < span > test</ span > ;
187- trackPromiseAPI . getCounter = jest . fn ( ) . mockImplementation ( ( ) => 1 ) ;
187+ trackPromiseAPI . getCounter = jest . fn ( ) . mockReturnValue ( 1 ) ;
188188
189189 const progress = true ;
190190 const area = "otherArea" ;
@@ -205,7 +205,7 @@ describe("trackerHoc", () => {
205205 it ( "should render component with trackedPromiseInProgress equals false when counter is 1 and emit event with progress equals false" , ( ) => {
206206 // Arrange
207207 const TestSpinnerComponent = props => < span > test</ span > ;
208- trackPromiseAPI . getCounter = jest . fn ( ) . mockImplementation ( ( ) => 1 ) ;
208+ trackPromiseAPI . getCounter = jest . fn ( ) . mockReturnValue ( 1 ) ;
209209
210210 const progress = false ;
211211 const area = defaultArea ;
@@ -226,7 +226,7 @@ describe("trackerHoc", () => {
226226 it ( "should render component with trackedPromiseInProgress equals true when counter is 1 and emit event with progress equals false to different area" , ( ) => {
227227 // Arrange
228228 const TestSpinnerComponent = props => < span > test</ span > ;
229- trackPromiseAPI . getCounter = jest . fn ( ) . mockImplementation ( ( ) => 1 ) ;
229+ trackPromiseAPI . getCounter = jest . fn ( ) . mockReturnValue ( 1 ) ;
230230
231231 const progress = false ;
232232 const area = "otherArea" ;
@@ -262,7 +262,7 @@ describe("trackerHoc", () => {
262262 it ( "should render component with trackedPromiseInProgress equals false when counter is 0 and delay equals 300" , ( ) => {
263263 // Arrange
264264 const TestSpinnerComponent = props => < span > test</ span > ;
265- trackPromiseAPI . getCounter = jest . fn ( ) . mockImplementation ( ( ) => 0 ) ;
265+ trackPromiseAPI . getCounter = jest . fn ( ) . mockReturnValue ( 0 ) ;
266266
267267 // Act
268268 const TrackedComponent = promiseTrackerHoc ( TestSpinnerComponent ) ;
@@ -277,7 +277,7 @@ describe("trackerHoc", () => {
277277 it ( "should render component with trackedPromiseInProgress equals false when counter is 0 and emit event with progress equals false and delay equals 300" , ( ) => {
278278 // Arrange
279279 const TestSpinnerComponent = props => < span > test</ span > ;
280- trackPromiseAPI . getCounter = jest . fn ( ) . mockImplementation ( ( ) => 0 ) ;
280+ trackPromiseAPI . getCounter = jest . fn ( ) . mockReturnValue ( 0 ) ;
281281
282282 const progress = false ;
283283 const area = defaultArea ;
@@ -298,7 +298,7 @@ describe("trackerHoc", () => {
298298 it ( "should render component with trackedPromiseInProgress equals false when counter is 0 and emit event with progress equals false to different area and delay equals 300" , ( ) => {
299299 // Arrange
300300 const TestSpinnerComponent = props => < span > test</ span > ;
301- trackPromiseAPI . getCounter = jest . fn ( ) . mockImplementation ( ( ) => 0 ) ;
301+ trackPromiseAPI . getCounter = jest . fn ( ) . mockReturnValue ( 0 ) ;
302302
303303 const progress = false ;
304304 const area = "otherArea" ;
@@ -319,7 +319,8 @@ describe("trackerHoc", () => {
319319 it ( "should render component with trackedPromiseInProgress equals true when counter is 0 and emit event with progress equals true and delay equals 300" , ( ) => {
320320 // Arrange
321321 const TestSpinnerComponent = props => < span > test</ span > ;
322- trackPromiseAPI . getCounter = jest . fn ( ) . mockImplementation ( ( ) => 0 ) ;
322+ trackPromiseAPI . getCounter = jest . fn ( ) . mockReturnValueOnce ( 0 )
323+ . mockReturnValueOnce ( 1 ) ;
323324
324325 const progress = true ;
325326 const area = defaultArea ;
@@ -343,7 +344,7 @@ describe("trackerHoc", () => {
343344 it ( "should render component with trackedPromiseInProgress equals false when counter is 0 and emit event with progress equals true to different area and delay equals 300" , ( ) => {
344345 // Arrange
345346 const TestSpinnerComponent = props => < span > test</ span > ;
346- trackPromiseAPI . getCounter = jest . fn ( ) . mockImplementation ( ( ) => 0 ) ;
347+ trackPromiseAPI . getCounter = jest . fn ( ) . mockReturnValue ( 0 ) ;
347348
348349 const progress = true ;
349350 const area = "otherArea" ;
@@ -364,7 +365,7 @@ describe("trackerHoc", () => {
364365 it ( "should render component with trackedPromiseInProgress equals true when counter is 1 and delay equals 300" , ( ) => {
365366 // Arrange
366367 const TestSpinnerComponent = props => < span > test</ span > ;
367- trackPromiseAPI . getCounter = jest . fn ( ) . mockImplementation ( ( ) => 1 ) ;
368+ trackPromiseAPI . getCounter = jest . fn ( ) . mockReturnValue ( 1 ) ;
368369
369370 // Act
370371 const TrackedComponent = promiseTrackerHoc ( TestSpinnerComponent ) ;
@@ -379,7 +380,7 @@ describe("trackerHoc", () => {
379380 it ( "should render component with trackedPromiseInProgress equals true when counter is 1 and emit event with progress equals true and delay equals 300" , ( ) => {
380381 // Arrange
381382 const TestSpinnerComponent = props => < span > test</ span > ;
382- trackPromiseAPI . getCounter = jest . fn ( ) . mockImplementation ( ( ) => 1 ) ;
383+ trackPromiseAPI . getCounter = jest . fn ( ) . mockReturnValue ( 1 ) ;
383384
384385 const progress = true ;
385386 const area = defaultArea ;
@@ -400,7 +401,7 @@ describe("trackerHoc", () => {
400401 it ( "should render component with trackedPromiseInProgress equals true when counter is 1 and emit event with progress equals true to different area and delay equals 300" , ( ) => {
401402 // Arrange
402403 const TestSpinnerComponent = props => < span > test</ span > ;
403- trackPromiseAPI . getCounter = jest . fn ( ) . mockImplementation ( ( ) => 1 ) ;
404+ trackPromiseAPI . getCounter = jest . fn ( ) . mockReturnValue ( 1 ) ;
404405
405406 const progress = true ;
406407 const area = "otherArea" ;
@@ -421,7 +422,7 @@ describe("trackerHoc", () => {
421422 it ( "should render component with trackedPromiseInProgress equals false when counter is 1 and emit event with progress equals false and delay equals 300" , ( ) => {
422423 // Arrange
423424 const TestSpinnerComponent = props => < span > test</ span > ;
424- trackPromiseAPI . getCounter = jest . fn ( ) . mockImplementation ( ( ) => 1 ) ;
425+ trackPromiseAPI . getCounter = jest . fn ( ) . mockReturnValue ( 1 ) ;
425426
426427 const progress = false ;
427428 const area = defaultArea ;
@@ -442,7 +443,7 @@ describe("trackerHoc", () => {
442443 it ( "should render component with trackedPromiseInProgress equals true when counter is 1 and emit event with progress equals false to different area and delay equals 300" , ( ) => {
443444 // Arrange
444445 const TestSpinnerComponent = props => < span > test</ span > ;
445- trackPromiseAPI . getCounter = jest . fn ( ) . mockImplementation ( ( ) => 1 ) ;
446+ trackPromiseAPI . getCounter = jest . fn ( ) . mockReturnValue ( 1 ) ;
446447
447448 const progress = false ;
448449 const area = "otherArea" ;
@@ -482,7 +483,6 @@ describe("trackerHoc", () => {
482483
483484 const getCounterStub = jest
484485 . spyOn ( trackPromiseAPI , "getCounter" )
485- . mockReturnValue ( 0 ) ;
486486
487487 // Act
488488 const TrackedComponent = promiseTrackerHoc ( TestSpinnerComponent ) ;
@@ -531,5 +531,108 @@ describe("trackerHoc", () => {
531531 done ( ) ;
532532 } ) ;
533533 } ) ;
534+
535+ it ( "should render <h1>SPINNER</h2> when counter is 1, delay is set to 1000 and promise has 2000 timeout" , done => {
536+ // Arrange
537+ const TestSpinnerComponent = props => {
538+ return (
539+ < div >
540+ { props . promiseInProgress ? < h1 > SPINNER</ h1 > : < h2 > NO SPINNER</ h2 > }
541+ </ div >
542+ ) ;
543+ } ;
544+
545+ const getCounterStub = jest
546+ . spyOn ( trackPromiseAPI , "getCounter" ) ;
547+
548+ // Act
549+ const TrackedComponent = promiseTrackerHoc ( TestSpinnerComponent ) ;
550+ const component = mount ( < TrackedComponent config = { { delay : 1000 } } /> ) ;
551+
552+ expect ( component . text ( ) ) . toEqual ( "NO SPINNER" ) ;
553+ expect ( getCounterStub ) . toHaveBeenCalled ( ) ;
554+
555+ // Assert
556+ const myFakePromise = new Promise ( resolve => {
557+ setTimeout ( ( ) => {
558+ resolve ( true ) ;
559+ } , 2000 ) ;
560+ } ) ;
561+
562+ trackPromiseAPI . trackPromise ( myFakePromise ) ;
563+
564+ jest . advanceTimersByTime ( 500 ) ;
565+ expect ( component . text ( ) ) . toEqual ( "NO SPINNER" ) ;
566+
567+ // Total advance 1000
568+ jest . advanceTimersByTime ( 500 ) ;
569+ expect ( component . text ( ) ) . toEqual ( "SPINNER" ) ;
570+
571+ // Total advance 1999
572+ jest . advanceTimersByTime ( 999 ) ;
573+ expect ( component . text ( ) ) . toEqual ( "SPINNER" ) ;
574+
575+ // After the promise get's resolved
576+ jest . runAllTimers ( ) ;
577+
578+ myFakePromise . then ( ( ) => {
579+ expect ( component . text ( ) ) . toEqual ( "NO SPINNER" ) ;
580+
581+ // Total advance 2010
582+ jest . advanceTimersByTime ( 11 ) ;
583+ expect ( component . text ( ) ) . toEqual ( "NO SPINNER" ) ;
584+
585+ done ( ) ;
586+ } ) ;
587+ } ) ;
588+
589+ it ( "should render <h1>NO SPINNER</h2> when counter is 1, delay is set to 2000 and promise has 1000 timeout" , done => {
590+ // Arrange
591+ const TestSpinnerComponent = props => {
592+ return (
593+ < div >
594+ { props . promiseInProgress ? < h1 > SPINNER</ h1 > : < h2 > NO SPINNER</ h2 > }
595+ </ div >
596+ ) ;
597+ } ;
598+
599+ const getCounterStub = jest
600+ . spyOn ( trackPromiseAPI , "getCounter" ) ;
601+
602+ // Act
603+ const TrackedComponent = promiseTrackerHoc ( TestSpinnerComponent ) ;
604+ const component = mount ( < TrackedComponent config = { { delay : 2000 } } /> ) ;
605+
606+ expect ( component . text ( ) ) . toEqual ( "NO SPINNER" ) ;
607+ expect ( getCounterStub ) . toHaveBeenCalled ( ) ;
608+
609+ // Assert
610+ const myFakePromise = new Promise ( resolve => {
611+ setTimeout ( ( ) => {
612+ resolve ( true ) ;
613+ } , 1000 ) ;
614+ } ) ;
615+
616+ trackPromiseAPI . trackPromise ( myFakePromise ) ;
617+
618+ jest . advanceTimersByTime ( 500 ) ;
619+ expect ( component . text ( ) ) . toEqual ( "NO SPINNER" ) ;
620+
621+ // Total advance 1000
622+ jest . advanceTimersByTime ( 500 ) ;
623+ expect ( component . text ( ) ) . toEqual ( "NO SPINNER" ) ;
624+
625+ myFakePromise . then ( ( ) => {
626+ expect ( component . text ( ) ) . toEqual ( "NO SPINNER" ) ;
627+
628+ // Total advance 1999
629+ jest . advanceTimersByTime ( 999 ) ;
630+ expect ( component . text ( ) ) . toEqual ( "NO SPINNER" ) ;
631+
632+ // After the promise get's resolved
633+ jest . runAllTimers ( ) ;
634+ done ( ) ;
635+ } ) ;
636+ } ) ;
534637 } ) ;
535638} ) ;
0 commit comments