|
| 1 | +import { normalizeColorString } from '../normalizeColorString'; |
| 2 | + |
| 3 | +describe('normalizeColorString', () => { |
| 4 | + beforeEach(() => { |
| 5 | + jest.spyOn(console, 'warn').mockImplementation(() => {}) as jest.Mock; |
| 6 | + }); |
| 7 | + |
| 8 | + afterEach(() => { |
| 9 | + jest.clearAllMocks(); |
| 10 | + }); |
| 11 | + |
| 12 | + // Hex color tests |
| 13 | + test('should keep 3-char hex colors unchanged', () => { |
| 14 | + expect(normalizeColorString('#123')).toBe('#123'); |
| 15 | + expect(console.warn).not.toHaveBeenCalled(); |
| 16 | + }); |
| 17 | + |
| 18 | + test('should keep 6-char hex colors unchanged', () => { |
| 19 | + expect(normalizeColorString('#123456')).toBe('#123456'); |
| 20 | + expect(console.warn).not.toHaveBeenCalled(); |
| 21 | + }); |
| 22 | + |
| 23 | + test('should remove alpha from 4-char hex colors', () => { |
| 24 | + expect(normalizeColorString('#123F')).toBe('#123'); |
| 25 | + expect(console.warn).not.toHaveBeenCalled(); |
| 26 | + }); |
| 27 | + |
| 28 | + test('should remove alpha from 8-char hex colors', () => { |
| 29 | + expect(normalizeColorString('#12345678')).toBe('#123456'); |
| 30 | + expect(console.warn).not.toHaveBeenCalled(); |
| 31 | + }); |
| 32 | + |
| 33 | + test('should warn for invalid hex formats but return the original', () => { |
| 34 | + expect(normalizeColorString('#12')).toBe('#12'); |
| 35 | + expect(console.warn).toHaveBeenCalledTimes(1); |
| 36 | + |
| 37 | + (console.warn as jest.Mock).mockClear(); |
| 38 | + expect(normalizeColorString('#12345')).toBe('#12345'); |
| 39 | + expect(console.warn).toHaveBeenCalledTimes(1); |
| 40 | + }); |
| 41 | + |
| 42 | + // RGB color tests |
| 43 | + test('should keep rgb format unchanged but normalize whitespace', () => { |
| 44 | + expect(normalizeColorString('rgb(255, 0, 0)')).toBe('rgb(255, 0, 0)'); |
| 45 | + expect(console.warn).not.toHaveBeenCalled(); |
| 46 | + }); |
| 47 | + |
| 48 | + test('should convert rgba to rgb', () => { |
| 49 | + expect(normalizeColorString('rgba(255, 0, 0, 0.5)')).toBe('rgb(255, 0, 0)'); |
| 50 | + expect(console.warn).not.toHaveBeenCalled(); |
| 51 | + }); |
| 52 | + |
| 53 | + test('should handle rgb with whitespace variations', () => { |
| 54 | + expect(normalizeColorString('rgb(255,0,0)')).toBe('rgb(255, 0, 0)'); |
| 55 | + expect(normalizeColorString('rgb( 255 , 0 , 0 )')).toBe('rgb(255, 0, 0)'); |
| 56 | + expect(console.warn).not.toHaveBeenCalled(); |
| 57 | + }); |
| 58 | + |
| 59 | + // HSL color tests |
| 60 | + test('should keep hsl format unchanged but normalize whitespace', () => { |
| 61 | + expect(normalizeColorString('hsl(120, 100%, 50%)')).toBe('hsl(120, 100%, 50%)'); |
| 62 | + expect(console.warn).not.toHaveBeenCalled(); |
| 63 | + }); |
| 64 | + |
| 65 | + test('should convert hsla to hsl', () => { |
| 66 | + expect(normalizeColorString('hsla(120, 100%, 50%, 0.8)')).toBe('hsl(120, 100%, 50%)'); |
| 67 | + expect(console.warn).not.toHaveBeenCalled(); |
| 68 | + }); |
| 69 | + |
| 70 | + test('should handle hsl with whitespace variations', () => { |
| 71 | + expect(normalizeColorString('hsl(120,100%,50%)')).toBe('hsl(120, 100%, 50%)'); |
| 72 | + expect(normalizeColorString('hsl( 120 , 100% , 50% )')).toBe('hsl(120, 100%, 50%)'); |
| 73 | + expect(console.warn).not.toHaveBeenCalled(); |
| 74 | + }); |
| 75 | + |
| 76 | + // Warning tests for invalid inputs |
| 77 | + test('should warn for invalid color formats but return the original', () => { |
| 78 | + expect(normalizeColorString('')).toBe(''); |
| 79 | + expect(console.warn).toHaveBeenCalledTimes(1); |
| 80 | + |
| 81 | + (console.warn as jest.Mock).mockClear(); |
| 82 | + expect(normalizeColorString('invalid')).toBe('invalid'); |
| 83 | + expect(console.warn).toHaveBeenCalledTimes(1); |
| 84 | + |
| 85 | + (console.warn as jest.Mock).mockClear(); |
| 86 | + expect(normalizeColorString('rgb(255,0)')).toBe('rgb(255,0)'); |
| 87 | + expect(console.warn).toHaveBeenCalledTimes(1); |
| 88 | + }); |
| 89 | + |
| 90 | + test('should warn for non-string inputs but return the original or empty string', () => { |
| 91 | + expect(normalizeColorString(null as any)).toBe(''); |
| 92 | + expect(console.warn).toHaveBeenCalledTimes(1); |
| 93 | + |
| 94 | + (console.warn as jest.Mock).mockClear(); |
| 95 | + expect(normalizeColorString(123 as any)).toBe(123 as any); |
| 96 | + expect(console.warn).toHaveBeenCalledTimes(1); |
| 97 | + }); |
| 98 | + |
| 99 | + // Edge cases |
| 100 | + test('should handle trimming whitespace', () => { |
| 101 | + expect(normalizeColorString(' #123 ')).toBe('#123'); |
| 102 | + expect(normalizeColorString('\n rgb(255, 0, 0) \t')).toBe('rgb(255, 0, 0)'); |
| 103 | + expect(console.warn).not.toHaveBeenCalled(); |
| 104 | + }); |
| 105 | +}); |
0 commit comments