2020-03-16 20:51:04 +03:00
|
|
|
import { v4 as uuid } from 'uuid';
|
2018-08-26 11:52:45 +03:00
|
|
|
import reducer, {
|
2018-12-18 21:45:09 +03:00
|
|
|
selectServer,
|
2018-08-12 10:22:18 +03:00
|
|
|
resetSelectedServer,
|
2018-12-18 21:45:09 +03:00
|
|
|
RESET_SELECTED_SERVER,
|
2018-08-12 10:22:18 +03:00
|
|
|
SELECT_SERVER,
|
2019-10-18 18:39:38 +03:00
|
|
|
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', () => {
|
2018-08-12 10:34:14 +03:00
|
|
|
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));
|
2018-08-12 10:34:14 +03:00
|
|
|
|
|
|
|
it('returns selected server when action is SELECT_SERVER', () => {
|
|
|
|
const selectedServer = { id: 'abc123' };
|
2018-08-26 00:39:27 +03:00
|
|
|
|
2018-08-26 11:52:45 +03:00
|
|
|
expect(reducer(null, { type: SELECT_SERVER, selectedServer })).toEqual(selectedServer);
|
2018-08-12 10:34:14 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-08-12 10:22:18 +03:00
|
|
|
describe('resetSelectedServer', () => {
|
|
|
|
it('returns proper action', () => {
|
|
|
|
expect(resetSelectedServer()).toEqual({ type: RESET_SELECTED_SERVER });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-08-12 10:34:14 +03:00
|
|
|
describe('selectServer', () => {
|
2018-08-12 10:22:18 +03:00
|
|
|
const selectedServer = {
|
2020-03-16 20:51:04 +03:00
|
|
|
id: 'abc123',
|
2018-08-12 10:22:18 +03:00
|
|
|
};
|
2019-10-05 11:20:33 +03:00
|
|
|
const version = '1.19.0';
|
2018-08-12 10:22:18 +03:00
|
|
|
const ServersServiceMock = {
|
2019-04-19 11:29:49 +03:00
|
|
|
findServerById: jest.fn(() => selectedServer),
|
2018-08-12 10:22:18 +03:00
|
|
|
};
|
2019-10-05 11:20:33 +03:00
|
|
|
const apiClientMock = {
|
2019-10-18 18:39:38 +03:00
|
|
|
health: jest.fn(),
|
2019-10-05 11:20:33 +03:00
|
|
|
};
|
2020-03-05 11:23:53 +03:00
|
|
|
const buildApiClient = jest.fn().mockReturnValue(apiClientMock);
|
2019-10-18 18:39:38 +03:00
|
|
|
const dispatch = jest.fn();
|
2019-10-05 20:31:47 +03:00
|
|
|
|
2019-10-18 18:39:38 +03:00
|
|
|
afterEach(jest.clearAllMocks);
|
2018-08-12 10:22:18 +03:00
|
|
|
|
2020-02-17 20:21:52 +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) => {
|
2019-10-05 11:20:33 +03:00
|
|
|
const expectedSelectedServer = {
|
|
|
|
...selectedServer,
|
2019-10-18 18:39:38 +03:00
|
|
|
version: expectedVersion,
|
2020-03-05 13:58:35 +03:00
|
|
|
printableVersion: expectedPrintableVersion,
|
2019-10-05 11:20:33 +03:00
|
|
|
};
|
2018-08-12 10:22:18 +03:00
|
|
|
|
2019-10-18 18:39:38 +03:00
|
|
|
apiClientMock.health.mockResolvedValue({ version: serverVersion });
|
|
|
|
|
2020-03-16 20:51:04 +03:00
|
|
|
await selectServer(ServersServiceMock, buildApiClient)(uuid())(dispatch);
|
2018-08-12 10:22:18 +03:00
|
|
|
|
2020-03-08 14:57:01 +03:00
|
|
|
expect(dispatch).toHaveBeenCalledTimes(3);
|
|
|
|
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 });
|
2018-08-12 10:22:18 +03:00
|
|
|
});
|
|
|
|
|
2019-10-05 20:31:47 +03:00
|
|
|
it('invokes dependencies', async () => {
|
2020-03-16 20:51:04 +03:00
|
|
|
await selectServer(ServersServiceMock, buildApiClient)(uuid())(() => {});
|
2018-08-12 10:22:18 +03:00
|
|
|
|
2019-04-19 11:29:49 +03:00
|
|
|
expect(ServersServiceMock.findServerById).toHaveBeenCalledTimes(1);
|
2019-10-05 20:31:47 +03:00
|
|
|
expect(buildApiClient).toHaveBeenCalledTimes(1);
|
2018-08-12 10:22:18 +03:00
|
|
|
});
|
2019-10-18 18:39:38 +03:00
|
|
|
|
2020-03-08 14:57:01 +03:00
|
|
|
it('dispatches error when health endpoint fails', async () => {
|
2020-03-15 11:56:16 +03:00
|
|
|
const expectedSelectedServer = { ...selectedServer, serverNotReachable: true };
|
2019-10-18 18:39:38 +03:00
|
|
|
|
|
|
|
apiClientMock.health.mockRejectedValue({});
|
|
|
|
|
2020-03-16 20:51:04 +03:00
|
|
|
await selectServer(ServersServiceMock, buildApiClient)(uuid())(dispatch);
|
2019-10-18 18:39:38 +03:00
|
|
|
|
2020-03-08 14:57:01 +03:00
|
|
|
expect(apiClientMock.health).toHaveBeenCalled();
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(3, { type: SELECT_SERVER, selectedServer: expectedSelectedServer });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('dispatches error when server is not found', async () => {
|
|
|
|
const expectedSelectedServer = { serverNotFound: true };
|
|
|
|
|
|
|
|
ServersServiceMock.findServerById.mockReturnValue(undefined);
|
|
|
|
|
2020-03-16 20:51:04 +03:00
|
|
|
await selectServer(ServersServiceMock, buildApiClient)(uuid())(dispatch);
|
2020-03-08 14:57:01 +03:00
|
|
|
|
|
|
|
expect(ServersServiceMock.findServerById).toHaveBeenCalled();
|
|
|
|
expect(apiClientMock.health).not.toHaveBeenCalled();
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(3, { type: SELECT_SERVER, selectedServer: expectedSelectedServer });
|
2019-10-18 18:39:38 +03:00
|
|
|
});
|
2018-08-12 10:22:18 +03:00
|
|
|
});
|
|
|
|
});
|