Refactor of redux tests to avoid covering RTK implementation details

This commit is contained in:
Alejandro Celaya
2023-03-18 12:35:33 +01:00
parent 9cefdb7977
commit 4e8e16f16d
21 changed files with 222 additions and 730 deletions

View File

@@ -16,21 +16,21 @@ describe('mercureInfoReducer', () => {
describe('reducer', () => {
it('returns loading on GET_MERCURE_INFO_START', () => {
expect(reducer(undefined, { type: loadMercureInfo.pending.toString() })).toEqual({
expect(reducer(undefined, loadMercureInfo.pending(''))).toEqual({
loading: true,
error: false,
});
});
it('returns error on GET_MERCURE_INFO_ERROR', () => {
expect(reducer(undefined, { type: loadMercureInfo.rejected.toString() })).toEqual({
expect(reducer(undefined, loadMercureInfo.rejected(null, ''))).toEqual({
loading: false,
error: true,
});
});
it('returns mercure info on GET_MERCURE_INFO', () => {
expect(reducer(undefined, { type: loadMercureInfo.fulfilled.toString(), payload: mercureInfo })).toEqual(
expect(reducer(undefined, loadMercureInfo.fulfilled(mercureInfo, ''))).toEqual(
expect.objectContaining({ ...mercureInfo, loading: false, error: false }),
);
});
@@ -52,11 +52,8 @@ describe('mercureInfoReducer', () => {
expect(getMercureInfo).not.toHaveBeenCalled();
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, expect.objectContaining({
type: loadMercureInfo.pending.toString(),
}));
expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({
type: loadMercureInfo.rejected.toString(),
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({
error: new Error('Real time updates not enabled'),
}));
});
@@ -68,31 +65,7 @@ describe('mercureInfoReducer', () => {
expect(getMercureInfo).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, expect.objectContaining({
type: loadMercureInfo.pending.toString(),
}));
expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({
type: loadMercureInfo.fulfilled.toString(),
payload: mercureInfo,
}));
});
it('throws error on failure', async () => {
const error = 'Error';
const getState = createGetStateMock(true);
getMercureInfo.mockRejectedValue(error);
await loadMercureInfo()(dispatch, getState, {});
expect(getMercureInfo).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, expect.objectContaining({
type: loadMercureInfo.pending.toString(),
}));
expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({
type: loadMercureInfo.rejected.toString(),
}));
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({ payload: mercureInfo }));
});
});
});