1
- import { always , compound , cond , entry , exit , on , start } from "./index" ;
1
+ /**
2
+ * @jest -environment jsdom
3
+ */
4
+
5
+ import {
6
+ always ,
7
+ compound ,
8
+ cond ,
9
+ entry ,
10
+ exit ,
11
+ on ,
12
+ listenTo ,
13
+ send ,
14
+ start ,
15
+ } from "./index" ;
16
+
17
+ test ( "node version " + process . version , ( ) => { } ) ;
2
18
3
19
const fetch = jest . fn ( ) ;
4
20
beforeEach ( fetch . mockClear ) ;
@@ -64,7 +80,9 @@ describe("Machine with entry and exit actions", () => {
64
80
expect ( finishedLoading ) . toHaveBeenCalledTimes ( 0 ) ;
65
81
66
82
await expect ( loader . results ) . resolves . toEqual ( { fetchData : 42 } ) ;
67
- await expect ( Promise . resolve ( transitionResult ) ) . resolves . toEqual ( { fetchData : 42 } ) ;
83
+ await expect ( Promise . resolve ( transitionResult ) ) . resolves . toEqual ( {
84
+ fetchData : 42 ,
85
+ } ) ;
68
86
expect ( finishedLoading ) . toHaveBeenCalledTimes ( 1 ) ;
69
87
expect ( loader . changeCount ) . toEqual ( 2 ) ;
70
88
expect ( loader . current ) . toEqual ( "success" ) ;
@@ -130,7 +148,7 @@ describe.skip("Fetch with abort signal", () => {
130
148
}
131
149
132
150
function Loader ( ) {
133
- const aborterKey = Symbol ( ' aborter' ) ;
151
+ const aborterKey = Symbol ( " aborter" ) ;
134
152
// yield register(aborterKey, () => new AbortController());
135
153
// yield register(function aborter() { return new AbortController() });
136
154
@@ -188,7 +206,9 @@ describe.skip("Fetch with abort signal", () => {
188
206
expect ( finishedLoading ) . toHaveBeenCalledTimes ( 0 ) ;
189
207
190
208
await expect ( loader . results ) . resolves . toEqual ( { fetchData : 42 } ) ;
191
- await expect ( Promise . resolve ( transitionResult ) ) . resolves . toEqual ( { fetchData : 42 } ) ;
209
+ await expect ( Promise . resolve ( transitionResult ) ) . resolves . toEqual ( {
210
+ fetchData : 42 ,
211
+ } ) ;
192
212
expect ( finishedLoading ) . toHaveBeenCalledTimes ( 1 ) ;
193
213
expect ( loader . changeCount ) . toEqual ( 2 ) ;
194
214
expect ( loader . current ) . toEqual ( "success" ) ;
@@ -372,27 +392,27 @@ describe("Hierarchical Traffic Lights Machine", () => {
372
392
expect ( machine . changeCount ) . toEqual ( 1 ) ;
373
393
374
394
machine . next ( "TIMER" ) ;
375
- expect ( machine . current ) . toEqual ( { " red" : "walk" } ) ;
395
+ expect ( machine . current ) . toEqual ( { red : "walk" } ) ;
376
396
// expect(machine.current).toEqual([["red", "walk"]]); // Like a Map key
377
397
// expect(machine.currentMap).toEqual(new Map([["red", "walk"]]));
378
398
expect ( machine . changeCount ) . toEqual ( 3 ) ;
379
399
380
400
machine . next ( "TIMER" ) ;
381
401
expect ( machine . current ) . toEqual ( "green" ) ;
382
402
expect ( machine . changeCount ) . toEqual ( 4 ) ;
383
-
403
+
384
404
machine . next ( "POWER_RESTORED" ) ;
385
- expect ( machine . current ) . toEqual ( { " red" : "walk" } ) ;
405
+ expect ( machine . current ) . toEqual ( { red : "walk" } ) ;
386
406
expect ( machine . changeCount ) . toEqual ( 6 ) ;
387
-
407
+
388
408
machine . next ( "POWER_OUTAGE" ) ;
389
- expect ( machine . current ) . toEqual ( { " red" : "blinking" } ) ;
409
+ expect ( machine . current ) . toEqual ( { red : "blinking" } ) ;
390
410
expect ( machine . changeCount ) . toEqual ( 7 ) ;
391
411
} ) ;
392
412
} ) ;
393
413
394
414
describe ( "Switch" , ( ) => {
395
- function * Switch ( ) {
415
+ function Switch ( ) {
396
416
function * OFF ( ) {
397
417
yield on ( "FLICK" , ON ) ;
398
418
}
@@ -419,9 +439,9 @@ describe("Switch", () => {
419
439
} ) ;
420
440
421
441
describe ( "Switch with symbol messages" , ( ) => {
422
- const FLICK = Symbol ( ' FLICK' ) ;
442
+ const FLICK = Symbol ( " FLICK" ) ;
423
443
424
- function * Switch ( ) {
444
+ function Switch ( ) {
425
445
function * OFF ( ) {
426
446
yield on ( FLICK , ON ) ;
427
447
}
@@ -445,12 +465,128 @@ describe("Switch with symbol messages", () => {
445
465
expect ( machine . current ) . toEqual ( "OFF" ) ;
446
466
expect ( machine . changeCount ) . toEqual ( 2 ) ;
447
467
448
- machine . next ( Symbol ( ' will be ignored' ) ) ;
468
+ machine . next ( Symbol ( " will be ignored" ) ) ;
449
469
expect ( machine . current ) . toEqual ( "OFF" ) ;
450
470
expect ( machine . changeCount ) . toEqual ( 2 ) ;
451
471
} ) ;
452
472
} ) ;
453
473
474
+ describe ( "Wrapping AbortController as a state machine" , ( ) => {
475
+ function AbortSender ( controller : AbortController ) {
476
+ function * initial ( ) {
477
+ yield cond ( controller . signal . aborted , aborted ) ;
478
+ yield on ( "abort" , aborted ) ;
479
+ }
480
+ function * aborted ( ) {
481
+ // yield entry(controller.abort.bind(controller));
482
+ yield entry ( function abort ( ) {
483
+ controller . abort ( ) ;
484
+ } ) ;
485
+ }
486
+
487
+ return initial ;
488
+ }
489
+
490
+ function AbortListener ( controller : AbortController ) {
491
+ function * initial ( ) {
492
+ if ( controller . signal . aborted ) {
493
+ yield always ( aborted ) ;
494
+ } else {
495
+ yield on ( "abort" , aborted ) ;
496
+ yield listenTo ( controller . signal , "abort" ) ;
497
+ }
498
+ }
499
+ function * aborted ( ) { }
500
+
501
+ return initial ;
502
+ }
503
+
504
+ function AbortOwner ( ) {
505
+ // const controllerKey = Symbol('AbortController');
506
+ function controller ( ) {
507
+ return new AbortController ( ) ;
508
+ }
509
+
510
+ function * initial ( ) {
511
+ yield entry ( controller ) ;
512
+ yield on ( "abort" , aborted ) ;
513
+ }
514
+ function * aborted ( ) {
515
+ yield entry ( send ( controller , "abort" , [ ] ) ) ;
516
+ }
517
+
518
+ return initial ;
519
+ }
520
+
521
+ describe ( "AbortSender" , ( ) => {
522
+ it ( "is already aborted if passed controller is aborted" , ( ) => {
523
+ const aborter = new AbortController ( ) ;
524
+ aborter . abort ( ) ;
525
+ const machine = start ( AbortSender . bind ( null , aborter ) ) ;
526
+ expect ( machine . current ) . toEqual ( "aborted" ) ;
527
+ expect ( machine . changeCount ) . toEqual ( 0 ) ;
528
+ } ) ;
529
+
530
+ it ( "tells AbortController to abort" , ( ) => {
531
+ const aborter = new AbortController ( ) ;
532
+ const machine = start ( AbortSender . bind ( null , aborter ) ) ;
533
+
534
+ expect ( machine . current ) . toEqual ( "initial" ) ;
535
+ expect ( machine . changeCount ) . toEqual ( 0 ) ;
536
+
537
+ expect ( aborter . signal . aborted ) . toBe ( false ) ;
538
+
539
+ machine . next ( "abort" ) ;
540
+ expect ( machine . current ) . toEqual ( "aborted" ) ;
541
+ expect ( machine . changeCount ) . toEqual ( 1 ) ;
542
+
543
+ expect ( aborter . signal . aborted ) . toBe ( true ) ;
544
+ } ) ;
545
+ } ) ;
546
+
547
+ describe ( "AbortListener" , ( ) => {
548
+ it ( "is already aborted if passed controller is aborted" , ( ) => {
549
+ const aborter = new AbortController ( ) ;
550
+ aborter . abort ( ) ;
551
+ const machine = start ( AbortListener . bind ( null , aborter ) ) ;
552
+ expect ( machine . current ) . toEqual ( "aborted" ) ;
553
+ expect ( machine . changeCount ) . toEqual ( 0 ) ;
554
+ } ) ;
555
+
556
+ it ( "listens when AbortController aborts" , ( ) => {
557
+ const aborter = new AbortController ( ) ;
558
+ const machine = start ( AbortListener . bind ( null , aborter ) ) ;
559
+
560
+ expect ( machine . current ) . toEqual ( "initial" ) ;
561
+ expect ( machine . changeCount ) . toEqual ( 0 ) ;
562
+ expect ( aborter . signal . aborted ) . toBe ( false ) ;
563
+
564
+ aborter . abort ( ) ;
565
+ expect ( machine . current ) . toEqual ( "aborted" ) ;
566
+ expect ( machine . changeCount ) . toEqual ( 1 ) ;
567
+ } ) ;
568
+ } ) ;
569
+
570
+ describe ( "AbortOwner" , ( ) => {
571
+ it ( "aborts" , async ( ) => {
572
+ const machine = start ( AbortOwner ) ;
573
+
574
+ expect ( machine . current ) . toEqual ( "initial" ) ;
575
+ expect ( machine . changeCount ) . toEqual ( 0 ) ;
576
+
577
+ const { controller } = await machine . results as { controller : AbortController } ;
578
+ expect ( controller ) . toBeInstanceOf ( AbortController ) ;
579
+ expect ( controller . signal . aborted ) . toBe ( false ) ;
580
+
581
+ machine . next ( "abort" ) ;
582
+ expect ( machine . current ) . toEqual ( "aborted" ) ;
583
+ expect ( machine . changeCount ) . toEqual ( 1 ) ;
584
+
585
+ expect ( controller . signal . aborted ) . toBe ( true ) ;
586
+ } ) ;
587
+ } ) ;
588
+ } ) ;
589
+
454
590
/*describe("Counter", () => {
455
591
function* Counter() {
456
592
function* initial() {
0 commit comments