shlink-web-client/test/mercure/reducers/mercureInfo.test.ts

99 lines
3.4 KiB
TypeScript
Raw Normal View History

import { Mock } from 'ts-mockery';
2023-02-18 12:40:37 +03:00
import type { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
import type { GetState } from '../../../src/container/types';
2023-02-18 13:11:01 +03:00
import { mercureInfoReducerCreator } from '../../../src/mercure/reducers/mercureInfo';
describe('mercureInfoReducer', () => {
const mercureInfo = {
mercureHubUrl: 'http://example.com/.well-known/mercure',
token: 'abc.123.def',
};
2022-11-04 22:52:06 +03:00
const getMercureInfo = jest.fn();
const buildShlinkApiClient = () => Mock.of<ShlinkApiClient>({ mercureInfo: getMercureInfo });
const { loadMercureInfo, reducer } = mercureInfoReducerCreator(buildShlinkApiClient);
2022-11-04 22:52:06 +03:00
beforeEach(jest.resetAllMocks);
2022-11-04 22:52:06 +03:00
describe('reducer', () => {
it('returns loading on GET_MERCURE_INFO_START', () => {
2022-11-04 22:52:06 +03:00
expect(reducer(undefined, { type: loadMercureInfo.pending.toString() })).toEqual({
loading: true,
error: false,
});
});
it('returns error on GET_MERCURE_INFO_ERROR', () => {
2022-11-04 22:52:06 +03:00
expect(reducer(undefined, { type: loadMercureInfo.rejected.toString() })).toEqual({
loading: false,
error: true,
});
});
it('returns mercure info on GET_MERCURE_INFO', () => {
2022-11-04 22:52:06 +03:00
expect(reducer(undefined, { type: loadMercureInfo.fulfilled.toString(), payload: mercureInfo })).toEqual(
expect.objectContaining({ ...mercureInfo, loading: false, error: false }),
);
});
});
describe('loadMercureInfo', () => {
const dispatch = jest.fn();
const createGetStateMock = (enabled: boolean): GetState => jest.fn().mockReturnValue({
settings: {
realTimeUpdates: { enabled },
},
});
it('dispatches error when real time updates are disabled', async () => {
2022-11-04 22:52:06 +03:00
getMercureInfo.mockResolvedValue(mercureInfo);
const getState = createGetStateMock(false);
2022-11-04 22:52:06 +03:00
await loadMercureInfo()(dispatch, getState, {});
2022-11-04 22:52:06 +03:00
expect(getMercureInfo).not.toHaveBeenCalled();
expect(dispatch).toHaveBeenCalledTimes(2);
2022-11-04 22:52:06 +03:00
expect(dispatch).toHaveBeenNthCalledWith(1, expect.objectContaining({
type: loadMercureInfo.pending.toString(),
}));
expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({
type: loadMercureInfo.rejected.toString(),
}));
});
it('calls API on success', async () => {
2022-11-04 22:52:06 +03:00
getMercureInfo.mockResolvedValue(mercureInfo);
const getState = createGetStateMock(true);
2022-11-04 22:52:06 +03:00
await loadMercureInfo()(dispatch, getState, {});
2022-11-04 22:52:06 +03:00
expect(getMercureInfo).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledTimes(2);
2022-11-04 22:52:06 +03:00
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);
2022-11-04 22:52:06 +03:00
getMercureInfo.mockRejectedValue(error);
2022-11-04 22:52:06 +03:00
await loadMercureInfo()(dispatch, getState, {});
expect(getMercureInfo).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledTimes(2);
2022-11-04 22:52:06 +03:00
expect(dispatch).toHaveBeenNthCalledWith(1, expect.objectContaining({
type: loadMercureInfo.pending.toString(),
}));
expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({
type: loadMercureInfo.rejected.toString(),
}));
});
});
});