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

107 lines
3.9 KiB
JavaScript
Raw Normal View History

import { v4 as uuid } from 'uuid';
2018-08-26 11:52:45 +03:00
import reducer, {
selectServer,
2018-08-12 10:22:18 +03:00
resetSelectedServer,
RESET_SELECTED_SERVER,
2018-08-12 10:22:18 +03:00
SELECT_SERVER,
MAX_FALLBACK_VERSION,
MIN_FALLBACK_VERSION,
2018-08-12 10:22:18 +03:00
} from '../../../src/servers/reducers/selectedServer';
import { RESET_SHORT_URL_PARAMS } from '../../../src/short-urls/reducers/shortUrlsListParams';
describe('selectedServerReducer', () => {
2018-08-26 11:52:45 +03:00
describe('reducer', () => {
it('returns default when action is RESET_SELECTED_SERVER', () =>
2018-08-26 11:52:45 +03:00
expect(reducer(null, { type: RESET_SELECTED_SERVER })).toEqual(null));
it('returns selected server when action is SELECT_SERVER', () => {
const selectedServer = { id: 'abc123' };
2018-08-26 11:52:45 +03:00
expect(reducer(null, { type: SELECT_SERVER, selectedServer })).toEqual(selectedServer);
});
});
2018-08-12 10:22:18 +03:00
describe('resetSelectedServer', () => {
it('returns proper action', () => {
expect(resetSelectedServer()).toEqual({ type: RESET_SELECTED_SERVER });
});
});
describe('selectServer', () => {
2018-08-12 10:22:18 +03:00
const selectedServer = {
id: 'abc123',
2018-08-12 10:22:18 +03:00
};
const version = '1.19.0';
const createGetStateMock = (id) => jest.fn().mockReturnValue({ servers: { [id]: selectedServer } });
const apiClientMock = {
health: jest.fn(),
};
const buildApiClient = jest.fn().mockReturnValue(apiClientMock);
const dispatch = jest.fn();
const loadMercureInfo = jest.fn();
2019-10-05 20:31:47 +03:00
afterEach(jest.clearAllMocks);
2018-08-12 10:22:18 +03:00
it.each([
2020-03-05 13:58:35 +03:00
[ version, version, `v${version}` ],
[ 'latest', MAX_FALLBACK_VERSION, 'latest' ],
[ '%invalid_semver%', MIN_FALLBACK_VERSION, '%invalid_semver%' ],
])('dispatches proper actions', async (serverVersion, expectedVersion, expectedPrintableVersion) => {
const id = uuid();
const getState = createGetStateMock(id);
const expectedSelectedServer = {
...selectedServer,
version: expectedVersion,
2020-03-05 13:58:35 +03:00
printableVersion: expectedPrintableVersion,
};
2018-08-12 10:22:18 +03:00
apiClientMock.health.mockResolvedValue({ version: serverVersion });
await selectServer(buildApiClient, loadMercureInfo)(id)(dispatch, getState);
2018-08-12 10:22:18 +03:00
expect(dispatch).toHaveBeenCalledTimes(4);
2020-03-08 14:57:01 +03:00
expect(dispatch).toHaveBeenNthCalledWith(1, { type: RESET_SELECTED_SERVER });
expect(dispatch).toHaveBeenNthCalledWith(2, { type: RESET_SHORT_URL_PARAMS });
expect(dispatch).toHaveBeenNthCalledWith(3, { type: SELECT_SERVER, selectedServer: expectedSelectedServer });
expect(loadMercureInfo).toHaveBeenCalledTimes(1);
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(buildApiClient, loadMercureInfo)(id)(() => {}, 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);
2020-03-15 11:56:16 +03:00
const expectedSelectedServer = { ...selectedServer, serverNotReachable: true };
apiClientMock.health.mockRejectedValue({});
await selectServer(buildApiClient, loadMercureInfo)(id)(dispatch, getState);
2020-03-08 14:57:01 +03:00
expect(apiClientMock.health).toHaveBeenCalled();
expect(dispatch).toHaveBeenNthCalledWith(3, { type: SELECT_SERVER, selectedServer: expectedSelectedServer });
expect(loadMercureInfo).not.toHaveBeenCalled();
2020-03-08 14:57:01 +03:00
});
it('dispatches error when server is not found', async () => {
const id = uuid();
const getState = jest.fn(() => ({ servers: {} }));
2020-03-08 14:57:01 +03:00
const expectedSelectedServer = { serverNotFound: true };
await selectServer(buildApiClient, loadMercureInfo)(id)(dispatch, getState);
2020-03-08 14:57:01 +03:00
expect(getState).toHaveBeenCalled();
2020-03-08 14:57:01 +03:00
expect(apiClientMock.health).not.toHaveBeenCalled();
expect(dispatch).toHaveBeenNthCalledWith(3, { type: SELECT_SERVER, selectedServer: expectedSelectedServer });
expect(loadMercureInfo).not.toHaveBeenCalled();
});
2018-08-12 10:22:18 +03:00
});
});