WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit ff4b1d9

Browse files
committed
chore: Improve test coverage
1 parent ce22487 commit ff4b1d9

File tree

4 files changed

+209
-1
lines changed

4 files changed

+209
-1
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
import { describe, test, expect } from 'vitest';
4+
import { MinimalTransformer } from '../transformer';
5+
import Stylesheet, { Rule, Declaration } from '../stylesheet';
6+
7+
describe('MinimalTransformer', () => {
8+
test('removes empty global selector rules', () => {
9+
const stylesheet = new Stylesheet();
10+
const rootRule = new Rule(':root');
11+
rootRule.appendDeclaration(new Declaration('--color', 'blue'));
12+
stylesheet.appendRuleWithPath(rootRule, []);
13+
14+
const childRule = new Rule('body');
15+
childRule.appendDeclaration(new Declaration('--color', 'blue'));
16+
stylesheet.appendRuleWithPath(childRule, [rootRule]);
17+
18+
const transformer = new MinimalTransformer();
19+
const result = transformer.transform(stylesheet);
20+
21+
expect(result.getAllRules().length).toBe(1);
22+
expect(result.findRule(':root')).toBeDefined();
23+
expect(result.findRule('body')).toBeUndefined();
24+
});
25+
26+
test('keeps non-empty global selector rules', () => {
27+
const stylesheet = new Stylesheet();
28+
const rootRule = new Rule(':root');
29+
rootRule.appendDeclaration(new Declaration('--color', 'blue'));
30+
stylesheet.appendRuleWithPath(rootRule, []);
31+
32+
const childRule = new Rule('html');
33+
childRule.appendDeclaration(new Declaration('--color', 'red'));
34+
stylesheet.appendRuleWithPath(childRule, [rootRule]);
35+
36+
const transformer = new MinimalTransformer();
37+
const result = transformer.transform(stylesheet);
38+
39+
expect(result.getAllRules().length).toBe(2);
40+
expect(result.findRule('html')?.size()).toBe(1);
41+
});
42+
43+
test('removes empty non-global selector rules', () => {
44+
const stylesheet = new Stylesheet();
45+
const rootRule = new Rule(':root');
46+
rootRule.appendDeclaration(new Declaration('--color', 'blue'));
47+
stylesheet.appendRuleWithPath(rootRule, []);
48+
49+
const childRule = new Rule('.child');
50+
childRule.appendDeclaration(new Declaration('--color', 'blue'));
51+
stylesheet.appendRuleWithPath(childRule, [rootRule]);
52+
53+
const transformer = new MinimalTransformer();
54+
const result = transformer.transform(stylesheet);
55+
56+
expect(result.getAllRules().length).toBe(1);
57+
expect(result.findRule('.child')).toBeUndefined();
58+
});
59+
});

src/shared/theme/__tests__/builder.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,19 @@ test('theme adds reference tokens', () => {
7878
colorWarning400: '#ff9900',
7979
});
8080
});
81+
82+
test('theme adds reference tokens with mode', () => {
83+
builder.addReferenceTokens(
84+
{
85+
color: {
86+
primary: { 500: { default: '#0073bb', optional: '#66b3ff' } },
87+
},
88+
},
89+
mode
90+
);
91+
92+
const theme = builder.build();
93+
94+
expect(theme.tokens.colorPrimary500).toEqual({ default: '#0073bb', optional: '#66b3ff' });
95+
expect(theme.tokenModeMap.colorPrimary500).toBe('mode');
96+
});

src/shared/theme/__tests__/process.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ vi.mock('../color-generation/hct-utils', () => ({
1111
hexToHct: vi.fn((hex: string) => ({ hue: 180, chroma: 50, tone: 50 })),
1212
hctToHex: vi.fn(() => '#008080'),
1313
createHct: vi.fn((hue: number, chroma: number, tone: number) => ({ hue, chroma, tone })),
14+
Hct: class Hct {
15+
constructor(public hue: number, public chroma: number, public tone: number) {}
16+
static from(hue: number, chroma: number, tone: number) {
17+
return new Hct(hue, chroma, tone);
18+
}
19+
},
1420
}));
1521

1622
describe('processReferenceTokens', () => {
@@ -166,4 +172,32 @@ describe('processColorPaletteInput', () => {
166172

167173
expect(result).toEqual({});
168174
});
175+
176+
test('processes mode-based seed input', () => {
177+
const input = {
178+
seed: {
179+
light: '#0073bb',
180+
dark: '#66b3ff',
181+
},
182+
};
183+
184+
const result = processColorPaletteInput('primary', input);
185+
186+
// Should generate palette with mode values
187+
expect(result[500]).toEqual({ light: '#008080', dark: '#008080' });
188+
});
189+
190+
test('skips non-string seed values in mode objects', () => {
191+
const input = {
192+
seed: {
193+
light: '#0073bb',
194+
dark: null as any,
195+
},
196+
};
197+
198+
const result = processColorPaletteInput('primary', input);
199+
200+
// Should only process light mode
201+
expect(result[500]).toEqual({ light: '#008080' });
202+
});
169203
});

src/shared/theme/__tests__/resolve.test.ts

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import {
88
themesWithCircularDependencies,
99
themeWithNonExistingToken,
1010
themeWithTokenWithoutModeResolution,
11+
colorMode,
1112
} from '../../../__fixtures__/common';
12-
import { resolveTheme, resolveThemeWithPaths } from '../resolve';
13+
import { resolveTheme, resolveThemeWithPaths, resolveContext } from '../resolve';
14+
import { Theme, Context } from '../interfaces';
1315

1416
describe('resolve', () => {
1517
test('resolves theme to full resolution', () => {
@@ -43,3 +45,100 @@ describe('resolve', () => {
4345
);
4446
});
4547
});
48+
49+
describe('resolveContext', () => {
50+
test('resolves context without defaultMode', () => {
51+
const theme: Theme = {
52+
id: 'test',
53+
selector: 'body',
54+
tokens: { color: 'blue' },
55+
tokenModeMap: {},
56+
contexts: {},
57+
modes: {},
58+
};
59+
60+
const context: Context = {
61+
id: 'ctx',
62+
selector: '.ctx',
63+
tokens: { color: 'red' },
64+
};
65+
66+
const result = resolveContext(theme, context);
67+
expect(result.color).toBe('red');
68+
});
69+
70+
test('resolves context with defaultMode', () => {
71+
const theme: Theme = {
72+
id: 'test',
73+
selector: 'body',
74+
tokens: { color: { light: 'purple', dark: 'blue' } },
75+
tokenModeMap: { color: 'color' },
76+
contexts: {},
77+
modes: { color: colorMode },
78+
};
79+
80+
const context: Context = {
81+
id: 'ctx',
82+
selector: '.ctx',
83+
tokens: {},
84+
defaultMode: 'dark',
85+
};
86+
87+
const result = resolveContext(theme, context);
88+
expect(result.color).toEqual({ light: 'purple', dark: 'blue' });
89+
});
90+
91+
test('resolves context with defaultMode but mode not found', () => {
92+
const theme: Theme = {
93+
id: 'test',
94+
selector: ':root',
95+
tokens: { color: 'blue' },
96+
tokenModeMap: {},
97+
contexts: {},
98+
modes: { color: colorMode },
99+
};
100+
101+
const context: Context = {
102+
id: 'ctx',
103+
selector: '.ctx',
104+
tokens: {},
105+
defaultMode: 'nonexistent',
106+
};
107+
108+
const result = resolveContext(theme, context);
109+
expect(result.color).toBe('blue');
110+
});
111+
112+
test('collects reference tokens when resolving context with defaultMode', () => {
113+
const theme: Theme = {
114+
id: 'test',
115+
selector: ':root',
116+
tokens: {
117+
colorPrimary500: { light: '#0073bb', dark: '#66b3ff' },
118+
colorNeutral500: '#888888',
119+
buttonBackground: '{colorPrimary500}',
120+
},
121+
tokenModeMap: { colorPrimary500: 'color' },
122+
referenceTokens: {
123+
color: {
124+
primary: { 500: { light: '#0073bb', dark: '#66b3ff' } },
125+
neutral: { 500: '#888888' },
126+
},
127+
},
128+
contexts: {},
129+
modes: { color: colorMode },
130+
};
131+
132+
const context: Context = {
133+
id: 'ctx',
134+
selector: '.ctx',
135+
tokens: { buttonBackground: '{colorNeutral500}' },
136+
defaultMode: 'light',
137+
};
138+
139+
resolveContext(theme, context);
140+
141+
expect(context.tokens.colorPrimary500).toBe('#0073bb');
142+
expect(context.tokens.buttonBackground).toBe('{colorNeutral500}');
143+
});
144+
});

0 commit comments

Comments
 (0)