mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-12 18:43:50 +00:00
Migrated selectServer action to RTK and moved loadMercureInfo to an action listener
This commit is contained in:
@@ -1,31 +1,36 @@
|
||||
import { v4 as uuid } from 'uuid';
|
||||
import { Mock } from 'ts-mockery';
|
||||
import reducer, {
|
||||
selectServer,
|
||||
selectServer as selectServerCreator,
|
||||
resetSelectedServer,
|
||||
RESET_SELECTED_SERVER,
|
||||
SELECT_SERVER,
|
||||
MAX_FALLBACK_VERSION,
|
||||
MIN_FALLBACK_VERSION,
|
||||
} from '../../../src/servers/reducers/selectedServer';
|
||||
import { ShlinkState } from '../../../src/container/types';
|
||||
import { NonReachableServer, NotFoundServer, RegularServer } from '../../../src/servers/data';
|
||||
import { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
|
||||
|
||||
describe('selectedServerReducer', () => {
|
||||
const health = jest.fn();
|
||||
const buildApiClient = jest.fn().mockReturnValue(Mock.of<ShlinkApiClient>({ health }));
|
||||
const selectServer = selectServerCreator(buildApiClient);
|
||||
|
||||
afterEach(jest.clearAllMocks);
|
||||
|
||||
describe('reducer', () => {
|
||||
it('returns default when action is RESET_SELECTED_SERVER', () =>
|
||||
expect(reducer(null, { type: RESET_SELECTED_SERVER, payload: null })).toBeNull());
|
||||
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: SELECT_SERVER, payload })).toEqual(payload);
|
||||
expect(reducer(null, { type: selectServer.fulfilled.toString(), payload })).toEqual(payload);
|
||||
});
|
||||
});
|
||||
|
||||
describe('resetSelectedServer', () => {
|
||||
it('returns proper action', () => {
|
||||
expect(resetSelectedServer()).toEqual({ type: RESET_SELECTED_SERVER });
|
||||
expect(resetSelectedServer()).toEqual({ type: resetSelectedServer.toString() });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -35,14 +40,7 @@ describe('selectedServerReducer', () => {
|
||||
};
|
||||
const version = '1.19.0';
|
||||
const createGetStateMock = (id: string) => 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();
|
||||
|
||||
afterEach(jest.clearAllMocks);
|
||||
|
||||
it.each([
|
||||
[version, version, `v${version}`],
|
||||
@@ -57,21 +55,24 @@ describe('selectedServerReducer', () => {
|
||||
printableVersion: expectedPrintableVersion,
|
||||
};
|
||||
|
||||
apiClientMock.health.mockResolvedValue({ version: serverVersion });
|
||||
health.mockResolvedValue({ version: serverVersion });
|
||||
|
||||
await selectServer(buildApiClient, loadMercureInfo)(id)(dispatch, getState);
|
||||
await selectServer(id)(dispatch, getState, {});
|
||||
|
||||
expect(dispatch).toHaveBeenCalledTimes(3);
|
||||
expect(dispatch).toHaveBeenNthCalledWith(1, { type: RESET_SELECTED_SERVER });
|
||||
expect(dispatch).toHaveBeenNthCalledWith(2, { type: SELECT_SERVER, payload: expectedSelectedServer });
|
||||
expect(loadMercureInfo).toHaveBeenCalledTimes(1);
|
||||
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,
|
||||
}));
|
||||
});
|
||||
|
||||
it('invokes dependencies', async () => {
|
||||
const id = uuid();
|
||||
const getState = createGetStateMock(id);
|
||||
|
||||
await selectServer(buildApiClient, loadMercureInfo)(id)(jest.fn(), getState);
|
||||
await selectServer(id)(jest.fn(), getState, {});
|
||||
|
||||
expect(getState).toHaveBeenCalledTimes(1);
|
||||
expect(buildApiClient).toHaveBeenCalledTimes(1);
|
||||
@@ -82,13 +83,15 @@ describe('selectedServerReducer', () => {
|
||||
const getState = createGetStateMock(id);
|
||||
const expectedSelectedServer = Mock.of<NonReachableServer>({ ...selectedServer, serverNotReachable: true });
|
||||
|
||||
apiClientMock.health.mockRejectedValue({});
|
||||
health.mockRejectedValue({});
|
||||
|
||||
await selectServer(buildApiClient, loadMercureInfo)(id)(dispatch, getState);
|
||||
await selectServer(id)(dispatch, getState, {});
|
||||
|
||||
expect(apiClientMock.health).toHaveBeenCalled();
|
||||
expect(dispatch).toHaveBeenNthCalledWith(2, { type: SELECT_SERVER, payload: expectedSelectedServer });
|
||||
expect(loadMercureInfo).not.toHaveBeenCalled();
|
||||
expect(health).toHaveBeenCalled();
|
||||
expect(dispatch).toHaveBeenNthCalledWith(3, expect.objectContaining({
|
||||
type: selectServer.fulfilled.toString(),
|
||||
payload: expectedSelectedServer,
|
||||
}));
|
||||
});
|
||||
|
||||
it('dispatches error when server is not found', async () => {
|
||||
@@ -96,12 +99,14 @@ describe('selectedServerReducer', () => {
|
||||
const getState = jest.fn(() => Mock.of<ShlinkState>({ servers: {} }));
|
||||
const expectedSelectedServer: NotFoundServer = { serverNotFound: true };
|
||||
|
||||
await selectServer(buildApiClient, loadMercureInfo)(id)(dispatch, getState);
|
||||
await selectServer(id)(dispatch, getState, {});
|
||||
|
||||
expect(getState).toHaveBeenCalled();
|
||||
expect(apiClientMock.health).not.toHaveBeenCalled();
|
||||
expect(dispatch).toHaveBeenNthCalledWith(2, { type: SELECT_SERVER, payload: expectedSelectedServer });
|
||||
expect(loadMercureInfo).not.toHaveBeenCalled();
|
||||
expect(health).not.toHaveBeenCalled();
|
||||
expect(dispatch).toHaveBeenNthCalledWith(3, expect.objectContaining({
|
||||
type: selectServer.fulfilled.toString(),
|
||||
payload: expectedSelectedServer,
|
||||
}));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user