2022-12-05 14:15:26 +08:00
|
|
|
import React, { useState } from 'react';
|
2023-11-30 15:02:32 +08:00
|
|
|
|
2022-12-05 14:15:26 +08:00
|
|
|
import QRCode from '..';
|
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
|
|
|
import rtlTest from '../../../tests/shared/rtlTest';
|
|
|
|
import { fireEvent, render } from '../../../tests/utils';
|
|
|
|
import type { QRCodeProps } from '../interface';
|
|
|
|
|
|
|
|
describe('QRCode test', () => {
|
2024-05-02 12:17:27 +08:00
|
|
|
mountTest(() => <QRCode value="" />);
|
|
|
|
rtlTest(() => <QRCode value="" />);
|
2022-12-05 14:15:26 +08:00
|
|
|
|
|
|
|
it('should correct render', () => {
|
|
|
|
const { container } = render(<QRCode value="test" />);
|
2024-05-02 12:17:27 +08:00
|
|
|
expect(container?.querySelector<HTMLCanvasElement>('.ant-qrcode canvas')).toBeTruthy();
|
2022-12-05 14:15:26 +08:00
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should render `null` and console Error when value not exist', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
2022-12-05 14:15:26 +08:00
|
|
|
const { container } = render(<QRCode value={undefined as unknown as string} />);
|
|
|
|
expect(container.firstChild).toBe(null);
|
|
|
|
expect(container.firstChild).toMatchSnapshot();
|
|
|
|
expect(errSpy).toHaveBeenCalledWith('Warning: [antd: QRCode] need to receive `value` props');
|
|
|
|
errSpy.mockRestore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('support custom icon', () => {
|
|
|
|
const { container } = render(<QRCode value="test" icon="test" />);
|
2024-05-02 12:17:27 +08:00
|
|
|
expect(container?.querySelector<HTMLImageElement>('.ant-qrcode img')).toBeTruthy();
|
2022-12-05 14:15:26 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('support custom size', () => {
|
|
|
|
const { container } = render(<QRCode value="test" size={100} />);
|
2024-04-30 17:03:00 +08:00
|
|
|
const canvas = container.querySelector<HTMLCanvasElement>('.ant-qrcode > canvas')!;
|
|
|
|
expect(canvas.width).toBe(100);
|
|
|
|
expect(canvas.height).toBe(100);
|
2022-12-05 14:15:26 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('support refresh', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const refresh = jest.fn();
|
2022-12-05 14:15:26 +08:00
|
|
|
const { container } = render(<QRCode value="test" status="expired" onRefresh={refresh} />);
|
|
|
|
fireEvent.click(
|
2024-05-02 12:17:27 +08:00
|
|
|
container?.querySelector<HTMLButtonElement>('.ant-qrcode button.ant-btn-link')!,
|
2022-12-05 14:15:26 +08:00
|
|
|
);
|
|
|
|
expect(refresh).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2024-06-10 17:47:45 +08:00
|
|
|
it('support click', () => {
|
|
|
|
const handleClick = jest.fn();
|
|
|
|
const { container } = render(<QRCode value="test" onClick={handleClick} />);
|
|
|
|
fireEvent.click(container?.querySelector<HTMLDivElement>('.ant-qrcode')!);
|
|
|
|
expect(handleClick).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-12-05 14:15:26 +08:00
|
|
|
it('support loading', () => {
|
|
|
|
const Demo: React.FC = () => {
|
|
|
|
const [status, setStatus] = useState<QRCodeProps['status']>('active');
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<QRCode value="test" status={status} />
|
|
|
|
<button type="button" onClick={() => setStatus('loading')}>
|
|
|
|
set loading
|
|
|
|
</button>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
const { container } = render(<Demo />);
|
|
|
|
expect(container.querySelector<HTMLDivElement>('.ant-spin-spinning')).toBeFalsy();
|
|
|
|
fireEvent.click(container?.querySelector<HTMLButtonElement>('button')!);
|
|
|
|
expect(container.querySelector<HTMLDivElement>('.ant-spin-spinning')).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('support bordered', () => {
|
|
|
|
const { container } = render(<QRCode value="test" bordered={false} />);
|
|
|
|
expect(container?.querySelector<HTMLDivElement>('.ant-qrcode')).toHaveClass(
|
|
|
|
'ant-qrcode-borderless',
|
|
|
|
);
|
|
|
|
});
|
2022-12-07 23:32:04 +08:00
|
|
|
|
|
|
|
it('should console Error when icon exist && errorLevel is `L`', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
2022-12-07 23:32:04 +08:00
|
|
|
render(<QRCode value="test" icon="test" errorLevel="L" />);
|
|
|
|
expect(errSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antd: QRCode] ErrorLevel `L` is not recommended to be used with `icon`, for scanning result would be affected by low level.',
|
|
|
|
);
|
|
|
|
errSpy.mockRestore();
|
|
|
|
});
|
2024-03-25 10:59:44 +08:00
|
|
|
|
2024-05-02 12:17:27 +08:00
|
|
|
it('correct style for wrapper & canvas', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<QRCode value="test" size={60} style={{ width: '100%', height: '80%' }} />,
|
|
|
|
);
|
|
|
|
expect(container.querySelector<HTMLElement>('.ant-qrcode')).toHaveStyle(
|
|
|
|
'width: 100%; height: 80%;',
|
|
|
|
);
|
|
|
|
expect(container.querySelector<HTMLElement>('.ant-qrcode canvas')).toHaveStyle(
|
|
|
|
'width: 100%; height: 80%;',
|
2024-03-25 10:59:44 +08:00
|
|
|
);
|
|
|
|
});
|
2024-07-12 10:49:47 +08:00
|
|
|
it('custom status render', () => {
|
2024-10-20 10:39:38 +08:00
|
|
|
const refreshCb = jest.fn();
|
2024-07-12 10:49:47 +08:00
|
|
|
const customStatusRender: QRCodeProps['statusRender'] = (info) => {
|
|
|
|
switch (info.status) {
|
|
|
|
case 'expired':
|
2024-10-20 10:39:38 +08:00
|
|
|
return (
|
|
|
|
<div className="custom-expired">
|
|
|
|
<span>{info.locale?.expired}</span>
|
|
|
|
<button id="refresh" onClick={info.onRefresh} type="button">
|
|
|
|
refresh
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
);
|
2024-07-12 10:49:47 +08:00
|
|
|
case 'loading':
|
|
|
|
return <div className="custom-loading">Loading</div>;
|
|
|
|
case 'scanned':
|
|
|
|
return <div className="custom-scanned">{info.locale?.scanned}</div>;
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const { container } = render(
|
|
|
|
<>
|
|
|
|
<QRCode
|
|
|
|
className="qrcode-expired"
|
|
|
|
value="test"
|
|
|
|
status="expired"
|
|
|
|
statusRender={customStatusRender}
|
2024-10-20 10:39:38 +08:00
|
|
|
onRefresh={refreshCb}
|
2024-07-12 10:49:47 +08:00
|
|
|
/>
|
|
|
|
<QRCode
|
|
|
|
className="qrcode-loading"
|
|
|
|
value="test"
|
|
|
|
status="loading"
|
|
|
|
statusRender={customStatusRender}
|
|
|
|
/>
|
|
|
|
<QRCode
|
|
|
|
className="qrcode-scanned"
|
|
|
|
value="test"
|
|
|
|
status="scanned"
|
|
|
|
statusRender={customStatusRender}
|
|
|
|
/>
|
|
|
|
</>,
|
|
|
|
);
|
|
|
|
expect(
|
2024-10-20 10:39:38 +08:00
|
|
|
container.querySelector<HTMLDivElement>('.qrcode-expired .custom-expired>span')?.textContent,
|
2024-07-12 10:49:47 +08:00
|
|
|
).toBe('QR code expired');
|
2024-10-20 10:39:38 +08:00
|
|
|
fireEvent.click(container?.querySelector<HTMLButtonElement>('#refresh')!);
|
|
|
|
expect(refreshCb).toHaveBeenCalled();
|
2024-07-12 10:49:47 +08:00
|
|
|
expect(
|
|
|
|
container.querySelector<HTMLDivElement>('.qrcode-loading .custom-loading')?.textContent,
|
|
|
|
).toBe('Loading');
|
|
|
|
expect(
|
|
|
|
container.querySelector<HTMLDivElement>('.qrcode-scanned .custom-scanned')?.textContent,
|
|
|
|
).toBe('Scanned');
|
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
});
|
2024-11-04 14:50:00 +08:00
|
|
|
|
|
|
|
it('should pass aria and data props to qrcode element', () => {
|
|
|
|
const { container } = render(<QRCode value="test" aria-label="Test QR Code" />);
|
|
|
|
const qrcodeElement = container.querySelector('.ant-qrcode canvas');
|
|
|
|
expect(qrcodeElement).toHaveAttribute('aria-label', 'Test QR Code');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not pass other props to qrcode element', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<QRCode
|
|
|
|
value="test"
|
|
|
|
aria-label="Test QR Code"
|
|
|
|
title="qr-title" // This prop should not be passed to canvas
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
|
|
|
|
const qrcodeElement = container.querySelector('.ant-qrcode canvas');
|
|
|
|
expect(qrcodeElement).toHaveAttribute('aria-label', 'Test QR Code');
|
|
|
|
expect(qrcodeElement).not.toHaveAttribute('title', 'qr-title');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with both canvas and svg type', () => {
|
|
|
|
const ariaLabel = 'Test QR Code';
|
|
|
|
// test canvas type
|
|
|
|
const { container: canvasContainer } = render(
|
|
|
|
<QRCode value="test" type="canvas" aria-label={ariaLabel} />,
|
|
|
|
);
|
|
|
|
expect(canvasContainer.querySelector('canvas')).toHaveAttribute('aria-label', ariaLabel);
|
|
|
|
// test svg type
|
|
|
|
const { container: svgContainer } = render(
|
|
|
|
<QRCode value="test" type="svg" aria-label={ariaLabel} />,
|
|
|
|
);
|
|
|
|
expect(svgContainer.querySelector('svg')).toHaveAttribute('aria-label', ariaLabel);
|
|
|
|
});
|
2022-12-05 14:15:26 +08:00
|
|
|
});
|