2020-08-23 10:52:09 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2020-04-18 13:09:51 +03:00
|
|
|
import reducer, {
|
|
|
|
GET_MERCURE_INFO_START,
|
|
|
|
GET_MERCURE_INFO_ERROR,
|
|
|
|
GET_MERCURE_INFO,
|
|
|
|
loadMercureInfo,
|
2020-08-25 20:41:48 +03:00
|
|
|
GetMercureInfoAction,
|
2020-08-23 10:52:09 +03:00
|
|
|
} from '../../../src/mercure/reducers/mercureInfo';
|
|
|
|
import { ShlinkMercureInfo } from '../../../src/utils/services/types';
|
|
|
|
import ShlinkApiClient from '../../../src/utils/services/ShlinkApiClient';
|
|
|
|
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',
|
|
|
|
};
|
|
|
|
|
|
|
|
describe('reducer', () => {
|
2020-08-25 20:41:48 +03:00
|
|
|
const action = (type: string, args: Partial<ShlinkMercureInfo> = {}) => Mock.of<GetMercureInfoAction>(
|
|
|
|
{ type, ...args },
|
|
|
|
);
|
|
|
|
|
2020-04-18 13:09:51 +03:00
|
|
|
it('returns loading on GET_MERCURE_INFO_START', () => {
|
2020-08-25 20:41:48 +03:00
|
|
|
expect(reducer(undefined, action(GET_MERCURE_INFO_START))).toEqual({
|
2020-04-18 13:09:51 +03:00
|
|
|
loading: true,
|
|
|
|
error: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns error on GET_MERCURE_INFO_ERROR', () => {
|
2020-08-25 20:41:48 +03:00
|
|
|
expect(reducer(undefined, action(GET_MERCURE_INFO_ERROR))).toEqual({
|
2020-04-18 13:09:51 +03:00
|
|
|
loading: false,
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns mercure info on GET_MERCURE_INFO', () => {
|
2020-09-12 09:52:03 +03:00
|
|
|
expect(reducer(undefined, { type: GET_MERCURE_INFO, ...mercureInfo })).toEqual(expect.objectContaining({
|
2020-04-18 13:09:51 +03:00
|
|
|
...mercureInfo,
|
|
|
|
loading: false,
|
|
|
|
error: false,
|
2020-09-12 09:52:03 +03:00
|
|
|
}));
|
2020-04-18 13:09:51 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('loadMercureInfo', () => {
|
2020-08-23 10:52:09 +03:00
|
|
|
const createApiClientMock = (result: Promise<ShlinkMercureInfo>) => Mock.of<ShlinkApiClient>({
|
|
|
|
mercureInfo: jest.fn().mockReturnValue(result),
|
2020-04-18 13:09:51 +03:00
|
|
|
});
|
|
|
|
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
|
|
|
|
|
|
|
afterEach(jest.resetAllMocks);
|
|
|
|
|
2020-06-06 10:24:05 +03:00
|
|
|
it('dispatches error when real time updates are disabled', async () => {
|
|
|
|
const apiClientMock = createApiClientMock(Promise.resolve(mercureInfo));
|
|
|
|
const getState = createGetStateMock(false);
|
|
|
|
|
|
|
|
await loadMercureInfo(() => apiClientMock)()(dispatch, getState);
|
|
|
|
|
|
|
|
expect(apiClientMock.mercureInfo).not.toHaveBeenCalled();
|
|
|
|
expect(dispatch).toHaveBeenCalledTimes(2);
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(1, { type: GET_MERCURE_INFO_START });
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(2, { type: GET_MERCURE_INFO_ERROR });
|
|
|
|
});
|
|
|
|
|
2020-04-18 13:09:51 +03:00
|
|
|
it('calls API on success', async () => {
|
|
|
|
const apiClientMock = createApiClientMock(Promise.resolve(mercureInfo));
|
2020-06-06 10:24:05 +03:00
|
|
|
const getState = createGetStateMock(true);
|
2020-04-18 13:09:51 +03:00
|
|
|
|
2020-06-06 10:24:05 +03:00
|
|
|
await loadMercureInfo(() => apiClientMock)()(dispatch, getState);
|
2020-04-18 13:09:51 +03:00
|
|
|
|
|
|
|
expect(apiClientMock.mercureInfo).toHaveBeenCalledTimes(1);
|
|
|
|
expect(dispatch).toHaveBeenCalledTimes(2);
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(1, { type: GET_MERCURE_INFO_START });
|
2020-08-25 20:41:48 +03:00
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(2, { type: GET_MERCURE_INFO, ...mercureInfo });
|
2020-04-18 13:09:51 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('throws error on failure', async () => {
|
|
|
|
const error = 'Error';
|
|
|
|
const apiClientMock = createApiClientMock(Promise.reject(error));
|
2020-06-06 10:24:05 +03:00
|
|
|
const getState = createGetStateMock(true);
|
2020-04-18 13:09:51 +03:00
|
|
|
|
2020-06-06 10:24:05 +03:00
|
|
|
await loadMercureInfo(() => apiClientMock)()(dispatch, getState);
|
2020-04-18 13:09:51 +03:00
|
|
|
|
|
|
|
expect(apiClientMock.mercureInfo).toHaveBeenCalledTimes(1);
|
|
|
|
expect(dispatch).toHaveBeenCalledTimes(2);
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(1, { type: GET_MERCURE_INFO_START });
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(2, { type: GET_MERCURE_INFO_ERROR });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|