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

70 lines
2.5 KiB
TypeScript
Raw Normal View History

import { fromPartial } from '@total-typescript/shoehorn';
2023-02-18 12:40:37 +03:00
import type { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
import type { GetState } from '../../../src/container/types';
import { mercureInfoReducerCreator } from '../../../src/shlink-web-component/mercure/reducers/mercureInfo';
describe('mercureInfoReducer', () => {
const mercureInfo = {
mercureHubUrl: 'http://example.com/.well-known/mercure',
token: 'abc.123.def',
};
2023-05-27 12:57:26 +03:00
const getMercureInfo = vi.fn();
const buildShlinkApiClient = () => fromPartial<ShlinkApiClient>({ mercureInfo: getMercureInfo });
2022-11-04 22:52:06 +03:00
const { loadMercureInfo, reducer } = mercureInfoReducerCreator(buildShlinkApiClient);
2022-11-04 22:52:06 +03:00
describe('reducer', () => {
it('returns loading on GET_MERCURE_INFO_START', () => {
expect(reducer(undefined, loadMercureInfo.pending(''))).toEqual({
loading: true,
error: false,
});
});
it('returns error on GET_MERCURE_INFO_ERROR', () => {
expect(reducer(undefined, loadMercureInfo.rejected(null, ''))).toEqual({
loading: false,
error: true,
});
});
it('returns mercure info on GET_MERCURE_INFO', () => {
expect(reducer(undefined, loadMercureInfo.fulfilled(mercureInfo, ''))).toEqual(
2022-11-04 22:52:06 +03:00
expect.objectContaining({ ...mercureInfo, loading: false, error: false }),
);
});
});
describe('loadMercureInfo', () => {
2023-05-27 12:57:26 +03:00
const dispatch = vi.fn();
const createGetStateMock = (enabled: boolean): GetState => vi.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);
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({
error: new Error('Real time updates not enabled'),
2022-11-04 22:52:06 +03:00
}));
});
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);
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({ payload: mercureInfo }));
});
});
});