Migrated shortUrlCreation reducer to RTK

This commit is contained in:
Alejandro Celaya
2022-11-06 10:11:44 +01:00
parent 50823003b4
commit a316366ae9
8 changed files with 80 additions and 83 deletions

View File

@@ -1,19 +1,19 @@
import { Mock } from 'ts-mockery';
import reducer, {
CREATE_SHORT_URL_START,
CREATE_SHORT_URL_ERROR,
CREATE_SHORT_URL,
RESET_CREATE_SHORT_URL,
createShortUrl,
resetCreateShortUrl,
import {
CreateShortUrlAction,
shortUrlCreationReducerCreator,
} from '../../../src/short-urls/reducers/shortUrlCreation';
import { ShortUrl } from '../../../src/short-urls/data';
import { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
import { ShlinkState } from '../../../src/container/types';
describe('shortUrlCreationReducer', () => {
const shortUrl = Mock.all<ShortUrl>();
const shortUrl = Mock.of<ShortUrl>();
const createShortUrlCall = jest.fn();
const buildShlinkApiClient = () => Mock.of<ShlinkApiClient>({ createShortUrl: createShortUrlCall });
const { reducer, createShortUrl, resetCreateShortUrl } = shortUrlCreationReducerCreator(buildShlinkApiClient);
afterEach(jest.resetAllMocks);
describe('reducer', () => {
const action = (type: string, args: Partial<CreateShortUrlAction> = {}) => Mock.of<CreateShortUrlAction>(
@@ -21,7 +21,7 @@ describe('shortUrlCreationReducer', () => {
);
it('returns loading on CREATE_SHORT_URL_START', () => {
expect(reducer(undefined, action(CREATE_SHORT_URL_START))).toEqual({
expect(reducer(undefined, action(createShortUrl.pending.toString()))).toEqual({
result: null,
saving: true,
error: false,
@@ -29,7 +29,7 @@ describe('shortUrlCreationReducer', () => {
});
it('returns error on CREATE_SHORT_URL_ERROR', () => {
expect(reducer(undefined, action(CREATE_SHORT_URL_ERROR))).toEqual({
expect(reducer(undefined, action(createShortUrl.rejected.toString()))).toEqual({
result: null,
saving: false,
error: true,
@@ -37,7 +37,7 @@ describe('shortUrlCreationReducer', () => {
});
it('returns result on CREATE_SHORT_URL', () => {
expect(reducer(undefined, action(CREATE_SHORT_URL, { result: shortUrl }))).toEqual({
expect(reducer(undefined, action(createShortUrl.fulfilled.toString(), { payload: shortUrl }))).toEqual({
result: shortUrl,
saving: false,
error: false,
@@ -45,7 +45,7 @@ describe('shortUrlCreationReducer', () => {
});
it('returns default state on RESET_CREATE_SHORT_URL', () => {
expect(reducer(undefined, action(RESET_CREATE_SHORT_URL))).toEqual({
expect(reducer(undefined, action(resetCreateShortUrl.toString()))).toEqual({
result: null,
saving: false,
error: false,
@@ -54,47 +54,43 @@ describe('shortUrlCreationReducer', () => {
});
describe('resetCreateShortUrl', () => {
it('returns proper action', () => expect(resetCreateShortUrl()).toEqual({ type: RESET_CREATE_SHORT_URL }));
it('returns proper action', () => expect(resetCreateShortUrl()).toEqual({ type: resetCreateShortUrl.toString() }));
});
describe('createShortUrl', () => {
const createApiClientMock = (result: Promise<ShortUrl>) => Mock.of<ShlinkApiClient>({
createShortUrl: jest.fn().mockReturnValue(result),
});
const dispatch = jest.fn();
const getState = () => Mock.all<ShlinkState>();
afterEach(jest.resetAllMocks);
it('calls API on success', async () => {
const apiClientMock = createApiClientMock(Promise.resolve(shortUrl));
const dispatchable = createShortUrl(() => apiClientMock)({ longUrl: 'foo' });
createShortUrlCall.mockResolvedValue(shortUrl);
await createShortUrl({ longUrl: 'foo' })(dispatch, getState, {});
await dispatchable(dispatch, getState);
expect(apiClientMock.createShortUrl).toHaveBeenCalledTimes(1);
expect(createShortUrlCall).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, { type: CREATE_SHORT_URL_START });
expect(dispatch).toHaveBeenNthCalledWith(2, { type: CREATE_SHORT_URL, result: shortUrl });
expect(dispatch).toHaveBeenNthCalledWith(1, expect.objectContaining({
type: createShortUrl.pending.toString(),
}));
expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({
type: createShortUrl.fulfilled.toString(),
payload: shortUrl,
}));
});
it('throws on error', async () => {
const error = 'Error';
const apiClientMock = createApiClientMock(Promise.reject(error));
const dispatchable = createShortUrl(() => apiClientMock)({ longUrl: 'foo' });
const error = new Error('Error message');
createShortUrlCall.mockRejectedValue(error);
expect.assertions(5);
await createShortUrl({ longUrl: 'foo' })(dispatch, getState, {});
try {
await dispatchable(dispatch, getState);
} catch (e) {
expect(e).toEqual(error);
}
expect(apiClientMock.createShortUrl).toHaveBeenCalledTimes(1);
expect(createShortUrlCall).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, { type: CREATE_SHORT_URL_START });
expect(dispatch).toHaveBeenNthCalledWith(2, { type: CREATE_SHORT_URL_ERROR });
expect(dispatch).toHaveBeenNthCalledWith(1, expect.objectContaining({
type: createShortUrl.pending.toString(),
}));
expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({
type: createShortUrl.rejected.toString(),
error: expect.objectContaining({ message: 'Error message' }),
}));
});
});
});

View File

@@ -142,7 +142,7 @@ describe('shortUrlsListReducer', () => {
error: false,
};
expect(reducer(state, { type: CREATE_SHORT_URL, result: newShortUrl } as any)).toEqual({
expect(reducer(state, { type: `${CREATE_SHORT_URL}/fulfilled`, payload: newShortUrl } as any)).toEqual({
shortUrls: {
data: expectedData,
pagination: { totalItems: 16 },

View File

@@ -83,9 +83,9 @@ describe('tagsListReducer', () => {
[['new', 'tag'], ['foo', 'bar', 'baz', 'foo2', 'fo', 'new', 'tag']],
])('appends new short URL\'s tags to the list of tags on CREATE_SHORT_URL', (shortUrlTags, expectedTags) => {
const tags = ['foo', 'bar', 'baz', 'foo2', 'fo'];
const result = Mock.of<ShortUrl>({ tags: shortUrlTags });
const payload = Mock.of<ShortUrl>({ tags: shortUrlTags });
expect(reducer(state({ tags }), { type: CREATE_SHORT_URL, result } as any)).toEqual({
expect(reducer(state({ tags }), { type: `${CREATE_SHORT_URL}/fulfilled`, payload } as any)).toEqual({
tags: expectedTags,
});
});