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

77 lines
2.6 KiB
TypeScript
Raw Normal View History

2023-04-13 23:47:13 +03:00
import { fromPartial } from '@total-typescript/shoehorn';
import type { ShlinkApiClient } from '../../../../src/api/services/ShlinkApiClient';
2023-08-04 12:16:01 +03:00
import type { ProblemDetailsError } from '../../../src/api-contract';
2022-11-09 21:13:44 +03:00
import {
2023-04-13 23:47:13 +03:00
deleteShortUrl as deleteShortUrlCreator,
2023-02-18 13:11:01 +03:00
shortUrlDeletionReducerCreator,
} from '../../../src/short-urls/reducers/shortUrlDeletion';
2018-12-21 12:02:42 +03:00
describe('shortUrlDeletionReducer', () => {
2023-05-27 12:57:26 +03:00
const deleteShortUrlCall = vi.fn();
2023-04-13 23:47:13 +03:00
const buildShlinkApiClient = () => fromPartial<ShlinkApiClient>({ deleteShortUrl: deleteShortUrlCall });
const deleteShortUrl = deleteShortUrlCreator(buildShlinkApiClient);
2022-11-09 21:13:44 +03:00
const { reducer, resetDeleteShortUrl } = shortUrlDeletionReducerCreator(deleteShortUrl);
2018-12-21 12:02:42 +03:00
describe('reducer', () => {
it('returns loading on DELETE_SHORT_URL_START', () =>
expect(reducer(undefined, deleteShortUrl.pending('', { shortCode: '' }))).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', () =>
expect(reducer(undefined, resetDeleteShortUrl())).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, deleteShortUrl.fulfilled({ shortCode: 'foo' }, '', { shortCode: 'foo' }))).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', () => {
2023-04-13 23:47:13 +03:00
const errorData = fromPartial<ProblemDetailsError>(
{ type: 'bar', detail: 'detail', title: 'title', status: 400 },
);
const error = errorData as unknown as Error;
2018-12-21 12:02:42 +03:00
expect(reducer(undefined, deleteShortUrl.rejected(error, '', { shortCode: '' }))).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('deleteShortUrl', () => {
2023-05-27 12:57:26 +03:00
const dispatch = vi.fn();
const getState = vi.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).toHaveBeenLastCalledWith(expect.objectContaining({
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
});
});
});