Skip to content
This repository was archived by the owner on Nov 26, 2024. It is now read-only.

Commit 52fab7e

Browse files
feat: Created Unit Tests for CSS Color Variables Alignment with Figma Token JSON (#719)
* feat: added tests for css variables * refactor: removed lint ignore
1 parent 7f51e5a commit 52fab7e

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

src/css/brandColors.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { readFileSync } from 'fs';
2+
import { resolve } from 'path';
3+
4+
const brandColorsPath = resolve(__dirname, '../figma/brandColors.json');
5+
const brandColorsCSSPath = resolve(__dirname, 'brand-colors.css');
6+
7+
const brandColors = JSON.parse(readFileSync(brandColorsPath, 'utf8'));
8+
const brandColorsCSS = readFileSync(brandColorsCSSPath, 'utf8');
9+
10+
describe('Brand Colors CSS', () => {
11+
for (const color in brandColors) {
12+
if (Object.prototype.hasOwnProperty.call(brandColors, color)) {
13+
if (color !== 'white' && color !== 'black') {
14+
for (const shade in brandColors[color]) {
15+
if (Object.prototype.hasOwnProperty.call(brandColors[color], shade)) {
16+
const variableName = `--brand-colors-${color}-${color}${shade}`;
17+
const colorValue: string = brandColors[color][shade].value;
18+
it(`should have the correct value for ${variableName}`, () => {
19+
expect(brandColorsCSS).toContain(
20+
`${variableName}: ${colorValue};`,
21+
);
22+
});
23+
}
24+
}
25+
} else {
26+
const variableName = `--brand-colors-${color}`;
27+
const colorValue: string = brandColors[color].value;
28+
it(`should have the correct value for ${variableName}`, () => {
29+
expect(brandColorsCSS).toContain(`${variableName}: ${colorValue};`);
30+
});
31+
}
32+
}
33+
}
34+
});

src/css/darkTheme.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { readFileSync } from 'fs';
2+
import { resolve } from 'path';
3+
4+
const darkThemePath = resolve(__dirname, '../figma/darkTheme.json');
5+
const darkThemeCSSPath = resolve(__dirname, 'dark-theme-colors.css');
6+
7+
const darkTheme = JSON.parse(readFileSync(darkThemePath, 'utf8'));
8+
const darkThemeCSS = readFileSync(darkThemeCSSPath, 'utf8');
9+
10+
describe('Dark Theme Colors CSS', () => {
11+
for (const section in darkTheme) {
12+
if (Object.prototype.hasOwnProperty.call(darkTheme, section)) {
13+
for (const key in darkTheme[section]) {
14+
if (Object.prototype.hasOwnProperty.call(darkTheme[section], key)) {
15+
const variableName = `--color-${section}-${key.replace(/_/gu, '-')}`;
16+
const { value } = darkTheme[section][key];
17+
18+
let cssValue: string;
19+
if (value.startsWith('{')) {
20+
const parts: string[] = value.slice(1, -1).split('.');
21+
const color: string | undefined = parts[0];
22+
const shade: string | undefined = parts[1];
23+
if (color && shade) {
24+
cssValue = `var(--brand-colors-${color}-${color}${shade})`;
25+
} else {
26+
throw new Error(`Invalid color or shade: ${value as string}`);
27+
}
28+
} else {
29+
cssValue = value;
30+
}
31+
32+
it(`should have the correct value for ${variableName}`, () => {
33+
expect(darkThemeCSS).toContain(`${variableName}: ${cssValue};`);
34+
});
35+
}
36+
}
37+
}
38+
}
39+
});

src/css/lightTheme.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { readFileSync } from 'fs';
2+
import { resolve } from 'path';
3+
4+
const lightThemePath = resolve(__dirname, '../figma/lightTheme.json');
5+
const lightThemeCSSPath = resolve(__dirname, 'light-theme-colors.css');
6+
7+
const lightTheme = JSON.parse(readFileSync(lightThemePath, 'utf8'));
8+
const lightThemeCSS = readFileSync(lightThemeCSSPath, 'utf8');
9+
10+
describe('Light Theme Colors CSS', () => {
11+
for (const section in lightTheme) {
12+
if (Object.prototype.hasOwnProperty.call(lightTheme, section)) {
13+
for (const key in lightTheme[section]) {
14+
if (Object.prototype.hasOwnProperty.call(lightTheme[section], key)) {
15+
const variableName = `--color-${section}-${key.replace(/_/gu, '-')}`;
16+
const { value } = lightTheme[section][key];
17+
18+
let cssValue: string;
19+
if (value.startsWith('{')) {
20+
const parts: string[] = value.slice(1, -1).split('.');
21+
const color: string | undefined = parts[0];
22+
const shade: string | undefined = parts[1];
23+
if (color && shade) {
24+
cssValue = `var(--brand-colors-${color}-${color}${shade})`;
25+
} else {
26+
throw new Error(`Invalid color or shade: ${value as string}`);
27+
}
28+
} else {
29+
cssValue = value;
30+
}
31+
32+
it(`should have the correct value for ${variableName}`, () => {
33+
expect(lightThemeCSS).toContain(`${variableName}: ${cssValue};`);
34+
});
35+
}
36+
}
37+
}
38+
}
39+
});

0 commit comments

Comments
 (0)