73 lines
2.5 KiB
TypeScript
73 lines
2.5 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { primitiveTokens } from './primitives';
|
|
import { semanticLightTokens } from './semantic.light';
|
|
import { componentTokens } from './components';
|
|
import { resolveToken } from './resolveToken';
|
|
|
|
const aliasPattern = /^\{([^}]+)\}$/;
|
|
const isAlias = (value: string | number | readonly number[] | readonly string[]): boolean =>
|
|
typeof value === 'string' && aliasPattern.test(value);
|
|
|
|
const allTokens = {
|
|
...primitiveTokens,
|
|
...semanticLightTokens,
|
|
...componentTokens,
|
|
} as const;
|
|
|
|
describe('token integrity', () => {
|
|
it('primitives contain no aliases', () => {
|
|
for (const [name, def] of Object.entries(primitiveTokens)) {
|
|
expect(isAlias(def.$value), `${name} should not be an alias`).toBe(false);
|
|
}
|
|
});
|
|
|
|
it('semantic tokens only use aliases', () => {
|
|
for (const [name, def] of Object.entries(semanticLightTokens)) {
|
|
expect(isAlias(def.$value), `${name} should be an alias`).toBe(true);
|
|
}
|
|
});
|
|
|
|
it('component tokens only use aliases', () => {
|
|
for (const [name, def] of Object.entries(componentTokens)) {
|
|
expect(isAlias(def.$value), `${name} should be an alias`).toBe(true);
|
|
}
|
|
});
|
|
|
|
it('all tokens resolve without errors', () => {
|
|
for (const name of Object.keys(allTokens)) {
|
|
expect(() => resolveToken(allTokens, name), `Failed to resolve ${name}`).not.toThrow();
|
|
}
|
|
});
|
|
|
|
it('has unique names across all collections', () => {
|
|
const allNames = [
|
|
...Object.keys(primitiveTokens),
|
|
...Object.keys(semanticLightTokens),
|
|
...Object.keys(componentTokens),
|
|
];
|
|
const uniqueNames = new Set(allNames);
|
|
expect(uniqueNames.size).toBe(allNames.length);
|
|
});
|
|
|
|
it('semantic tokens reference only primitive tokens', () => {
|
|
const primitiveNames = new Set(Object.keys(primitiveTokens));
|
|
for (const [name, def] of Object.entries(semanticLightTokens)) {
|
|
if (typeof def.$value === 'string' && aliasPattern.test(def.$value)) {
|
|
const target = def.$value.match(aliasPattern)![1];
|
|
expect(primitiveNames.has(target), `${name} references non-primitive "${target}"`).toBe(
|
|
true,
|
|
);
|
|
}
|
|
}
|
|
});
|
|
|
|
it('component tokens avoid direct primitive value literals', () => {
|
|
const literalPattern = /(px|#|rgba|rgb)/;
|
|
for (const [name, def] of Object.entries(componentTokens)) {
|
|
if (typeof def.$value === 'string') {
|
|
expect(literalPattern.test(def.$value), `${name} contains a literal value`).toBe(false);
|
|
}
|
|
}
|
|
});
|
|
});
|