shlink-web-client/test/short-urls/reducers/shortUrlDeletion.test.ts

105 lines
3.7 KiB
TypeScript
Raw Normal View History

2020-08-26 19:55:40 +03:00
import { Mock } from 'ts-mockery';
2022-11-09 21:13:44 +03:00
import {
shortUrlDeletionReducerCreator,
deleteShortUrl as deleteShortUrlCretor,
} from '../../../src/short-urls/reducers/shortUrlDeletion';
2022-05-28 11:47:39 +03:00
import { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
import { ProblemDetailsError } from '../../../src/api/types/errors';
2018-12-21 12:02:42 +03:00
describe('shortUrlDeletionReducer', () => {
const deleteShortUrlCall = jest.fn();
const buildShlinkApiClient = () => Mock.of<ShlinkApiClient>({ deleteShortUrl: deleteShortUrlCall });
2022-11-09 21:13:44 +03:00
const deleteShortUrl = deleteShortUrlCretor(buildShlinkApiClient);
const { reducer, resetDeleteShortUrl } = shortUrlDeletionReducerCreator(deleteShortUrl);
beforeEach(jest.clearAllMocks);
2018-12-21 12:02:42 +03:00
describe('reducer', () => {
it('returns loading on DELETE_SHORT_URL_START', () =>
2022-11-06 15:21:46 +03:00
expect(reducer(undefined, { type: deleteShortUrl.pending.toString() })).toEqual({
2018-12-21 12:02:42 +03:00
shortCode: '',
loading: true,
error: false,
deleted: false,
2018-12-21 12:02:42 +03:00
}));
it('returns default on RESET_DELETE_SHORT_URL', () =>
2022-11-06 15:21:46 +03:00
expect(reducer(undefined, { type: resetDeleteShortUrl.toString() })).toEqual({
2018-12-21 12:02:42 +03:00
shortCode: '',
loading: false,
error: false,
deleted: false,
2018-12-21 12:02:42 +03:00
}));
it('returns shortCode on SHORT_URL_DELETED', () =>
expect(reducer(undefined, {
type: deleteShortUrl.fulfilled.toString(),
payload: { shortCode: 'foo' },
2022-11-06 15:21:46 +03:00
})).toEqual({
2018-12-21 12:02:42 +03:00
shortCode: 'foo',
loading: false,
error: false,
deleted: true,
2018-12-21 12:02:42 +03:00
}));
it('returns errorData on DELETE_SHORT_URL_ERROR', () => {
2022-11-15 13:50:04 +03:00
const errorData = Mock.of<ProblemDetailsError>({ type: 'bar', detail: 'detail', title: 'title', status: 400 });
const error = errorData;
2018-12-21 12:02:42 +03:00
2022-11-06 15:21:46 +03:00
expect(reducer(undefined, { type: deleteShortUrl.rejected.toString(), error })).toEqual({
2018-12-21 12:02:42 +03:00
shortCode: '',
loading: false,
error: true,
deleted: false,
2018-12-21 12:02:42 +03:00
errorData,
});
});
});
describe('resetDeleteShortUrl', () => {
it('returns expected action', () =>
expect(resetDeleteShortUrl()).toEqual({ type: resetDeleteShortUrl.toString() }));
2018-12-21 12:02:42 +03:00
});
describe('deleteShortUrl', () => {
2019-04-19 13:41:59 +03:00
const dispatch = jest.fn();
const getState = jest.fn().mockReturnValue({ selectedServer: {} });
2018-12-21 12:02:42 +03:00
it.each(
2022-03-26 14:17:42 +03:00
[[undefined], [null], ['example.com']],
)('dispatches proper actions if API client request succeeds', async (domain) => {
2018-12-21 12:02:42 +03:00
const shortCode = 'abc123';
await deleteShortUrl({ shortCode, domain })(dispatch, getState, {});
2018-12-21 12:02:42 +03:00
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, expect.objectContaining({ type: deleteShortUrl.pending.toString() }));
expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({
type: deleteShortUrl.fulfilled.toString(),
payload: { shortCode, domain },
}));
2018-12-21 12:02:42 +03:00
expect(deleteShortUrlCall).toHaveBeenCalledTimes(1);
expect(deleteShortUrlCall).toHaveBeenCalledWith(shortCode, domain);
2018-12-21 12:02:42 +03:00
});
it('dispatches proper actions if API client request fails', async () => {
const data = { foo: 'bar' };
const shortCode = 'abc123';
deleteShortUrlCall.mockRejectedValue({ response: { data } });
await deleteShortUrl({ shortCode })(dispatch, getState, {});
2018-12-21 12:02:42 +03:00
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, expect.objectContaining({ type: deleteShortUrl.pending.toString() }));
expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({
type: deleteShortUrl.rejected.toString(),
}));
2018-12-21 12:02:42 +03:00
expect(deleteShortUrlCall).toHaveBeenCalledTimes(1);
expect(deleteShortUrlCall).toHaveBeenCalledWith(shortCode, undefined);
2018-12-21 12:02:42 +03:00
});
});
});