Stop injecting dependencies in selectServer action

This commit is contained in:
Alejandro Celaya
2025-11-14 08:50:08 +01:00
parent e9951e95a9
commit b295240d28
6 changed files with 33 additions and 37 deletions

View File

@@ -7,14 +7,13 @@ import {
MIN_FALLBACK_VERSION,
resetSelectedServer,
selectedServerReducer as reducer,
selectServer as selectServerCreator,
selectServer,
} from '../../../src/servers/reducers/selectedServer';
describe('selectedServerReducer', () => {
const dispatch = vi.fn();
const health = vi.fn();
const buildApiClient = vi.fn().mockReturnValue(fromPartial<ShlinkApiClient>({ health }));
const selectServer = selectServerCreator(buildApiClient);
const buildShlinkApiClient = vi.fn().mockReturnValue(fromPartial<ShlinkApiClient>({ health }));
describe('reducer', () => {
it('returns default when action is RESET_SELECTED_SERVER', () =>
@@ -22,7 +21,7 @@ describe('selectedServerReducer', () => {
it('returns selected server when action is SELECT_SERVER', () => {
const payload = fromPartial<RegularServer>({ id: 'abc123' });
expect(reducer(null, selectServer.fulfilled(payload, '', ''))).toEqual(payload);
expect(reducer(null, selectServer.fulfilled(payload, '', { serverId: '', buildShlinkApiClient }))).toEqual(payload);
});
});
@@ -49,10 +48,10 @@ describe('selectedServerReducer', () => {
health.mockResolvedValue({ version: serverVersion });
await selectServer(id)(dispatch, getState, {});
await selectServer({ serverId: id, buildShlinkApiClient })(dispatch, getState, {});
expect(getState).toHaveBeenCalledTimes(1);
expect(buildApiClient).toHaveBeenCalledTimes(1);
expect(buildShlinkApiClient).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledTimes(3); // "Pending", "reset" and "fulfilled"
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({ payload: expectedSelectedServer }));
});
@@ -64,7 +63,7 @@ describe('selectedServerReducer', () => {
health.mockRejectedValue({});
await selectServer(id)(dispatch, getState, {});
await selectServer({ serverId: id, buildShlinkApiClient })(dispatch, getState, {});
expect(health).toHaveBeenCalled();
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({ payload: expectedSelectedServer }));
@@ -75,7 +74,7 @@ describe('selectedServerReducer', () => {
const getState = vi.fn(() => fromPartial<ShlinkState>({ servers: {} }));
const expectedSelectedServer: NotFoundServer = { serverNotFound: true };
await selectServer(id)(dispatch, getState, {});
await selectServer({ serverId: id, buildShlinkApiClient })(dispatch, getState, {});
expect(getState).toHaveBeenCalled();
expect(health).not.toHaveBeenCalled();