mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-04-20 05:26:20 +00:00
Migrated tagEdit reducer to RTK
This commit is contained in:
@@ -1,13 +1,5 @@
|
||||
import { Mock } from 'ts-mockery';
|
||||
import reducer, {
|
||||
EDIT_TAG_START,
|
||||
EDIT_TAG_ERROR,
|
||||
EDIT_TAG,
|
||||
TAG_EDITED,
|
||||
tagEdited,
|
||||
editTag,
|
||||
EditTagAction,
|
||||
} from '../../../src/tags/reducers/tagEdit';
|
||||
import { tagEdited, EditTagAction, tagEditReducerCreator } from '../../../src/tags/reducers/tagEdit';
|
||||
import { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
|
||||
import { ColorGenerator } from '../../../src/utils/services/ColorGenerator';
|
||||
import { ShlinkState } from '../../../src/container/types';
|
||||
@@ -16,10 +8,14 @@ describe('tagEditReducer', () => {
|
||||
const oldName = 'foo';
|
||||
const newName = 'bar';
|
||||
const color = '#ff0000';
|
||||
const editTagCall = jest.fn();
|
||||
const buildShlinkApiClient = () => Mock.of<ShlinkApiClient>({ editTag: editTagCall });
|
||||
const colorGenerator = Mock.of<ColorGenerator>({ setColorForKey: jest.fn() });
|
||||
const { reducer, editTag } = tagEditReducerCreator(buildShlinkApiClient, colorGenerator);
|
||||
|
||||
describe('reducer', () => {
|
||||
it('returns loading on EDIT_TAG_START', () => {
|
||||
expect(reducer(undefined, Mock.of<EditTagAction>({ type: EDIT_TAG_START }))).toEqual({
|
||||
expect(reducer(undefined, Mock.of<EditTagAction>({ type: editTag.pending.toString() }))).toEqual({
|
||||
editing: true,
|
||||
edited: false,
|
||||
error: false,
|
||||
@@ -27,7 +23,7 @@ describe('tagEditReducer', () => {
|
||||
});
|
||||
|
||||
it('returns error on EDIT_TAG_ERROR', () => {
|
||||
expect(reducer(undefined, Mock.of<EditTagAction>({ type: EDIT_TAG_ERROR }))).toEqual({
|
||||
expect(reducer(undefined, Mock.of<EditTagAction>({ type: editTag.rejected.toString() }))).toEqual({
|
||||
editing: false,
|
||||
edited: false,
|
||||
error: true,
|
||||
@@ -36,7 +32,7 @@ describe('tagEditReducer', () => {
|
||||
|
||||
it('returns tag names on EDIT_TAG', () => {
|
||||
expect(reducer(undefined, {
|
||||
type: EDIT_TAG,
|
||||
type: editTag.fulfilled.toString(),
|
||||
payload: { oldName, newName, color },
|
||||
})).toEqual({
|
||||
editing: false,
|
||||
@@ -51,7 +47,7 @@ describe('tagEditReducer', () => {
|
||||
describe('tagEdited', () => {
|
||||
it('returns action based on provided params', () =>
|
||||
expect(tagEdited({ oldName: 'foo', newName: 'bar', color: '#ff0000' })).toEqual({
|
||||
type: TAG_EDITED,
|
||||
type: tagEdited.toString(),
|
||||
payload: {
|
||||
oldName: 'foo',
|
||||
newName: 'bar',
|
||||
@@ -61,56 +57,48 @@ describe('tagEditReducer', () => {
|
||||
});
|
||||
|
||||
describe('editTag', () => {
|
||||
const createApiClientMock = (result: Promise<any>) => Mock.of<ShlinkApiClient>({
|
||||
editTag: jest.fn(async () => result),
|
||||
});
|
||||
const colorGenerator = Mock.of<ColorGenerator>({
|
||||
setColorForKey: jest.fn(),
|
||||
});
|
||||
const dispatch = jest.fn();
|
||||
const getState = () => Mock.of<ShlinkState>();
|
||||
|
||||
afterEach(jest.clearAllMocks);
|
||||
|
||||
it('calls API on success', async () => {
|
||||
const apiClientMock = createApiClientMock(Promise.resolve());
|
||||
const dispatchable = editTag(() => apiClientMock, colorGenerator)({ oldName, newName, color });
|
||||
editTagCall.mockResolvedValue(undefined);
|
||||
|
||||
await dispatchable(dispatch, getState);
|
||||
await editTag({ oldName, newName, color })(dispatch, getState, {});
|
||||
|
||||
expect(apiClientMock.editTag).toHaveBeenCalledTimes(1);
|
||||
expect(apiClientMock.editTag).toHaveBeenCalledWith(oldName, newName);
|
||||
expect(editTagCall).toHaveBeenCalledTimes(1);
|
||||
expect(editTagCall).toHaveBeenCalledWith(oldName, newName);
|
||||
|
||||
expect(colorGenerator.setColorForKey).toHaveBeenCalledTimes(1);
|
||||
expect(colorGenerator.setColorForKey).toHaveBeenCalledWith(newName, color);
|
||||
|
||||
expect(dispatch).toHaveBeenCalledTimes(2);
|
||||
expect(dispatch).toHaveBeenNthCalledWith(1, { type: EDIT_TAG_START });
|
||||
expect(dispatch).toHaveBeenNthCalledWith(2, {
|
||||
type: EDIT_TAG,
|
||||
expect(dispatch).toHaveBeenNthCalledWith(1, expect.objectContaining({ type: editTag.pending.toString() }));
|
||||
expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({
|
||||
type: editTag.fulfilled.toString(),
|
||||
payload: { oldName, newName, color },
|
||||
});
|
||||
}));
|
||||
});
|
||||
|
||||
it('throws on error', async () => {
|
||||
const error = 'Error';
|
||||
const apiClientMock = createApiClientMock(Promise.reject(error));
|
||||
const dispatchable = editTag(() => apiClientMock, colorGenerator)({ oldName, newName, color });
|
||||
editTagCall.mockRejectedValue(error);
|
||||
|
||||
try {
|
||||
await dispatchable(dispatch, getState);
|
||||
await editTag({ oldName, newName, color })(dispatch, getState, {});
|
||||
} catch (e) {
|
||||
expect(e).toEqual(error);
|
||||
}
|
||||
|
||||
expect(apiClientMock.editTag).toHaveBeenCalledTimes(1);
|
||||
expect(apiClientMock.editTag).toHaveBeenCalledWith(oldName, newName);
|
||||
expect(editTagCall).toHaveBeenCalledTimes(1);
|
||||
expect(editTagCall).toHaveBeenCalledWith(oldName, newName);
|
||||
|
||||
expect(colorGenerator.setColorForKey).not.toHaveBeenCalled();
|
||||
|
||||
expect(dispatch).toHaveBeenCalledTimes(2);
|
||||
expect(dispatch).toHaveBeenNthCalledWith(1, { type: EDIT_TAG_START });
|
||||
expect(dispatch).toHaveBeenNthCalledWith(2, { type: EDIT_TAG_ERROR });
|
||||
expect(dispatch).toHaveBeenNthCalledWith(1, expect.objectContaining({ type: editTag.pending.toString() }));
|
||||
expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({ type: editTag.rejected.toString() }));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -9,10 +9,10 @@ import reducer, {
|
||||
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';
|
||||
import { ShortUrl } from '../../../src/short-urls/data';
|
||||
import { CREATE_SHORT_URL } from '../../../src/short-urls/reducers/shortUrlCreation';
|
||||
import { tagEdited } from '../../../src/tags/reducers/tagEdit';
|
||||
|
||||
describe('tagsListReducer', () => {
|
||||
const state = (props: Partial<TagsList>) => Mock.of<TagsList>(props);
|
||||
@@ -63,7 +63,7 @@ describe('tagsListReducer', () => {
|
||||
expect(reducer(
|
||||
state({ tags, filteredTags: tags }),
|
||||
{
|
||||
type: TAG_EDITED,
|
||||
type: tagEdited.toString(),
|
||||
payload: { oldName, newName },
|
||||
} as any,
|
||||
)).toEqual({
|
||||
|
||||
Reference in New Issue
Block a user