Migrated short URL detail reducer to RTK

This commit is contained in:
Alejandro Celaya
2022-11-06 19:32:02 +01:00
parent f93bb88d35
commit f803941fe4
4 changed files with 73 additions and 59 deletions

View File

@@ -1,31 +1,29 @@
import { Mock } from 'ts-mockery';
import reducer, {
getShortUrlDetail,
GET_SHORT_URL_DETAIL_START,
GET_SHORT_URL_DETAIL_ERROR,
GET_SHORT_URL_DETAIL,
ShortUrlDetailAction,
} from '../../../src/short-urls/reducers/shortUrlDetail';
import { ShortUrlDetailAction, shortUrlDetailReducerCreator } from '../../../src/short-urls/reducers/shortUrlDetail';
import { ShortUrl } from '../../../src/short-urls/data';
import { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
import { ShlinkState } from '../../../src/container/types';
import { ShortUrlsList } from '../../../src/short-urls/reducers/shortUrlsList';
describe('shortUrlDetailReducer', () => {
const getShortUrlCall = jest.fn();
const buildShlinkApiClient = () => Mock.of<ShlinkApiClient>({ getShortUrl: getShortUrlCall });
const { reducer, getShortUrlDetail } = shortUrlDetailReducerCreator(buildShlinkApiClient);
beforeEach(jest.clearAllMocks);
describe('reducer', () => {
const action = (type: string) => Mock.of<ShortUrlDetailAction>({ type });
it('returns loading on GET_SHORT_URL_DETAIL_START', () => {
const state = reducer({ loading: false, error: false }, action(GET_SHORT_URL_DETAIL_START));
const state = reducer({ loading: false, error: false }, action(getShortUrlDetail.pending.toString()));
const { loading } = state;
expect(loading).toEqual(true);
});
it('stops loading and returns error on GET_SHORT_URL_DETAIL_ERROR', () => {
const state = reducer({ loading: true, error: false }, action(GET_SHORT_URL_DETAIL_ERROR));
const state = reducer({ loading: true, error: false }, action(getShortUrlDetail.rejected.toString()));
const { loading, error } = state;
expect(loading).toEqual(false);
@@ -34,7 +32,10 @@ describe('shortUrlDetailReducer', () => {
it('return short URL on GET_SHORT_URL_DETAIL', () => {
const actionShortUrl = Mock.of<ShortUrl>({ longUrl: 'foo', shortCode: 'bar' });
const state = reducer({ loading: true, error: false }, { type: GET_SHORT_URL_DETAIL, payload: actionShortUrl });
const state = reducer(
{ loading: true, error: false },
{ type: getShortUrlDetail.fulfilled.toString(), payload: actionShortUrl },
);
const { loading, error, shortUrl } = state;
expect(loading).toEqual(false);
@@ -44,21 +45,22 @@ describe('shortUrlDetailReducer', () => {
});
describe('getShortUrlDetail', () => {
const buildApiClientMock = (returned: Promise<ShortUrl>) => Mock.of<ShlinkApiClient>({
getShortUrl: jest.fn(async () => returned),
});
const dispatchMock = jest.fn();
const buildGetState = (shortUrlsList?: ShortUrlsList) => () => Mock.of<ShlinkState>({ shortUrlsList });
it('dispatches start and error when promise is rejected', async () => {
const ShlinkApiClient = buildApiClientMock(Promise.reject({}));
getShortUrlCall.mockRejectedValue({});
await getShortUrlDetail(() => ShlinkApiClient)({ shortCode: 'abc123', domain: '' })(dispatchMock, buildGetState());
await getShortUrlDetail({ shortCode: 'abc123', domain: '' })(dispatchMock, buildGetState(), {});
expect(dispatchMock).toHaveBeenCalledTimes(2);
expect(dispatchMock).toHaveBeenNthCalledWith(1, { type: GET_SHORT_URL_DETAIL_START });
expect(dispatchMock).toHaveBeenNthCalledWith(2, { type: GET_SHORT_URL_DETAIL_ERROR });
expect(ShlinkApiClient.getShortUrl).toHaveBeenCalledTimes(1);
expect(dispatchMock).toHaveBeenNthCalledWith(1, expect.objectContaining({
type: getShortUrlDetail.pending.toString(),
}));
expect(dispatchMock).toHaveBeenNthCalledWith(2, expect.objectContaining({
type: getShortUrlDetail.rejected.toString(),
}));
expect(getShortUrlCall).toHaveBeenCalledTimes(1);
});
it.each([
@@ -78,33 +80,44 @@ describe('shortUrlDetailReducer', () => {
],
])('performs API call when short URL is not found in local state', async (shortUrlsList?: ShortUrlsList) => {
const resolvedShortUrl = Mock.of<ShortUrl>({ longUrl: 'foo', shortCode: 'abc123' });
const ShlinkApiClient = buildApiClientMock(Promise.resolve(resolvedShortUrl));
getShortUrlCall.mockResolvedValue(resolvedShortUrl);
await getShortUrlDetail(() => ShlinkApiClient)({ shortCode: 'abc123', domain: '' })(dispatchMock, buildGetState(shortUrlsList));
await getShortUrlDetail({ shortCode: 'abc123', domain: '' })(dispatchMock, buildGetState(shortUrlsList), {});
expect(dispatchMock).toHaveBeenCalledTimes(2);
expect(dispatchMock).toHaveBeenNthCalledWith(1, { type: GET_SHORT_URL_DETAIL_START });
expect(dispatchMock).toHaveBeenNthCalledWith(2, { type: GET_SHORT_URL_DETAIL, payload: resolvedShortUrl });
expect(ShlinkApiClient.getShortUrl).toHaveBeenCalledTimes(1);
expect(dispatchMock).toHaveBeenNthCalledWith(1, expect.objectContaining({
type: getShortUrlDetail.pending.toString(),
}));
expect(dispatchMock).toHaveBeenNthCalledWith(2, expect.objectContaining({
type: getShortUrlDetail.fulfilled.toString(),
payload: resolvedShortUrl,
}));
expect(getShortUrlCall).toHaveBeenCalledTimes(1);
});
it('avoids API calls when short URL is found in local state', async () => {
const foundShortUrl = Mock.of<ShortUrl>({ longUrl: 'foo', shortCode: 'abc123' });
const ShlinkApiClient = buildApiClientMock(Promise.resolve(Mock.all<ShortUrl>()));
getShortUrlCall.mockResolvedValue(Mock.all<ShortUrl>());
await getShortUrlDetail(() => ShlinkApiClient)(foundShortUrl)(
await getShortUrlDetail(foundShortUrl)(
dispatchMock,
buildGetState(Mock.of<ShortUrlsList>({
shortUrls: {
data: [foundShortUrl],
},
})),
{},
);
expect(dispatchMock).toHaveBeenCalledTimes(2);
expect(dispatchMock).toHaveBeenNthCalledWith(1, { type: GET_SHORT_URL_DETAIL_START });
expect(dispatchMock).toHaveBeenNthCalledWith(2, { type: GET_SHORT_URL_DETAIL, payload: foundShortUrl });
expect(ShlinkApiClient.getShortUrl).not.toHaveBeenCalled();
expect(dispatchMock).toHaveBeenNthCalledWith(1, expect.objectContaining({
type: getShortUrlDetail.pending.toString(),
}));
expect(dispatchMock).toHaveBeenNthCalledWith(2, expect.objectContaining({
type: getShortUrlDetail.fulfilled.toString(),
payload: foundShortUrl,
}));
expect(getShortUrlCall).not.toHaveBeenCalled();
});
});
});