Skip to content

Commit 69b4b29

Browse files
authored
Fix image/frame clearing when explicitly set to undefined (#278)
1 parent 420f7d3 commit 69b4b29

2 files changed

Lines changed: 58 additions & 3 deletions

File tree

src/lib/qr-code/core.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { mergeConfig } from './core'
3+
import { DEFAULT_CONFIG, type ResolvedQRCodeConfig } from './types'
4+
5+
function makeCurrent(overrides: Partial<ResolvedQRCodeConfig> = {}): ResolvedQRCodeConfig {
6+
return {
7+
data: 'hi',
8+
size: DEFAULT_CONFIG.size,
9+
margin: DEFAULT_CONFIG.margin,
10+
errorCorrectionLevel: DEFAULT_CONFIG.errorCorrectionLevel,
11+
dots: { ...DEFAULT_CONFIG.dots },
12+
cornerSquares: { ...DEFAULT_CONFIG.cornerSquares },
13+
cornerDots: { ...DEFAULT_CONFIG.cornerDots },
14+
background: { ...DEFAULT_CONFIG.background },
15+
...overrides
16+
}
17+
}
18+
19+
describe('mergeConfig', () => {
20+
it('clears the image when partial explicitly sets image to undefined', () => {
21+
const current = makeCurrent({
22+
image: { href: 'https://example.com/logo.png', sizeRatio: 0.4 }
23+
})
24+
25+
const merged = mergeConfig(current, { image: undefined })
26+
27+
expect(merged.image).toBeUndefined()
28+
})
29+
30+
it('keeps the current image when partial omits the image key', () => {
31+
const current = makeCurrent({
32+
image: { href: 'https://example.com/logo.png', sizeRatio: 0.4 }
33+
})
34+
35+
const merged = mergeConfig(current, { data: 'new data' })
36+
37+
expect(merged.image?.href).toBe('https://example.com/logo.png')
38+
})
39+
40+
it('replaces the current image when partial provides a new image', () => {
41+
const current = makeCurrent({
42+
image: { href: 'https://old.example.com/logo.png', sizeRatio: 0.4 }
43+
})
44+
45+
const merged = mergeConfig(current, {
46+
image: { href: 'https://new.example.com/logo.png', sizeRatio: 0.5 }
47+
})
48+
49+
expect(merged.image?.href).toBe('https://new.example.com/logo.png')
50+
expect(merged.image?.sizeRatio).toBe(0.5)
51+
})
52+
})

src/lib/qr-code/core.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@ export function createQRCode(config: QRCodeConfig): QRCodeInstance {
108108
}
109109
}
110110

111-
function mergeConfig(current: ResolvedQRCodeConfig, partial: Partial<QRCodeConfig>): QRCodeConfig {
111+
export function mergeConfig(
112+
current: ResolvedQRCodeConfig,
113+
partial: Partial<QRCodeConfig>
114+
): QRCodeConfig {
112115
return {
113116
data: partial.data ?? current.data,
114117
size: partial.size ?? current.size,
@@ -124,7 +127,7 @@ function mergeConfig(current: ResolvedQRCodeConfig, partial: Partial<QRCodeConfi
124127
background: partial.background
125128
? { ...current.background, ...partial.background }
126129
: current.background,
127-
image: partial.image === null ? undefined : (partial.image ?? current.image),
128-
frame: partial.frame === null ? undefined : (partial.frame ?? current.frame)
130+
image: 'image' in partial ? (partial.image ?? undefined) : current.image,
131+
frame: 'frame' in partial ? (partial.frame ?? undefined) : current.frame
129132
}
130133
}

0 commit comments

Comments
 (0)