2020-08-30 10:59:14 +03:00
|
|
|
import { shallow, ShallowWrapper } from 'enzyme';
|
2020-01-15 20:16:12 +03:00
|
|
|
import { ExternalLink } from 'react-external-link';
|
2021-08-16 18:13:31 +03:00
|
|
|
import { Button, FormGroup, Modal, ModalBody, ModalHeader, Row } from 'reactstrap';
|
2020-08-30 10:59:14 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2021-08-16 14:12:07 +03:00
|
|
|
import createQrCodeModal from '../../../src/short-urls/helpers/QrCodeModal';
|
2020-08-30 10:59:14 +03:00
|
|
|
import { ShortUrl } from '../../../src/short-urls/data';
|
2021-01-21 18:51:54 +03:00
|
|
|
import { ReachableServer } from '../../../src/servers/data';
|
2021-01-24 19:37:31 +03:00
|
|
|
import { CopyToClipboardIcon } from '../../../src/utils/CopyToClipboardIcon';
|
2021-03-06 11:38:48 +03:00
|
|
|
import { SemVer } from '../../../src/utils/helpers/version';
|
2021-08-16 14:12:07 +03:00
|
|
|
import { ImageDownloader } from '../../../src/common/services/ImageDownloader';
|
2021-08-16 18:26:54 +03:00
|
|
|
import { QrFormatDropdown } from '../../../src/short-urls/helpers/qr-codes/QrFormatDropdown';
|
|
|
|
import { QrErrorCorrectionDropdown } from '../../../src/short-urls/helpers/qr-codes/QrErrorCorrectionDropdown';
|
2018-11-01 14:35:51 +03:00
|
|
|
|
|
|
|
describe('<QrCodeModal />', () => {
|
2020-08-30 10:59:14 +03:00
|
|
|
let wrapper: ShallowWrapper;
|
2021-08-16 14:12:07 +03:00
|
|
|
const saveImage = jest.fn();
|
|
|
|
const QrCodeModal = createQrCodeModal(Mock.of<ImageDownloader>({ saveImage }), () => null);
|
2020-08-30 10:59:14 +03:00
|
|
|
const shortUrl = 'https://doma.in/abc123';
|
2021-03-06 11:38:48 +03:00
|
|
|
const createWrapper = (version: SemVer = '2.6.0') => {
|
2021-01-21 18:51:54 +03:00
|
|
|
const selectedServer = Mock.of<ReachableServer>({ version });
|
2018-11-01 14:35:51 +03:00
|
|
|
|
2021-01-21 18:51:54 +03:00
|
|
|
wrapper = shallow(
|
|
|
|
<QrCodeModal
|
|
|
|
shortUrl={Mock.of<ShortUrl>({ shortUrl })}
|
|
|
|
isOpen={true}
|
|
|
|
toggle={() => {}}
|
|
|
|
selectedServer={selectedServer}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
};
|
|
|
|
|
|
|
|
afterEach(() => wrapper?.unmount());
|
2021-08-16 14:12:07 +03:00
|
|
|
afterEach(jest.clearAllMocks);
|
2018-11-01 14:35:51 +03:00
|
|
|
|
2021-01-21 18:51:54 +03:00
|
|
|
it('shows an external link to the URL in the header', () => {
|
|
|
|
const wrapper = createWrapper();
|
|
|
|
const externalLink = wrapper.find(ModalHeader).find(ExternalLink);
|
2018-11-01 14:35:51 +03:00
|
|
|
|
|
|
|
expect(externalLink).toHaveLength(1);
|
2020-08-30 10:59:14 +03:00
|
|
|
expect(externalLink.prop('href')).toEqual(shortUrl);
|
2018-11-01 14:35:51 +03:00
|
|
|
});
|
|
|
|
|
2021-01-24 19:37:31 +03:00
|
|
|
it.each([
|
2021-03-06 11:38:48 +03:00
|
|
|
[ '2.5.0' as SemVer, 0, '/qr-code?size=300&format=png' ],
|
|
|
|
[ '2.6.0' as SemVer, 0, '/qr-code?size=300&format=png' ],
|
|
|
|
[ '2.6.0' as SemVer, 10, '/qr-code?size=300&format=png&margin=10' ],
|
2021-08-16 18:13:31 +03:00
|
|
|
[ '2.8.0' as SemVer, 0, '/qr-code?size=300&format=png&errorCorrection=L' ],
|
2021-02-14 12:16:30 +03:00
|
|
|
])('displays an image with the QR code of the URL', (version, margin, expectedUrl) => {
|
2021-01-24 19:37:31 +03:00
|
|
|
const wrapper = createWrapper(version);
|
2021-02-14 12:16:30 +03:00
|
|
|
const formControls = wrapper.find('.form-control-range');
|
|
|
|
|
|
|
|
if (formControls.length > 1) {
|
|
|
|
formControls.at(1).simulate('change', { target: { value: `${margin}` } });
|
|
|
|
}
|
|
|
|
|
2021-01-24 19:37:31 +03:00
|
|
|
const modalBody = wrapper.find(ModalBody);
|
|
|
|
const img = modalBody.find('img');
|
|
|
|
const linkInBody = modalBody.find(ExternalLink);
|
|
|
|
const copyToClipboard = modalBody.find(CopyToClipboardIcon);
|
|
|
|
|
|
|
|
expect(img.prop('src')).toEqual(`${shortUrl}${expectedUrl}`);
|
|
|
|
expect(linkInBody.prop('href')).toEqual(`${shortUrl}${expectedUrl}`);
|
|
|
|
expect(copyToClipboard.prop('text')).toEqual(`${shortUrl}${expectedUrl}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
2021-02-14 12:16:30 +03:00
|
|
|
[ 530, 0, 'lg' ],
|
|
|
|
[ 200, 0, undefined ],
|
|
|
|
[ 830, 0, 'xl' ],
|
|
|
|
[ 430, 80, 'lg' ],
|
|
|
|
[ 200, 50, undefined ],
|
|
|
|
[ 720, 100, 'xl' ],
|
|
|
|
])('renders expected size', (size, margin, modalSize) => {
|
2021-01-21 18:51:54 +03:00
|
|
|
const wrapper = createWrapper();
|
2021-02-14 12:16:30 +03:00
|
|
|
const formControls = wrapper.find('.form-control-range');
|
|
|
|
const sizeInput = formControls.at(0);
|
|
|
|
const marginInput = formControls.at(1);
|
2021-01-24 19:37:31 +03:00
|
|
|
|
|
|
|
sizeInput.simulate('change', { target: { value: `${size}` } });
|
2021-02-14 12:16:30 +03:00
|
|
|
marginInput.simulate('change', { target: { value: `${margin}` } });
|
2021-01-24 19:37:31 +03:00
|
|
|
|
2021-02-14 12:16:30 +03:00
|
|
|
expect(wrapper.find('label').at(0).text()).toEqual(`Size: ${size}px`);
|
|
|
|
expect(wrapper.find('label').at(1).text()).toEqual(`Margin: ${margin}px`);
|
2021-01-24 19:37:31 +03:00
|
|
|
expect(wrapper.find(Modal).prop('size')).toEqual(modalSize);
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
2021-03-06 11:38:48 +03:00
|
|
|
[ '2.6.0' as SemVer, 1, 'col-md-4' ],
|
2021-08-16 18:13:31 +03:00
|
|
|
[ '2.8.0' as SemVer, 2, 'col-md-6' ],
|
2021-01-24 19:37:31 +03:00
|
|
|
])('shows expected components based on server version', (version, expectedAmountOfDropdowns, expectedRangeClass) => {
|
|
|
|
const wrapper = createWrapper(version);
|
2021-08-16 18:26:54 +03:00
|
|
|
const dropdownsLength = wrapper.find(QrFormatDropdown).length + wrapper.find(QrErrorCorrectionDropdown).length;
|
2021-08-16 18:13:31 +03:00
|
|
|
const firstCol = wrapper.find(Row).find(FormGroup).first();
|
2018-11-01 14:35:51 +03:00
|
|
|
|
2021-08-16 18:26:54 +03:00
|
|
|
expect(dropdownsLength).toEqual(expectedAmountOfDropdowns);
|
2022-03-07 18:27:25 +03:00
|
|
|
expect(firstCol.prop('className')).toEqual(`d-grid ${expectedRangeClass}`);
|
2018-11-01 14:35:51 +03:00
|
|
|
});
|
2021-08-16 14:12:07 +03:00
|
|
|
|
|
|
|
it('saves the QR code image when clicking the Download button', () => {
|
|
|
|
const wrapper = createWrapper();
|
|
|
|
const downloadBtn = wrapper.find(Button);
|
|
|
|
|
|
|
|
expect(saveImage).not.toHaveBeenCalled();
|
|
|
|
downloadBtn.simulate('click');
|
|
|
|
expect(saveImage).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
2018-11-01 14:35:51 +03:00
|
|
|
});
|