2018-11-01 14:24:16 +03:00
|
|
|
import React from 'react';
|
|
|
|
import { shallow } from 'enzyme';
|
|
|
|
import { identity } from 'ramda';
|
2020-01-11 14:24:45 +03:00
|
|
|
import each from 'jest-each';
|
2018-12-18 01:11:55 +03:00
|
|
|
import DeleteShortUrlModal from '../../../src/short-urls/helpers/DeleteShortUrlModal';
|
2018-11-01 14:24:16 +03:00
|
|
|
|
|
|
|
describe('<DeleteShortUrlModal />', () => {
|
|
|
|
let wrapper;
|
|
|
|
const shortUrl = {
|
|
|
|
tags: [],
|
|
|
|
shortCode: 'abc123',
|
|
|
|
originalUrl: 'https://long-domain.com/foo/bar',
|
|
|
|
};
|
2019-04-19 13:41:59 +03:00
|
|
|
const deleteShortUrl = jest.fn(() => Promise.resolve());
|
2018-11-01 14:24:16 +03:00
|
|
|
const createWrapper = (shortUrlDeletion) => {
|
|
|
|
wrapper = shallow(
|
|
|
|
<DeleteShortUrlModal
|
|
|
|
isOpen
|
|
|
|
shortUrl={shortUrl}
|
|
|
|
shortUrlDeletion={shortUrlDeletion}
|
|
|
|
toggle={identity}
|
|
|
|
deleteShortUrl={deleteShortUrl}
|
|
|
|
resetDeleteShortUrl={identity}
|
|
|
|
shortUrlDeleted={identity}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
};
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
wrapper && wrapper.unmount();
|
2019-04-19 13:41:59 +03:00
|
|
|
deleteShortUrl.mockClear();
|
2018-11-01 14:24:16 +03:00
|
|
|
});
|
|
|
|
|
2020-01-11 14:24:45 +03:00
|
|
|
each([
|
|
|
|
[
|
|
|
|
{ error: 'INVALID_SHORTCODE_DELETION' },
|
|
|
|
'This short URL has received too many visits, and therefore, it cannot be deleted.',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{ type: 'INVALID_SHORTCODE_DELETION' },
|
|
|
|
'This short URL has received too many visits, and therefore, it cannot be deleted.',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{ error: 'INVALID_SHORTCODE_DELETION', threshold: 35 },
|
|
|
|
'This short URL has received more than 35 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.',
|
|
|
|
],
|
|
|
|
]).it('shows threshold error message when threshold error occurs', (errorData, expectedMessage) => {
|
2018-11-01 14:24:16 +03:00
|
|
|
const wrapper = createWrapper({
|
|
|
|
loading: false,
|
|
|
|
error: true,
|
|
|
|
shortCode: 'abc123',
|
2020-01-11 14:24:45 +03:00
|
|
|
errorData,
|
2018-11-01 14:24:16 +03:00
|
|
|
});
|
|
|
|
const warning = wrapper.find('.bg-warning');
|
|
|
|
|
|
|
|
expect(warning).toHaveLength(1);
|
2020-01-11 14:24:45 +03:00
|
|
|
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',
|
|
|
|
errorData: { error: 'OTHER_ERROR' },
|
|
|
|
});
|
|
|
|
const error = wrapper.find('.bg-danger');
|
|
|
|
|
|
|
|
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',
|
|
|
|
errorData: {},
|
|
|
|
});
|
|
|
|
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,
|
|
|
|
errorData: {},
|
|
|
|
});
|
|
|
|
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,
|
|
|
|
errorData: {},
|
|
|
|
});
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|