@@ -2,7 +2,6 @@ import React from "react";
22import { promiseTrackerHoc } from "./trackerHoc" ;
33import * as trackPromiseAPI from "./trackPromise" ;
44import { defaultArea } from "./constants" ;
5- import { act } from "react-dom/test-utils" ; // ES6
65
76describe ( "trackerHoc" , ( ) => {
87 describe ( "Initial Status" , ( ) => {
@@ -471,90 +470,65 @@ describe("trackerHoc", () => {
471470 jest . useRealTimers ( ) ;
472471 } ) ;
473472
474- it ( "should render <h1>NO SPINNER</h2> when counter is 1 but delay is set to 200 (before timing out)" , ( ) => {
473+ it ( "should render <h1>NO SPINNER</h2> when counter is 1 but delay is set to 300 (before timing out)" , done => {
475474 // Arrange
476- let TestSpinnerComponent = null ;
477- act ( ( ) => {
478- TestSpinnerComponent = props => {
479- return (
480- < div >
481- { props . promiseInProgress ? < h1 > SPINNER</ h1 > : < h2 > NO SPINNER</ h2 > }
482- </ div >
483- ) ;
484- } ;
485-
486- trackPromiseAPI . getCounter = jest . fn ( ) . mockImplementation ( ( ) => 0 ) ;
487- } ) ;
475+ const TestSpinnerComponent = props => {
476+ return (
477+ < div >
478+ { props . promiseInProgress ? < h1 > SPINNER</ h1 > : < h2 > NO SPINNER</ h2 > }
479+ </ div >
480+ ) ;
481+ } ;
482+
483+ const getCounterStub = jest
484+ . spyOn ( trackPromiseAPI , "getCounter" )
485+ . mockReturnValue ( 0 ) ;
488486
489487 // Act
490- let component = null ;
491- act ( ( ) => {
492- const TrackedComponent = promiseTrackerHoc ( TestSpinnerComponent ) ;
493- component = mount ( < TrackedComponent config = { { delay : 300 } } /> ) ;
494- } ) ;
488+ const TrackedComponent = promiseTrackerHoc ( TestSpinnerComponent ) ;
489+ const component = mount ( < TrackedComponent config = { { delay : 300 } } /> ) ;
495490
496491 // Check very beginning (no promises going on) NO SPINNER is shown
497492 // TODO: this assert could be skipped (move to another test)
498- expect ( component . text ( ) ) . toMatch ( "NO SPINNER" ) ;
499- expect ( trackPromiseAPI . getCounter ) . toHaveBeenCalled ( ) ;
493+ expect ( component . text ( ) ) . toEqual ( "NO SPINNER" ) ;
494+ expect ( getCounterStub ) . toHaveBeenCalled ( ) ;
500495
501496 // Assert
502497 // This promise will resolved after 1 seconds, by doing this
503498 // we will be able to test 2 scenarios:
504499 // [0] first 200ms spinner won't be shown (text NOSPINNER)
505500 // [1] after 200ms spinner will be shown (text SPINNER)
506501 // [2] after 1000ms spinner will be hidded again (text NOSPINNER)
507- let myFakePromise = null ;
508-
509- act ( ( ) => {
510- myFakePromise = new Promise ( resolve => {
511- setTimeout ( ( ) => {
512- resolve ( true ) ;
513- } , 1000 ) ;
514- } ) ;
515- } ) ;
516-
517- act ( ( ) => {
518- trackPromiseAPI . trackPromise ( myFakePromise ) ;
519502
520- // Runs all pending timers. whether it's a second from now or a year.
521- // https://jestjs.io/docs/en/timer-mocks.html
522- //jest.advanceTimersByTime(300);
503+ const myFakePromise = new Promise ( resolve => {
504+ setTimeout ( ( ) => {
505+ resolve ( true ) ;
506+ } , 1000 ) ;
523507 } ) ;
524508
525- act ( ( ) => {
526- // Runs all pending timers. whether it's a second from now or a year.
527- // https://jestjs.io/docs/en/timer-mocks.html
528- jest . advanceTimersByTime ( 100 ) ;
529- } ) ;
509+ trackPromiseAPI . trackPromise ( myFakePromise ) ;
510+
511+ jest . advanceTimersByTime ( 100 ) ;
530512
531513 // [0] first 200ms spinner won't be shown (text NOSPINNER)
532- expect ( component . text ( ) ) . toMatch ( "NO SPINNER" ) ;
514+ expect ( component . text ( ) ) . toEqual ( "NO SPINNER" ) ;
533515
534- act ( ( ) => {
535- // Runs all pending timers. whether it's a second from now or a year.
536- // https://jestjs.io/docs/en/timer-mocks.html
537- jest . advanceTimersByTime ( 300 ) ;
538- } ) ;
516+ jest . advanceTimersByTime ( 300 ) ;
539517
540518 // Before the promise get's resolved
541519 // [1] after 200ms spinner will be shown (text SPINNER)
542- expect ( component . text ( ) ) . toMatch ( "SPINNER" ) ;
520+ expect ( component . text ( ) ) . toEqual ( "SPINNER" ) ;
543521
544522 // After the promise get's resolved
545-
546- act ( ( ) => {
547- jest . advanceTimersByTime ( 1500 ) ;
548- //jest.runAllTimers();
549- } ) ;
523+ jest . runAllTimers ( ) ;
550524
551525 // [2] after 1000ms spinner will be hidded again (text NOSPINNER)
552526 // Wait for fakePromise (simulated ajax call) to be completed
553527 // no spinner should be shown
554- act ( ( ) => {
555- myFakePromise . then ( ( ) => {
556- expect ( component . text ( ) ) . toMatch ( "NO SPINNER" ) ;
557- } ) ;
528+
529+ myFakePromise . then ( ( ) => {
530+ expect ( component . text ( ) ) . toEqual ( "NO SPINNER" ) ;
531+ done ( ) ;
558532 } ) ;
559533 } ) ;
560534 } ) ;
0 commit comments