Removed redundant createServer action, leaving just createServers

This commit is contained in:
Alejandro Celaya 2022-11-05 09:40:12 +01:00
parent 90ef41b419
commit 6f67f7bbf0
5 changed files with 11 additions and 24 deletions

View file

@ -13,7 +13,7 @@ import { DuplicatedServersModal } from './helpers/DuplicatedServersModal';
const SHOW_IMPORT_MSG_TIME = 4000; const SHOW_IMPORT_MSG_TIME = 4000;
interface CreateServerProps { interface CreateServerProps {
createServer: (server: ServerWithId) => void; createServers: (servers: ServerWithId[]) => void;
servers: ServersMap; servers: ServersMap;
} }
@ -27,7 +27,7 @@ const ImportResult = ({ type }: { type: 'error' | 'success' }) => (
); );
export const CreateServer = (ImportServersBtn: FC<ImportServersBtnProps>, useTimeoutToggle: TimeoutToggle) => ( export const CreateServer = (ImportServersBtn: FC<ImportServersBtnProps>, useTimeoutToggle: TimeoutToggle) => (
{ servers, createServer }: CreateServerProps, { servers, createServers }: CreateServerProps,
) => { ) => {
const navigate = useNavigate(); const navigate = useNavigate();
const goBack = useGoBack(); const goBack = useGoBack();
@ -43,7 +43,7 @@ export const CreateServer = (ImportServersBtn: FC<ImportServersBtnProps>, useTim
const id = uuid(); const id = uuid();
createServer({ ...serverData, id }); createServers([{ ...serverData, id }]);
navigate(`/server/${id}`); navigate(`/server/${id}`);
}; };

View file

@ -66,8 +66,6 @@ export const createServers = pipe(
(newServers: ServersMap) => ({ type: CREATE_SERVERS, payload: newServers }), (newServers: ServersMap) => ({ type: CREATE_SERVERS, payload: newServers }),
); );
export const createServer = (server: ServerWithId) => createServers([server]);
export const editServer = (serverId: string, serverData: Partial<ServerData>) => ({ export const editServer = (serverId: string, serverData: Partial<ServerData>) => ({
type: EDIT_SERVER, type: EDIT_SERVER,
payload: { serverId, serverData }, payload: { serverId, serverData },

View file

@ -6,7 +6,7 @@ import { DeleteServerButton } from '../DeleteServerButton';
import { EditServer } from '../EditServer'; import { EditServer } from '../EditServer';
import { ImportServersBtn } from '../helpers/ImportServersBtn'; import { ImportServersBtn } from '../helpers/ImportServersBtn';
import { resetSelectedServer, selectServer } from '../reducers/selectedServer'; import { resetSelectedServer, selectServer } from '../reducers/selectedServer';
import { createServer, createServers, deleteServer, editServer, setAutoConnect } from '../reducers/servers'; import { createServers, deleteServer, editServer, setAutoConnect } from '../reducers/servers';
import { fetchServers } from '../reducers/remoteServers'; import { fetchServers } from '../reducers/remoteServers';
import { ServerError } from '../helpers/ServerError'; import { ServerError } from '../helpers/ServerError';
import { ConnectDecorator } from '../../container/types'; import { ConnectDecorator } from '../../container/types';
@ -38,7 +38,7 @@ const provideServices = (bottle: Bottle, connect: ConnectDecorator) => {
bottle.serviceFactory('CreateServer', CreateServer, 'ImportServersBtn', 'useTimeoutToggle'); bottle.serviceFactory('CreateServer', CreateServer, 'ImportServersBtn', 'useTimeoutToggle');
bottle.decorator('CreateServer', withoutSelectedServer); bottle.decorator('CreateServer', withoutSelectedServer);
bottle.decorator('CreateServer', connect(['selectedServer', 'servers'], ['createServer', 'resetSelectedServer'])); bottle.decorator('CreateServer', connect(['selectedServer', 'servers'], ['createServers', 'resetSelectedServer']));
bottle.serviceFactory('EditServer', EditServer, 'ServerError'); bottle.serviceFactory('EditServer', EditServer, 'ServerError');
bottle.decorator('EditServer', connect(['selectedServer'], ['editServer', 'selectServer', 'resetSelectedServer'])); bottle.decorator('EditServer', connect(['selectedServer'], ['editServer', 'selectServer', 'resetSelectedServer']));
@ -70,7 +70,6 @@ const provideServices = (bottle: Bottle, connect: ConnectDecorator) => {
// Actions // Actions
bottle.serviceFactory('selectServer', selectServer, 'buildShlinkApiClient', 'loadMercureInfo'); bottle.serviceFactory('selectServer', selectServer, 'buildShlinkApiClient', 'loadMercureInfo');
bottle.serviceFactory('createServer', () => createServer);
bottle.serviceFactory('createServers', () => createServers); bottle.serviceFactory('createServers', () => createServers);
bottle.serviceFactory('deleteServer', () => deleteServer); bottle.serviceFactory('deleteServer', () => deleteServer);
bottle.serviceFactory('editServer', () => editServer); bottle.serviceFactory('editServer', () => editServer);

View file

@ -8,7 +8,7 @@ import { renderWithEvents } from '../__helpers__/setUpTest';
jest.mock('react-router-dom', () => ({ ...jest.requireActual('react-router-dom'), useNavigate: jest.fn() })); jest.mock('react-router-dom', () => ({ ...jest.requireActual('react-router-dom'), useNavigate: jest.fn() }));
describe('<CreateServer />', () => { describe('<CreateServer />', () => {
const createServerMock = jest.fn(); const createServersMock = jest.fn();
const navigate = jest.fn(); const navigate = jest.fn();
const servers = { foo: Mock.of<ServerWithId>({ url: 'https://existing_url.com', apiKey: 'existing_api_key' }) }; const servers = { foo: Mock.of<ServerWithId>({ url: 'https://existing_url.com', apiKey: 'existing_api_key' }) };
const setUp = (serversImported = false, importFailed = false) => { const setUp = (serversImported = false, importFailed = false) => {
@ -22,7 +22,7 @@ describe('<CreateServer />', () => {
}); });
const CreateServer = createCreateServer(() => <>ImportServersBtn</>, useTimeoutToggle); const CreateServer = createCreateServer(() => <>ImportServersBtn</>, useTimeoutToggle);
return renderWithEvents(<CreateServer createServer={createServerMock} servers={servers} />); return renderWithEvents(<CreateServer createServers={createServersMock} servers={servers} />);
}; };
beforeEach(jest.clearAllMocks); beforeEach(jest.clearAllMocks);
@ -48,18 +48,18 @@ describe('<CreateServer />', () => {
it('creates server data when form is submitted', async () => { it('creates server data when form is submitted', async () => {
const { user } = setUp(); const { user } = setUp();
expect(createServerMock).not.toHaveBeenCalled(); expect(createServersMock).not.toHaveBeenCalled();
await user.type(screen.getByLabelText(/^Name/), 'the_name'); await user.type(screen.getByLabelText(/^Name/), 'the_name');
await user.type(screen.getByLabelText(/^URL/), 'https://the_url.com'); await user.type(screen.getByLabelText(/^URL/), 'https://the_url.com');
await user.type(screen.getByLabelText(/^API key/), 'the_api_key'); await user.type(screen.getByLabelText(/^API key/), 'the_api_key');
fireEvent.submit(screen.getByRole('form')); fireEvent.submit(screen.getByRole('form'));
expect(createServerMock).toHaveBeenCalledWith(expect.objectContaining({ expect(createServersMock).toHaveBeenCalledWith([expect.objectContaining({
name: 'the_name', name: 'the_name',
url: 'https://the_url.com', url: 'https://the_url.com',
apiKey: 'the_api_key', apiKey: 'the_api_key',
})); })]);
expect(navigate).toHaveBeenCalledWith(expect.stringMatching(/^\/server\//)); expect(navigate).toHaveBeenCalledWith(expect.stringMatching(/^\/server\//));
expect(screen.queryByRole('dialog')).not.toBeInTheDocument(); expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
}); });
@ -75,7 +75,7 @@ describe('<CreateServer />', () => {
await waitFor(() => expect(screen.getByRole('dialog')).toBeInTheDocument()); await waitFor(() => expect(screen.getByRole('dialog')).toBeInTheDocument());
await user.click(screen.getByRole('button', { name: 'Discard' })); await user.click(screen.getByRole('button', { name: 'Discard' }));
expect(createServerMock).not.toHaveBeenCalled(); expect(createServersMock).not.toHaveBeenCalled();
expect(navigate).toHaveBeenCalledWith(-1); expect(navigate).toHaveBeenCalledWith(-1);
}); });
}); });

View file

@ -1,7 +1,6 @@
import { dissoc, values } from 'ramda'; import { dissoc, values } from 'ramda';
import { Mock } from 'ts-mockery'; import { Mock } from 'ts-mockery';
import reducer, { import reducer, {
createServer,
deleteServer, deleteServer,
createServers, createServers,
editServer, editServer,
@ -104,15 +103,6 @@ describe('serversReducer', () => {
}); });
describe('action creators', () => { describe('action creators', () => {
describe('createServer', () => {
it('returns expected action', () => {
const serverToCreate = Mock.of<RegularServer>({ id: 'abc123' });
const result = createServer(serverToCreate);
expect(result).toEqual(expect.objectContaining({ type: CREATE_SERVERS }));
});
});
describe('editServer', () => { describe('editServer', () => {
it('returns expected action', () => { it('returns expected action', () => {
const serverData = { name: 'edited' }; const serverData = { name: 'edited' };