I am creating a web app using React and Redux Observables and I would like to create a unit test for the timeout scenario of one of my epics.
Here is the Epic:
export const loginUserEpic = (action$: ActionsObservable<any>, store, { ajax, scheduler }): Observable<Action> =>
action$.pipe(
ofType<LoginAction>(LoginActionTypes.LOGIN_ACTION),
switchMap((action: LoginAction) =>
ajax({
url,
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: { email: action.payload.username, password: action.payload.password },
}).pipe(
timeout(timeoutValue, scheduler),
map((response: AjaxResponse) => loginSuccess(response.response.token)),
catchError((error: Error) => of(loginFailed(error))),
),
),
);
And here is my test:
it('should handle a timeout error', () => {
// My current timeout value is 20 millseconds
const inputMarble = '------a';
const inputValues = {
a: login('fake-user', 'fake-password'),
};
const outputMarble = '--b';
const outputValues = {
b: loginFailed(new Error('timeout')),
};
const ajaxMock = jest.fn().mockReturnValue(of({ response: { token: 'fake-token' } }));
const action$ = new ActionsObservable<Action>(ts.createHotObservable(inputMarble, inputValues));
const outputAction = loginUserEpic(action$, undefined, { ajax: ajaxMock, scheduler: ts });
// I am not sure what error to expect here...
ts.expectObservable(outputAction).toBe(outputMarble, outputValues);
ts.flush();
expect(ajaxMock).toHaveBeenCalled();
});
What I expected is that the Epic would throw a timeout error, because my timeout value is of 20ms and the Observer is delaying 60ms before emmiting a value. I would then take this error and compare it in the end to make the test pass.
Unfortunately no timeout error is being thrown. Am I doing something wrong?