import React, { useState } from 'react'; 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', () => { mountTest(() => ); rtlTest(() => ); it('should correct render', () => { const { container } = render(); expect(container?.querySelector('.ant-qrcode canvas')).toBeTruthy(); expect(container).toMatchSnapshot(); }); it('should render `null` and console Error when value not exist', () => { const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); const { container } = render(); 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(); expect(container?.querySelector('.ant-qrcode img')).toBeTruthy(); }); it('support custom size', () => { const { container } = render(); const canvas = container.querySelector('.ant-qrcode > canvas')!; expect(canvas.width).toBe(100); expect(canvas.height).toBe(100); }); it('support refresh', () => { const refresh = jest.fn(); const { container } = render(); fireEvent.click( container?.querySelector('.ant-qrcode button.ant-btn-link')!, ); expect(refresh).toHaveBeenCalled(); }); it('support click', () => { const handleClick = jest.fn(); const { container } = render(); fireEvent.click(container?.querySelector('.ant-qrcode')!); expect(handleClick).toHaveBeenCalled(); }); it('support loading', () => { const Demo: React.FC = () => { const [status, setStatus] = useState('active'); return ( <> ); }; const { container } = render(); expect(container.querySelector('.ant-spin-spinning')).toBeFalsy(); fireEvent.click(container?.querySelector('button')!); expect(container.querySelector('.ant-spin-spinning')).toBeTruthy(); }); it('support bordered', () => { const { container } = render(); expect(container?.querySelector('.ant-qrcode')).toHaveClass( 'ant-qrcode-borderless', ); }); it('should console Error when icon exist && errorLevel is `L`', () => { const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); render(); 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(); }); it('correct style for wrapper & canvas', () => { const { container } = render( , ); expect(container.querySelector('.ant-qrcode')).toHaveStyle( 'width: 100%; height: 80%;', ); expect(container.querySelector('.ant-qrcode canvas')).toHaveStyle( 'width: 100%; height: 80%;', ); }); it('custom status render', () => { const refreshCb = jest.fn(); const customStatusRender: QRCodeProps['statusRender'] = (info) => { switch (info.status) { case 'expired': return (
{info.locale?.expired}
); case 'loading': return
Loading
; case 'scanned': return
{info.locale?.scanned}
; default: return null; } }; const { container } = render( <> , ); expect( container.querySelector('.qrcode-expired .custom-expired>span')?.textContent, ).toBe('QR code expired'); fireEvent.click(container?.querySelector('#refresh')!); expect(refreshCb).toHaveBeenCalled(); expect( container.querySelector('.qrcode-loading .custom-loading')?.textContent, ).toBe('Loading'); expect( container.querySelector('.qrcode-scanned .custom-scanned')?.textContent, ).toBe('Scanned'); expect(container).toMatchSnapshot(); }); it('should pass aria and data props to qrcode element', () => { const { container } = render(); 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( , ); 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( , ); expect(canvasContainer.querySelector('canvas')).toHaveAttribute('aria-label', ariaLabel); // test svg type const { container: svgContainer } = render( , ); expect(svgContainer.querySelector('svg')).toHaveAttribute('aria-label', ariaLabel); }); });