|
1 | | -import { render } from '@testing-library/react' |
2 | | -import { expect, test } from 'vitest' |
| 1 | +import { render, screen } from '@testing-library/react' |
| 2 | +import { createRef } from 'react' |
| 3 | +import { describe, expect, it, vi } from 'vitest' |
3 | 4 |
|
| 5 | +import { BG_GRADIENT_ID, DEFAULT_BGCOLOR, DEFAULT_SIZE } from './constants' |
4 | 6 | import { ReactQRCode } from './react-qr-code' |
| 7 | +import type { |
| 8 | + BackgroundSettings, |
| 9 | + GradientSettingsType, |
| 10 | + ReactQRCodeRef, |
| 11 | +} from './types/lib' |
| 12 | +import { downloadRaster, downloadSVG } from './utils/download' |
5 | 13 |
|
6 | | -test('renders QR code component', () => { |
7 | | - const result = render(<ReactQRCode value='test' />) |
8 | | - expect(result.container).toBeInTheDocument() |
9 | | - expect(result).toMatchSnapshot() |
| 14 | +vi.mock('./utils/download', () => ({ |
| 15 | + downloadSVG: vi.fn(), |
| 16 | + downloadRaster: vi.fn(), |
| 17 | +})) |
| 18 | + |
| 19 | +describe('ReactQRCode', () => { |
| 20 | + it('renders with default props', () => { |
| 21 | + render(<ReactQRCode value='test' />) |
| 22 | + |
| 23 | + const svg = screen.getByRole('img') |
| 24 | + expect(svg).toBeInTheDocument() |
| 25 | + expect(svg).toHaveAttribute('height', DEFAULT_SIZE.toString()) |
| 26 | + expect(svg).toHaveAttribute('width', DEFAULT_SIZE.toString()) |
| 27 | + expect(svg).toHaveAttribute('aria-label', 'QR Code') |
| 28 | + }) |
| 29 | + |
| 30 | + it('applies custom size', () => { |
| 31 | + const customSize = 300 |
| 32 | + render(<ReactQRCode value='test' size={customSize} />) |
| 33 | + |
| 34 | + const svg = screen.getByRole('img') |
| 35 | + expect(svg).toHaveAttribute('height', customSize.toString()) |
| 36 | + expect(svg).toHaveAttribute('width', customSize.toString()) |
| 37 | + }) |
| 38 | + |
| 39 | + it('applies custom aria-label', () => { |
| 40 | + const customLabel = 'Custom QR Code' |
| 41 | + render(<ReactQRCode value='test' svgProps={{ 'aria-label': customLabel }} />) |
| 42 | + |
| 43 | + const svg = screen.getByRole('img') |
| 44 | + expect(svg).toHaveAttribute('aria-label', customLabel) |
| 45 | + }) |
| 46 | + |
| 47 | + describe('background', () => { |
| 48 | + it('renders with default background color', () => { |
| 49 | + render(<ReactQRCode value='test' />) |
| 50 | + |
| 51 | + const background = screen.getByTestId('background') |
| 52 | + expect(background).toHaveAttribute('fill', DEFAULT_BGCOLOR) |
| 53 | + }) |
| 54 | + |
| 55 | + it('renders with custom background color', () => { |
| 56 | + const color = '#ff0000' |
| 57 | + render(<ReactQRCode value='test' background={color} />) |
| 58 | + |
| 59 | + const background = screen.getByTestId('background') |
| 60 | + expect(background).toHaveAttribute('fill', color) |
| 61 | + }) |
| 62 | + |
| 63 | + it.each([ |
| 64 | + ['linear', 'linearGradient'], |
| 65 | + ['radial', 'radialGradient'], |
| 66 | + ])('renders with %s gradient background', (type, selector) => { |
| 67 | + const gradient: BackgroundSettings = { |
| 68 | + type: type as GradientSettingsType, |
| 69 | + rotation: 0, |
| 70 | + stops: [ |
| 71 | + { offset: '0%', color: '#4568DC' }, |
| 72 | + { offset: '100%', color: '#B06AB3' }, |
| 73 | + ], |
| 74 | + } |
| 75 | + const { container } = render(<ReactQRCode value='test' background={gradient} />) |
| 76 | + |
| 77 | + const background = screen.getByTestId('background') |
| 78 | + const stops = container.querySelectorAll(`${selector} stop`) |
| 79 | + |
| 80 | + expect(background).toHaveAttribute('fill', `url(#${BG_GRADIENT_ID})`) |
| 81 | + expect(container.querySelector(`${selector}`)).toBeInTheDocument() |
| 82 | + expect(stops).toHaveLength(2) |
| 83 | + expect(stops[0]).toHaveAttribute('stop-color', gradient.stops[0].color) |
| 84 | + expect(stops[0]).toHaveAttribute('offset', gradient.stops[0].offset) |
| 85 | + expect(stops[1]).toHaveAttribute('stop-color', gradient.stops[1].color) |
| 86 | + expect(stops[1]).toHaveAttribute('offset', gradient.stops[1].offset) |
| 87 | + }) |
| 88 | + }) |
| 89 | + |
| 90 | + describe('Image settings', () => { |
| 91 | + it('renders with image settings', () => { |
| 92 | + const imageSettings = { |
| 93 | + src: 'test-image.png', |
| 94 | + height: 30, |
| 95 | + width: 30, |
| 96 | + excavate: true, |
| 97 | + } |
| 98 | + const { container } = render( |
| 99 | + <ReactQRCode value='test' imageSettings={imageSettings} />, |
| 100 | + ) |
| 101 | + |
| 102 | + const image = container.querySelector('image') |
| 103 | + expect(image).toBeInTheDocument() |
| 104 | + expect(image).toHaveAttribute('href', imageSettings.src) |
| 105 | + }) |
| 106 | + }) |
| 107 | + |
| 108 | + describe('ref functionality', () => { |
| 109 | + it('provides access to SVG element', () => { |
| 110 | + const ref = createRef<ReactQRCodeRef>() |
| 111 | + render(<ReactQRCode value='test' ref={ref} />) |
| 112 | + |
| 113 | + expect(ref.current).toBeTruthy() |
| 114 | + expect(ref.current?.svg instanceof SVGSVGElement).toBe(true) |
| 115 | + }) |
| 116 | + |
| 117 | + it('provides download method', async () => { |
| 118 | + const ref = createRef<ReactQRCodeRef>() |
| 119 | + render(<ReactQRCode value='test' ref={ref} />) |
| 120 | + |
| 121 | + expect(ref.current).toBeTruthy() |
| 122 | + expect(typeof ref.current?.download).toBe('function') |
| 123 | + }) |
| 124 | + |
| 125 | + it('handles svg download method call when ref is available', () => { |
| 126 | + const ref = createRef<ReactQRCodeRef>() |
| 127 | + render(<ReactQRCode value='test' ref={ref} />) |
| 128 | + |
| 129 | + if (ref.current) { |
| 130 | + ref.current.download({ format: 'svg', name: 'test-name', size: 300 }) |
| 131 | + expect(vi.mocked(downloadSVG)).toHaveBeenCalledWith({ |
| 132 | + svgRef: { current: ref.current.svg }, |
| 133 | + fileSize: 300, |
| 134 | + fileName: 'test-name', |
| 135 | + }) |
| 136 | + } |
| 137 | + }) |
| 138 | + |
| 139 | + it('handles raster download method call when ref is available', () => { |
| 140 | + const ref = createRef<ReactQRCodeRef>() |
| 141 | + render(<ReactQRCode value='test' ref={ref} />) |
| 142 | + |
| 143 | + if (ref.current) { |
| 144 | + ref.current.download({ format: 'png', name: 'test-name', size: 300 }) |
| 145 | + expect(vi.mocked(downloadRaster)).toHaveBeenCalledWith( |
| 146 | + expect.objectContaining({ |
| 147 | + svgRef: { current: ref.current.svg }, |
| 148 | + fileSize: 300, |
| 149 | + fileName: 'test-name', |
| 150 | + }), |
| 151 | + ) |
| 152 | + } |
| 153 | + }) |
| 154 | + }) |
10 | 155 | }) |
0 commit comments