shlink-web-client/test/servers/reducers/selectedServer.test.ts

147 lines
5.3 KiB
TypeScript
Raw Normal View History

import { Mock } from 'ts-mockery';
2023-02-18 13:11:01 +03:00
import { v4 as uuid } from 'uuid';
import type { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
import type { ShlinkState } from '../../../src/container/types';
import type { NonReachableServer, NotFoundServer, ReachableServer, RegularServer } from '../../../src/servers/data';
2022-11-11 21:31:05 +03:00
import {
2023-02-18 13:11:01 +03:00
MAX_FALLBACK_VERSION,
MIN_FALLBACK_VERSION,
2018-08-12 10:22:18 +03:00
resetSelectedServer,
2022-11-11 21:31:05 +03:00
selectedServerReducerCreator,
2023-02-18 13:11:01 +03:00
selectServer as selectServerCreator,
selectServerListener,
2018-08-12 10:22:18 +03:00
} from '../../../src/servers/reducers/selectedServer';
describe('selectedServerReducer', () => {
const dispatch = jest.fn();
const health = jest.fn();
const buildApiClient = jest.fn().mockReturnValue(Mock.of<ShlinkApiClient>({ health }));
const selectServer = selectServerCreator(buildApiClient);
2022-11-11 21:31:05 +03:00
const { reducer } = selectedServerReducerCreator(selectServer);
afterEach(jest.clearAllMocks);
2018-08-26 11:52:45 +03:00
describe('reducer', () => {
it('returns default when action is RESET_SELECTED_SERVER', () =>
expect(reducer(null, { type: resetSelectedServer.toString(), payload: null })).toBeNull());
it('returns selected server when action is SELECT_SERVER', () => {
const payload = Mock.of<RegularServer>({ id: 'abc123' });
expect(reducer(null, { type: selectServer.fulfilled.toString(), payload })).toEqual(payload);
});
});
2018-08-12 10:22:18 +03:00
describe('resetSelectedServer', () => {
it('returns proper action', () => {
expect(resetSelectedServer()).toEqual({ type: resetSelectedServer.toString() });
2018-08-12 10:22:18 +03:00
});
});
describe('selectServer', () => {
const version = '1.19.0';
2022-12-31 18:56:22 +03:00
const createGetStateMock = (id: string) => jest.fn().mockReturnValue({
servers: {
[id]: { id },
},
});
2018-08-12 10:22:18 +03:00
it.each([
2022-03-26 14:17:42 +03:00
[version, version, `v${version}`],
['latest', MAX_FALLBACK_VERSION, 'latest'],
['%invalid_semver%', MIN_FALLBACK_VERSION, '%invalid_semver%'],
2020-03-05 13:58:35 +03:00
])('dispatches proper actions', async (serverVersion, expectedVersion, expectedPrintableVersion) => {
const id = uuid();
const getState = createGetStateMock(id);
const expectedSelectedServer = {
2022-12-31 18:56:22 +03:00
id,
version: expectedVersion,
2020-03-05 13:58:35 +03:00
printableVersion: expectedPrintableVersion,
};
2018-08-12 10:22:18 +03:00
health.mockResolvedValue({ version: serverVersion });
await selectServer(id)(dispatch, getState, {});
2018-08-12 10:22:18 +03:00
expect(dispatch).toHaveBeenCalledTimes(3);
expect(dispatch).toHaveBeenNthCalledWith(1, expect.objectContaining({ type: selectServer.pending.toString() }));
expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({ type: resetSelectedServer.toString() }));
expect(dispatch).toHaveBeenNthCalledWith(3, expect.objectContaining({
type: selectServer.fulfilled.toString(),
payload: expectedSelectedServer,
}));
2018-08-12 10:22:18 +03:00
});
2019-10-05 20:31:47 +03:00
it('invokes dependencies', async () => {
const id = uuid();
const getState = createGetStateMock(id);
await selectServer(id)(jest.fn(), getState, {});
2018-08-12 10:22:18 +03:00
expect(getState).toHaveBeenCalledTimes(1);
2019-10-05 20:31:47 +03:00
expect(buildApiClient).toHaveBeenCalledTimes(1);
2018-08-12 10:22:18 +03:00
});
2020-03-08 14:57:01 +03:00
it('dispatches error when health endpoint fails', async () => {
const id = uuid();
const getState = createGetStateMock(id);
2022-12-31 18:56:22 +03:00
const expectedSelectedServer = Mock.of<NonReachableServer>({ id, serverNotReachable: true });
health.mockRejectedValue({});
await selectServer(id)(dispatch, getState, {});
expect(health).toHaveBeenCalled();
expect(dispatch).toHaveBeenNthCalledWith(3, expect.objectContaining({
type: selectServer.fulfilled.toString(),
payload: expectedSelectedServer,
}));
2020-03-08 14:57:01 +03:00
});
it('dispatches error when server is not found', async () => {
const id = uuid();
const getState = jest.fn(() => Mock.of<ShlinkState>({ servers: {} }));
const expectedSelectedServer: NotFoundServer = { serverNotFound: true };
2020-03-08 14:57:01 +03:00
await selectServer(id)(dispatch, getState, {});
2020-03-08 14:57:01 +03:00
expect(getState).toHaveBeenCalled();
expect(health).not.toHaveBeenCalled();
expect(dispatch).toHaveBeenNthCalledWith(3, expect.objectContaining({
type: selectServer.fulfilled.toString(),
payload: expectedSelectedServer,
}));
});
2018-08-12 10:22:18 +03:00
});
describe('selectServerListener', () => {
const getState = jest.fn(() => ({}));
const loadMercureInfo = jest.fn();
const { middleware } = selectServerListener(selectServer, loadMercureInfo);
it.each([
[Mock.of<ReachableServer>({ version: '1.2.3' }), 1],
[Mock.of<NotFoundServer>({ serverNotFound: true }), 0],
[Mock.of<NonReachableServer>({ serverNotReachable: true }), 0],
])('dispatches loadMercureInfo when provided server is reachable', (payload, expectedCalls) => {
middleware({ dispatch, getState })(jest.fn())({
payload,
type: selectServer.fulfilled.toString(),
});
expect(dispatch).toHaveBeenCalledTimes(expectedCalls);
expect(loadMercureInfo).toHaveBeenCalledTimes(expectedCalls);
});
it('does not dispatch loadMercureInfo when action is not of the proper type', () => {
middleware({ dispatch, getState })(jest.fn())({
payload: Mock.of<ReachableServer>({ version: '1.2.3' }),
type: 'something_else',
});
expect(dispatch).not.toHaveBeenCalled();
expect(loadMercureInfo).not.toHaveBeenCalled();
});
});
2018-08-12 10:22:18 +03:00
});