shlink-web-client/test/short-urls/helpers/CreateShortUrlResult.test.js

55 lines
1.8 KiB
JavaScript
Raw Normal View History

2018-11-01 11:30:05 +03:00
import React from 'react';
import { shallow } from 'enzyme';
import { identity } from 'ramda';
import { CopyToClipboard } from 'react-copy-to-clipboard';
import { Tooltip } from 'reactstrap';
2019-01-08 23:19:38 +03:00
import createCreateShortUrlResult from '../../../src/short-urls/helpers/CreateShortUrlResult';
2018-11-01 11:30:05 +03:00
describe('<CreateShortUrlResult />', () => {
let wrapper;
2019-04-19 13:41:59 +03:00
const stateFlagTimeout = jest.fn();
2018-11-01 11:30:05 +03:00
const createWrapper = (result, error = false) => {
2019-01-08 23:19:38 +03:00
const CreateShortUrlResult = createCreateShortUrlResult(stateFlagTimeout);
2018-11-01 11:30:05 +03:00
wrapper = shallow(<CreateShortUrlResult resetCreateShortUrl={identity} result={result} error={error} />);
return wrapper;
};
2019-01-08 23:19:38 +03:00
afterEach(() => {
2019-04-19 13:41:59 +03:00
stateFlagTimeout.mockReset();
2019-01-08 23:19:38 +03:00
wrapper && wrapper.unmount();
});
2018-11-01 11:30:05 +03:00
it('renders an error when error is true', () => {
const wrapper = createWrapper({}, true);
const errorCard = wrapper.find('.bg-danger');
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', () => {
const wrapper = createWrapper({ shortUrl: 'https://doma.in/abc123' });
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', () => {
2018-11-01 11:30:05 +03:00
const wrapper = createWrapper({ shortUrl: 'https://doma.in/abc123' });
const copyBtn = wrapper.find(CopyToClipboard);
2019-04-19 13:41:59 +03:00
expect(stateFlagTimeout).not.toHaveBeenCalled();
2018-11-01 11:30:05 +03:00
copyBtn.simulate('copy');
2019-04-19 13:41:59 +03:00
expect(stateFlagTimeout).toHaveBeenCalledTimes(1);
2018-11-01 11:30:05 +03:00
});
});