2022-07-15 22:42:16 +03:00
|
|
|
import { fireEvent, screen } from '@testing-library/react';
|
2023-04-13 22:48:29 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2023-08-02 10:01:44 +03:00
|
|
|
import type { SemVer } from '../../../../src/utils/helpers/version';
|
|
|
|
import { QrCodeModal as createQrCodeModal } from '../../../src/short-urls/helpers/QrCodeModal';
|
2022-07-15 22:42:16 +03:00
|
|
|
import { renderWithEvents } from '../../__helpers__/setUpTest';
|
2018-11-01 14:35:51 +03:00
|
|
|
|
|
|
|
describe('<QrCodeModal />', () => {
|
2023-05-27 12:57:26 +03:00
|
|
|
const saveImage = vi.fn().mockReturnValue(Promise.resolve());
|
2023-04-13 22:48:29 +03:00
|
|
|
const QrCodeModal = createQrCodeModal(fromPartial({ 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
|
2023-04-13 22:48:29 +03:00
|
|
|
shortUrl={fromPartial({ shortUrl })}
|
|
|
|
selectedServer={fromPartial({ version })}
|
2022-07-15 22:42:16 +03:00
|
|
|
toggle={() => {}}
|
|
|
|
/>,
|
|
|
|
);
|
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
|
|
|
});
|