Reputation: 75
How do you unit test an NgRx Effect that listens to a window event?
I know how the Unit Test is setup with the actions stream as this is well documented, but I could not find an example to test this with event listeners.
Here is the effect I would like to unit test:
openKeyboard$ = createEffect(() => {
return fromEvent(window, 'ionKeyboardDidShow').pipe(mapTo(KeyboardActions.keyboardOpened()));
});
The unit test description would look something like that:
describe('openKeyboard$', () => {
it('dispatches keyboardOpened action on ionKeyboardDidShow window event', () => {
// how to test this?
});
});
Thanks for your help.
Upvotes: 0
Views: 268
Reputation: 75
I kind of found a solution that works by just dispatching the window event but I am not really sure if this is an elegant one
describe('openKeyboard$', () => {
it('dispatches keyboardOpened action on ionKeyboardDidShow window event', (done) => {
effects.openKeyboard$.subscribe((res) => {
expect(res).toEqual(KeyboardActions.keyboardOpened());
done();
});
const eventMock = new Event('ionKeyboardDidShow');
window.dispatchEvent(eventMock);
});
});
Upvotes: 1