|
| 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 | +}) |
0 commit comments