2020-08-30 10:59:14 +03:00
|
|
|
import { shallow, ShallowWrapper } from 'enzyme';
|
|
|
|
import CopyToClipboard from 'react-copy-to-clipboard';
|
2018-11-01 11:30:05 +03:00
|
|
|
import { Tooltip } from 'reactstrap';
|
2020-08-30 10:59:14 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2022-05-28 12:16:59 +03:00
|
|
|
import { CreateShortUrlResult as createResult } from '../../../src/short-urls/helpers/CreateShortUrlResult';
|
2020-08-30 10:59:14 +03:00
|
|
|
import { ShortUrl } from '../../../src/short-urls/data';
|
2022-05-29 13:18:21 +03:00
|
|
|
import { TimeoutToggle } from '../../../src/utils/helpers/hooks';
|
2020-12-21 19:54:05 +03:00
|
|
|
import { Result } from '../../../src/utils/Result';
|
2018-11-01 11:30:05 +03:00
|
|
|
|
|
|
|
describe('<CreateShortUrlResult />', () => {
|
2020-08-30 10:59:14 +03:00
|
|
|
let wrapper: ShallowWrapper;
|
2020-05-31 11:16:09 +03:00
|
|
|
const copyToClipboard = jest.fn();
|
2022-05-29 13:18:21 +03:00
|
|
|
const useTimeoutToggle = jest.fn(() => [false, copyToClipboard]) as TimeoutToggle;
|
|
|
|
const CreateShortUrlResult = createResult(useTimeoutToggle);
|
2020-08-30 10:59:14 +03:00
|
|
|
const createWrapper = (result: ShortUrl | null = null, error = false) => {
|
|
|
|
wrapper = shallow(
|
2020-11-14 00:44:26 +03:00
|
|
|
<CreateShortUrlResult resetCreateShortUrl={() => {}} result={result} error={error} saving={false} />,
|
2020-08-30 10:59:14 +03:00
|
|
|
);
|
2018-11-01 11:30:05 +03:00
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
};
|
|
|
|
|
2020-08-30 10:59:14 +03:00
|
|
|
afterEach(jest.clearAllMocks);
|
|
|
|
afterEach(() => wrapper?.unmount());
|
2018-11-01 11:30:05 +03:00
|
|
|
|
|
|
|
it('renders an error when error is true', () => {
|
2020-08-30 10:59:14 +03:00
|
|
|
const wrapper = createWrapper(Mock.all<ShortUrl>(), true);
|
2020-12-21 19:54:05 +03:00
|
|
|
const errorCard = wrapper.find(Result).filterWhere((result) => result.prop('type') === 'error');
|
2018-11-01 11:30:05 +03:00
|
|
|
|
|
|
|
expect(errorCard).toHaveLength(1);
|
|
|
|
expect(errorCard.html()).toContain('An error occurred while creating the URL :(');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders nothing when no result is provided', () => {
|
|
|
|
const wrapper = createWrapper();
|
|
|
|
|
|
|
|
expect(wrapper.html()).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders a result message when result is provided', () => {
|
2020-08-30 10:59:14 +03:00
|
|
|
const wrapper = createWrapper(Mock.of<ShortUrl>({ shortUrl: 'https://doma.in/abc123' }));
|
2018-11-01 11:30:05 +03:00
|
|
|
|
|
|
|
expect(wrapper.html()).toContain('<b>Great!</b> The short URL is <b>https://doma.in/abc123</b>');
|
|
|
|
expect(wrapper.find(CopyToClipboard)).toHaveLength(1);
|
|
|
|
expect(wrapper.find(Tooltip)).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
2019-01-08 23:19:38 +03:00
|
|
|
it('Invokes tooltip timeout when copy to clipboard button is clicked', () => {
|
2020-08-30 10:59:14 +03:00
|
|
|
const wrapper = createWrapper(Mock.of<ShortUrl>({ shortUrl: 'https://doma.in/abc123' }));
|
2018-11-01 11:30:05 +03:00
|
|
|
const copyBtn = wrapper.find(CopyToClipboard);
|
|
|
|
|
2020-05-31 11:16:09 +03:00
|
|
|
expect(copyToClipboard).not.toHaveBeenCalled();
|
2018-11-01 11:30:05 +03:00
|
|
|
copyBtn.simulate('copy');
|
2020-05-31 11:16:09 +03:00
|
|
|
expect(copyToClipboard).toHaveBeenCalledTimes(1);
|
2018-11-01 11:30:05 +03:00
|
|
|
});
|
|
|
|
});
|