2020-03-16 18:51:04 +01:00
|
|
|
import { v4 as uuid } from 'uuid';
|
2020-08-23 10:58:43 +02:00
|
|
|
import { Mock } from 'ts-mockery';
|
2018-08-26 10:52:45 +02:00
|
|
|
import reducer, {
|
2018-12-18 19:45:09 +01:00
|
|
|
selectServer,
|
2018-08-12 09:22:18 +02:00
|
|
|
resetSelectedServer,
|
2018-12-18 19:45:09 +01:00
|
|
|
RESET_SELECTED_SERVER,
|
2018-08-12 09:22:18 +02:00
|
|
|
SELECT_SERVER,
|
2019-10-18 17:39:38 +02:00
|
|
|
MAX_FALLBACK_VERSION,
|
|
|
|
MIN_FALLBACK_VERSION,
|
2018-08-12 09:22:18 +02:00
|
|
|
} from '../../../src/servers/reducers/selectedServer';
|
2020-08-23 10:58:43 +02:00
|
|
|
import { ShlinkState } from '../../../src/container/types';
|
2020-08-25 19:41:48 +02:00
|
|
|
import { NonReachableServer, NotFoundServer, RegularServer } from '../../../src/servers/data';
|
2018-08-12 09:22:18 +02:00
|
|
|
|
|
|
|
describe('selectedServerReducer', () => {
|
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', () =>
|
2020-08-25 19:41:48 +02:00
|
|
|
expect(reducer(null, { type: RESET_SELECTED_SERVER, selectedServer: null })).toEqual(null));
|
2018-08-12 09:34:14 +02:00
|
|
|
|
|
|
|
it('returns selected server when action is SELECT_SERVER', () => {
|
2020-08-25 19:41:48 +02:00
|
|
|
const selectedServer = Mock.of<RegularServer>({ id: 'abc123' });
|
2018-08-25 23:39:27 +02:00
|
|
|
|
2020-08-25 19:41:48 +02:00
|
|
|
expect(reducer(null, { type: SELECT_SERVER, selectedServer })).toEqual(selectedServer);
|
2018-08-12 09:34:14 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-08-12 09:22:18 +02:00
|
|
|
describe('resetSelectedServer', () => {
|
|
|
|
it('returns proper action', () => {
|
|
|
|
expect(resetSelectedServer()).toEqual({ type: RESET_SELECTED_SERVER });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-08-12 09:34:14 +02:00
|
|
|
describe('selectServer', () => {
|
2018-08-12 09:22:18 +02:00
|
|
|
const selectedServer = {
|
2020-03-16 18:51:04 +01:00
|
|
|
id: 'abc123',
|
2018-08-12 09:22:18 +02:00
|
|
|
};
|
2019-10-05 10:20:33 +02:00
|
|
|
const version = '1.19.0';
|
2020-08-23 10:58:43 +02:00
|
|
|
const createGetStateMock = (id: string) => jest.fn().mockReturnValue({ servers: { [id]: selectedServer } });
|
2019-10-05 10:20:33 +02:00
|
|
|
const apiClientMock = {
|
2019-10-18 17:39:38 +02:00
|
|
|
health: jest.fn(),
|
2019-10-05 10:20:33 +02:00
|
|
|
};
|
2020-03-05 09:23:53 +01:00
|
|
|
const buildApiClient = jest.fn().mockReturnValue(apiClientMock);
|
2019-10-18 17:39:38 +02:00
|
|
|
const dispatch = jest.fn();
|
2020-04-17 15:51:18 +02:00
|
|
|
const loadMercureInfo = jest.fn();
|
2019-10-05 19:31:47 +02:00
|
|
|
|
2019-10-18 17:39:38 +02:00
|
|
|
afterEach(jest.clearAllMocks);
|
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) => {
|
2020-04-27 13:21:07 +02:00
|
|
|
const id = uuid();
|
|
|
|
const getState = createGetStateMock(id);
|
2019-10-05 10:20:33 +02:00
|
|
|
const expectedSelectedServer = {
|
|
|
|
...selectedServer,
|
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
|
|
|
|
2019-10-18 17:39:38 +02:00
|
|
|
apiClientMock.health.mockResolvedValue({ version: serverVersion });
|
|
|
|
|
2020-04-27 13:21:07 +02:00
|
|
|
await selectServer(buildApiClient, loadMercureInfo)(id)(dispatch, getState);
|
2018-08-12 09:22:18 +02:00
|
|
|
|
2021-12-24 13:39:51 +01:00
|
|
|
expect(dispatch).toHaveBeenCalledTimes(3);
|
2020-03-08 12:57:01 +01:00
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(1, { type: RESET_SELECTED_SERVER });
|
2021-12-24 13:39:51 +01:00
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(2, { type: SELECT_SERVER, selectedServer: expectedSelectedServer });
|
2020-04-17 15:51:18 +02:00
|
|
|
expect(loadMercureInfo).toHaveBeenCalledTimes(1);
|
2018-08-12 09:22:18 +02:00
|
|
|
});
|
|
|
|
|
2019-10-05 19:31:47 +02:00
|
|
|
it('invokes dependencies', async () => {
|
2020-04-27 13:21:07 +02:00
|
|
|
const id = uuid();
|
|
|
|
const getState = createGetStateMock(id);
|
|
|
|
|
2020-08-23 10:58:43 +02:00
|
|
|
await selectServer(buildApiClient, loadMercureInfo)(id)(jest.fn(), 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);
|
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 () => {
|
2020-04-27 13:21:07 +02:00
|
|
|
const id = uuid();
|
|
|
|
const getState = createGetStateMock(id);
|
2020-11-13 23:06:03 +01:00
|
|
|
const expectedSelectedServer = Mock.of<NonReachableServer>({ ...selectedServer, serverNotReachable: true });
|
2019-10-18 17:39:38 +02:00
|
|
|
|
|
|
|
apiClientMock.health.mockRejectedValue({});
|
|
|
|
|
2020-04-27 13:21:07 +02:00
|
|
|
await selectServer(buildApiClient, loadMercureInfo)(id)(dispatch, getState);
|
2019-10-18 17:39:38 +02:00
|
|
|
|
2020-03-08 12:57:01 +01:00
|
|
|
expect(apiClientMock.health).toHaveBeenCalled();
|
2021-12-24 13:39:51 +01:00
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(2, { type: SELECT_SERVER, selectedServer: expectedSelectedServer });
|
2020-04-17 15:51:18 +02:00
|
|
|
expect(loadMercureInfo).not.toHaveBeenCalled();
|
2020-03-08 12:57:01 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('dispatches error when server is not found', async () => {
|
2020-04-27 13:21:07 +02:00
|
|
|
const id = uuid();
|
2020-08-23 10:58:43 +02:00
|
|
|
const getState = jest.fn(() => Mock.of<ShlinkState>({ servers: {} }));
|
|
|
|
const expectedSelectedServer: NotFoundServer = { serverNotFound: true };
|
2020-03-08 12:57:01 +01:00
|
|
|
|
2020-04-27 13:21:07 +02:00
|
|
|
await selectServer(buildApiClient, loadMercureInfo)(id)(dispatch, getState);
|
2020-03-08 12:57:01 +01:00
|
|
|
|
2020-04-27 13:21:07 +02:00
|
|
|
expect(getState).toHaveBeenCalled();
|
2020-03-08 12:57:01 +01:00
|
|
|
expect(apiClientMock.health).not.toHaveBeenCalled();
|
2021-12-24 13:39:51 +01:00
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(2, { type: SELECT_SERVER, selectedServer: expectedSelectedServer });
|
2020-04-17 15:51:18 +02:00
|
|
|
expect(loadMercureInfo).not.toHaveBeenCalled();
|
2019-10-18 17:39:38 +02:00
|
|
|
});
|
2018-08-12 09:22:18 +02:00
|
|
|
});
|
|
|
|
});
|