Skip to content

Instantly share code, notes, and snippets.

@smolak
Last active September 26, 2018 14:34
Show Gist options
  • Save smolak/d12b893efa377ce9f920d58171581c7d to your computer and use it in GitHub Desktop.
Save smolak/d12b893efa377ce9f920d58171581c7d to your computer and use it in GitHub Desktop.
describe('executing handlers', () => {
context('for synchronous handlers', () => {
it('should happen in sequence', async () => {
const hook1 = {
event: 'preEventName',
handler: sinon.spy()
};
const hook2 = {
event: 'preEventName',
handler: sinon.spy()
};
cacheInstance.addHooks([ hook1, hook2 ]);
await getPreData('eventName', anyValidArgs);
expect(hook1.handler).to.have.been.calledBefore(hook2.handler);
});
});
context('for asynchronous handlers', () => {
it('should happen in sequence', async () => {
const stallFor = async (time) => await new Promise(resolve => setTimeout(resolve, time));
const spyForSlowHandler = sinon.spy();
const hook1 = {
event: 'preEventName',
handler: async () => {
await stallFor(50);
spyForSlowHandler();
}
};
const hook2 = {
event: 'preEventName',
handler: sinon.spy()
};
cacheInstance.addHooks([ hook1, hook2 ]);
await getPreData('eventName', anyValidArgs);
expect(spyForSlowHandler).to.have.been.calledBefore(hook2.handler);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment