From 90ef41b41990c70cd05740463f5d2a3c8767deba Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 5 Nov 2022 09:24:12 +0100 Subject: [PATCH] Migrated server list actions to use payload prop --- src/servers/reducers/servers.ts | 43 +++++++++-------- test/servers/reducers/remoteServers.test.ts | 2 +- test/servers/reducers/servers.test.ts | 53 ++++++++++++--------- 3 files changed, 55 insertions(+), 43 deletions(-) diff --git a/src/servers/reducers/servers.ts b/src/servers/reducers/servers.ts index 31cd4d13..2f0a5057 100644 --- a/src/servers/reducers/servers.ts +++ b/src/servers/reducers/servers.ts @@ -1,6 +1,6 @@ +import { PayloadAction } from '@reduxjs/toolkit'; import { assoc, dissoc, fromPairs, map, pipe, reduce, toPairs } from 'ramda'; import { v4 as uuid } from 'uuid'; -import { Action } from 'redux'; import { ServerData, ServersMap, ServerWithId } from '../data'; import { buildReducer } from '../../utils/helpers/redux'; @@ -9,18 +9,16 @@ export const DELETE_SERVER = 'shlink/servers/DELETE_SERVER'; export const CREATE_SERVERS = 'shlink/servers/CREATE_SERVERS'; export const SET_AUTO_CONNECT = 'shlink/servers/SET_AUTO_CONNECT'; -export interface CreateServersAction extends Action { - newServers: ServersMap; -} +export type CreateServersAction = PayloadAction; -interface DeleteServerAction extends Action { +type DeleteServerAction = PayloadAction<{ serverId: string; -} +}>; -interface SetAutoConnectAction extends Action { +type SetAutoConnectAction = PayloadAction<{ serverId: string; autoConnect: boolean; -} +}>; const initialState: ServersMap = {}; @@ -33,12 +31,16 @@ const serverWithId = (server: ServerWithId | ServerData): ServerWithId => { }; export default buildReducer({ - [CREATE_SERVERS]: (state, { newServers }) => ({ ...state, ...newServers }), - [DELETE_SERVER]: (state, { serverId }) => dissoc(serverId, state), - [EDIT_SERVER]: (state, { serverId, serverData }: any) => ( - !state[serverId] ? state : assoc(serverId, { ...state[serverId], ...serverData }, state) - ), - [SET_AUTO_CONNECT]: (state, { serverId, autoConnect }) => { + [CREATE_SERVERS]: (state, { payload: newServers }) => ({ ...state, ...newServers }), + [DELETE_SERVER]: (state, { payload }) => dissoc(payload.serverId, state), + [EDIT_SERVER]: (state, { payload }: any) => { + const { serverId, serverData } = payload; + return ( + !state[serverId] ? state : assoc(serverId, { ...state[serverId], ...serverData }, state) + ); + }, + [SET_AUTO_CONNECT]: (state, { payload }) => { + const { serverId, autoConnect } = payload; if (!state[serverId]) { return state; } @@ -61,21 +63,22 @@ const serversListToMap = reduce((acc, server) => assoc export const createServers = pipe( map(serverWithId), serversListToMap, - (newServers: ServersMap) => ({ type: CREATE_SERVERS, newServers }), + (newServers: ServersMap) => ({ type: CREATE_SERVERS, payload: newServers }), ); export const createServer = (server: ServerWithId) => createServers([server]); export const editServer = (serverId: string, serverData: Partial) => ({ type: EDIT_SERVER, - serverId, - serverData, + payload: { serverId, serverData }, }); -export const deleteServer = ({ id }: ServerWithId): DeleteServerAction => ({ type: DELETE_SERVER, serverId: id }); +export const deleteServer = ({ id }: ServerWithId): DeleteServerAction => ({ + type: DELETE_SERVER, + payload: { serverId: id }, +}); export const setAutoConnect = ({ id }: ServerWithId, autoConnect: boolean): SetAutoConnectAction => ({ type: SET_AUTO_CONNECT, - serverId: id, - autoConnect, + payload: { serverId: id, autoConnect }, }); diff --git a/test/servers/reducers/remoteServers.test.ts b/test/servers/reducers/remoteServers.test.ts index 56310e09..815e5490 100644 --- a/test/servers/reducers/remoteServers.test.ts +++ b/test/servers/reducers/remoteServers.test.ts @@ -87,7 +87,7 @@ describe('remoteServersReducer', () => { await fetchServers(axios)()(dispatch); - expect(dispatch).toHaveBeenCalledWith({ type: CREATE_SERVERS, newServers: expectedNewServers }); + expect(dispatch).toHaveBeenCalledWith({ type: CREATE_SERVERS, payload: expectedNewServers }); expect(get).toHaveBeenCalledTimes(1); }); }); diff --git a/test/servers/reducers/servers.test.ts b/test/servers/reducers/servers.test.ts index e39d3561..11f36655 100644 --- a/test/servers/reducers/servers.test.ts +++ b/test/servers/reducers/servers.test.ts @@ -13,7 +13,7 @@ import reducer, { } from '../../../src/servers/reducers/servers'; import { RegularServer } from '../../../src/servers/data'; -describe('serverReducer', () => { +describe('serversReducer', () => { const list = { abc123: Mock.of({ id: 'abc123' }), def456: Mock.of({ id: 'def456' }), @@ -23,32 +23,35 @@ describe('serverReducer', () => { describe('reducer', () => { it('returns edited server when action is EDIT_SERVER', () => - expect(reducer( - list, - { type: EDIT_SERVER, serverId: 'abc123', serverData: { foo: 'foo' } } as any, - )).toEqual({ + expect(reducer(list, { + type: EDIT_SERVER, + payload: { serverId: 'abc123', serverData: { foo: 'foo' } }, + } as any)).toEqual({ abc123: { id: 'abc123', foo: 'foo' }, def456: { id: 'def456' }, })); it('returns as it is when action is EDIT_SERVER and server does not exist', () => - expect(reducer( - list, - { type: EDIT_SERVER, serverId: 'invalid', serverData: { foo: 'foo' } } as any, - )).toEqual({ + expect(reducer(list, { + type: EDIT_SERVER, + payload: { serverId: 'invalid', serverData: { foo: 'foo' } }, + } as any)).toEqual({ abc123: { id: 'abc123' }, def456: { id: 'def456' }, })); it('removes server when action is DELETE_SERVER', () => - expect(reducer(list, { type: DELETE_SERVER, serverId: 'abc123' } as any)).toEqual({ + expect(reducer(list, { + type: DELETE_SERVER, + payload: { serverId: 'abc123' }, + } as any)).toEqual({ def456: { id: 'def456' }, })); it('appends server when action is CREATE_SERVERS', () => expect(reducer(list, { type: CREATE_SERVERS, - newServers: { + payload: { ghi789: { id: 'ghi789' }, }, } as any)).toEqual({ @@ -63,8 +66,7 @@ describe('serverReducer', () => { ])('returns state as it is when trying to set auto-connect on invalid server', (autoConnect) => expect(reducer(list, { type: SET_AUTO_CONNECT, - serverId: 'invalid', - autoConnect, + payload: { serverId: 'invalid', autoConnect }, } as any)).toEqual({ abc123: { id: 'abc123' }, def456: { id: 'def456' }, @@ -78,8 +80,7 @@ describe('serverReducer', () => { expect(reducer(listWithDisabledAutoConnect, { type: SET_AUTO_CONNECT, - serverId: 'abc123', - autoConnect: false, + payload: { serverId: 'abc123', autoConnect: false }, } as any)).toEqual({ abc123: { id: 'abc123', autoConnect: false }, def456: { id: 'def456' }, @@ -94,8 +95,7 @@ describe('serverReducer', () => { expect(reducer(listWithEnabledAutoConnect, { type: SET_AUTO_CONNECT, - serverId: 'def456', - autoConnect: true, + payload: { serverId: 'def456', autoConnect: true }, } as any)).toEqual({ abc123: { id: 'abc123', autoConnect: false }, def456: { id: 'def456', autoConnect: true }, @@ -118,7 +118,10 @@ describe('serverReducer', () => { const serverData = { name: 'edited' }; const result = editServer('123', serverData); - expect(result).toEqual({ type: EDIT_SERVER, serverId: '123', serverData }); + expect(result).toEqual({ + type: EDIT_SERVER, + payload: { serverId: '123', serverData }, + }); }); }); @@ -127,7 +130,10 @@ describe('serverReducer', () => { const serverToDelete = Mock.of({ id: 'abc123' }); const result = deleteServer(serverToDelete); - expect(result).toEqual({ type: DELETE_SERVER, serverId: 'abc123' }); + expect(result).toEqual({ + type: DELETE_SERVER, + payload: { serverId: 'abc123' }, + }); }); }); @@ -141,9 +147,9 @@ describe('serverReducer', () => { it('generates an id for every provided server if they do not have it', () => { const servers = values(list).map(dissoc('id')); - const { newServers } = createServers(servers); + const { payload } = createServers(servers); - expect(values(newServers).every(({ id }) => !!id)).toEqual(true); + expect(values(payload).every(({ id }) => !!id)).toEqual(true); }); }); @@ -155,7 +161,10 @@ describe('serverReducer', () => { const serverToEdit = Mock.of({ id: 'abc123' }); const result = setAutoConnect(serverToEdit, autoConnect); - expect(result).toEqual({ type: SET_AUTO_CONNECT, serverId: 'abc123', autoConnect }); + expect(result).toEqual({ + type: SET_AUTO_CONNECT, + payload: { serverId: 'abc123', autoConnect }, + }); }); }); });