2023-08-29 23:12:25 +02:00
|
|
|
import type { ShlinkApiClient } from '@shlinkio/shlink-js-sdk';
|
2023-04-13 21:48:29 +02:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2023-02-18 11:11:01 +01:00
|
|
|
import type { ShlinkState } from '../../../src/container/types';
|
2023-07-24 20:14:59 +02:00
|
|
|
import type { NonReachableServer, NotFoundServer, RegularServer } from '../../../src/servers/data';
|
2022-11-11 19:31:05 +01:00
|
|
|
import {
|
2023-02-18 11:11:01 +01:00
|
|
|
MAX_FALLBACK_VERSION,
|
|
|
|
MIN_FALLBACK_VERSION,
|
2018-08-12 09:22:18 +02:00
|
|
|
resetSelectedServer,
|
2022-11-11 19:31:05 +01:00
|
|
|
selectedServerReducerCreator,
|
2023-02-18 11:11:01 +01:00
|
|
|
selectServer as selectServerCreator,
|
2018-08-12 09:22:18 +02:00
|
|
|
} from '../../../src/servers/reducers/selectedServer';
|
|
|
|
|
|
|
|
describe('selectedServerReducer', () => {
|
2023-05-27 11:57:26 +02:00
|
|
|
const dispatch = vi.fn();
|
|
|
|
const health = vi.fn();
|
|
|
|
const buildApiClient = vi.fn().mockReturnValue(fromPartial<ShlinkApiClient>({ health }));
|
2022-11-10 22:44:25 +01:00
|
|
|
const selectServer = selectServerCreator(buildApiClient);
|
2022-11-11 19:31:05 +01:00
|
|
|
const { reducer } = selectedServerReducerCreator(selectServer);
|
2022-11-10 22:44:25 +01:00
|
|
|
|
2018-08-26 10:52:45 +02:00
|
|
|
describe('reducer', () => {
|
2018-08-12 09:34:14 +02:00
|
|
|
it('returns default when action is RESET_SELECTED_SERVER', () =>
|
2023-03-18 12:35:33 +01:00
|
|
|
expect(reducer(null, resetSelectedServer())).toBeNull());
|
2018-08-12 09:34:14 +02:00
|
|
|
|
|
|
|
it('returns selected server when action is SELECT_SERVER', () => {
|
2023-04-13 21:48:29 +02:00
|
|
|
const payload = fromPartial<RegularServer>({ id: 'abc123' });
|
2023-03-18 12:35:33 +01:00
|
|
|
expect(reducer(null, selectServer.fulfilled(payload, '', ''))).toEqual(payload);
|
2018-08-12 09:22:18 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-08-12 09:34:14 +02:00
|
|
|
describe('selectServer', () => {
|
2019-10-05 10:20:33 +02:00
|
|
|
const version = '1.19.0';
|
2023-05-27 11:57:26 +02:00
|
|
|
const createGetStateMock = (id: string) => vi.fn().mockReturnValue({
|
2022-12-31 16:56:22 +01:00
|
|
|
servers: {
|
|
|
|
[id]: { id },
|
|
|
|
},
|
|
|
|
});
|
2018-08-12 09:22:18 +02:00
|
|
|
|
2020-02-17 18:21:52 +01:00
|
|
|
it.each([
|
2022-03-26 12:17:42 +01:00
|
|
|
[version, version, `v${version}`],
|
|
|
|
['latest', MAX_FALLBACK_VERSION, 'latest'],
|
|
|
|
['%invalid_semver%', MIN_FALLBACK_VERSION, '%invalid_semver%'],
|
2020-03-05 11:58:35 +01:00
|
|
|
])('dispatches proper actions', async (serverVersion, expectedVersion, expectedPrintableVersion) => {
|
2024-10-02 12:06:22 +02:00
|
|
|
const id = crypto.randomUUID();
|
2020-04-27 13:21:07 +02:00
|
|
|
const getState = createGetStateMock(id);
|
2019-10-05 10:20:33 +02:00
|
|
|
const expectedSelectedServer = {
|
2022-12-31 16:56:22 +01:00
|
|
|
id,
|
2019-10-18 17:39:38 +02:00
|
|
|
version: expectedVersion,
|
2020-03-05 11:58:35 +01:00
|
|
|
printableVersion: expectedPrintableVersion,
|
2019-10-05 10:20:33 +02:00
|
|
|
};
|
2018-08-12 09:22:18 +02:00
|
|
|
|
2022-11-10 22:44:25 +01:00
|
|
|
health.mockResolvedValue({ version: serverVersion });
|
2019-10-18 17:39:38 +02:00
|
|
|
|
2022-11-10 22:44:25 +01:00
|
|
|
await selectServer(id)(dispatch, getState, {});
|
2018-08-12 09:22:18 +02:00
|
|
|
|
2020-04-27 13:21:07 +02:00
|
|
|
expect(getState).toHaveBeenCalledTimes(1);
|
2019-10-05 19:31:47 +02:00
|
|
|
expect(buildApiClient).toHaveBeenCalledTimes(1);
|
2023-03-18 12:35:33 +01:00
|
|
|
expect(dispatch).toHaveBeenCalledTimes(3); // "Pending", "reset" and "fulfilled"
|
|
|
|
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({ payload: expectedSelectedServer }));
|
2018-08-12 09:22:18 +02:00
|
|
|
});
|
2019-10-18 17:39:38 +02:00
|
|
|
|
2020-03-08 12:57:01 +01:00
|
|
|
it('dispatches error when health endpoint fails', async () => {
|
2024-10-02 12:06:22 +02:00
|
|
|
const id = crypto.randomUUID();
|
2020-04-27 13:21:07 +02:00
|
|
|
const getState = createGetStateMock(id);
|
2023-04-13 21:48:29 +02:00
|
|
|
const expectedSelectedServer = fromPartial<NonReachableServer>({ id, serverNotReachable: true });
|
2019-10-18 17:39:38 +02:00
|
|
|
|
2022-11-10 22:44:25 +01:00
|
|
|
health.mockRejectedValue({});
|
2019-10-18 17:39:38 +02:00
|
|
|
|
2022-11-10 22:44:25 +01:00
|
|
|
await selectServer(id)(dispatch, getState, {});
|
2019-10-18 17:39:38 +02:00
|
|
|
|
2022-11-10 22:44:25 +01:00
|
|
|
expect(health).toHaveBeenCalled();
|
2023-03-18 12:35:33 +01:00
|
|
|
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({ payload: expectedSelectedServer }));
|
2020-03-08 12:57:01 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('dispatches error when server is not found', async () => {
|
2024-10-02 12:06:22 +02:00
|
|
|
const id = crypto.randomUUID();
|
2023-05-27 11:57:26 +02:00
|
|
|
const getState = vi.fn(() => fromPartial<ShlinkState>({ servers: {} }));
|
2020-08-23 10:58:43 +02:00
|
|
|
const expectedSelectedServer: NotFoundServer = { serverNotFound: true };
|
2020-03-08 12:57:01 +01:00
|
|
|
|
2022-11-10 22:44:25 +01:00
|
|
|
await selectServer(id)(dispatch, getState, {});
|
2020-03-08 12:57:01 +01:00
|
|
|
|
2020-04-27 13:21:07 +02:00
|
|
|
expect(getState).toHaveBeenCalled();
|
2022-11-10 22:44:25 +01:00
|
|
|
expect(health).not.toHaveBeenCalled();
|
2023-03-18 12:35:33 +01:00
|
|
|
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({ payload: expectedSelectedServer }));
|
2019-10-18 17:39:38 +02:00
|
|
|
});
|
2018-08-12 09:22:18 +02:00
|
|
|
});
|
|
|
|
});
|