Skip to content

Commit

Permalink
adding tests for preprocessor
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasoppermann committed Dec 5, 2024
1 parent 7f0212a commit cff2cf7
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 60 deletions.
6 changes: 4 additions & 2 deletions src/platforms/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const css: PlatformInitializer = (outputFile, prefix, buildPath, options)
return {
prefix,
buildPath,
preprocessors: ['overrideThemeValue'],
preprocessors: ['themeOverride'],
transforms: [
'name/pathToKebabCase',
'color/hex',
Expand All @@ -40,7 +40,9 @@ export const css: PlatformInitializer = (outputFile, prefix, buildPath, options)
],
options: {
basePxFontSize: 16,
theme: options?.theme,
themeOverrides: {
theme: options?.theme,
},
},
files: [
{
Expand Down
23 changes: 0 additions & 23 deletions src/preprocessor/overrideThemeValue.ts

This file was deleted.

144 changes: 144 additions & 0 deletions src/preprocessor/themeOverride.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import {getMockDictionary} from '../test-utilities/getMockDictionary.js'
import {themeOverride} from './themeOverride.js'
import {getMockToken} from '../test-utilities/getMockToken.js'

describe('Preprocessor: themeOverride', () => {
it('works with default settings', () => {
const dictionary = getMockDictionary({
valueOverride: getMockToken({
name: 'red',
description: 'This is a description',
$value: 'transformedValue',
path: ['tokens', 'subgroup', 'red'],
$extensions: {
'org.primer.overrides': {
dark: 'darkValue',
},
},
}),
objectOverride: getMockToken({
name: 'red',
description: 'This is a description',
$value: 'transformedValue',
path: ['tokens', 'subgroup', 'red'],
$extensions: {
'org.primer.overrides': {
dark: {
$value: 'darkValue',
description: 'DarkMode description',
},
},
},
}),
})

const resultDictionary = getMockDictionary({
valueOverride: getMockToken({
name: 'red',
description: 'This is a description',
$value: 'darkValue',
path: ['tokens', 'subgroup', 'red'],
$extensions: {
'org.primer.overrides': {
dark: 'darkValue',
},
},
}),
objectOverride: getMockToken({
name: 'red',
description: 'DarkMode description',
$value: 'darkValue',
path: ['tokens', 'subgroup', 'red'],
$extensions: {
'org.primer.overrides': {
dark: {
$value: 'darkValue',
description: 'DarkMode description',
},
},
},
}),
})

expect(themeOverride.preprocessor(dictionary.tokens, {})).toStrictEqual(dictionary.tokens)
expect(
themeOverride.preprocessor(dictionary.tokens, {
options: {themeOverrides: {theme: 'dark'}},
}),
).toStrictEqual(resultDictionary.tokens)
})
})

describe('Preprocessor: themeOverride', () => {
it('works with custom configuration', () => {
const dictionary = getMockDictionary({
valueOverride: getMockToken({
name: 'red',
description: 'This is a description',
value: 'transformedValue',
path: ['tokens', 'subgroup', 'red'],
$extensions: {
theme: {
dark: 'darkValue',
},
},
}),
objectOverride: getMockToken({
name: 'red',
description: 'This is a description',
value: 'transformedValue',
path: ['tokens', 'subgroup', 'red'],
$extensions: {
theme: {
dark: {
value: 'darkValue',
description: 'DarkMode description',
},
},
},
}),
})

const resultDictionary = getMockDictionary({
valueOverride: getMockToken({
name: 'red',
description: 'This is a description',
value: 'darkValue',
path: ['tokens', 'subgroup', 'red'],
$extensions: {
theme: {
dark: 'darkValue',
},
},
}),
objectOverride: getMockToken({
name: 'red',
description: 'DarkMode description',
value: 'darkValue',
path: ['tokens', 'subgroup', 'red'],
$extensions: {
theme: {
dark: {
value: 'darkValue',
description: 'DarkMode description',
},
},
},
}),
})

expect(
themeOverride.preprocessor(dictionary.tokens, {
themeOverrides: {
valueProp: 'value',
extensionProp: 'theme',
},
}),
).toStrictEqual(dictionary.tokens)
expect(
themeOverride.preprocessor(dictionary.tokens, {
options: {themeOverrides: {theme: 'dark', valueProp: 'value', extensionProp: 'theme'}},
}),
).toStrictEqual(resultDictionary.tokens)
})
})
25 changes: 25 additions & 0 deletions src/preprocessor/themeOverride.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type {PlatformConfig, PreprocessedTokens, Preprocessor} from 'style-dictionary/types'
import {transformTokens} from './utilities/transformTokens.js'

export const themeOverride: Preprocessor = {
name: 'themeOverride',
preprocessor: (dictionary: PreprocessedTokens, config: PlatformConfig): PreprocessedTokens => {
const extensionProp = config.options?.themeOverrides?.extensionProp || 'org.primer.overrides'
const valueProp = config.options?.themeOverrides?.valueProp || '$value'
const currentTheme = config.options?.themeOverrides?.theme

const tokens = transformTokens(dictionary, token => {
// return early if no theme value is set
if (!currentTheme || !token.$extensions?.[extensionProp] || !token.$extensions?.[extensionProp][currentTheme])
return token
// get override
const override = token.$extensions?.[extensionProp][currentTheme]
// token an theme value exist
return {
...token,
...(typeof override === 'object' ? override : {[valueProp]: override}),
}
})
return tokens
},
}
4 changes: 2 additions & 2 deletions src/primerStyleDictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
cssAdvanced,
jsonFigma,
} from './formats/index.js'
import {overrideThemeValue} from './preprocessor/overrideThemeValue.js'
import {themeOverride} from './preprocessor/themeOverride.js'

/**
* @name {@link PrimerStyleDictionary}
Expand Down Expand Up @@ -152,4 +152,4 @@ PrimerStyleDictionary.registerTransform(fontFamilyToCss)

PrimerStyleDictionary.registerTransform(fontFamilyToFigma)

PrimerStyleDictionary.registerPreprocessor(overrideThemeValue)
PrimerStyleDictionary.registerPreprocessor(themeOverride)
31 changes: 24 additions & 7 deletions src/test-utilities/getMockDictionary.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import type StyleDictionary from 'style-dictionary'
import {getMockToken} from './getMockToken.js'
import type {Dictionary, TransformedToken, TransformedTokens} from 'style-dictionary/types'

const flattenTokens = (tokenTree: TransformedTokens): TransformedToken[] => {
const output: TransformedToken[] = []

const getToken = (tokens: TransformedTokens, flatTokens: TransformedToken[]) => {
for (const token of Object.values(tokens)) {
if (
Object.prototype.hasOwnProperty.call(token, 'value') ||
Object.prototype.hasOwnProperty.call(token, '$value')
) {
flatTokens.push(token as TransformedToken)
continue
}
getToken(token, flatTokens)
}
}

getToken(tokenTree, output)

return output
}

const mockDictionaryDefault = {
tokens: {
Expand All @@ -14,11 +35,7 @@ const mockDictionaryDefault = {
},
}

export const getMockDictionary = (tokens?: StyleDictionary.TransformedTokens): StyleDictionary.Dictionary => ({
allTokens: Object.values((tokens || mockDictionaryDefault).tokens.subgroup),
export const getMockDictionary = (tokens?: TransformedTokens): Dictionary => ({
allTokens: flattenTokens(tokens || mockDictionaryDefault),
tokens: tokens || mockDictionaryDefault,
allProperties: Object.values((tokens || mockDictionaryDefault).tokens.subgroup),
properties: tokens || mockDictionaryDefault,
usesReference: _value => false,
getReferences: _value => [],
})
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,12 @@
group: 'component',
scopes: ['bgColor'],
},
'org.primer.overrides': [
{
theme: 'light',
override: {
$value: '{base.color.neutral.13}',
},
'org.primer.overrides': {
light: '{base.color.neutral.13}',
dark: {
$value: '{base.color.neutral.10}',
},
{
theme: 'dark',
override: {
$value: '{base.color.neutral.10}',
},
},
],
},
},
},
borderColor: {
Expand All @@ -36,20 +28,14 @@
group: 'component',
scopes: ['borderColor'],
},
'org.primer.overrides': [
{
theme: 'light',
override: {
$value: '{base.color.neutral.10}',
},
'org.primer.overrides': {
light: {
$value: '{base.color.neutral.10}',
},
{
theme: 'dark',
override: {
$value: '{base.color.neutral.7}',
},
dark: {
$value: '{base.color.neutral.7}',
},
],
},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
group: 'component',
scopes: ['borderColor'],
},
'org.primer.theme': {
'org.primer.overrides': {
light: '{base.color.transparent}',
dark: '{base.color.transparent}',
'dark-high-contrast': '{borderColor.accent.emphasis}',
Expand Down

0 comments on commit cff2cf7

Please sign in to comment.