mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-10 09:33:51 +00:00
Refactor of redux tests to avoid covering RTK implementation details
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import { Mock } from 'ts-mockery';
|
||||
import type { HttpClient } from '../../../src/common/services/HttpClient';
|
||||
import { fetchServers } from '../../../src/servers/reducers/remoteServers';
|
||||
import { createServers } from '../../../src/servers/reducers/servers';
|
||||
|
||||
describe('remoteServersReducer', () => {
|
||||
afterEach(jest.clearAllMocks);
|
||||
@@ -84,13 +83,9 @@ describe('remoteServersReducer', () => {
|
||||
|
||||
await doFetchServers()(dispatch, jest.fn(), {});
|
||||
|
||||
expect(dispatch).toHaveBeenNthCalledWith(1, expect.objectContaining({
|
||||
type: doFetchServers.pending.toString(),
|
||||
}));
|
||||
expect(dispatch).toHaveBeenNthCalledWith(2, { type: createServers.toString(), payload: expectedNewServers });
|
||||
expect(dispatch).toHaveBeenNthCalledWith(3, expect.objectContaining({
|
||||
type: doFetchServers.fulfilled.toString(),
|
||||
}));
|
||||
expect(dispatch).toHaveBeenCalledTimes(3);
|
||||
expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({ payload: expectedNewServers }));
|
||||
expect(dispatch).toHaveBeenNthCalledWith(3, expect.objectContaining({ payload: undefined }));
|
||||
expect(fetchJson).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -23,18 +23,12 @@ describe('selectedServerReducer', () => {
|
||||
|
||||
describe('reducer', () => {
|
||||
it('returns default when action is RESET_SELECTED_SERVER', () =>
|
||||
expect(reducer(null, { type: resetSelectedServer.toString(), payload: null })).toBeNull());
|
||||
expect(reducer(null, resetSelectedServer())).toBeNull());
|
||||
|
||||
it('returns selected server when action is SELECT_SERVER', () => {
|
||||
const payload = Mock.of<RegularServer>({ id: 'abc123' });
|
||||
|
||||
expect(reducer(null, { type: selectServer.fulfilled.toString(), payload })).toEqual(payload);
|
||||
});
|
||||
});
|
||||
|
||||
describe('resetSelectedServer', () => {
|
||||
it('returns proper action', () => {
|
||||
expect(resetSelectedServer()).toEqual({ type: resetSelectedServer.toString() });
|
||||
expect(reducer(null, selectServer.fulfilled(payload, '', ''))).toEqual(payload);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -63,23 +57,10 @@ describe('selectedServerReducer', () => {
|
||||
|
||||
await selectServer(id)(dispatch, getState, {});
|
||||
|
||||
expect(dispatch).toHaveBeenCalledTimes(3);
|
||||
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(id)(jest.fn(), getState, {});
|
||||
|
||||
expect(getState).toHaveBeenCalledTimes(1);
|
||||
expect(buildApiClient).toHaveBeenCalledTimes(1);
|
||||
expect(dispatch).toHaveBeenCalledTimes(3); // "Pending", "reset" and "fulfilled"
|
||||
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({ payload: expectedSelectedServer }));
|
||||
});
|
||||
|
||||
it('dispatches error when health endpoint fails', async () => {
|
||||
@@ -92,10 +73,7 @@ describe('selectedServerReducer', () => {
|
||||
await selectServer(id)(dispatch, getState, {});
|
||||
|
||||
expect(health).toHaveBeenCalled();
|
||||
expect(dispatch).toHaveBeenNthCalledWith(3, expect.objectContaining({
|
||||
type: selectServer.fulfilled.toString(),
|
||||
payload: expectedSelectedServer,
|
||||
}));
|
||||
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({ payload: expectedSelectedServer }));
|
||||
});
|
||||
|
||||
it('dispatches error when server is not found', async () => {
|
||||
@@ -107,10 +85,7 @@ describe('selectedServerReducer', () => {
|
||||
|
||||
expect(getState).toHaveBeenCalled();
|
||||
expect(health).not.toHaveBeenCalled();
|
||||
expect(dispatch).toHaveBeenNthCalledWith(3, expect.objectContaining({
|
||||
type: selectServer.fulfilled.toString(),
|
||||
payload: expectedSelectedServer,
|
||||
}));
|
||||
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({ payload: expectedSelectedServer }));
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { dissoc, values } from 'ramda';
|
||||
import { Mock } from 'ts-mockery';
|
||||
import type { RegularServer } from '../../../src/servers/data';
|
||||
import type { RegularServer, ServerWithId } from '../../../src/servers/data';
|
||||
import {
|
||||
createServers,
|
||||
deleteServer,
|
||||
@@ -19,38 +19,24 @@ describe('serversReducer', () => {
|
||||
|
||||
describe('reducer', () => {
|
||||
it('returns edited server when action is EDIT_SERVER', () =>
|
||||
expect(serversReducer(list, {
|
||||
type: editServer.toString(),
|
||||
payload: { serverId: 'abc123', serverData: { foo: 'foo' } },
|
||||
})).toEqual({
|
||||
abc123: { id: 'abc123', foo: 'foo' },
|
||||
expect(serversReducer(list, editServer('abc123', { name: 'foo' }))).toEqual({
|
||||
abc123: { id: 'abc123', name: 'foo' },
|
||||
def456: { id: 'def456' },
|
||||
}));
|
||||
|
||||
it('returns as it is when action is EDIT_SERVER and server does not exist', () =>
|
||||
expect(serversReducer(list, {
|
||||
type: editServer.toString(),
|
||||
payload: { serverId: 'invalid', serverData: { foo: 'foo' } },
|
||||
})).toEqual({
|
||||
expect(serversReducer(list, editServer('invalid', { name: 'foo' }))).toEqual({
|
||||
abc123: { id: 'abc123' },
|
||||
def456: { id: 'def456' },
|
||||
}));
|
||||
|
||||
it('removes server when action is DELETE_SERVER', () =>
|
||||
expect(serversReducer(list, {
|
||||
type: deleteServer.toString(),
|
||||
payload: { id: 'abc123' },
|
||||
})).toEqual({
|
||||
expect(serversReducer(list, deleteServer(Mock.of<ServerWithId>({ id: 'abc123' })))).toEqual({
|
||||
def456: { id: 'def456' },
|
||||
}));
|
||||
|
||||
it('appends server when action is CREATE_SERVERS', () =>
|
||||
expect(serversReducer(list, {
|
||||
type: createServers.toString(),
|
||||
payload: {
|
||||
ghi789: { id: 'ghi789' },
|
||||
},
|
||||
})).toEqual({
|
||||
expect(serversReducer(list, createServers([Mock.of<ServerWithId>({ id: 'ghi789' })]))).toEqual({
|
||||
abc123: { id: 'abc123' },
|
||||
def456: { id: 'def456' },
|
||||
ghi789: { id: 'ghi789' },
|
||||
@@ -60,10 +46,7 @@ describe('serversReducer', () => {
|
||||
[true],
|
||||
[false],
|
||||
])('returns state as it is when trying to set auto-connect on invalid server', (autoConnect) =>
|
||||
expect(serversReducer(list, {
|
||||
type: setAutoConnect.toString(),
|
||||
payload: { serverId: 'invalid', autoConnect },
|
||||
})).toEqual({
|
||||
expect(serversReducer(list, setAutoConnect(Mock.of<ServerWithId>({ id: 'invalid' }), autoConnect))).toEqual({
|
||||
abc123: { id: 'abc123' },
|
||||
def456: { id: 'def456' },
|
||||
}));
|
||||
@@ -74,10 +57,10 @@ describe('serversReducer', () => {
|
||||
abc123: { ...list.abc123, autoConnect: true },
|
||||
};
|
||||
|
||||
expect(serversReducer(listWithDisabledAutoConnect, {
|
||||
type: setAutoConnect.toString(),
|
||||
payload: { serverId: 'abc123', autoConnect: false },
|
||||
})).toEqual({
|
||||
expect(serversReducer(
|
||||
listWithDisabledAutoConnect,
|
||||
setAutoConnect(Mock.of<ServerWithId>({ id: 'abc123' }), false),
|
||||
)).toEqual({
|
||||
abc123: { id: 'abc123', autoConnect: false },
|
||||
def456: { id: 'def456' },
|
||||
});
|
||||
@@ -89,10 +72,10 @@ describe('serversReducer', () => {
|
||||
abc123: { ...list.abc123, autoConnect: true },
|
||||
};
|
||||
|
||||
expect(serversReducer(listWithEnabledAutoConnect, {
|
||||
type: setAutoConnect.toString(),
|
||||
payload: { serverId: 'def456', autoConnect: true },
|
||||
})).toEqual({
|
||||
expect(serversReducer(
|
||||
listWithEnabledAutoConnect,
|
||||
setAutoConnect(Mock.of<ServerWithId>({ id: 'def456' }), true),
|
||||
)).toEqual({
|
||||
abc123: { id: 'abc123', autoConnect: false },
|
||||
def456: { id: 'def456', autoConnect: true },
|
||||
});
|
||||
@@ -103,33 +86,27 @@ describe('serversReducer', () => {
|
||||
describe('editServer', () => {
|
||||
it('returns expected action', () => {
|
||||
const serverData = { name: 'edited' };
|
||||
const result = editServer('123', serverData);
|
||||
const { payload } = editServer('123', serverData);
|
||||
|
||||
expect(result).toEqual({
|
||||
type: editServer.toString(),
|
||||
payload: { serverId: '123', serverData },
|
||||
});
|
||||
expect(payload).toEqual({ serverId: '123', serverData });
|
||||
});
|
||||
});
|
||||
|
||||
describe('deleteServer', () => {
|
||||
it('returns expected action', () => {
|
||||
const serverToDelete = Mock.of<RegularServer>({ id: 'abc123' });
|
||||
const result = deleteServer(serverToDelete);
|
||||
const { payload } = deleteServer(serverToDelete);
|
||||
|
||||
expect(result).toEqual({
|
||||
type: deleteServer.toString(),
|
||||
payload: { id: 'abc123' },
|
||||
});
|
||||
expect(payload).toEqual({ id: 'abc123' });
|
||||
});
|
||||
});
|
||||
|
||||
describe('createServers', () => {
|
||||
it('returns expected action', () => {
|
||||
const newServers = values(list);
|
||||
const result = createServers(newServers);
|
||||
const { payload } = createServers(newServers);
|
||||
|
||||
expect(result).toEqual(expect.objectContaining({ type: createServers.toString() }));
|
||||
expect(payload).toEqual(list);
|
||||
});
|
||||
|
||||
it('generates an id for every provided server if they do not have it', () => {
|
||||
@@ -146,12 +123,9 @@ describe('serversReducer', () => {
|
||||
[false],
|
||||
])('returns expected action', (autoConnect) => {
|
||||
const serverToEdit = Mock.of<RegularServer>({ id: 'abc123' });
|
||||
const result = setAutoConnect(serverToEdit, autoConnect);
|
||||
const { payload } = setAutoConnect(serverToEdit, autoConnect);
|
||||
|
||||
expect(result).toEqual({
|
||||
type: setAutoConnect.toString(),
|
||||
payload: { serverId: 'abc123', autoConnect },
|
||||
});
|
||||
expect(payload).toEqual({ serverId: 'abc123', autoConnect });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user