當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript Dispatcher.Dispatcher類代碼示例

本文整理匯總了TypeScript中Core/Dispatcher.Dispatcher的典型用法代碼示例。如果您正苦於以下問題:TypeScript Dispatcher類的具體用法?TypeScript Dispatcher怎麽用?TypeScript Dispatcher使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Dispatcher類的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: describe

describe('Core/Dispatcher', () => {
  const actionTypeFactory = new ActionTypeFactory('Dummy');
  let dispatcher = new Dispatcher;

  beforeEach(() => {
    dispatcher = new Dispatcher;
  });

  describe('#dispatch()', () => {
    it('should work successfully', () => {
      const actionType = actionTypeFactory.create('dummyAction');
      const action = { type: actionType };

      expect(() => dispatcher.dispatch(action)).to.not.throw(Error);
    });

    it('should return promise', () => {
      const actionType = actionTypeFactory.create('dummyAction');
      const action = { type: actionType };

      expect(dispatcher.dispatch(action)).to.be.a('promise');
    });

    context('when payload is promise', () => {
      const actionType = actionTypeFactory.create('dummyAction');
      const value = 'value';

      context('when payload promise is resolved', () => {
        it('should dispatch another action when payload is promise', (done) => {
          const action = { type: actionType, payload: Promise.resolve(value) };

          let callCount = 0;
          dispatcher.registerSubscriber(({payload, asyncStatus}) => {
            callCount++;

            if (callCount === 1) {
              expect(payload).to.be.a('promise');
              expect(asyncStatus).to.be.undefined;
            }

            if (callCount === 2) {
              expect(payload).to.equal(value);
              expect(asyncStatus).to.equal('resolved');
              done();
            }
          });

          dispatcher.dispatch(action);
        });
      });

      context('when payload promise is rejected', () => {
        it('should dispatch another action when payload is promise', (done) => {
          const action = { type: actionType, payload: Promise.reject(value) };

          let callCount = 0;
          dispatcher.registerSubscriber(({payload, asyncStatus}) => {
            callCount++;

            if (callCount === 1) {
              expect(payload).to.be.a('promise');
              expect(asyncStatus).to.be.undefined;
            }

            if (callCount === 2) {
              expect(payload).to.equal(value);
              expect(asyncStatus).to.equal('rejected');
              done();
            }
          });

          dispatcher.dispatch(action);
        });
      });
    });
  });

  describe('#registerSubscriber()', () => {
    it('should pass correct action to subscriber', async () => {
      const actionType = actionTypeFactory.create('dummyAction');
      const action = { type: actionType };

      dispatcher.registerSubscriber((dispatchedAction) => {
        expect(dispatchedAction).to.equal(action);
      });

      await dispatcher.dispatch(action);
    });

    it('should not register duplicated subscriber', async () => {
      const actionType = actionTypeFactory.create('dummyAction');
      const action = { type: actionType };

      let callCount = 0;
      const subscriber = () => callCount++;
      dispatcher.registerSubscriber(subscriber);
      dispatcher.registerSubscriber(subscriber);
      await dispatcher.dispatch(action);
      expect(callCount).to.equal(1);
    });
//.........這裏部分代碼省略.........
開發者ID:mozisan,項目名稱:luxi,代碼行數:101,代碼來源:Dispatcher.ts

示例2: it

    it('should pass correct action to subscriber', async () => {
      const actionType = actionTypeFactory.create('dummyAction');
      const action = { type: actionType };

      dispatcher.registerSubscriber((dispatchedAction) => {
        expect(dispatchedAction).to.equal(action);
      });

      await dispatcher.dispatch(action);
    });
開發者ID:mozisan,項目名稱:luxi,代碼行數:10,代碼來源:Dispatcher.ts

示例3: expect

 expect(() => dispatcher.dispatch(action)).to.not.throw(Error);
開發者ID:mozisan,項目名稱:luxi,代碼行數:1,代碼來源:Dispatcher.ts


注:本文中的Core/Dispatcher.Dispatcher類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。