1- import { call , entry , on , start } from "./index" ;
1+ import { call , entry , exit , on , start } from "./index" ;
22
33const fetch = jest . fn ( ) ;
44beforeEach ( fetch . mockClear ) ;
55
6+ const finishedLoading = jest . fn ( ) ;
7+ beforeEach ( finishedLoading . mockClear ) ;
8+
69describe ( "Machine with entry" , ( ) => {
710 function Loader ( ) {
811 function * idle ( ) {
912 yield on ( "FETCH" , loading ) ;
1013 }
1114 function * loading ( ) {
1215 yield entry ( fetchData ) ;
16+ yield exit ( finishedLoading ) ;
1317 yield on ( "SUCCESS" , success ) ;
1418 yield on ( "FAILURE" , failure ) ;
1519 }
@@ -47,16 +51,21 @@ describe("Machine with entry", () => {
4751
4852 const transitionResult = loader . next ( "FETCH" ) ;
4953 expect ( fetch ) . toHaveBeenCalledWith ( "https://example.org/" ) ;
50- expect ( transitionResult . actions ) . toEqual ( [ { type : "entry" , f : fetchData } ] ) ;
54+ expect ( transitionResult . actions ) . toEqual ( [
55+ { type : "entry" , f : fetchData } ,
56+ ] ) ;
5157 expect ( loader . value ) . toEqual ( "loading" ) ;
5258 expect ( loader . changeCount ) . toEqual ( 1 ) ;
59+ expect ( finishedLoading ) . toHaveBeenCalledTimes ( 0 ) ;
5360
5461 await expect ( loader . resolved ) . resolves . toEqual ( [ 42 ] ) ;
5562 await expect ( Promise . resolve ( transitionResult ) ) . resolves . toEqual ( [ 42 ] ) ;
63+ expect ( finishedLoading ) . toHaveBeenCalledTimes ( 1 ) ;
5664 expect ( loader . changeCount ) . toEqual ( 2 ) ;
5765 expect ( loader . value ) . toEqual ( "success" ) ;
58-
59- loader . next ( "FETCH" ) ;
66+
67+ const transitionResult2 = loader . next ( "FETCH" ) ;
68+ expect ( transitionResult2 . actions ) . toEqual ( [ ] ) ;
6069 expect ( loader . changeCount ) . toEqual ( 2 ) ;
6170 expect ( loader . value ) . toEqual ( "success" ) ;
6271
@@ -73,25 +82,26 @@ describe("Machine with entry", () => {
7382 const loader = start ( Loader , [ { url : someURL } ] ) ;
7483 expect ( loader . value ) . toEqual ( "idle" ) ;
7584
76- const { actions } = loader . next ( "FETCH" ) ;
85+ const transitionResult = loader . next ( "FETCH" ) ;
7786 expect ( fetch ) . toHaveBeenCalledTimes ( 1 ) ;
7887 expect ( fetch ) . toHaveBeenLastCalledWith ( "https://example.org/" ) ;
79- expect ( actions ) . toEqual ( [ { type : "entry" , f : fetchData } ] ) ;
88+ expect ( transitionResult . actions ) . toEqual ( [ { type : "entry" , f : fetchData } ] ) ;
8089 expect ( loader . value ) . toEqual ( "loading" ) ;
8190 expect ( loader . changeCount ) . toEqual ( 1 ) ;
8291
8392 await expect ( loader . resolved ) . rejects . toEqual ( new Error ( "Failed!" ) ) ;
93+ await expect ( Promise . resolve ( transitionResult ) ) . rejects . toEqual ( new Error ( "Failed!" ) ) ;
8494 expect ( loader . changeCount ) . toEqual ( 2 ) ;
8595 expect ( loader . value ) . toEqual ( "failure" ) ;
8696
8797 loader . next ( "FETCH" ) ;
8898 expect ( fetch ) . toHaveBeenCalledTimes ( 1 ) ;
8999 expect ( loader . changeCount ) . toEqual ( 2 ) ;
90-
100+
91101 loader . next ( "RETRY" ) ;
92102 expect ( loader . value ) . toEqual ( "loading" ) ;
93103 expect ( loader . changeCount ) . toEqual ( 3 ) ;
94-
104+
95105 expect ( fetch ) . toHaveBeenCalledTimes ( 2 ) ;
96106 expect ( fetch ) . toHaveBeenLastCalledWith ( "https://example.org/" ) ;
97107
0 commit comments