2020-08-26 21:37:36 +03:00
|
|
|
import { shallow, ShallowWrapper } from 'enzyme';
|
2018-11-01 14:24:16 +03:00
|
|
|
import { identity } from 'ramda';
|
2020-08-26 21:37:36 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2018-12-18 01:11:55 +03:00
|
|
|
import DeleteShortUrlModal from '../../../src/short-urls/helpers/DeleteShortUrlModal';
|
2020-08-26 21:37:36 +03:00
|
|
|
import { ShortUrl } from '../../../src/short-urls/data';
|
|
|
|
import { ShortUrlDeletion } from '../../../src/short-urls/reducers/shortUrlDeletion';
|
2020-12-22 11:49:13 +03:00
|
|
|
import { ProblemDetailsError } from '../../../src/api/types';
|
2020-12-21 19:54:05 +03:00
|
|
|
import { Result } from '../../../src/utils/Result';
|
2018-11-01 14:24:16 +03:00
|
|
|
|
|
|
|
describe('<DeleteShortUrlModal />', () => {
|
2020-08-26 21:37:36 +03:00
|
|
|
let wrapper: ShallowWrapper;
|
|
|
|
const shortUrl = Mock.of<ShortUrl>({
|
2018-11-01 14:24:16 +03:00
|
|
|
tags: [],
|
|
|
|
shortCode: 'abc123',
|
2020-08-26 21:37:36 +03:00
|
|
|
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}
|
2020-08-26 21:37:36 +03:00
|
|
|
shortUrlDeletion={Mock.of<ShortUrlDeletion>(shortUrlDeletion)}
|
|
|
|
toggle={() => {}}
|
2018-11-01 14:24:16 +03:00
|
|
|
deleteShortUrl={deleteShortUrl}
|
2020-08-26 21:37:36 +03:00
|
|
|
resetDeleteShortUrl={() => {}}
|
2020-08-22 09:10:31 +03:00
|
|
|
/>,
|
2018-11-01 14:24:16 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
};
|
|
|
|
|
2020-08-26 21:37:36 +03:00
|
|
|
afterEach(() => wrapper?.unmount());
|
|
|
|
afterEach(jest.clearAllMocks);
|
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
|
|
|
});
|
2020-12-21 19:54:05 +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...');
|
|
|
|
});
|
|
|
|
|
2021-10-31 14:33:17 +03:00
|
|
|
it('enables submit button when proper short code is provided', () => {
|
2018-11-01 14:24:16 +03:00
|
|
|
const shortCode = 'abc123';
|
|
|
|
const wrapper = createWrapper({
|
|
|
|
loading: false,
|
|
|
|
error: false,
|
|
|
|
shortCode,
|
|
|
|
});
|
|
|
|
|
2021-10-31 14:33:17 +03:00
|
|
|
expect(wrapper.find('.btn-danger').prop('disabled')).toEqual(true);
|
|
|
|
wrapper.find('.form-control').simulate('change', { target: { value: shortCode } });
|
|
|
|
expect(wrapper.find('.btn-danger').prop('disabled')).toEqual(false);
|
2018-11-01 14:24:16 +03:00
|
|
|
});
|
|
|
|
|
2021-10-31 14:33:17 +03:00
|
|
|
it('tries to delete short URL when form is submit', () => {
|
2018-11-01 14:24:16 +03:00
|
|
|
const shortCode = 'abc123';
|
|
|
|
const wrapper = createWrapper({
|
|
|
|
loading: false,
|
|
|
|
error: false,
|
|
|
|
shortCode,
|
|
|
|
});
|
|
|
|
|
2021-10-31 14:33:17 +03:00
|
|
|
expect(deleteShortUrl).not.toHaveBeenCalled();
|
|
|
|
wrapper.find('form').simulate('submit', { preventDefault: identity });
|
|
|
|
expect(deleteShortUrl).toHaveBeenCalledTimes(1);
|
2018-11-01 14:24:16 +03:00
|
|
|
});
|
|
|
|
});
|