@@ -136,7 +136,8 @@ describe("Form Field Machine with always()", () => {
136136 yield on ( "BLUR" , validating ) ;
137137 }
138138 function * validating ( ) {
139- yield always ( cond ( isValid , valid ) ) ;
139+ yield cond ( isValid , valid ) ;
140+ // yield cond(true, invalid);
140141 yield always ( invalid ) ;
141142 }
142143 function * invalid ( ) {
@@ -247,9 +248,9 @@ describe("Hierarchical Traffic Lights Machine", () => {
247248 expect ( machine . changeCount ) . toEqual ( 1 ) ;
248249
249250 machine . next ( "TIMER" ) ;
250- // expect(machine.current).toEqual("red");
251- // expect(machine.current).toEqual(["red", "walk"]);
252251 expect ( machine . current ) . toEqual ( { "red" : "walk" } ) ;
252+ // expect(machine.current).toEqual([["red", "walk"]]); // Like a Map key
253+ // expect(machine.currentMap).toEqual(new Map([["red", "walk"]]));
253254 expect ( machine . changeCount ) . toEqual ( 3 ) ;
254255
255256 machine . next ( "TIMER" ) ;
@@ -265,3 +266,116 @@ describe("Hierarchical Traffic Lights Machine", () => {
265266 expect ( machine . changeCount ) . toEqual ( 7 ) ;
266267 } ) ;
267268} ) ;
269+
270+ describe ( "Switch" , ( ) => {
271+ function * Switch ( ) {
272+ function * OFF ( ) {
273+ yield on ( "FLICK" , ON ) ;
274+ }
275+ function * ON ( ) {
276+ yield on ( "FLICK" , OFF ) ;
277+ }
278+
279+ return OFF ;
280+ }
281+
282+ test ( "sending events" , ( ) => {
283+ const machine = start ( Switch ) ;
284+ expect ( machine ) . toBeDefined ( ) ;
285+ expect ( machine . current ) . toEqual ( "OFF" ) ;
286+
287+ machine . next ( "FLICK" ) ;
288+ expect ( machine . current ) . toEqual ( "ON" ) ;
289+ expect ( machine . changeCount ) . toEqual ( 1 ) ;
290+
291+ machine . next ( "FLICK" ) ;
292+ expect ( machine . current ) . toEqual ( "OFF" ) ;
293+ expect ( machine . changeCount ) . toEqual ( 2 ) ;
294+ } ) ;
295+ } ) ;
296+
297+ /*describe("Counter", () => {
298+ function* Counter() {
299+ function* initial() {
300+ yield entry(function counter() { return 0 });
301+ }
302+ function* positive() {
303+ yield entry(function counter(n) {
304+ console.log({ n });
305+ return 1
306+ });
307+ // yield on("INCREMENT", action(function counter(n) { return n + 1 }));
308+ }
309+
310+ // yield association(function *(events) {
311+ // let n = 0;
312+ // yield n;
313+
314+ // for (const event of events()) {
315+ // if (event.type === "INCREMENT") {
316+ // n += 1;
317+ // yield n;
318+ // }
319+ // }
320+ // })
321+
322+ yield on("RESET", compound(initial));
323+ yield on("INCREMENT", compound(positive));
324+
325+ // const counter = yield reducer("counter", 0, n => n + 1);
326+ // const counter = yield reducer(0, {
327+ // increment: n => n + 1,
328+ // reset: () => 0
329+ // });
330+
331+ // yield on("INCREMENT", counter.increment);
332+ // yield on("RESET", counter.reset);
333+
334+ //////////
335+
336+ const Counter = yield atom(0);
337+ yield output("counter", Counter);
338+ // yield output("counter", Counter(n => n));
339+ // yield output("counter", Counter, n => n);
340+
341+ // const Counter = yield atom(function counter() {
342+ // return 0;
343+ // });
344+
345+ // const { increment, reset } = (yield atom(function counter() {
346+ // return 0;
347+ // }))({ increment: (n) => n + 1, reset: () => 0 });
348+
349+
350+ yield on("INCREMENT", Counter(n => n + 1));
351+ yield on("RESET", Counter(0));
352+
353+ // yield on("INCREMENT", counter(n => n + 1));
354+ // yield on("RESET", counter(() => 0));
355+
356+ return initial;
357+ }
358+
359+ test("sending events", () => {
360+ const machine = start(Counter);
361+ expect(machine.current).toEqual("initial");
362+ expect(machine.results).resolves.toEqual({ counter: 0 });
363+
364+ machine.next("INCREMENT");
365+ expect(machine.current).toEqual("positive");
366+ expect(machine.changeCount).toEqual(1);
367+ expect(machine.results).resolves.toEqual({ counter: 1 });
368+
369+ machine.next("INCREMENT");
370+ expect(machine.current).toEqual("positive");
371+ expect(machine.changeCount).toEqual(1);
372+ expect(machine.results).resolves.toEqual({ counter: 1 });
373+
374+ machine.next("RESET");
375+ expect(machine.current).toEqual("initial");
376+ expect(machine.changeCount).toEqual(2);
377+ expect(machine.results).resolves.toEqual({ counter: 0 });
378+
379+
380+ });
381+ });*/
0 commit comments