2020-08-23 10:52:09 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2022-11-04 22:52:06 +03:00
|
|
|
import { mercureInfoReducerCreator } from '../../../src/mercure/reducers/mercureInfo';
|
2022-05-28 11:47:39 +03:00
|
|
|
import { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
|
2020-08-23 10:52:09 +03:00
|
|
|
import { GetState } from '../../../src/container/types';
|
2020-04-18 13:09:51 +03:00
|
|
|
|
|
|
|
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);
|
2020-04-18 13:09:51 +03:00
|
|
|
|
2022-11-04 22:52:06 +03:00
|
|
|
beforeEach(jest.resetAllMocks);
|
2020-08-25 20:41:48 +03:00
|
|
|
|
2022-11-04 22:52:06 +03:00
|
|
|
describe('reducer', () => {
|
2020-04-18 13:09:51 +03:00
|
|
|
it('returns loading on GET_MERCURE_INFO_START', () => {
|
2022-11-04 22:52:06 +03:00
|
|
|
expect(reducer(undefined, { type: loadMercureInfo.pending.toString() })).toEqual({
|
2020-04-18 13:09:51 +03:00
|
|
|
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({
|
2020-04-18 13:09:51 +03:00
|
|
|
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 }),
|
|
|
|
);
|
2020-04-18 13:09:51 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('loadMercureInfo', () => {
|
|
|
|
const dispatch = jest.fn();
|
2020-08-23 10:52:09 +03:00
|
|
|
const createGetStateMock = (enabled: boolean): GetState => jest.fn().mockReturnValue({
|
2020-06-06 10:24:05 +03:00
|
|
|
settings: {
|
|
|
|
realTimeUpdates: { enabled },
|
|
|
|
},
|
2020-08-23 10:52:09 +03:00
|
|
|
});
|
2020-04-18 13:09:51 +03:00
|
|
|
|
2020-06-06 10:24:05 +03:00
|
|
|
it('dispatches error when real time updates are disabled', async () => {
|
2022-11-04 22:52:06 +03:00
|
|
|
getMercureInfo.mockResolvedValue(mercureInfo);
|
2020-06-06 10:24:05 +03:00
|
|
|
const getState = createGetStateMock(false);
|
|
|
|
|
2022-11-04 22:52:06 +03:00
|
|
|
await loadMercureInfo()(dispatch, getState, {});
|
2020-06-06 10:24:05 +03:00
|
|
|
|
2022-11-04 22:52:06 +03:00
|
|
|
expect(getMercureInfo).not.toHaveBeenCalled();
|
2020-06-06 10:24:05 +03:00
|
|
|
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(),
|
|
|
|
}));
|
2020-06-06 10:24:05 +03:00
|
|
|
});
|
|
|
|
|
2020-04-18 13:09:51 +03:00
|
|
|
it('calls API on success', async () => {
|
2022-11-04 22:52:06 +03:00
|
|
|
getMercureInfo.mockResolvedValue(mercureInfo);
|
2020-06-06 10:24:05 +03:00
|
|
|
const getState = createGetStateMock(true);
|
2020-04-18 13:09:51 +03:00
|
|
|
|
2022-11-04 22:52:06 +03:00
|
|
|
await loadMercureInfo()(dispatch, getState, {});
|
2020-04-18 13:09:51 +03:00
|
|
|
|
2022-11-04 22:52:06 +03:00
|
|
|
expect(getMercureInfo).toHaveBeenCalledTimes(1);
|
2020-04-18 13:09:51 +03:00
|
|
|
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,
|
|
|
|
}));
|
2020-04-18 13:09:51 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('throws error on failure', async () => {
|
|
|
|
const error = 'Error';
|
2020-06-06 10:24:05 +03:00
|
|
|
const getState = createGetStateMock(true);
|
2020-04-18 13:09:51 +03:00
|
|
|
|
2022-11-04 22:52:06 +03:00
|
|
|
getMercureInfo.mockRejectedValue(error);
|
2020-04-18 13:09:51 +03:00
|
|
|
|
2022-11-04 22:52:06 +03:00
|
|
|
await loadMercureInfo()(dispatch, getState, {});
|
|
|
|
|
|
|
|
expect(getMercureInfo).toHaveBeenCalledTimes(1);
|
2020-04-18 13:09:51 +03:00
|
|
|
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(),
|
|
|
|
}));
|
2020-04-18 13:09:51 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|