本文整理匯總了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);
});
//.........這裏部分代碼省略.........
示例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);
});
示例3: expect
expect(() => dispatcher.dispatch(action)).to.not.throw(Error);