shlink-web-client/test/short-urls/helpers/DeleteShortUrlModal.test.tsx

122 lines
3.9 KiB
TypeScript
Raw Normal View History

import { shallow, ShallowWrapper } from 'enzyme';
2018-11-01 14:24:16 +03:00
import { identity } from 'ramda';
import { Mock } from 'ts-mockery';
import DeleteShortUrlModal from '../../../src/short-urls/helpers/DeleteShortUrlModal';
import { ShortUrl } from '../../../src/short-urls/data';
import { ShortUrlDeletion } from '../../../src/short-urls/reducers/shortUrlDeletion';
import { ProblemDetailsError } from '../../../src/utils/services/types';
import { Result } from '../../../src/utils/Result';
2018-11-01 14:24:16 +03:00
describe('<DeleteShortUrlModal />', () => {
let wrapper: ShallowWrapper;
const shortUrl = Mock.of<ShortUrl>({
2018-11-01 14:24:16 +03:00
tags: [],
shortCode: 'abc123',
longUrl: 'https://long-domain.com/foo/bar',
});
const deleteShortUrl = jest.fn(async () => Promise.resolve());
const createWrapper = (shortUrlDeletion: Partial<ShortUrlDeletion>) => {
2018-11-01 14:24:16 +03:00
wrapper = shallow(
<DeleteShortUrlModal
isOpen
shortUrl={shortUrl}
shortUrlDeletion={Mock.of<ShortUrlDeletion>(shortUrlDeletion)}
toggle={() => {}}
2018-11-01 14:24:16 +03:00
deleteShortUrl={deleteShortUrl}
resetDeleteShortUrl={() => {}}
2020-08-22 09:10:31 +03:00
/>,
2018-11-01 14:24:16 +03:00
);
return wrapper;
};
afterEach(() => wrapper?.unmount());
afterEach(jest.clearAllMocks);
2018-11-01 14:24:16 +03:00
it.each([
[
{ type: 'INVALID_SHORTCODE_DELETION' },
'This short URL has received too many visits, and therefore, it cannot be deleted.',
],
[
{ type: 'INVALID_SHORTCODE_DELETION', threshold: 8 },
'This short URL has received more than 8 visits, and therefore, it cannot be deleted.',
],
])('shows threshold error message when threshold error occurs', (errorData: Partial<ProblemDetailsError>, expectedMessage) => {
2018-11-01 14:24:16 +03:00
const wrapper = createWrapper({
loading: false,
error: true,
shortCode: 'abc123',
errorData: Mock.of<ProblemDetailsError>(errorData),
2018-11-01 14:24:16 +03:00
});
const warning = wrapper.find(Result).filterWhere((result) => result.prop('type') === 'warning');
2018-11-01 14:24:16 +03:00
expect(warning).toHaveLength(1);
expect(warning.html()).toContain(expectedMessage);
2018-11-01 14:24:16 +03:00
});
it('shows generic error when non-threshold error occurs', () => {
const wrapper = createWrapper({
loading: false,
error: true,
shortCode: 'abc123',
2020-12-12 15:33:21 +03:00
errorData: Mock.of<ProblemDetailsError>({ type: 'OTHER_ERROR' }),
2018-11-01 14:24:16 +03:00
});
const error = wrapper.find(Result).filterWhere((result) => result.prop('type') === 'error');
2018-11-01 14:24:16 +03:00
expect(error).toHaveLength(1);
expect(error.html()).toContain('Something went wrong while deleting the URL :(');
});
it('disables submit button when loading', () => {
const wrapper = createWrapper({
loading: true,
error: false,
shortCode: 'abc123',
});
const submit = wrapper.find('.btn-danger');
expect(submit).toHaveLength(1);
expect(submit.prop('disabled')).toEqual(true);
expect(submit.html()).toContain('Deleting...');
});
it('enables submit button when proper short code is provided', (done) => {
const shortCode = 'abc123';
const wrapper = createWrapper({
loading: false,
error: false,
shortCode,
});
const input = wrapper.find('.form-control');
input.simulate('change', { target: { value: shortCode } });
setImmediate(() => {
const submit = wrapper.find('.btn-danger');
expect(submit.prop('disabled')).toEqual(false);
done();
});
});
it('tries to delete short URL when form is submit', (done) => {
const shortCode = 'abc123';
const wrapper = createWrapper({
loading: false,
error: false,
shortCode,
});
const input = wrapper.find('.form-control');
input.simulate('change', { target: { value: shortCode } });
setImmediate(() => {
const form = wrapper.find('form');
2019-04-19 13:41:59 +03:00
expect(deleteShortUrl).not.toHaveBeenCalled();
2018-11-01 14:24:16 +03:00
form.simulate('submit', { preventDefault: identity });
2019-04-19 13:41:59 +03:00
expect(deleteShortUrl).toHaveBeenCalledTimes(1);
2018-11-01 14:24:16 +03:00
done();
});
});
});