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

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

View File

@@ -1,9 +1,12 @@
import { Mock } from 'ts-mockery';
import reducer, {
EDIT_SHORT_URL_START,
EDIT_SHORT_URL_ERROR,
SHORT_URL_EDITED,
editShortUrl,
ShortUrlEditedAction,
} from '../../../src/short-urls/reducers/shortUrlEdition';
import { ShlinkState } from '../../../src/container/types';
describe('shortUrlEditionReducer', () => {
const longUrl = 'https://shlink.io';
@@ -11,21 +14,25 @@ describe('shortUrlEditionReducer', () => {
describe('reducer', () => {
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,
error: false,
});
});
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,
error: true,
});
});
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,
shortCode,
saving: false,
@@ -38,11 +45,12 @@ describe('shortUrlEditionReducer', () => {
const updateShortUrlMeta = jest.fn().mockResolvedValue({});
const buildShlinkApiClient = jest.fn().mockReturnValue({ updateShortUrlMeta });
const dispatch = jest.fn();
const getState = () => Mock.of<ShlinkState>();
afterEach(jest.clearAllMocks);
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(updateShortUrlMeta).toHaveBeenCalledTimes(1);
@@ -58,7 +66,7 @@ describe('shortUrlEditionReducer', () => {
updateShortUrlMeta.mockRejectedValue(error);
try {
await editShortUrl(buildShlinkApiClient)(shortCode, undefined, longUrl)(dispatch);
await editShortUrl(buildShlinkApiClient)(shortCode, undefined, longUrl)(dispatch, getState);
} catch (e) {
expect(e).toBe(error);
}

View File

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

View File

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