Skip to content

Commit 2ca1188

Browse files
committed
test: data modules
1 parent 9c84322 commit 2ca1188

7 files changed

Lines changed: 580 additions & 22 deletions

File tree

Lines changed: 103 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,110 @@
11
import { render } from '@testing-library/react'
2-
import { describe, expect, it } from 'vitest'
2+
import { describe, expect, it, vi } from 'vitest'
33

4-
import { ReactQRCode } from '../react-qr-code'
4+
import {
5+
dataModulesHorizontalLineNeighbours,
6+
dataModulesLeafNeighbours,
7+
dataModulesRoundedNeighbours,
8+
dataModulesVerticalLineNeighbours,
9+
} from '../test/data-modules-neightbours'
10+
import type { DataModulesStyle } from '../types/lib'
11+
import type { DataModulesNeighbours } from '../types/utils'
12+
import * as dataModulesUtils from '../utils/data-modules'
13+
import * as svgUtils from '../utils/svg'
14+
import { DataModules } from './data-modules'
515

616
describe('DataModules', () => {
7-
it('renders with default props', () => {
8-
render(<ReactQRCode value='test' />)
17+
const mockModules = [
18+
[true, false, true],
19+
[false, true, false],
20+
[true, false, true],
21+
]
922

10-
expect(true).toBe(true)
23+
const getModuleNeighboursSpy = vi.spyOn(dataModulesUtils, 'getModuleNeighbours')
24+
25+
const defaultProps = {
26+
modules: mockModules,
27+
margin: 2,
28+
}
29+
30+
it('calls the correct shape function based on style prop', () => {
31+
const stylesToMethods = {
32+
square: 'square',
33+
circle: 'circle',
34+
diamond: 'diamond',
35+
heart: 'heart',
36+
star: 'star',
37+
}
38+
39+
Object.entries(stylesToMethods).forEach(([style, method]) => {
40+
const utils = style === 'heart' || style === 'star' ? svgUtils : dataModulesUtils
41+
const spy = vi.spyOn(utils, method as keyof typeof utils)
42+
43+
render(
44+
<DataModules settings={{ style: style as DataModulesStyle }} {...defaultProps} />,
45+
)
46+
47+
expect(spy).toHaveBeenCalled()
48+
})
1149
})
50+
51+
it.each(dataModulesRoundedNeighbours)(
52+
`with neighbours %j calls the correct shape function %s for style rounded`,
53+
(neighbours, method) => {
54+
getModuleNeighboursSpy.mockImplementation(() => neighbours as DataModulesNeighbours)
55+
const methodSpy = vi.spyOn(
56+
dataModulesUtils,
57+
method as keyof typeof dataModulesUtils,
58+
)
59+
60+
render(<DataModules settings={{ style: 'rounded' }} {...defaultProps} />)
61+
62+
expect(methodSpy).toHaveBeenCalled()
63+
},
64+
)
65+
66+
it.each(dataModulesLeafNeighbours)(
67+
`with neighbours %j calls the correct shape function %s for style leaf`,
68+
(neighbours, method) => {
69+
getModuleNeighboursSpy.mockImplementation(() => neighbours as DataModulesNeighbours)
70+
const methodSpy = vi.spyOn(
71+
dataModulesUtils,
72+
method as keyof typeof dataModulesUtils,
73+
)
74+
75+
render(<DataModules settings={{ style: 'leaf' }} {...defaultProps} />)
76+
77+
expect(methodSpy).toHaveBeenCalled()
78+
},
79+
)
80+
81+
it.each(dataModulesVerticalLineNeighbours)(
82+
`with neighbours %j calls the correct shape function %s for style leaf`,
83+
(neighbours, method) => {
84+
getModuleNeighboursSpy.mockImplementation(() => neighbours as DataModulesNeighbours)
85+
const methodSpy = vi.spyOn(
86+
dataModulesUtils,
87+
method as keyof typeof dataModulesUtils,
88+
)
89+
90+
render(<DataModules settings={{ style: 'vertical-line' }} {...defaultProps} />)
91+
92+
expect(methodSpy).toHaveBeenCalled()
93+
},
94+
)
95+
96+
it.each(dataModulesHorizontalLineNeighbours)(
97+
`with neighbours %j calls the correct shape function %s for style leaf`,
98+
(neighbours, method) => {
99+
getModuleNeighboursSpy.mockImplementation(() => neighbours as DataModulesNeighbours)
100+
const methodSpy = vi.spyOn(
101+
dataModulesUtils,
102+
method as keyof typeof dataModulesUtils,
103+
)
104+
105+
render(<DataModules settings={{ style: 'horizontal-line' }} {...defaultProps} />)
106+
107+
expect(methodSpy).toHaveBeenCalled()
108+
},
109+
)
12110
})

packages/react-qr-code/src/components/data-modules.tsx

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
circle,
88
dataModuleCanBeRandomSize,
99
diamond,
10+
getScaleFactor,
1011
leaf,
1112
leftRounded,
1213
rightRounded,
@@ -40,14 +41,10 @@ export const DataModules = ({
4041
const numCells = modules.length
4142
const isRandom = dataModuleCanBeRandomSize(style) && randomSize
4243

43-
const getScaleFactor = useCallback(() => {
44-
if (style === 'square-sm') {
45-
return 0.75
46-
} else if (isRandom) {
47-
return Math.random() * (1 - 0.75) + 0.75
48-
}
49-
return 1
50-
}, [style, isRandom])
44+
const scaleFactor = useCallback(
45+
() => getScaleFactor(style, isRandom),
46+
[style, isRandom],
47+
)
5148

5249
modules.forEach((row, y) => {
5350
row.forEach((cell, x) => {
@@ -59,9 +56,9 @@ export const DataModules = ({
5956
return
6057
}
6158

62-
const scaleFactor = getScaleFactor()
63-
const size = 1 * scaleFactor
64-
const posOffset = (1 - 1 * scaleFactor) / 2
59+
const scale = scaleFactor()
60+
const size = 1 * scale
61+
const posOffset = (1 - 1 * scale) / 2
6562
const xPos = x + margin + posOffset
6663
const yPos = y + margin + posOffset
6764

0 commit comments

Comments
 (0)