|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 2 | +import { getFieldHighlightJson } from './exporter.js'; |
| 3 | + |
| 4 | +const extractFill = (result) => result?.elements?.[0]?.attributes?.['w:fill']; |
| 5 | + |
| 6 | +describe('getFieldHighlightJson (non-throwing)', () => { |
| 7 | + let warnSpy; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 11 | + }); |
| 12 | + |
| 13 | + afterEach(() => { |
| 14 | + warnSpy.mockRestore(); |
| 15 | + }); |
| 16 | + |
| 17 | + it('returns null for falsy inputs (undefined, null, empty string)', () => { |
| 18 | + expect(getFieldHighlightJson()).toBeNull(); |
| 19 | + expect(getFieldHighlightJson(undefined)).toBeNull(); |
| 20 | + expect(getFieldHighlightJson(null)).toBeNull(); |
| 21 | + expect(getFieldHighlightJson('')).toBeNull(); |
| 22 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('accepts 3/4/6/8-digit HEX with or without # (case preserved)', () => { |
| 26 | + const cases = [ |
| 27 | + ['#FFF', '#FFF'], |
| 28 | + ['FFF', '#FFF'], |
| 29 | + ['#ffff', '#ffff'], |
| 30 | + ['FFFF', '#FFFF'], |
| 31 | + ['#A1B2C3', '#A1B2C3'], |
| 32 | + ['a1b2c3', '#a1b2c3'], |
| 33 | + ['#A1B2C3D4', '#A1B2C3D4'], |
| 34 | + ['a1b2c3d4', '#a1b2c3d4'], |
| 35 | + ]; |
| 36 | + |
| 37 | + for (const [input, expectedFill] of cases) { |
| 38 | + const out = getFieldHighlightJson(input); |
| 39 | + expect(out).toBeTruthy(); |
| 40 | + expect(out.name).toBe('w:rPr'); |
| 41 | + expect(extractFill(out)).toBe(expectedFill); |
| 42 | + expect(out.elements[0].attributes['w:color']).toBe('auto'); |
| 43 | + expect(out.elements[0].attributes['w:val']).toBe('clear'); |
| 44 | + } |
| 45 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('trims surrounding whitespace and still validates', () => { |
| 49 | + const out1 = getFieldHighlightJson(' #ABCDEF '); |
| 50 | + expect(extractFill(out1)).toBe('#ABCDEF'); |
| 51 | + |
| 52 | + const out2 = getFieldHighlightJson(' abc '); |
| 53 | + expect(extractFill(out2)).toBe('#abc'); |
| 54 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('treats pure whitespace as invalid (returns null and warns)', () => { |
| 58 | + const out = getFieldHighlightJson(' '); |
| 59 | + expect(out).toBeNull(); |
| 60 | + expect(warnSpy).toHaveBeenCalledTimes(1); |
| 61 | + expect(warnSpy.mock.calls[0][0]).toMatch(/Invalid HEX color/i); |
| 62 | + }); |
| 63 | + |
| 64 | + it('returns null and warns for invalid HEX formats', () => { |
| 65 | + const invalid = [ |
| 66 | + '#GGG', |
| 67 | + 'GGGZ', |
| 68 | + 'red', |
| 69 | + '12345', |
| 70 | + '#12345', |
| 71 | + '#1234567', |
| 72 | + '1234567', |
| 73 | + '#', |
| 74 | + '##123', |
| 75 | + 'xyz', |
| 76 | + '#ffffgfff', |
| 77 | + '12', |
| 78 | + '#12', |
| 79 | + ]; |
| 80 | + |
| 81 | + for (const input of invalid) { |
| 82 | + const out = getFieldHighlightJson(input); |
| 83 | + expect(out).toBeNull(); |
| 84 | + } |
| 85 | + expect(warnSpy).toHaveBeenCalledTimes(invalid.length); |
| 86 | + for (let i = 0; i < invalid.length; i++) { |
| 87 | + expect(warnSpy.mock.calls[i][0]).toMatch(/Invalid HEX color/i); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + it('adds a leading # when missing', () => { |
| 92 | + const out = getFieldHighlightJson('ABCDEF'); |
| 93 | + expect(extractFill(out)).toBe('#ABCDEF'); |
| 94 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 95 | + }); |
| 96 | +}); |
0 commit comments