Skip to content

Commit

Permalink
test: add tests for the getter transform
Browse files Browse the repository at this point in the history
  • Loading branch information
cheton committed Dec 18, 2024
1 parent 96298ce commit 8f6de44
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions packages/styled-system/src/utils/__tests__/transforms.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { getter } from '../transforms';

describe('getter', () => {
const theme = {
colors: {
// flat theme tokens
'white:primary': 'rgba(255, 255, 255, .92)',
'white:secondary': 'rgba(255, 255, 255, .60)',
'black:primary': 'rgba(0, 0, 0, .92)',
'black:secondary': 'rgba(0, 0, 0, .65)',

// nested theme tokens
white: {
primary: {
value: 'rgba(255, 255, 255, .92)',
},
secondary: {
value: 'rgba(255, 255, 255, .60)',
},
},
black: {
primary: {
value: 'rgba(0, 0, 0, .92)',
},
secondary: {
value: 'rgba(0, 0, 0, .65)',
},
},
},
};

it('should resolve flat color tokens', () => {
expect(getter(theme.colors, 'white:primary')).toBe('rgba(255, 255, 255, .92)');
expect(getter(theme.colors, 'white:secondary')).toBe('rgba(255, 255, 255, .60)');
expect(getter(theme.colors, 'black:primary')).toBe('rgba(0, 0, 0, .92)');
expect(getter(theme.colors, 'black:secondary')).toBe('rgba(0, 0, 0, .65)');
});

it('should resolve nested color tokens with dot notation', () => {
expect(getter(theme.colors, 'white.primary')).toBe('rgba(255, 255, 255, .92)');
expect(getter(theme.colors, 'white.secondary')).toBe('rgba(255, 255, 255, .60)');
expect(getter(theme.colors, 'black.primary')).toBe('rgba(0, 0, 0, .92)');
expect(getter(theme.colors, 'black.secondary')).toBe('rgba(0, 0, 0, .65)');
});

it('should fallback to original value when token path does not exist', () => {
expect(getter(theme.colors, 'white')).toBe('white');
});

it('should handle undefined theme values', () => {
expect(getter(undefined, 'white.undefined')).toBe('white.undefined');
});

it('should handle nested objects without value property', () => {
const customTheme = {
colors: {
custom: {
primary: 'rgb(100, 100, 100)',
},
},
};
const result = getter(customTheme.colors, 'custom.primary');
expect(result).toBe('rgb(100, 100, 100)');
});
});

0 comments on commit 8f6de44

Please sign in to comment.