Migrated tags reducers to typescripts

This commit is contained in:
Alejandro Celaya
2020-08-27 19:12:09 +02:00
parent 83531666de
commit eb3775859a
10 changed files with 246 additions and 160 deletions

View File

@@ -1,3 +1,4 @@
import { Mock } from 'ts-mockery';
import reducer, {
DELETE_TAG_START,
DELETE_TAG_ERROR,
@@ -6,25 +7,27 @@ import reducer, {
tagDeleted,
deleteTag,
} from '../../../src/tags/reducers/tagDelete';
import ShlinkApiClient from '../../../src/utils/services/ShlinkApiClient';
import { ShlinkState } from '../../../src/container/types';
describe('tagDeleteReducer', () => {
describe('reducer', () => {
it('returns loading on DELETE_TAG_START', () => {
expect(reducer({}, { type: DELETE_TAG_START })).toEqual({
expect(reducer(undefined, { type: DELETE_TAG_START })).toEqual({
deleting: true,
error: false,
});
});
it('returns error on DELETE_TAG_ERROR', () => {
expect(reducer({}, { type: DELETE_TAG_ERROR })).toEqual({
expect(reducer(undefined, { type: DELETE_TAG_ERROR })).toEqual({
deleting: false,
error: true,
});
});
it('returns tag names on DELETE_TAG', () => {
expect(reducer({}, { type: DELETE_TAG })).toEqual({
expect(reducer(undefined, { type: DELETE_TAG })).toEqual({
deleting: false,
error: false,
});
@@ -40,11 +43,11 @@ describe('tagDeleteReducer', () => {
});
describe('deleteTag', () => {
const createApiClientMock = (result) => ({
deleteTags: jest.fn(() => result),
const createApiClientMock = (result: Promise<void>) => Mock.of<ShlinkApiClient>({
deleteTags: jest.fn(async () => result),
});
const dispatch = jest.fn();
const getState = () => ({});
const getState = () => Mock.all<ShlinkState>();
afterEach(() => dispatch.mockReset());

View File

@@ -1,3 +1,4 @@
import { Mock } from 'ts-mockery';
import reducer, {
EDIT_TAG_START,
EDIT_TAG_ERROR,
@@ -5,26 +6,38 @@ import reducer, {
TAG_EDITED,
tagEdited,
editTag,
EditTagAction,
} from '../../../src/tags/reducers/tagEdit';
import ShlinkApiClient from '../../../src/utils/services/ShlinkApiClient';
import ColorGenerator from '../../../src/utils/services/ColorGenerator';
import { ShlinkState } from '../../../src/container/types';
describe('tagEditReducer', () => {
const oldName = 'foo';
const newName = 'bar';
const color = '#ff0000';
describe('reducer', () => {
it('returns loading on EDIT_TAG_START', () => {
expect(reducer({}, { type: EDIT_TAG_START })).toEqual({
expect(reducer(undefined, Mock.of<EditTagAction>({ type: EDIT_TAG_START }))).toEqual({
editing: true,
error: false,
oldName: '',
newName: '',
});
});
it('returns error on EDIT_TAG_ERROR', () => {
expect(reducer({}, { type: EDIT_TAG_ERROR })).toEqual({
expect(reducer(undefined, Mock.of<EditTagAction>({ type: EDIT_TAG_ERROR }))).toEqual({
editing: false,
error: true,
oldName: '',
newName: '',
});
});
it('returns tag names on EDIT_TAG', () => {
expect(reducer({}, { type: EDIT_TAG, oldName: 'foo', newName: 'bar' })).toEqual({
expect(reducer(undefined, { type: EDIT_TAG, oldName, newName, color })).toEqual({
editing: false,
error: false,
oldName: 'foo',
@@ -44,24 +57,18 @@ describe('tagEditReducer', () => {
});
describe('editTag', () => {
const createApiClientMock = (result) => ({
editTag: jest.fn(() => result),
const createApiClientMock = (result: Promise<void>) => Mock.of<ShlinkApiClient>({
editTag: jest.fn(async () => result),
});
const colorGenerator = {
const colorGenerator = Mock.of<ColorGenerator>({
setColorForKey: jest.fn(),
};
const dispatch = jest.fn();
const getState = () => ({});
afterEach(() => {
colorGenerator.setColorForKey.mockReset();
dispatch.mockReset();
});
const dispatch = jest.fn();
const getState = () => Mock.of<ShlinkState>();
afterEach(jest.clearAllMocks);
it('calls API on success', async () => {
const oldName = 'foo';
const newName = 'bar';
const color = '#ff0000';
const apiClientMock = createApiClientMock(Promise.resolve());
const dispatchable = editTag(() => apiClientMock, colorGenerator)(oldName, newName, color);
@@ -80,9 +87,6 @@ describe('tagEditReducer', () => {
it('throws on error', async () => {
const error = 'Error';
const oldName = 'foo';
const newName = 'bar';
const color = '#ff0000';
const apiClientMock = createApiClientMock(Promise.reject(error));
const dispatchable = editTag(() => apiClientMock, colorGenerator)(oldName, newName, color);

View File

@@ -1,24 +1,30 @@
import { Mock } from 'ts-mockery';
import reducer, {
FILTER_TAGS,
filterTags,
LIST_TAGS,
LIST_TAGS_ERROR,
LIST_TAGS_START, listTags,
LIST_TAGS_START,
listTags,
TagsList,
} from '../../../src/tags/reducers/tagsList';
import { TAG_DELETED } from '../../../src/tags/reducers/tagDelete';
import { TAG_EDITED } from '../../../src/tags/reducers/tagEdit';
import { ShlinkState } from '../../../src/container/types';
describe('tagsListReducer', () => {
const state = (props: Partial<TagsList>) => Mock.of<TagsList>(props);
describe('reducer', () => {
it('returns loading on LIST_TAGS_START', () => {
expect(reducer({}, { type: LIST_TAGS_START })).toEqual(expect.objectContaining({
expect(reducer(undefined, { type: LIST_TAGS_START } as any)).toEqual(expect.objectContaining({
loading: true,
error: false,
}));
});
it('returns error on LIST_TAGS_ERROR', () => {
expect(reducer({}, { type: LIST_TAGS_ERROR })).toEqual(expect.objectContaining({
expect(reducer(undefined, { type: LIST_TAGS_ERROR } as any)).toEqual(expect.objectContaining({
loading: false,
error: true,
}));
@@ -27,7 +33,7 @@ describe('tagsListReducer', () => {
it('returns provided tags as filtered and regular tags on LIST_TAGS', () => {
const tags = [ 'foo', 'bar', 'baz' ];
expect(reducer({}, { type: LIST_TAGS, tags })).toEqual({
expect(reducer(undefined, { type: LIST_TAGS, tags } as any)).toEqual({
tags,
filteredTags: tags,
loading: false,
@@ -40,7 +46,7 @@ describe('tagsListReducer', () => {
const tag = 'foo';
const expectedTags = [ 'bar', 'baz' ];
expect(reducer({ tags, filteredTags: tags }, { type: TAG_DELETED, tag })).toEqual({
expect(reducer(state({ tags, filteredTags: tags }), { type: TAG_DELETED, tag } as any)).toEqual({
tags: expectedTags,
filteredTags: expectedTags,
});
@@ -52,7 +58,7 @@ describe('tagsListReducer', () => {
const newName = 'renamed';
const expectedTags = [ 'foo', 'renamed', 'baz' ].sort();
expect(reducer({ tags, filteredTags: tags }, { type: TAG_EDITED, oldName, newName })).toEqual({
expect(reducer(state({ tags, filteredTags: tags }), { type: TAG_EDITED, oldName, newName } as any)).toEqual({
tags: expectedTags,
filteredTags: expectedTags,
});
@@ -63,7 +69,7 @@ describe('tagsListReducer', () => {
const searchTerm = 'fo';
const filteredTags = [ 'foo', 'foo2', 'fo' ];
expect(reducer({ tags }, { type: FILTER_TAGS, searchTerm })).toEqual({
expect(reducer(state({ tags }), { type: FILTER_TAGS, searchTerm } as any)).toEqual({
tags,
filteredTags,
});
@@ -76,19 +82,14 @@ describe('tagsListReducer', () => {
describe('listTags', () => {
const dispatch = jest.fn();
const getState = jest.fn(() => ({}));
const getState = jest.fn(() => Mock.all<ShlinkState>());
const buildShlinkApiClient = jest.fn();
const listTagsMock = jest.fn();
afterEach(() => {
dispatch.mockReset();
getState.mockClear();
buildShlinkApiClient.mockReset();
listTagsMock.mockReset();
});
afterEach(jest.clearAllMocks);
const assertNoAction = async (tagsList) => {
getState.mockReturnValue({ tagsList });
const assertNoAction = async (tagsList: TagsList) => {
getState.mockReturnValue(Mock.of<ShlinkState>({ tagsList }));
await listTags(buildShlinkApiClient, false)()(dispatch, getState);
@@ -97,8 +98,11 @@ describe('tagsListReducer', () => {
expect(getState).toHaveBeenCalledTimes(1);
};
it('does nothing when loading', async () => await assertNoAction({ loading: true }));
it('does nothing when list is not empty', async () => await assertNoAction({ loading: false, tags: [ 'foo', 'bar' ] }));
it('does nothing when loading', async () => assertNoAction(state({ loading: true })));
it(
'does nothing when list is not empty',
async () => assertNoAction(state({ loading: false, tags: [ 'foo', 'bar' ] })),
);
it('dispatches loaded lists when no error occurs', async () => {
const tags = [ 'foo', 'bar', 'baz' ];