|
| 1 | +import { StylesGenerator } from '../../adminforth/modules/styleGenerator'; |
| 2 | + |
| 3 | +function colorsOf(light: Record<string, unknown>) { |
| 4 | + return new StylesGenerator({ colors: { light } }).mergeStyles().colors; |
| 5 | +} |
| 6 | + |
| 7 | +// a color which went through an alias chain is spaced differently, only the channels matter |
| 8 | +const compact = (color: string) => color.replace(/\s/g, ''); |
| 9 | + |
| 10 | +describe('customization.styles color aliases', () => { |
| 11 | + it('resolves an alias chain whichever order the tokens are listed in', () => { |
| 12 | + // lightCreateViewSaveButtonText aliases lightButtonsText, which comes later in styles.ts |
| 13 | + const colors = colorsOf({ primary: '#F59E0B', buttons: { text: 'alias:lightPrimary inverse' } }); |
| 14 | + expect(colors.lightButtonsText).toBe('rgba(0,0,0,1)'); |
| 15 | + expect(compact(colors.lightCreateViewSaveButtonText)).toBe('rgba(0,0,0,1)'); |
| 16 | + expect(compact(colors.lightEditViewSaveButtonText)).toBe('rgba(0,0,0,1)'); |
| 17 | + }); |
| 18 | + |
| 19 | + it('applies opacity, darken, lighten and inverse to the aliased color', () => { |
| 20 | + const colors = colorsOf({ |
| 21 | + primary: '#1a56db', |
| 22 | + buttons: { |
| 23 | + background: 'alias:lightPrimary', |
| 24 | + hover: 'alias:lightPrimary darken', |
| 25 | + border: 'alias:lightPrimary lighten', |
| 26 | + text: 'alias:lightPrimary inverse', |
| 27 | + }, |
| 28 | + }); |
| 29 | + expect(colors.lightButtonsBackground).toBe('rgba(26,86,219,1)'); |
| 30 | + expect(colors.lightButtonsHover).toBe('rgba(21, 69, 175, 1)'); |
| 31 | + expect(colors.lightButtonsBorder).toBe('rgba(31, 103, 255, 1)'); |
| 32 | + expect(colors.lightButtonsText).toBe('rgba(255,255,255,1)'); |
| 33 | + expect(colors.lightButtonFocusRing).toBe('rgba(26,86,219,0.26)'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('keeps the ink buttons when only primary is set', () => { |
| 37 | + const colors = colorsOf({ primary: '#b400b8' }); |
| 38 | + expect(colors.lightButtonsBackground).toBe('#18181B'); |
| 39 | + expect(colors.lightCheckboxBgChecked).toBe('rgba(180,0,184,1)'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('rejects an alias cycle instead of looping', () => { |
| 43 | + expect(() => colorsOf({ primary: 'alias:lightButtonsBackground', buttons: { background: 'alias:lightPrimary' } })) |
| 44 | + .toThrow('lightPrimary -> lightButtonsBackground -> lightPrimary'); |
| 45 | + expect(() => colorsOf({ primary: 'alias:lightPrimary' })).toThrow('lightPrimary -> lightPrimary'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('rejects an alias to a token which does not exist', () => { |
| 49 | + expect(() => colorsOf({ buttons: { background: 'alias:lightPrimry' } })).toThrow('unknown token lightPrimry'); |
| 50 | + }); |
| 51 | +}); |
0 commit comments