|
| 1 | +import { render, screen } from '@testing-library/react'; |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | + |
| 4 | +import { getTokenHex, getTokenValue, rgbToHex, TokenCode, toRgba } from '../stories/colorTokens'; |
| 5 | + |
| 6 | +const GRADIENT = 'linear-gradient(136deg, rgb(61, 214, 245) 22.68%, rgb(64, 91, 255) 127.6%)'; |
| 7 | + |
| 8 | +describe('rgbToHex', () => { |
| 9 | + it('converts an opaque rgb() string to 6-digit hex', () => { |
| 10 | + expect(rgbToHex('rgb(61, 214, 245)')).toBe('#3DD6F5'); |
| 11 | + }); |
| 12 | + |
| 13 | + it('converts a translucent rgba() string to 8-digit hex', () => { |
| 14 | + expect(rgbToHex('rgba(0, 0, 0, 0.5)')).toBe('#00000080'); |
| 15 | + }); |
| 16 | + |
| 17 | + it('omits the alpha channel when fully opaque', () => { |
| 18 | + expect(rgbToHex('rgba(255, 53, 162, 1)')).toBe('#FF35A2'); |
| 19 | + }); |
| 20 | + |
| 21 | + it('supports the space/slash-separated syntax', () => { |
| 22 | + expect(rgbToHex('rgb(61 214 245 / 0.5)')).toBe('#3DD6F580'); |
| 23 | + }); |
| 24 | + |
| 25 | + it('returns null for non-color values such as gradients', () => { |
| 26 | + expect(rgbToHex(GRADIENT)).toBeNull(); |
| 27 | + }); |
| 28 | +}); |
| 29 | + |
| 30 | +describe('getTokenHex', () => { |
| 31 | + it('returns the hex for a solid color', () => { |
| 32 | + expect(getTokenHex({ color: 'rgb(61, 214, 245)', image: 'none' })).toBe('#3DD6F5'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('returns null for a gradient token', () => { |
| 36 | + expect(getTokenHex({ color: 'rgba(0, 0, 0, 0)', image: GRADIENT })).toBeNull(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('returns null when the value is not yet computed', () => { |
| 40 | + expect(getTokenHex(undefined)).toBeNull(); |
| 41 | + }); |
| 42 | +}); |
| 43 | + |
| 44 | +describe('toRgba', () => { |
| 45 | + it('adds an explicit alpha to opaque rgb() values', () => { |
| 46 | + expect(toRgba('rgb(61, 214, 245)')).toBe('rgba(61, 214, 245, 1)'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('preserves an existing alpha channel', () => { |
| 50 | + expect(toRgba('rgba(0, 0, 0, 0.5)')).toBe('rgba(0, 0, 0, 0.5)'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('leaves non-color values untouched', () => { |
| 54 | + expect(toRgba(GRADIENT)).toBe(GRADIENT); |
| 55 | + }); |
| 56 | +}); |
| 57 | + |
| 58 | +describe('getTokenValue', () => { |
| 59 | + it('returns the rgba string for a solid color (kept for backwards compatibility)', () => { |
| 60 | + expect(getTokenValue({ color: 'rgb(61, 214, 245)', image: 'none' })).toBe( |
| 61 | + 'rgba(61, 214, 245, 1)', |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + it('returns the gradient definition instead of the transparent fallback', () => { |
| 66 | + expect(getTokenValue({ color: 'rgba(0, 0, 0, 0)', image: GRADIENT })).toBe(GRADIENT); |
| 67 | + }); |
| 68 | + |
| 69 | + it('returns null when the value is not yet computed', () => { |
| 70 | + expect(getTokenValue(undefined)).toBeNull(); |
| 71 | + }); |
| 72 | +}); |
| 73 | + |
| 74 | +describe('TokenCode', () => { |
| 75 | + it('renders its content', () => { |
| 76 | + render(<TokenCode>#3DD6F5</TokenCode>); |
| 77 | + |
| 78 | + expect(screen.getByText('#3DD6F5')).toBeInTheDocument(); |
| 79 | + }); |
| 80 | + |
| 81 | + it('renders nothing when there is no content', () => { |
| 82 | + const { container } = render(<TokenCode>{null}</TokenCode>); |
| 83 | + |
| 84 | + expect(container).toBeEmptyDOMElement(); |
| 85 | + }); |
| 86 | +}); |
0 commit comments