Updated listShortUrls action to use payload

This commit is contained in:
Alejandro Celaya 2022-11-09 18:27:05 +01:00
parent fe85291772
commit 979c16eb9c
2 changed files with 9 additions and 10 deletions

View File

@ -1,5 +1,6 @@
import { PayloadAction } from '@reduxjs/toolkit';
import { assoc, assocPath, last, pipe, reject } from 'ramda'; import { assoc, assocPath, last, pipe, reject } from 'ramda';
import { Action, Dispatch } from 'redux'; import { Dispatch } from 'redux';
import { shortUrlMatches } from '../helpers'; import { shortUrlMatches } from '../helpers';
import { createNewVisits, CreateVisitsAction } from '../../visits/reducers/visitCreation'; import { createNewVisits, CreateVisitsAction } from '../../visits/reducers/visitCreation';
import { buildReducer } from '../../utils/helpers/redux'; import { buildReducer } from '../../utils/helpers/redux';
@ -23,9 +24,7 @@ export interface ShortUrlsList {
error: boolean; error: boolean;
} }
export interface ListShortUrlsAction extends Action<string> { export type ListShortUrlsAction = PayloadAction<ShlinkShortUrlsResponse>;
shortUrls: ShlinkShortUrlsResponse;
}
export type ListShortUrlsCombinedAction = ( export type ListShortUrlsCombinedAction = (
ListShortUrlsAction ListShortUrlsAction
@ -43,7 +42,7 @@ const initialState: ShortUrlsList = {
export default buildReducer<ShortUrlsList, ListShortUrlsCombinedAction>({ export default buildReducer<ShortUrlsList, ListShortUrlsCombinedAction>({
[LIST_SHORT_URLS_START]: (state) => ({ ...state, loading: true, error: false }), [LIST_SHORT_URLS_START]: (state) => ({ ...state, loading: true, error: false }),
[LIST_SHORT_URLS_ERROR]: () => ({ loading: false, error: true }), [LIST_SHORT_URLS_ERROR]: () => ({ loading: false, error: true }),
[LIST_SHORT_URLS]: (_, { shortUrls }) => ({ loading: false, error: false, shortUrls }), [LIST_SHORT_URLS]: (_, { payload: shortUrls }) => ({ loading: false, error: false, shortUrls }),
[`${SHORT_URL_DELETED}/fulfilled`]: pipe( // TODO Do not hardcode action type here [`${SHORT_URL_DELETED}/fulfilled`]: pipe( // TODO Do not hardcode action type here
(state: ShortUrlsList, { payload }: DeleteShortUrlAction) => (!state.shortUrls ? state : assocPath( (state: ShortUrlsList, { payload }: DeleteShortUrlAction) => (!state.shortUrls ? state : assocPath(
['shortUrls', 'data'], ['shortUrls', 'data'],
@ -109,9 +108,9 @@ export const listShortUrls = (buildShlinkApiClient: ShlinkApiClientBuilder) => (
const { listShortUrls: shlinkListShortUrls } = buildShlinkApiClient(getState); const { listShortUrls: shlinkListShortUrls } = buildShlinkApiClient(getState);
try { try {
const shortUrls = await shlinkListShortUrls(params); const payload = await shlinkListShortUrls(params);
dispatch<ListShortUrlsAction>({ type: LIST_SHORT_URLS, shortUrls }); dispatch<ListShortUrlsAction>({ type: LIST_SHORT_URLS, payload });
} catch (e) { } catch (e) {
dispatch({ type: LIST_SHORT_URLS_ERROR }); dispatch({ type: LIST_SHORT_URLS_ERROR });
} }

View File

@ -24,7 +24,7 @@ describe('shortUrlsListReducer', () => {
})); }));
it('returns short URLs on LIST_SHORT_URLS', () => it('returns short URLs on LIST_SHORT_URLS', () =>
expect(reducer(undefined, { type: LIST_SHORT_URLS, shortUrls: { data: [] } } as any)).toEqual({ expect(reducer(undefined, { type: LIST_SHORT_URLS, payload: { data: [] } } as any)).toEqual({
shortUrls: { data: [] }, shortUrls: { data: [] },
loading: false, loading: false,
error: false, error: false,
@ -194,14 +194,14 @@ describe('shortUrlsListReducer', () => {
afterEach(jest.clearAllMocks); afterEach(jest.clearAllMocks);
it('dispatches proper actions if API client request succeeds', async () => { it('dispatches proper actions if API client request succeeds', async () => {
const listShortUrlsMock = jest.fn().mockResolvedValue([]); const listShortUrlsMock = jest.fn().mockResolvedValue({});
const apiClientMock = Mock.of<ShlinkApiClient>({ listShortUrls: listShortUrlsMock }); const apiClientMock = Mock.of<ShlinkApiClient>({ listShortUrls: listShortUrlsMock });
await listShortUrls(() => apiClientMock)()(dispatch, getState); await listShortUrls(() => apiClientMock)()(dispatch, getState);
expect(dispatch).toHaveBeenCalledTimes(2); expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, { type: LIST_SHORT_URLS_START }); expect(dispatch).toHaveBeenNthCalledWith(1, { type: LIST_SHORT_URLS_START });
expect(dispatch).toHaveBeenNthCalledWith(2, { type: LIST_SHORT_URLS, shortUrls: [] }); expect(dispatch).toHaveBeenNthCalledWith(2, { type: LIST_SHORT_URLS, payload: {} });
expect(listShortUrlsMock).toHaveBeenCalledTimes(1); expect(listShortUrlsMock).toHaveBeenCalledTimes(1);
}); });