it('should make new deps available to hooks', bDone => {
   const dispatch = expect.createSpy();
   const next = expect.createSpy();
   const storeFn = mw({ dispatch })(next);
   const arrFlow = [];
   mw.addDeps({ foo: 42, bar: 'hello' });
   mw.addLogic([
     createLogic({
       type: 'CAT',
       validate({ foo, bar, action }, allow) {
         expect(foo).toBe(42);
         expect(bar).toBe('hello');
         arrFlow.push('validate');
         allow(action);
       },
       process({ foo, bar }) {
         expect(foo).toBe(42);
         expect(bar).toBe('hello');
         arrFlow.push('process');
       }
     })
   ]);
   storeFn({ type: 'CAT' });
   mw.whenComplete(() => {
     expect(arrFlow).toEqual(['validate', 'process']);
     bDone();
   });
 });
 it('should allow call with same values/instances', bDone => {
   const dispatch = expect.createSpy();
   const next = expect.createSpy();
   const storeFn = mw({ dispatch })(next);
   const arrFlow = [];
   const egg = { hey: 'world' };
   mw.addDeps({ foo: 42, bar: 'hello', egg });
   mw.addDeps({ foo: 42, bar: 'hello', dog: 21, egg });
   mw.addLogic([
     createLogic({
       type: 'CAT',
       validate({ foo, bar, dog, egg, action }, allow) {
         expect(foo).toBe(42);
         expect(bar).toBe('hello');
         expect(dog).toBe(21);
         expect(egg).toEqual({ hey: 'world' });
         arrFlow.push('validate');
         allow(action);
       },
       process({ foo, bar, dog, egg }) {
         expect(foo).toBe(42);
         expect(bar).toBe('hello');
         expect(dog).toBe(21);
         expect(egg).toEqual({ hey: 'world' });
         arrFlow.push('process');
       }
     })
   ]);
   storeFn({ type: 'CAT' });
   mw.whenComplete(() => {
     expect(arrFlow).toEqual(['validate', 'process']);
     bDone();
   });
 });
 it('should throw an error if values are overridden', () => {
   mw.addDeps({ foo: 42, bar: 'hello' });
   function fn() {
     mw.addDeps({ foo: 30, dog: 21 });
   }
   expect(fn).toThrow('cannot override an existing dep value: foo');
 });
Пример #4
0
 it('mw.monitor$ should track flow', () => {
   expect(monArr).toEqual([
     { action: { type: 'FOO', allowMe: true }, op: 'top' },
     { action: { type: 'FOO', allowMe: true },
       name: 'L(FOO)-0',
       op: 'begin' },
     { action: { type: 'FOO', allowMe: true },
       nextAction: { type: 'FOO', allowMe: true, allowed: ['a'] },
       name: 'L(FOO)-0',
       shouldProcess: true,
       op: 'next' },
     { nextAction: { type: 'FOO', allowMe: true, allowed: ['a'] },
       op: 'bottom' },
     { action: { type: 'FOO', allowMe: true },
       dispAction:
                  { type: 'BAR',
                    allowMe: true,
                    allowed: ['a'],
                    processed: ['a'] },
       op: 'dispatch' },
     { action: { type: 'FOO', allowMe: true },
       name: 'L(FOO)-0',
       op: 'end' }
   ]);
 });
 it('should throw with error store is not defined', () => {
   const logic = createLogic({
     type: 'FOO',
     transform(deps, next) {
       next(action2);
     }
   });
   expect(() => {
     mw.addLogic([logic]);
   }).toThrow('store is not defined');
 });
 it('should throw with error store is not defined', () => {
   expect(() => {
     mw({})(undefined); // shouldn't really happen
     const logic = createLogic({
       type: 'FOO',
       transform(deps, next) {
         next(action2);
       }
     });
     mw.addLogic([logic]);
   }).toThrow('store is not defined');
 });
 it('mw.monitor$ should track flow', () => {
   expect(monArr).toEqual([
     { action: { type: 'FOO' }, op: 'top' },
     { action: { type: 'FOO' }, name: 'L(FOO)-0', op: 'begin' },
     { action: { type: 'FOO' },
       nextAction: { type: 'FOO', tid: 2 },
       name: 'L(FOO)-0',
       shouldProcess: true,
       op: 'next' },
     { nextAction: { type: 'FOO', tid: 2 }, op: 'bottom' },
     { action: { type: 'FOO' }, name: 'L(FOO)-0', op: 'end' }
   ]);
 });
 it('throws an error', () => {
   const dispatch = expect.createSpy();
   const next = expect.createSpy();
   const fooLogic = createLogic({ type: 'FOO' });
   const barLogic = createLogic({ type: 'BAR' });
   const arrLogic = [
     fooLogic,
     barLogic
   ];
   const mw = createLogicMiddleware(arrLogic);
   mw({ dispatch })(next); // simulate store creation
   expect(() => {
     mw.addLogic([fooLogic]); // duplicates existing
   }).toThrow('duplicate logic');
 });
 it('should throw an error if called without an object', () => {
   function fn() {
     mw.addDeps(42);
   }
   expect(fn).toThrow('called with an object');
 });
 mw.whenComplete(() => {
   expect(arrFlow).toEqual(['validate', 'process']);
   bDone();
 });
Пример #11
0
 it('createLogicMiddleware should load verifying its rx props', () => {
   expect(createLogicMiddleware).toExist();
 });
 it('passes only actionFoo1 since validate async', () => {
   expect(next.calls.length).toBe(1);
   expect(next.calls[0].arguments[0]).toEqual(actionFoo1);
 });
 it('passes actionFoo1', () => {
   expect(next.calls.length).toBe(1);
   expect(next.calls[0].arguments[0]).toEqual(actionFoo1);
 });
 it('works as middleware', () => {
   expect(next.calls.length).toBe(1);
   expect(next.calls[0].arguments[0]).toEqual(actionA);
 });
 it('returns a mw fn with addLogic and replaceLogic props', () => {
   expect(mw).toBeA(Function);
   expect(mw.addLogic).toBeA(Function);
   expect(mw.replaceLogic).toBeA(Function);
 });
Пример #16
0
 it('logicWrapper should load verifying its rx props', () => {
   expect(logicWrapper).toExist();
 });
 it('dispatch only actionResult1', () => {
   expect(dispatch.calls.length).toBe(1);
   expect(dispatch.calls[0].arguments[0]).toEqual(actionResultFoo1);
 });
 it('should return count of 1', () => {
   expect(logicCount).toBe(1);
 });
 it('dispatch only sync1, async1', () => {
   expect(dispatch.calls.length).toBe(2);
   expect(dispatch.calls[0].arguments[0]).toEqual(actionSyncResult1);
   expect(dispatch.calls[1].arguments[0]).toEqual(actionResultFoo1);
 });
 it('should transform action', () => {
   expect(next.calls.length).toBe(1);
   expect(next.calls[0].arguments[0]).toEqual(action2);
 });
 it('no dispatches', () => {
   expect(dispatch.calls.length).toBe(0);
 });
Пример #22
0
 it('createLogicAction$ should load verifying its rx props', () => {
   expect(createLogicAction$).toExist();
 });