1- import { call , entry , exit , on , start } from "./index" ;
1+ import { always , call , cond , entry , exit , on , start } from "./index" ;
22
33const fetch = jest . fn ( ) ;
44beforeEach ( fetch . mockClear ) ;
@@ -7,6 +7,11 @@ const finishedLoading = jest.fn();
77beforeEach ( finishedLoading . mockClear ) ;
88
99describe ( "Machine with entry and exit actions" , ( ) => {
10+ const someURL = new URL ( "https://example.org/" ) ;
11+ function fetchData ( ) {
12+ return fetch ( someURL . toString ( ) ) ;
13+ }
14+
1015 function Loader ( ) {
1116 function * idle ( ) {
1217 yield on ( "FETCH" , loading ) ;
@@ -25,11 +30,6 @@ describe("Machine with entry and exit actions", () => {
2530 return idle ;
2631 }
2732
28- const someURL = new URL ( "https://example.org/" ) ;
29- function fetchData ( ) {
30- return fetch ( someURL . toString ( ) ) ;
31- }
32-
3333 test ( "creating" , ( ) => {
3434 const loader = start ( Loader ) ;
3535 expect ( loader ) . toBeDefined ( ) ;
@@ -85,12 +85,16 @@ describe("Machine with entry and exit actions", () => {
8585 const transitionResult = loader . next ( "FETCH" ) ;
8686 expect ( fetch ) . toHaveBeenCalledTimes ( 1 ) ;
8787 expect ( fetch ) . toHaveBeenLastCalledWith ( "https://example.org/" ) ;
88- expect ( transitionResult . actions ) . toEqual ( [ { type : "entry" , f : fetchData } ] ) ;
88+ expect ( transitionResult . actions ) . toEqual ( [
89+ { type : "entry" , f : fetchData } ,
90+ ] ) ;
8991 expect ( loader . value ) . toEqual ( "loading" ) ;
9092 expect ( loader . changeCount ) . toEqual ( 1 ) ;
9193
9294 await expect ( loader . resolved ) . rejects . toEqual ( new Error ( "Failed!" ) ) ;
93- await expect ( Promise . resolve ( transitionResult ) ) . rejects . toEqual ( new Error ( "Failed!" ) ) ;
95+ await expect ( Promise . resolve ( transitionResult ) ) . rejects . toEqual (
96+ new Error ( "Failed!" )
97+ ) ;
9498 expect ( loader . changeCount ) . toEqual ( 2 ) ;
9599 expect ( loader . value ) . toEqual ( "failure" ) ;
96100
@@ -112,6 +116,92 @@ describe("Machine with entry and exit actions", () => {
112116 } ) ;
113117} ) ;
114118
119+ describe ( "Form Field Machine with entry and exit actions" , ( ) => {
120+ // const validate = jest.fn();
121+ // beforeEach(validate.mockClear);
122+ const isValid = jest . fn ( ) ;
123+ beforeEach ( isValid . mockClear ) ;
124+
125+ function FormField ( ) {
126+ function * initial ( ) {
127+ yield on ( "CHANGE" , editing ) ;
128+ }
129+ function * editing ( ) {
130+ // yield exit(validate);
131+ yield on ( "CHANGE" , editing ) ;
132+ yield on ( "BLUR" , validating ) ;
133+ }
134+ function * validating ( ) {
135+ yield always ( cond ( isValid , valid ) ) ;
136+ yield always ( invalid ) ;
137+
138+ // yield on(null, cond(isValid, valid));
139+ // yield on(null, invalid);
140+
141+ // yield always([cond(isValid, valid), invalid]);
142+ // return [cond(isValid, valid), invalid];
143+ // return conds([[isValid, valid], [true, invalid]]);
144+ }
145+ function * invalid ( ) { }
146+ function * valid ( ) { }
147+
148+ return initial ;
149+ }
150+
151+ test ( "creating" , ( ) => {
152+ const formField = start ( FormField ) ;
153+ expect ( formField ) . toBeDefined ( ) ;
154+ } ) ;
155+
156+ describe ( "when is valid" , ( ) => {
157+ beforeEach ( ( ) => {
158+ isValid . mockReturnValue ( true ) ;
159+ } ) ;
160+
161+ test ( "sending events" , ( ) => {
162+ const formField = start ( FormField ) ;
163+ expect ( formField ) . toBeDefined ( ) ;
164+ expect ( formField . value ) . toEqual ( "initial" ) ;
165+
166+ formField . next ( "CHANGE" ) ;
167+ expect ( formField . value ) . toEqual ( "editing" ) ;
168+ expect ( formField . changeCount ) . toEqual ( 1 ) ;
169+
170+ formField . next ( "CHANGE" ) ;
171+ expect ( formField . value ) . toEqual ( "editing" ) ;
172+ expect ( formField . changeCount ) . toEqual ( 2 ) ;
173+
174+ formField . next ( "BLUR" ) ;
175+ expect ( formField . value ) . toEqual ( "valid" ) ;
176+ expect ( formField . changeCount ) . toEqual ( 4 ) ;
177+ } ) ;
178+ } ) ;
179+
180+ describe ( "when is invalid" , ( ) => {
181+ beforeEach ( ( ) => {
182+ isValid . mockReturnValue ( false ) ;
183+ } ) ;
184+
185+ test ( "sending events" , ( ) => {
186+ const formField = start ( FormField ) ;
187+ expect ( formField ) . toBeDefined ( ) ;
188+ expect ( formField . value ) . toEqual ( "initial" ) ;
189+
190+ formField . next ( "CHANGE" ) ;
191+ expect ( formField . value ) . toEqual ( "editing" ) ;
192+ expect ( formField . changeCount ) . toEqual ( 1 ) ;
193+
194+ formField . next ( "CHANGE" ) ;
195+ expect ( formField . value ) . toEqual ( "editing" ) ;
196+ expect ( formField . changeCount ) . toEqual ( 2 ) ;
197+
198+ formField . next ( "BLUR" ) ;
199+ expect ( formField . value ) . toEqual ( "invalid" ) ;
200+ expect ( formField . changeCount ) . toEqual ( 4 ) ;
201+ } ) ;
202+ } ) ;
203+ } ) ;
204+
115205describe ( "Machine with call" , ( ) => {
116206 function Loader ( { url } : { url : URL } ) {
117207 function * idle ( ) {
0 commit comments