Migrated more short-url reducers to TS

This commit is contained in:
Alejandro Celaya 2020-08-26 18:55:40 +02:00
parent 6696fb13d6
commit 1b03d04318
15 changed files with 169 additions and 57 deletions

View file

@ -31,7 +31,8 @@
"max-len": ["error", { "max-len": ["error", {
"code": 120, "code": 120,
"ignoreStrings": true, "ignoreStrings": true,
"ignoreTemplateLiterals": true "ignoreTemplateLiterals": true,
"ignoreComments": true
}], }],
"no-mixed-operators": "off", "no-mixed-operators": "off",
"comma-dangle": ["error", "always-multiline"], "comma-dangle": ["error", "always-multiline"],
@ -65,7 +66,7 @@
"code": 120, "code": 120,
"ignoreStrings": true, "ignoreStrings": true,
"ignoreTemplateLiterals": true, "ignoreTemplateLiterals": true,
"ignoreTrailingComments": true "ignoreComments": true
}], }],
"no-mixed-operators": "off", "no-mixed-operators": "off",
"react/display-name": "off" "react/display-name": "off"

6
package-lock.json generated
View file

@ -22452,9 +22452,9 @@
} }
}, },
"typescript": { "typescript": {
"version": "3.9.7", "version": "4.0.2",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-3.9.7.tgz", "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.0.2.tgz",
"integrity": "sha512-BLbiRkiBzAwsjut4x/dsibSTB6yWpwT5qWmC2OfuCg3GgVQCSgMs4vEctYPhsaGtd0AeuuHMkjZ2h2WG8MSzRw==", "integrity": "sha512-e4ERvRV2wb+rRZ/IQeb3jm2VxBsirQLpQhdxplZ2MEzGvDkkMmPglecnNDfSUBivMjP93vRbngYYDQqQ/78bcQ==",
"dev": true "dev": true
}, },
"uglify-es": { "uglify-es": {

View file

@ -141,7 +141,7 @@
"terser-webpack-plugin": "^2.1.2", "terser-webpack-plugin": "^2.1.2",
"ts-jest": "^26.0.0", "ts-jest": "^26.0.0",
"ts-mockery": "^1.2.0", "ts-mockery": "^1.2.0",
"typescript": "^3.9.3", "typescript": "^4.0.2",
"url-loader": "^2.2.0", "url-loader": "^2.2.0",
"webpack": "^4.41.0", "webpack": "^4.41.0",
"webpack-dev-server": "^3.8.2", "webpack-dev-server": "^3.8.2",

View file

@ -4,19 +4,21 @@ import { SelectedServer } from '../servers/data';
import { Settings } from '../settings/reducers/settings'; import { Settings } from '../settings/reducers/settings';
import { ShortUrlMetaEdition } from '../short-urls/reducers/shortUrlMeta'; import { ShortUrlMetaEdition } from '../short-urls/reducers/shortUrlMeta';
import { ShortUrlCreation } from '../short-urls/reducers/shortUrlCreation'; import { ShortUrlCreation } from '../short-urls/reducers/shortUrlCreation';
import { ShortUrlDeletion } from '../short-urls/reducers/shortUrlDeletion';
export type ConnectDecorator = (props: string[], actions?: string[]) => any; import { ShortUrlEdition } from '../short-urls/reducers/shortUrlEdition';
import { ShortUrlsListParams } from '../short-urls/reducers/shortUrlsListParams';
import { ShortUrlTags } from '../short-urls/reducers/shortUrlTags';
export interface ShlinkState { export interface ShlinkState {
servers: ServersMap; servers: ServersMap;
selectedServer: SelectedServer; selectedServer: SelectedServer;
shortUrlsList: any; shortUrlsList: any;
shortUrlsListParams: any; shortUrlsListParams: ShortUrlsListParams;
shortUrlCreationResult: ShortUrlCreation; shortUrlCreationResult: ShortUrlCreation;
shortUrlDeletion: any; shortUrlDeletion: ShortUrlDeletion;
shortUrlTags: any; shortUrlTags: ShortUrlTags;
shortUrlMeta: ShortUrlMetaEdition; shortUrlMeta: ShortUrlMetaEdition;
shortUrlEdition: any; shortUrlEdition: ShortUrlEdition;
shortUrlVisits: any; shortUrlVisits: any;
tagVisits: any; tagVisits: any;
shortUrlDetail: any; shortUrlDetail: any;
@ -27,4 +29,6 @@ export interface ShlinkState {
settings: Settings; settings: Settings;
} }
export type ConnectDecorator = (props: string[], actions?: string[]) => any;
export type GetState = () => ShlinkState; export type GetState = () => ShlinkState;

View file

@ -22,7 +22,7 @@ const DeleteShortUrlModal = ({ shortUrl, toggle, isOpen, shortUrlDeletion, reset
useEffect(() => resetDeleteShortUrl, []); useEffect(() => resetDeleteShortUrl, []);
const { error, errorData } = shortUrlDeletion; const { error, errorData } = shortUrlDeletion;
const errorCode = error && (errorData.type || errorData.error); const errorCode = error && errorData && (errorData.type || errorData.error);
const hasThresholdError = errorCode === THRESHOLD_REACHED; const hasThresholdError = errorCode === THRESHOLD_REACHED;
const hasErrorOtherThanThreshold = error && errorCode !== THRESHOLD_REACHED; const hasErrorOtherThanThreshold = error && errorCode !== THRESHOLD_REACHED;
const close = pipe(resetDeleteShortUrl, toggle); const close = pipe(resetDeleteShortUrl, toggle);

View file

@ -1,6 +1,9 @@
import { createAction, handleActions } from 'redux-actions';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { Action, Dispatch } from 'redux';
import { apiErrorType } from '../../utils/services/ShlinkApiClient'; import { apiErrorType } from '../../utils/services/ShlinkApiClient';
import { buildActionCreator, buildReducer } from '../../utils/helpers/redux';
import { ProblemDetailsError, ShlinkApiClientBuilder } from '../../utils/services/types';
import { GetState } from '../../container/types';
/* eslint-disable padding-line-between-statements */ /* eslint-disable padding-line-between-statements */
export const DELETE_SHORT_URL_START = 'shlink/deleteShortUrl/DELETE_SHORT_URL_START'; export const DELETE_SHORT_URL_START = 'shlink/deleteShortUrl/DELETE_SHORT_URL_START';
@ -9,6 +12,7 @@ export const SHORT_URL_DELETED = 'shlink/deleteShortUrl/SHORT_URL_DELETED';
export const RESET_DELETE_SHORT_URL = 'shlink/deleteShortUrl/RESET_DELETE_SHORT_URL'; export const RESET_DELETE_SHORT_URL = 'shlink/deleteShortUrl/RESET_DELETE_SHORT_URL';
/* eslint-enable padding-line-between-statements */ /* eslint-enable padding-line-between-statements */
/** @deprecated Use ShortUrlDeletion interface */
export const shortUrlDeletionType = PropTypes.shape({ export const shortUrlDeletionType = PropTypes.shape({
shortCode: PropTypes.string.isRequired, shortCode: PropTypes.string.isRequired,
loading: PropTypes.bool.isRequired, loading: PropTypes.bool.isRequired,
@ -16,32 +20,50 @@ export const shortUrlDeletionType = PropTypes.shape({
errorData: apiErrorType.isRequired, errorData: apiErrorType.isRequired,
}); });
const initialState = { export interface ShortUrlDeletion {
shortCode: string;
loading: boolean;
error: boolean;
errorData?: ProblemDetailsError;
}
interface DeleteShortUrlAction extends Action<string> {
shortCode: string;
domain?: string | null;
}
interface DeleteShortUrlErrorAction extends Action<string> {
errorData: ProblemDetailsError;
}
const initialState: ShortUrlDeletion = {
shortCode: '', shortCode: '',
loading: false, loading: false,
error: false, error: false,
errorData: {},
}; };
export default handleActions({ export default buildReducer<ShortUrlDeletion, DeleteShortUrlAction & DeleteShortUrlErrorAction>({
[DELETE_SHORT_URL_START]: (state) => ({ ...state, loading: true, error: false }), [DELETE_SHORT_URL_START]: (state) => ({ ...state, loading: true, error: false }),
[DELETE_SHORT_URL_ERROR]: (state, { errorData }) => ({ ...state, errorData, loading: false, error: true }), [DELETE_SHORT_URL_ERROR]: (state, { errorData }) => ({ ...state, errorData, loading: false, error: true }),
[SHORT_URL_DELETED]: (state, { shortCode }) => ({ ...state, shortCode, loading: false, error: false }), [SHORT_URL_DELETED]: (state, { shortCode }) => ({ ...state, shortCode, loading: false, error: false }),
[RESET_DELETE_SHORT_URL]: () => initialState, [RESET_DELETE_SHORT_URL]: () => initialState,
}, initialState); }, initialState);
export const deleteShortUrl = (buildShlinkApiClient) => (shortCode, domain) => async (dispatch, getState) => { export const deleteShortUrl = (buildShlinkApiClient: ShlinkApiClientBuilder) => (
shortCode: string,
domain?: string | null,
) => async (dispatch: Dispatch, getState: GetState) => {
dispatch({ type: DELETE_SHORT_URL_START }); dispatch({ type: DELETE_SHORT_URL_START });
const { deleteShortUrl } = buildShlinkApiClient(getState); const { deleteShortUrl } = buildShlinkApiClient(getState);
try { try {
await deleteShortUrl(shortCode, domain); await deleteShortUrl(shortCode, domain);
dispatch({ type: SHORT_URL_DELETED, shortCode, domain }); dispatch<DeleteShortUrlAction>({ type: SHORT_URL_DELETED, shortCode, domain });
} catch (e) { } catch (e) {
dispatch({ type: DELETE_SHORT_URL_ERROR, errorData: e.response.data }); dispatch<DeleteShortUrlErrorAction>({ type: DELETE_SHORT_URL_ERROR, errorData: e.response.data });
throw e; throw e;
} }
}; };
export const resetDeleteShortUrl = createAction(RESET_DELETE_SHORT_URL); export const resetDeleteShortUrl = buildActionCreator(RESET_DELETE_SHORT_URL);

View file

@ -1,5 +1,8 @@
import { handleActions } from 'redux-actions';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { Action, Dispatch } from 'redux';
import { buildReducer } from '../../utils/helpers/redux';
import { ShlinkApiClientBuilder } from '../../utils/services/types';
import { GetState } from '../../container/types';
/* eslint-disable padding-line-between-statements */ /* eslint-disable padding-line-between-statements */
export const EDIT_SHORT_URL_START = 'shlink/shortUrlEdition/EDIT_SHORT_URL_START'; export const EDIT_SHORT_URL_START = 'shlink/shortUrlEdition/EDIT_SHORT_URL_START';
@ -7,6 +10,7 @@ export const EDIT_SHORT_URL_ERROR = 'shlink/shortUrlEdition/EDIT_SHORT_URL_ERROR
export const SHORT_URL_EDITED = 'shlink/shortUrlEdition/SHORT_URL_EDITED'; export const SHORT_URL_EDITED = 'shlink/shortUrlEdition/SHORT_URL_EDITED';
/* eslint-enable padding-line-between-statements */ /* eslint-enable padding-line-between-statements */
/** @deprecated Use ShortUrlEdition interface instead */
export const ShortUrlEditionType = PropTypes.shape({ export const ShortUrlEditionType = PropTypes.shape({
shortCode: PropTypes.string, shortCode: PropTypes.string,
longUrl: PropTypes.string, longUrl: PropTypes.string,
@ -14,26 +18,43 @@ export const ShortUrlEditionType = PropTypes.shape({
error: PropTypes.bool.isRequired, error: PropTypes.bool.isRequired,
}); });
const initialState = { export interface ShortUrlEdition {
shortCode: string | null;
longUrl: string | null;
saving: boolean;
error: boolean;
}
export interface ShortUrlEditedAction extends Action<string> {
shortCode: string;
longUrl: string;
domain: string | undefined | null;
}
const initialState: ShortUrlEdition = {
shortCode: null, shortCode: null,
longUrl: null, longUrl: null,
saving: false, saving: false,
error: false, error: false,
}; };
export default handleActions({ export default buildReducer<ShortUrlEdition, ShortUrlEditedAction>({
[EDIT_SHORT_URL_START]: (state) => ({ ...state, saving: true, error: false }), [EDIT_SHORT_URL_START]: (state) => ({ ...state, saving: true, error: false }),
[EDIT_SHORT_URL_ERROR]: (state) => ({ ...state, saving: false, error: true }), [EDIT_SHORT_URL_ERROR]: (state) => ({ ...state, saving: false, error: true }),
[SHORT_URL_EDITED]: (state, { shortCode, longUrl }) => ({ shortCode, longUrl, saving: false, error: false }), [SHORT_URL_EDITED]: (_, { shortCode, longUrl }) => ({ shortCode, longUrl, saving: false, error: false }),
}, initialState); }, initialState);
export const editShortUrl = (buildShlinkApiClient) => (shortCode, domain, longUrl) => async (dispatch, getState) => { export const editShortUrl = (buildShlinkApiClient: ShlinkApiClientBuilder) => (
shortCode: string,
domain: string | undefined | null,
longUrl: string,
) => async (dispatch: Dispatch, getState: GetState) => {
dispatch({ type: EDIT_SHORT_URL_START }); dispatch({ type: EDIT_SHORT_URL_START });
const { updateShortUrlMeta } = buildShlinkApiClient(getState); const { updateShortUrlMeta } = buildShlinkApiClient(getState);
try { try {
await updateShortUrlMeta(shortCode, domain, { longUrl }); await updateShortUrlMeta(shortCode, domain, { longUrl });
dispatch({ shortCode, longUrl, domain, type: SHORT_URL_EDITED }); dispatch<ShortUrlEditedAction>({ shortCode, longUrl, domain, type: SHORT_URL_EDITED });
} catch (e) { } catch (e) {
dispatch({ type: EDIT_SHORT_URL_ERROR }); dispatch({ type: EDIT_SHORT_URL_ERROR });

View file

@ -1,5 +1,8 @@
import { createAction, handleActions } from 'redux-actions';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { Action, Dispatch } from 'redux';
import { buildActionCreator, buildReducer } from '../../utils/helpers/redux';
import { ShlinkApiClientBuilder } from '../../utils/services/types';
import { GetState } from '../../container/types';
/* eslint-disable padding-line-between-statements */ /* eslint-disable padding-line-between-statements */
export const EDIT_SHORT_URL_TAGS_START = 'shlink/shortUrlTags/EDIT_SHORT_URL_TAGS_START'; export const EDIT_SHORT_URL_TAGS_START = 'shlink/shortUrlTags/EDIT_SHORT_URL_TAGS_START';
@ -8,6 +11,7 @@ export const SHORT_URL_TAGS_EDITED = 'shlink/shortUrlTags/SHORT_URL_TAGS_EDITED'
export const RESET_EDIT_SHORT_URL_TAGS = 'shlink/shortUrlTags/RESET_EDIT_SHORT_URL_TAGS'; export const RESET_EDIT_SHORT_URL_TAGS = 'shlink/shortUrlTags/RESET_EDIT_SHORT_URL_TAGS';
/* eslint-enable padding-line-between-statements */ /* eslint-enable padding-line-between-statements */
/** @deprecated Use ShortUrlTags interface */
export const shortUrlTagsType = PropTypes.shape({ export const shortUrlTagsType = PropTypes.shape({
shortCode: PropTypes.string, shortCode: PropTypes.string,
tags: PropTypes.arrayOf(PropTypes.string).isRequired, tags: PropTypes.arrayOf(PropTypes.string).isRequired,
@ -15,28 +19,45 @@ export const shortUrlTagsType = PropTypes.shape({
error: PropTypes.bool.isRequired, error: PropTypes.bool.isRequired,
}); });
const initialState = { export interface ShortUrlTags {
shortCode: string | null;
tags: string[];
saving: boolean;
error: boolean;
}
export interface EditShortUrlTagsAction extends Action<string> {
shortCode: string;
tags: string[];
domain: string | null | undefined;
}
const initialState: ShortUrlTags = {
shortCode: null, shortCode: null,
tags: [], tags: [],
saving: false, saving: false,
error: false, error: false,
}; };
export default handleActions({ export default buildReducer<ShortUrlTags, EditShortUrlTagsAction>({
[EDIT_SHORT_URL_TAGS_START]: (state) => ({ ...state, saving: true, error: false }), [EDIT_SHORT_URL_TAGS_START]: (state) => ({ ...state, saving: true, error: false }),
[EDIT_SHORT_URL_TAGS_ERROR]: (state) => ({ ...state, saving: false, error: true }), [EDIT_SHORT_URL_TAGS_ERROR]: (state) => ({ ...state, saving: false, error: true }),
[SHORT_URL_TAGS_EDITED]: (state, { shortCode, tags }) => ({ shortCode, tags, saving: false, error: false }), [SHORT_URL_TAGS_EDITED]: (_, { shortCode, tags }) => ({ shortCode, tags, saving: false, error: false }),
[RESET_EDIT_SHORT_URL_TAGS]: () => initialState, [RESET_EDIT_SHORT_URL_TAGS]: () => initialState,
}, initialState); }, initialState);
export const editShortUrlTags = (buildShlinkApiClient) => (shortCode, domain, tags) => async (dispatch, getState) => { export const editShortUrlTags = (buildShlinkApiClient: ShlinkApiClientBuilder) => (
shortCode: string,
domain: string | null | undefined,
tags: string[],
) => async (dispatch: Dispatch, getState: GetState) => {
dispatch({ type: EDIT_SHORT_URL_TAGS_START }); dispatch({ type: EDIT_SHORT_URL_TAGS_START });
const { updateShortUrlTags } = buildShlinkApiClient(getState); const { updateShortUrlTags } = buildShlinkApiClient(getState);
try { try {
const normalizedTags = await updateShortUrlTags(shortCode, domain, tags); const normalizedTags = await updateShortUrlTags(shortCode, domain, tags);
dispatch({ tags: normalizedTags, shortCode, domain, type: SHORT_URL_TAGS_EDITED }); dispatch<EditShortUrlTagsAction>({ tags: normalizedTags, shortCode, domain, type: SHORT_URL_TAGS_EDITED });
} catch (e) { } catch (e) {
dispatch({ type: EDIT_SHORT_URL_TAGS_ERROR }); dispatch({ type: EDIT_SHORT_URL_TAGS_ERROR });
@ -44,4 +65,4 @@ export const editShortUrlTags = (buildShlinkApiClient) => (shortCode, domain, ta
} }
}; };
export const resetShortUrlsTags = createAction(RESET_EDIT_SHORT_URL_TAGS); export const resetShortUrlsTags = buildActionCreator(RESET_EDIT_SHORT_URL_TAGS);

View file

@ -30,7 +30,6 @@ const initialState = {
error: false, error: false,
}; };
// TODO Make all actions fetch shortCode, domain and prop from payload
const setPropFromActionOnMatchingShortUrl = (prop) => (state, { shortCode, domain, [prop]: propValue }) => assocPath( const setPropFromActionOnMatchingShortUrl = (prop) => (state, { shortCode, domain, [prop]: propValue }) => assocPath(
[ 'shortUrls', 'data' ], [ 'shortUrls', 'data' ],
state.shortUrls.data.map( state.shortUrls.data.map(

View file

@ -1,9 +1,11 @@
import { createAction, handleActions } from 'redux-actions';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { Action } from 'redux';
import { buildActionCreator, buildReducer } from '../../utils/helpers/redux';
import { LIST_SHORT_URLS } from './shortUrlsList'; import { LIST_SHORT_URLS } from './shortUrlsList';
export const RESET_SHORT_URL_PARAMS = 'shlink/shortUrlsListParams/RESET_SHORT_URL_PARAMS'; export const RESET_SHORT_URL_PARAMS = 'shlink/shortUrlsListParams/RESET_SHORT_URL_PARAMS';
/** @deprecated Use ShortUrlsListParams interface instead */
export const shortUrlsListParamsType = PropTypes.shape({ export const shortUrlsListParamsType = PropTypes.shape({
page: PropTypes.string, page: PropTypes.string,
tags: PropTypes.arrayOf(PropTypes.string), tags: PropTypes.arrayOf(PropTypes.string),
@ -13,11 +15,24 @@ export const shortUrlsListParamsType = PropTypes.shape({
orderBy: PropTypes.object, orderBy: PropTypes.object,
}); });
export interface ShortUrlsListParams {
page: string;
tags?: string[];
searchTerm?: string;
startDate?: string;
endDate?: string;
orderBy?: object;
}
interface ListShortUrlsAction extends Action<string> {
params: ShortUrlsListParams;
}
const initialState = { page: '1' }; const initialState = { page: '1' };
export default handleActions({ export default buildReducer<ShortUrlsListParams, ListShortUrlsAction>({
[LIST_SHORT_URLS]: (state, { params }) => ({ ...state, ...params }), [LIST_SHORT_URLS]: (state, { params }) => ({ ...state, ...params }),
[RESET_SHORT_URL_PARAMS]: () => initialState, [RESET_SHORT_URL_PARAMS]: () => initialState,
}, initialState); }, initialState);
export const resetShortUrlParams = createAction(RESET_SHORT_URL_PARAMS); export const resetShortUrlParams = buildActionCreator(RESET_SHORT_URL_PARAMS);

View file

@ -14,3 +14,12 @@ export interface ShlinkHealth {
status: 'pass' | 'fail'; status: 'pass' | 'fail';
version: string; version: string;
} }
export interface ProblemDetailsError {
type: string;
detail: string;
title: string;
status: number;
error?: string; // Deprecated
message?: string; // Deprecated
}

View file

@ -1,3 +1,4 @@
import { Mock } from 'ts-mockery';
import reducer, { import reducer, {
DELETE_SHORT_URL_ERROR, DELETE_SHORT_URL_ERROR,
DELETE_SHORT_URL_START, DELETE_SHORT_URL_START,
@ -6,15 +7,17 @@ import reducer, {
resetDeleteShortUrl, resetDeleteShortUrl,
deleteShortUrl, deleteShortUrl,
} from '../../../src/short-urls/reducers/shortUrlDeletion'; } from '../../../src/short-urls/reducers/shortUrlDeletion';
import { ProblemDetailsError } from '../../../src/utils/services/types';
describe('shortUrlDeletionReducer', () => { describe('shortUrlDeletionReducer', () => {
describe('reducer', () => { describe('reducer', () => {
it('returns loading on DELETE_SHORT_URL_START', () => it('returns loading on DELETE_SHORT_URL_START', () =>
expect(reducer(undefined, { type: DELETE_SHORT_URL_START })).toEqual({ expect(reducer(undefined, { type: DELETE_SHORT_URL_START })).toEqual({
shortCode: '', shortCode: '',
loading: true, loading: true,
error: false, error: false,
errorData: {},
})); }));
it('returns default on RESET_DELETE_SHORT_URL', () => it('returns default on RESET_DELETE_SHORT_URL', () =>
@ -22,7 +25,6 @@ describe('shortUrlDeletionReducer', () => {
shortCode: '', shortCode: '',
loading: false, loading: false,
error: false, error: false,
errorData: {},
})); }));
it('returns shortCode on SHORT_URL_DELETED', () => it('returns shortCode on SHORT_URL_DELETED', () =>
@ -30,11 +32,10 @@ describe('shortUrlDeletionReducer', () => {
shortCode: 'foo', shortCode: 'foo',
loading: false, loading: false,
error: false, error: false,
errorData: {},
})); }));
it('returns errorData on DELETE_SHORT_URL_ERROR', () => { it('returns errorData on DELETE_SHORT_URL_ERROR', () => {
const errorData = { foo: 'bar' }; const errorData = Mock.of<ProblemDetailsError>({ type: 'bar' });
expect(reducer(undefined, { type: DELETE_SHORT_URL_ERROR, errorData })).toEqual({ expect(reducer(undefined, { type: DELETE_SHORT_URL_ERROR, errorData })).toEqual({
shortCode: '', shortCode: '',

View file

@ -1,9 +1,12 @@
import { Mock } from 'ts-mockery';
import reducer, { import reducer, {
EDIT_SHORT_URL_START, EDIT_SHORT_URL_START,
EDIT_SHORT_URL_ERROR, EDIT_SHORT_URL_ERROR,
SHORT_URL_EDITED, SHORT_URL_EDITED,
editShortUrl, editShortUrl,
ShortUrlEditedAction,
} from '../../../src/short-urls/reducers/shortUrlEdition'; } from '../../../src/short-urls/reducers/shortUrlEdition';
import { ShlinkState } from '../../../src/container/types';
describe('shortUrlEditionReducer', () => { describe('shortUrlEditionReducer', () => {
const longUrl = 'https://shlink.io'; const longUrl = 'https://shlink.io';
@ -11,21 +14,25 @@ describe('shortUrlEditionReducer', () => {
describe('reducer', () => { describe('reducer', () => {
it('returns loading on EDIT_SHORT_URL_START', () => { it('returns loading on EDIT_SHORT_URL_START', () => {
expect(reducer({}, { type: EDIT_SHORT_URL_START })).toEqual({ expect(reducer(undefined, Mock.of<ShortUrlEditedAction>({ type: EDIT_SHORT_URL_START }))).toEqual({
longUrl: null,
shortCode: null,
saving: true, saving: true,
error: false, error: false,
}); });
}); });
it('returns error on EDIT_SHORT_URL_ERROR', () => { it('returns error on EDIT_SHORT_URL_ERROR', () => {
expect(reducer({}, { type: EDIT_SHORT_URL_ERROR })).toEqual({ expect(reducer(undefined, Mock.of<ShortUrlEditedAction>({ type: EDIT_SHORT_URL_ERROR }))).toEqual({
longUrl: null,
shortCode: null,
saving: false, saving: false,
error: true, error: true,
}); });
}); });
it('returns provided tags and shortCode on SHORT_URL_EDITED', () => { it('returns provided tags and shortCode on SHORT_URL_EDITED', () => {
expect(reducer({}, { type: SHORT_URL_EDITED, longUrl, shortCode })).toEqual({ expect(reducer(undefined, { type: SHORT_URL_EDITED, longUrl, shortCode, domain: null })).toEqual({
longUrl, longUrl,
shortCode, shortCode,
saving: false, saving: false,
@ -38,11 +45,12 @@ describe('shortUrlEditionReducer', () => {
const updateShortUrlMeta = jest.fn().mockResolvedValue({}); const updateShortUrlMeta = jest.fn().mockResolvedValue({});
const buildShlinkApiClient = jest.fn().mockReturnValue({ updateShortUrlMeta }); const buildShlinkApiClient = jest.fn().mockReturnValue({ updateShortUrlMeta });
const dispatch = jest.fn(); const dispatch = jest.fn();
const getState = () => Mock.of<ShlinkState>();
afterEach(jest.clearAllMocks); afterEach(jest.clearAllMocks);
it.each([[ undefined ], [ null ], [ 'example.com' ]])('dispatches long URL on success', async (domain) => { it.each([[ undefined ], [ null ], [ 'example.com' ]])('dispatches long URL on success', async (domain) => {
await editShortUrl(buildShlinkApiClient)(shortCode, domain, longUrl)(dispatch); await editShortUrl(buildShlinkApiClient)(shortCode, domain, longUrl)(dispatch, getState);
expect(buildShlinkApiClient).toHaveBeenCalledTimes(1); expect(buildShlinkApiClient).toHaveBeenCalledTimes(1);
expect(updateShortUrlMeta).toHaveBeenCalledTimes(1); expect(updateShortUrlMeta).toHaveBeenCalledTimes(1);
@ -58,7 +66,7 @@ describe('shortUrlEditionReducer', () => {
updateShortUrlMeta.mockRejectedValue(error); updateShortUrlMeta.mockRejectedValue(error);
try { try {
await editShortUrl(buildShlinkApiClient)(shortCode, undefined, longUrl)(dispatch); await editShortUrl(buildShlinkApiClient)(shortCode, undefined, longUrl)(dispatch, getState);
} catch (e) { } catch (e) {
expect(e).toBe(error); expect(e).toBe(error);
} }

View file

@ -1,3 +1,4 @@
import { Mock } from 'ts-mockery';
import reducer, { import reducer, {
EDIT_SHORT_URL_TAGS_ERROR, EDIT_SHORT_URL_TAGS_ERROR,
EDIT_SHORT_URL_TAGS_START, EDIT_SHORT_URL_TAGS_START,
@ -5,29 +6,37 @@ import reducer, {
resetShortUrlsTags, resetShortUrlsTags,
SHORT_URL_TAGS_EDITED, SHORT_URL_TAGS_EDITED,
editShortUrlTags, editShortUrlTags,
EditShortUrlTagsAction,
} from '../../../src/short-urls/reducers/shortUrlTags'; } from '../../../src/short-urls/reducers/shortUrlTags';
import { ShlinkState } from '../../../src/container/types';
describe('shortUrlTagsReducer', () => { describe('shortUrlTagsReducer', () => {
const tags = [ 'foo', 'bar', 'baz' ]; const tags = [ 'foo', 'bar', 'baz' ];
const shortCode = 'abc123'; const shortCode = 'abc123';
describe('reducer', () => { describe('reducer', () => {
const action = (type: string) => Mock.of<EditShortUrlTagsAction>({ type });
it('returns loading on EDIT_SHORT_URL_TAGS_START', () => { it('returns loading on EDIT_SHORT_URL_TAGS_START', () => {
expect(reducer({}, { type: EDIT_SHORT_URL_TAGS_START })).toEqual({ expect(reducer(undefined, action(EDIT_SHORT_URL_TAGS_START))).toEqual({
tags: [],
shortCode: null,
saving: true, saving: true,
error: false, error: false,
}); });
}); });
it('returns error on EDIT_SHORT_URL_TAGS_ERROR', () => { it('returns error on EDIT_SHORT_URL_TAGS_ERROR', () => {
expect(reducer({}, { type: EDIT_SHORT_URL_TAGS_ERROR })).toEqual({ expect(reducer(undefined, action(EDIT_SHORT_URL_TAGS_ERROR))).toEqual({
tags: [],
shortCode: null,
saving: false, saving: false,
error: true, error: true,
}); });
}); });
it('returns provided tags and shortCode on SHORT_URL_TAGS_EDITED', () => { it('returns provided tags and shortCode on SHORT_URL_TAGS_EDITED', () => {
expect(reducer({}, { type: SHORT_URL_TAGS_EDITED, tags, shortCode })).toEqual({ expect(reducer(undefined, { type: SHORT_URL_TAGS_EDITED, tags, shortCode, domain: null })).toEqual({
tags, tags,
shortCode, shortCode,
saving: false, saving: false,
@ -36,7 +45,7 @@ describe('shortUrlTagsReducer', () => {
}); });
it('goes back to initial state on RESET_EDIT_SHORT_URL_TAGS', () => { it('goes back to initial state on RESET_EDIT_SHORT_URL_TAGS', () => {
expect(reducer({}, { type: RESET_EDIT_SHORT_URL_TAGS })).toEqual({ expect(reducer(undefined, action(RESET_EDIT_SHORT_URL_TAGS))).toEqual({
tags: [], tags: [],
shortCode: null, shortCode: null,
saving: false, saving: false,
@ -53,6 +62,7 @@ describe('shortUrlTagsReducer', () => {
const updateShortUrlTags = jest.fn(); const updateShortUrlTags = jest.fn();
const buildShlinkApiClient = jest.fn().mockReturnValue({ updateShortUrlTags }); const buildShlinkApiClient = jest.fn().mockReturnValue({ updateShortUrlTags });
const dispatch = jest.fn(); const dispatch = jest.fn();
const getState = () => Mock.all<ShlinkState>();
afterEach(jest.clearAllMocks); afterEach(jest.clearAllMocks);
@ -61,7 +71,7 @@ describe('shortUrlTagsReducer', () => {
updateShortUrlTags.mockResolvedValue(normalizedTags); updateShortUrlTags.mockResolvedValue(normalizedTags);
await editShortUrlTags(buildShlinkApiClient)(shortCode, domain, tags)(dispatch); await editShortUrlTags(buildShlinkApiClient)(shortCode, domain, tags)(dispatch, getState);
expect(buildShlinkApiClient).toHaveBeenCalledTimes(1); expect(buildShlinkApiClient).toHaveBeenCalledTimes(1);
expect(updateShortUrlTags).toHaveBeenCalledTimes(1); expect(updateShortUrlTags).toHaveBeenCalledTimes(1);
@ -80,7 +90,7 @@ describe('shortUrlTagsReducer', () => {
updateShortUrlTags.mockRejectedValue(error); updateShortUrlTags.mockRejectedValue(error);
try { try {
await editShortUrlTags(buildShlinkApiClient)(shortCode, undefined, tags)(dispatch); await editShortUrlTags(buildShlinkApiClient)(shortCode, undefined, tags)(dispatch, getState);
} catch (e) { } catch (e) {
expect(e).toBe(error); expect(e).toBe(error);
} }

View file

@ -1,21 +1,22 @@
import reducer, { import reducer, {
RESET_SHORT_URL_PARAMS, RESET_SHORT_URL_PARAMS,
resetShortUrlParams, resetShortUrlParams,
ShortUrlsListParams,
} from '../../../src/short-urls/reducers/shortUrlsListParams'; } from '../../../src/short-urls/reducers/shortUrlsListParams';
import { LIST_SHORT_URLS } from '../../../src/short-urls/reducers/shortUrlsList'; import { LIST_SHORT_URLS } from '../../../src/short-urls/reducers/shortUrlsList';
describe('shortUrlsListParamsReducer', () => { describe('shortUrlsListParamsReducer', () => {
describe('reducer', () => { describe('reducer', () => {
const defaultState = { page: '1' }; const defaultState: ShortUrlsListParams = { page: '1' };
it('returns params when action is LIST_SHORT_URLS', () => it('returns params when action is LIST_SHORT_URLS', () =>
expect(reducer(defaultState, { type: LIST_SHORT_URLS, params: { searchTerm: 'foo' } })).toEqual({ expect(reducer(undefined, { type: LIST_SHORT_URLS, params: { searchTerm: 'foo', page: '2' } })).toEqual({
...defaultState, page: '2',
searchTerm: 'foo', searchTerm: 'foo',
})); }));
it('returns default value when action is RESET_SHORT_URL_PARAMS', () => it('returns default value when action is RESET_SHORT_URL_PARAMS', () =>
expect(reducer(defaultState, { type: RESET_SHORT_URL_PARAMS })).toEqual(defaultState)); expect(reducer(undefined, { type: RESET_SHORT_URL_PARAMS, params: defaultState })).toEqual(defaultState));
}); });
describe('resetShortUrlParams', () => { describe('resetShortUrlParams', () => {