2022-07-15 22:42:16 +03:00
|
|
|
import { fireEvent, screen } from '@testing-library/react';
|
2020-08-30 10:59:14 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2022-05-28 12:16:59 +03:00
|
|
|
import { QrCodeModal as createQrCodeModal } from '../../../src/short-urls/helpers/QrCodeModal';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { ShortUrl } from '../../../src/short-urls/data';
|
|
|
|
import type { ReachableServer } from '../../../src/servers/data';
|
|
|
|
import type { SemVer } from '../../../src/utils/helpers/version';
|
|
|
|
import type { ImageDownloader } from '../../../src/common/services/ImageDownloader';
|
2022-07-15 22:42:16 +03:00
|
|
|
import { renderWithEvents } from '../../__helpers__/setUpTest';
|
2018-11-01 14:35:51 +03:00
|
|
|
|
|
|
|
describe('<QrCodeModal />', () => {
|
2022-03-26 14:46:32 +03:00
|
|
|
const saveImage = jest.fn().mockReturnValue(Promise.resolve());
|
2022-05-02 20:24:57 +03:00
|
|
|
const QrCodeModal = createQrCodeModal(Mock.of<ImageDownloader>({ saveImage }));
|
2023-01-18 00:53:49 +03:00
|
|
|
const shortUrl = 'https://s.test/abc123';
|
2022-12-23 22:42:47 +03:00
|
|
|
const setUp = (version: SemVer = '2.8.0') => renderWithEvents(
|
2022-07-15 22:42:16 +03:00
|
|
|
<QrCodeModal
|
|
|
|
isOpen
|
|
|
|
shortUrl={Mock.of<ShortUrl>({ shortUrl })}
|
|
|
|
selectedServer={Mock.of<ReachableServer>({ version })}
|
|
|
|
toggle={() => {}}
|
|
|
|
/>,
|
|
|
|
);
|
2018-11-01 14:35:51 +03:00
|
|
|
|
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', () => {
|
2022-07-15 22:42:16 +03:00
|
|
|
setUp();
|
|
|
|
const externalLink = screen.getByRole('heading').querySelector('a');
|
2018-11-01 14:35:51 +03:00
|
|
|
|
2022-07-15 22:42:16 +03:00
|
|
|
expect(externalLink).toBeInTheDocument();
|
|
|
|
expect(externalLink).toHaveAttribute('href', shortUrl);
|
|
|
|
expect(externalLink).toHaveAttribute('rel', 'noopener noreferrer');
|
2018-11-01 14:35:51 +03:00
|
|
|
});
|
|
|
|
|
2021-01-24 19:37:31 +03:00
|
|
|
it.each([
|
2022-12-23 22:42:47 +03:00
|
|
|
[10, '/qr-code?size=300&format=png&errorCorrection=L&margin=10'],
|
|
|
|
[0, '/qr-code?size=300&format=png&errorCorrection=L'],
|
|
|
|
])('displays an image with the QR code of the URL', async (margin, expectedUrl) => {
|
|
|
|
const { container } = setUp();
|
2022-07-15 22:42:16 +03:00
|
|
|
const marginControl = container.parentNode?.querySelectorAll('.form-control-range').item(1);
|
2021-02-14 12:16:30 +03:00
|
|
|
|
2022-07-15 22:42:16 +03:00
|
|
|
if (marginControl) {
|
|
|
|
fireEvent.change(marginControl, { target: { value: `${margin}` } });
|
2021-02-14 12:16:30 +03:00
|
|
|
}
|
|
|
|
|
2022-07-15 22:42:16 +03:00
|
|
|
expect(screen.getByRole('img')).toHaveAttribute('src', `${shortUrl}${expectedUrl}`);
|
|
|
|
expect(screen.getByText(`${shortUrl}${expectedUrl}`)).toHaveAttribute('href', `${shortUrl}${expectedUrl}`);
|
2021-01-24 19:37:31 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
[530, 0, 'lg'],
|
|
|
|
[200, 0, undefined],
|
|
|
|
[830, 0, 'xl'],
|
|
|
|
[430, 80, 'lg'],
|
|
|
|
[200, 50, undefined],
|
|
|
|
[720, 100, 'xl'],
|
2021-02-14 12:16:30 +03:00
|
|
|
])('renders expected size', (size, margin, modalSize) => {
|
2022-07-15 22:42:16 +03:00
|
|
|
const { container } = setUp();
|
|
|
|
const formControls = container.parentNode?.querySelectorAll('.form-control-range');
|
|
|
|
const sizeInput = formControls?.[0];
|
|
|
|
const marginInput = formControls?.[1];
|
2021-01-24 19:37:31 +03:00
|
|
|
|
2022-07-15 22:42:16 +03:00
|
|
|
sizeInput && fireEvent.change(sizeInput, { target: { value: `${size}` } });
|
|
|
|
marginInput && fireEvent.change(marginInput, { target: { value: `${margin}` } });
|
2021-01-24 19:37:31 +03:00
|
|
|
|
2022-07-15 22:42:16 +03:00
|
|
|
expect(screen.getByText(`Size: ${size}px`)).toBeInTheDocument();
|
|
|
|
expect(screen.getByText(`Margin: ${margin}px`)).toBeInTheDocument();
|
|
|
|
modalSize && expect(screen.getByRole('document')).toHaveClass(`modal-${modalSize}`);
|
2021-01-24 19:37:31 +03:00
|
|
|
});
|
|
|
|
|
2022-12-23 22:42:47 +03:00
|
|
|
it('shows expected components based on server version', () => {
|
|
|
|
const { container } = setUp();
|
2022-07-15 22:42:16 +03:00
|
|
|
const dropdowns = screen.getAllByRole('button');
|
|
|
|
const firstCol = container.parentNode?.querySelectorAll('.d-grid').item(0);
|
2018-11-01 14:35:51 +03:00
|
|
|
|
2022-12-23 22:42:47 +03:00
|
|
|
expect(dropdowns).toHaveLength(2 + 1); // Add one because of the close button
|
|
|
|
expect(firstCol).toHaveClass('col-md-4');
|
2018-11-01 14:35:51 +03:00
|
|
|
});
|
2021-08-16 14:12:07 +03:00
|
|
|
|
2022-07-15 22:42:16 +03:00
|
|
|
it('saves the QR code image when clicking the Download button', async () => {
|
|
|
|
const { user } = setUp('2.9.0');
|
2021-08-16 14:12:07 +03:00
|
|
|
|
|
|
|
expect(saveImage).not.toHaveBeenCalled();
|
2022-07-15 22:42:16 +03:00
|
|
|
await user.click(screen.getByRole('button', { name: /^Download/ }));
|
2021-08-16 14:12:07 +03:00
|
|
|
expect(saveImage).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
2018-11-01 14:35:51 +03:00
|
|
|
});
|