Skip to content

Commit 0623981

Browse files
committed
test: complete tests
1 parent 9c9fd49 commit 0623981

4 files changed

Lines changed: 160 additions & 3 deletions

File tree

packages/react-qr-code/src/components/finder-patterns-outer.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,4 @@ export const FinderPatternsOuter = ({
122122
)
123123
})
124124
}
125-
126-
return null
127125
}

packages/react-qr-code/src/react-qr-code.test.tsx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ import { render, screen } from '@testing-library/react'
22
import { createRef } from 'react'
33
import { describe, expect, it, vi } from 'vitest'
44

5-
import { BG_GRADIENT_ID, DEFAULT_BGCOLOR, DEFAULT_SIZE, GRADIENT_ID } from './constants'
5+
import {
6+
BG_GRADIENT_ID,
7+
DEFAULT_BGCOLOR,
8+
DEFAULT_MARGIN_SIZE,
9+
DEFAULT_SIZE,
10+
GRADIENT_ID,
11+
} from './constants'
612
import { ReactQRCode } from './react-qr-code'
713
import type {
814
BackgroundSettings,
@@ -31,6 +37,22 @@ describe('ReactQRCode', () => {
3137
expect(svg).toHaveAttribute('aria-label', 'QR Code')
3238
})
3339

40+
it.each([
41+
[2, 2],
42+
[10, 10],
43+
[undefined, DEFAULT_MARGIN_SIZE],
44+
])('applies margin %s', (marginSize, expected) => {
45+
render(<ReactQRCode value='test' marginSize={marginSize} />)
46+
47+
const finderPattern = screen.getAllByTestId('finder-patterns-outer')
48+
expect(
49+
finderPattern[0]
50+
.getAttribute('d')!
51+
.toString()
52+
.startsWith(`M ${expected} ${expected}`),
53+
).toBe(true)
54+
})
55+
3456
it('applies custom size', () => {
3557
const customSize = 300
3658
render(<ReactQRCode value='test' size={customSize} />)
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import { describe, expect, it } from 'vitest'
2+
3+
import { DEFAULT_IMG_SCALE } from '../constants'
4+
import type { CrossOrigin, ImageSettings, Modules } from '../types/lib'
5+
import { getImageSettings } from './qr-code'
6+
7+
describe('getImageSettings', () => {
8+
const cells: Modules = [
9+
[true, false, true],
10+
[false, true, false],
11+
[true, false, true],
12+
]
13+
const size = 100
14+
const margin = 2
15+
16+
it('returns null when imageSettings is undefined', () => {
17+
const result = getImageSettings(cells, size, margin, undefined)
18+
expect(result).toBeNull()
19+
})
20+
21+
it('returns null when imageSettings is undefined', () => {
22+
const result = getImageSettings(cells, size, margin, undefined)
23+
expect(result).toBeNull()
24+
})
25+
26+
it('calculates default position when x and y are not provided', () => {
27+
const imageSettings = {
28+
width: 50,
29+
height: 50,
30+
src: 'test.png',
31+
}
32+
const result = getImageSettings(cells, size, margin, imageSettings)
33+
34+
expect(result).not.toBeNull()
35+
expect(result?.x).toBeCloseTo(
36+
cells.length / 2 - (50 * (cells.length + margin * 2)) / size / 2,
37+
)
38+
expect(result?.y).toBeCloseTo(
39+
cells.length / 2 - (50 * (cells.length + margin * 2)) / size / 2,
40+
)
41+
})
42+
43+
it('uses provided x and y coordinates', () => {
44+
const imageSettings = {
45+
width: 50,
46+
height: 50,
47+
x: 10,
48+
y: 20,
49+
src: 'test.png',
50+
}
51+
const scale = (cells.length + margin * 2) / size
52+
const result = getImageSettings(cells, size, margin, imageSettings)
53+
54+
expect(result?.x).toBe(10 * scale)
55+
expect(result?.y).toBe(20 * scale)
56+
})
57+
58+
it('uses default size when width and height are not provided', () => {
59+
const imageSettings = {
60+
src: 'test.png',
61+
} as ImageSettings
62+
const defaultSize = Math.floor(size * DEFAULT_IMG_SCALE)
63+
const scale = (cells.length + margin * 2) / size
64+
const result = getImageSettings(cells, size, margin, imageSettings)
65+
66+
expect(result?.w).toBe(defaultSize * scale)
67+
expect(result?.h).toBe(defaultSize * scale)
68+
})
69+
70+
it('handles opacity settings', () => {
71+
const imageSettings = {
72+
width: 50,
73+
height: 50,
74+
opacity: 0.5,
75+
src: 'test.png',
76+
}
77+
const result = getImageSettings(cells, size, margin, imageSettings)
78+
79+
expect(result?.opacity).toBe(0.5)
80+
})
81+
82+
it('uses default opacity when not provided', () => {
83+
const imageSettings = {
84+
width: 50,
85+
height: 50,
86+
src: 'test.png',
87+
}
88+
const result = getImageSettings(cells, size, margin, imageSettings)
89+
90+
expect(result?.opacity).toBe(1)
91+
})
92+
93+
it('calculates excavation when excavate is true', () => {
94+
const imageSettings = {
95+
width: 50,
96+
height: 50,
97+
x: 10.6,
98+
y: 20.3,
99+
excavate: true,
100+
src: 'test.png',
101+
}
102+
const scale = (cells.length + margin * 2) / size
103+
const result = getImageSettings(cells, size, margin, imageSettings)
104+
105+
expect(result?.excavation).toEqual({
106+
x: Math.floor(10.6 * scale),
107+
y: Math.floor(20.3 * scale),
108+
w: Math.ceil(50 * scale + (10.6 * scale - Math.floor(10.6 * scale))),
109+
h: Math.ceil(50 * scale + (20.3 * scale - Math.floor(20.3 * scale))),
110+
})
111+
})
112+
113+
it('excavation is null when excavate is false', () => {
114+
const imageSettings = {
115+
width: 50,
116+
height: 50,
117+
excavate: false,
118+
src: 'test.png',
119+
}
120+
const result = getImageSettings(cells, size, margin, imageSettings)
121+
122+
expect(result?.excavation).toBeNull()
123+
})
124+
125+
it('preserves crossOrigin setting', () => {
126+
const imageSettings = {
127+
width: 50,
128+
height: 50,
129+
crossOrigin: 'anonymous' as CrossOrigin,
130+
src: 'test.png',
131+
}
132+
const result = getImageSettings(cells, size, margin, imageSettings)
133+
134+
expect(result?.crossOrigin).toBe('anonymous')
135+
})
136+
})

packages/react-qr-code/src/utils/qr-code.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export const getImageSettings = (
3737
let excavation = null
3838
if (imageSettings.excavate) {
3939
const floorX = Math.floor(x)
40+
4041
const floorY = Math.floor(y)
4142
const ceilW = Math.ceil(w + x - floorX)
4243
const ceilH = Math.ceil(h + y - floorY)

0 commit comments

Comments
 (0)