Skip to content

Instantly share code, notes, and snippets.

@TracyNgot
Last active August 8, 2019 21:44
Show Gist options
  • Save TracyNgot/805b3a902bd62dcc5bfa507a824a0f01 to your computer and use it in GitHub Desktop.
Save TracyNgot/805b3a902bd62dcc5bfa507a824a0f01 to your computer and use it in GitHub Desktop.
Epic unit tests with TypeScript, Redux, RxJS 6 and Jest
// src/tests/unicorn/unicorn.epic.test.ts
import { ActionsObservable } from 'redux-observable';
import { of, throwError } from 'rxjs';
import { toArray } from 'rxjs/operators';
import UnicornActions from '../../store/unicorn/unicorn.action';
import UnicornApiService from '../../store/unicorn/unicorn.api.service';
import UnicornEpics from '../../store/unicorn/unicorn.epic';
describe('Unicorn test', () => {
describe('getUnicorn', () => {
test('send correct actions when success', (done) => {
const response = {data: {id: '1', name: 'My awesome Unicorn!'}};
jest.spyOn(UnicornApiService, 'getUnicorn')
.mockImplementation(() => of(response));
const expected = UnicornActions.done(response, UnicornActions.Types.GET_UNICORN);
const action$ = ActionsObservable.of(
UnicornActions.getUnicorn('1')
);
UnicornEpics.getUnicorn(action$)
.subscribe((actual: any) => {
expect(actual).toEqual(expected);
done();
}
);
});
test('send correct actions when error', (done) => {
const error = {error: 'Error'};
jest.spyOn(UnicornApiService, 'getUnicorn')
.mockImplementation(() => throwError(error));
const expected = UnicornActions.error(error, UnicornActions.Types.GET_UNICORN);
const action$ = ActionsObservable.of(
UnicornActions.getUnicorn('99')
);
UnicornEpics.getUnicorn(action$)
.subscribe((actual: any) => {
expect(actual).toEqual(expected);
done();
}
);
});
});
describe('likeUnicorn', () => {
// Multiple actions example
test('send correct actions when success', (done) => {
const response = {data: {id: '1', name: 'My awesome Unicorn!', likeCount: 1}};
jest.spyOn(UnicornApiService, 'likeUnicorn')
.mockImplementation(() => of(response));
const expected = [
UnicornActions.done(response, UnicornActions.Types.LIKE_UNICORN),
UnicornActions.updateUnicorn('1', {color: 'rainbow'})
];
const action$ = ActionsObservable.of(
UnicornActions.likeUnicorn('1')
);
UnicornEpics.likeUnicorn(action$)
.pipe(toArray()) // IMPORTANT: To get all the actions
.subscribe((actual: any) => {
expect(actual).toEqual(expected);
done();
}
);
});
test('send correct actions when error', (done) => {
const error = {error: 'Error'};
jest.spyOn(UnicornApiService, 'likeUnicorn')
.mockImplementation(() => throwError(error));
const expected = UnicornActions.error(error, UnicornActions.Types.LIKE_UNICORN);
const action$ = ActionsObservable.of(
UnicornActions.likeUnicorn('1')
);
UnicornEpics.likeUnicorn(action$)
.subscribe((actual: any) => {
expect(actual).toEqual(expected);
done();
}
);
});
});
describe('updateUnicorn', () => {
test('send correct actions when success', (done) => {
const response = {data: {id: '1', name: 'My awesome Unicorn!', likeCount: 1, color: 'blue'}};
jest.spyOn(UnicornApiService, 'updateUnicorn')
.mockImplementation(() => of(response));
const expected = UnicornActions.done(response, UnicornActions.Types.UPDATE_UNICORN);
const action$ = ActionsObservable.of(
UnicornActions.updateUnicorn('1', {color: 'blue'})
);
UnicornEpics.updateUnicorn(action$)
.subscribe((actual: any) => {
expect(actual).toEqual(expected);
done();
}
);
});
test('send correct actions when error', (done) => {
const error = {error: 'Error'};
jest.spyOn(UnicornApiService, 'updateUnicorn')
.mockImplementation(() => throwError(error));
const expected = UnicornActions.error(error, UnicornActions.Types.UPDATE_UNICORN);
const action$ = ActionsObservable.of(
UnicornActions.updateUnicorn('1', {color: 'blue'})
);
UnicornEpics.updateUnicorn(action$)
.subscribe((actual: any) => {
expect(actual).toEqual(expected);
done();
}
);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment