@@ -327,6 +327,148 @@ describe('createLogicMiddleware', () => {
327
327
} ) ;
328
328
} ) ;
329
329
330
+ describe ( '[logicA] type is symbol, match only' , ( ) => {
331
+ let monArr = [ ] ;
332
+ let mw ;
333
+ let logicA ;
334
+ let next ;
335
+ let dispatch ;
336
+ const FOO = Symbol ( 'FOO' ) ;
337
+ const BAR = Symbol ( 'BAR' ) ;
338
+ const CAT = Symbol ( 'CAT' ) ;
339
+ const actionA = { type : FOO } ;
340
+ const actionAResult = { type : FOO , allowed : [ 'a' ] } ;
341
+ const actionADispatch = { type : BAR , allowed : [ 'a' ] } ;
342
+ const actionIgnore = { type : CAT } ;
343
+ beforeEach ( done => {
344
+ monArr = [ ] ;
345
+ next = expect . createSpy ( ) ;
346
+ dispatch = expect . createSpy ( ) ;
347
+ logicA = createLogic ( {
348
+ type : FOO ,
349
+ validate ( { action } , allow ) {
350
+ allow ( {
351
+ ...action ,
352
+ allowed : [ 'a' ]
353
+ } ) ;
354
+ } ,
355
+ process ( { action } , dispatch ) {
356
+ dispatch ( {
357
+ ...action ,
358
+ type : BAR
359
+ } ) ;
360
+ }
361
+ } ) ;
362
+ mw = createLogicMiddleware ( [ logicA ] ) ;
363
+ mw . monitor$ . subscribe ( x => monArr . push ( x ) ) ;
364
+ const storeFn = mw ( { dispatch } ) ( next ) ;
365
+ storeFn ( actionIgnore ) ;
366
+ storeFn ( actionA ) ;
367
+ mw . whenComplete ( done ) ;
368
+ } ) ;
369
+
370
+ it ( 'both messages hit next, one bypassed validation/transform' , ( ) => {
371
+ expect ( next . calls . length ) . toBe ( 2 ) ;
372
+ expect ( next . calls [ 0 ] . arguments [ 0 ] ) . toEqual ( actionIgnore ) ;
373
+ expect ( next . calls [ 1 ] . arguments [ 0 ] ) . toEqual ( actionAResult ) ;
374
+ } ) ;
375
+
376
+ it ( 'only matching is processed and dispatched' , ( ) => {
377
+ expect ( dispatch . calls . length ) . toBe ( 1 ) ;
378
+ expect ( dispatch . calls [ 0 ] . arguments [ 0 ] ) . toEqual ( actionADispatch ) ;
379
+ } ) ;
380
+
381
+ it ( 'mw.monitor$ should track flow' , ( ) => {
382
+ expect ( monArr ) . toEqual ( [
383
+ { action : { type : CAT } , op : 'top' } ,
384
+ { nextAction : { type : CAT } , op : 'bottom' } ,
385
+ { action : { type : FOO } , op : 'top' } ,
386
+ { action : { type : FOO } , name : 'L(Symbol(FOO))-0' , op : 'begin' } ,
387
+ { action : { type : FOO } ,
388
+ nextAction : { type : FOO , allowed : [ 'a' ] } ,
389
+ name : 'L(Symbol(FOO))-0' ,
390
+ shouldProcess : true ,
391
+ op : 'next' } ,
392
+ { nextAction : { type : FOO , allowed : [ 'a' ] } , op : 'bottom' } ,
393
+ { action : { type : FOO } ,
394
+ dispAction : { type : BAR , allowed : [ 'a' ] } ,
395
+ op : 'dispatch' } ,
396
+ { action : { type : FOO } ,
397
+ name : 'L(Symbol(FOO))-0' ,
398
+ op : 'end'
399
+ }
400
+ ] ) ;
401
+ } ) ;
402
+
403
+ it ( 'mw.whenComplete(fn) should be called when complete' , ( done ) => {
404
+ mw . whenComplete ( done ) ;
405
+ } ) ;
406
+
407
+ it ( 'mw.whenComplete(fn) should resolve to promise' , ( done ) => {
408
+ function fn ( ) { }
409
+ mw . whenComplete ( fn ) . then ( done ) ;
410
+ } ) ;
411
+
412
+ it ( 'mw.whenComplete() should resolve to promise' , ( done ) => {
413
+ mw . whenComplete ( ) . then ( done ) ;
414
+ } ) ;
415
+
416
+ it ( 'delayed mw.whenComplete() should still resolve to promise' , ( done ) => {
417
+ setTimeout ( ( ) => {
418
+ mw . whenComplete ( ) . then ( done ) ;
419
+ } , 100 ) ;
420
+ } ) ;
421
+ } ) ;
422
+
423
+ describe ( '[logicA] type is arr of Symbols, match any' , ( ) => {
424
+ let mw ;
425
+ let logicA ;
426
+ let next ;
427
+ let dispatch ;
428
+ const FOO = Symbol ( 'FOO' ) ;
429
+ const BAR = Symbol ( 'BAR' ) ;
430
+ const CAT = Symbol ( 'CAT' ) ;
431
+ const DOG = Symbol ( 'DOG' ) ;
432
+ const actionA = { type : FOO } ;
433
+ const actionAResult = { type : FOO , allowed : [ 'a' ] } ;
434
+ const actionADispatch = { type : BAR , allowed : [ 'a' ] } ;
435
+ const actionIgnore = { type : CAT } ;
436
+ beforeEach ( done => {
437
+ next = expect . createSpy ( ) ;
438
+ dispatch = expect . createSpy ( ) . andCall ( ( ) => done ( ) ) ;
439
+ logicA = createLogic ( {
440
+ type : [ DOG , FOO ] ,
441
+ validate ( { action } , allow ) {
442
+ allow ( {
443
+ ...action ,
444
+ allowed : [ 'a' ]
445
+ } ) ;
446
+ } ,
447
+ process ( { action } , dispatch ) {
448
+ dispatch ( {
449
+ ...action ,
450
+ type : BAR
451
+ } ) ;
452
+ }
453
+ } ) ;
454
+ mw = createLogicMiddleware ( [ logicA ] ) ;
455
+ const storeFn = mw ( { dispatch } ) ( next ) ;
456
+ storeFn ( actionIgnore ) ;
457
+ storeFn ( actionA ) ;
458
+ } ) ;
459
+
460
+ it ( 'both messages hit next, one bypassed validation/transform' , ( ) => {
461
+ expect ( next . calls . length ) . toBe ( 2 ) ;
462
+ expect ( next . calls [ 0 ] . arguments [ 0 ] ) . toEqual ( actionIgnore ) ;
463
+ expect ( next . calls [ 1 ] . arguments [ 0 ] ) . toEqual ( actionAResult ) ;
464
+ } ) ;
465
+
466
+ it ( 'only matching is processed and dispatched' , ( ) => {
467
+ expect ( dispatch . calls . length ) . toBe ( 1 ) ;
468
+ expect ( dispatch . calls [ 0 ] . arguments [ 0 ] ) . toEqual ( actionADispatch ) ;
469
+ } ) ;
470
+ } ) ;
471
+
330
472
describe ( '[logicA] type is regex, match' , ( ) => {
331
473
let mw ;
332
474
let logicA ;
0 commit comments