mirror of
https://github.com/strapi/strapi.git
synced 2026-01-06 04:03:25 +00:00
Add event hub tests
This commit is contained in:
parent
a7520d8456
commit
6dced3ab3b
@ -2,18 +2,97 @@
|
||||
|
||||
const createEventHub = require('../event-hub');
|
||||
|
||||
test('Subscribe once will trigger the callback only once', async () => {
|
||||
const { once, emit } = createEventHub();
|
||||
describe('Event Hub', () => {
|
||||
it('only triggers the callback once with once()', async () => {
|
||||
const { once, emit } = createEventHub();
|
||||
|
||||
const fn = jest.fn();
|
||||
const fn = jest.fn();
|
||||
|
||||
const args = [1, 2, 3];
|
||||
once('my-event', fn);
|
||||
const args = [1, 2, 3];
|
||||
once('my-event', fn);
|
||||
|
||||
emit('my-event', ...args);
|
||||
emit('my-event');
|
||||
emit('my-event');
|
||||
await emit('my-event', ...args);
|
||||
await emit('my-event');
|
||||
await emit('my-event');
|
||||
|
||||
expect(fn).toHaveBeenCalledTimes(1);
|
||||
expect(fn).toHaveBeenCalledWith(...args);
|
||||
expect(fn).toHaveBeenCalledTimes(1);
|
||||
expect(fn).toHaveBeenCalledWith(...args);
|
||||
});
|
||||
|
||||
it('subscribes and unsubscribes to all events', async () => {
|
||||
const { subscribe, unsubscribe, emit } = createEventHub();
|
||||
|
||||
const fn = jest.fn();
|
||||
subscribe(fn);
|
||||
|
||||
const args1 = [1, 2, 3];
|
||||
const args2 = [4, 5, 6];
|
||||
|
||||
await emit('my-event', ...args1);
|
||||
await emit('my-event', ...args2);
|
||||
await emit('my-other-event');
|
||||
|
||||
expect(fn).toHaveBeenCalled();
|
||||
expect(fn).toHaveBeenNthCalledWith(1, 'my-event', ...args1);
|
||||
expect(fn).toHaveBeenNthCalledWith(2, 'my-event', ...args2);
|
||||
expect(fn).toHaveBeenNthCalledWith(3, 'my-other-event');
|
||||
|
||||
// Unsubscribes with unsubscribe()
|
||||
unsubscribe(fn);
|
||||
await emit('my-event');
|
||||
expect(fn).toHaveBeenCalledTimes(3);
|
||||
|
||||
// Unsubscribes with the returned function
|
||||
const unsubscribe2 = subscribe(fn);
|
||||
await emit('my-event');
|
||||
expect(fn).toHaveBeenCalledTimes(4);
|
||||
unsubscribe2();
|
||||
await emit('my-event');
|
||||
expect(fn).toHaveBeenCalledTimes(4);
|
||||
});
|
||||
|
||||
it('adds and removes simple listeners', async () => {
|
||||
const { on, off, emit } = createEventHub();
|
||||
|
||||
const fn = jest.fn();
|
||||
const args = [1, 2, 3];
|
||||
|
||||
// Listens to event with on()
|
||||
on('my-event', fn);
|
||||
await emit('my-event', ...args);
|
||||
expect(fn).toHaveBeenCalledWith(...args);
|
||||
|
||||
// Removes listener with off()
|
||||
off('my-event', fn);
|
||||
await emit('my-event');
|
||||
expect(fn).toHaveBeenCalledTimes(1);
|
||||
|
||||
// Removes listener with the returned function
|
||||
const off2 = on('my-event', fn);
|
||||
await emit('my-event', ...args);
|
||||
expect(fn).toHaveBeenCalledTimes(2);
|
||||
off2();
|
||||
await emit('my-event');
|
||||
expect(fn).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('removes all subscribers on destroy()', async () => {
|
||||
const { subscribe, on, emit, destroy } = createEventHub();
|
||||
|
||||
const fn = jest.fn();
|
||||
const fn2 = jest.fn();
|
||||
subscribe(fn);
|
||||
on('my-event', fn2);
|
||||
|
||||
await emit('my-event');
|
||||
expect(fn).toHaveBeenCalled();
|
||||
expect(fn2).toHaveBeenCalled();
|
||||
|
||||
destroy();
|
||||
|
||||
// Subscribers are removed
|
||||
await emit('my-event');
|
||||
expect(fn).toHaveBeenCalledTimes(1);
|
||||
expect(fn2).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user