diff --git a/src/preprocessor/themeOverrides.test.ts b/src/preprocessor/themeOverrides.test.ts index aaa5e607f..02d11c0c6 100644 --- a/src/preprocessor/themeOverrides.test.ts +++ b/src/preprocessor/themeOverrides.test.ts @@ -67,9 +67,7 @@ describe('Preprocessor: themeOverrides', () => { }), ).toStrictEqual(resultDictionary.tokens) }) -}) -describe('Preprocessor: themeOverrides', () => { it('works with custom configuration', () => { const dictionary = getMockDictionary({ valueOverride: getMockToken({ diff --git a/src/preprocessor/themeOverrides.ts b/src/preprocessor/themeOverrides.ts index 66409483c..b0cb81448 100644 --- a/src/preprocessor/themeOverrides.ts +++ b/src/preprocessor/themeOverrides.ts @@ -6,12 +6,14 @@ export const themeOverrides: Preprocessor = { 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 + let 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]) + if (!currentTheme || !token.$extensions?.[extensionProp] || !token.$extensions?.[extensionProp][currentTheme]) { return token + } + // get override const override = token.$extensions?.[extensionProp][currentTheme] // token an theme value exist diff --git a/src/test-utilities/getMockDictionary.ts b/src/test-utilities/getMockDictionary.ts index 18b3773c5..01c93a989 100644 --- a/src/test-utilities/getMockDictionary.ts +++ b/src/test-utilities/getMockDictionary.ts @@ -6,10 +6,7 @@ const flattenTokens = (tokenTree: TransformedTokens): 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') - ) { + if (Object.prototype.hasOwnProperty.call(token, 'name')) { flatTokens.push(token as TransformedToken) continue } diff --git a/src/test-utilities/getMockToken.ts b/src/test-utilities/getMockToken.ts index 20f357f17..a7c19c00c 100644 --- a/src/test-utilities/getMockToken.ts +++ b/src/test-utilities/getMockToken.ts @@ -1,4 +1,4 @@ -import type StyleDictionary from 'style-dictionary' +import {TransformedToken} from 'style-dictionary/types' const mockTokenDefaults = { name: 'tokenName', @@ -11,15 +11,34 @@ const mockTokenDefaults = { isSource: true, $value: 'transformedValue', attributes: {}, +} as const + +type getMockTokenOptions = { + remove: Array +} + +const removeProps = ( + token: Record, + props?: getMockTokenOptions['remove'], +): Partial => { + ;(props || []).forEach(prop => { + delete token[prop] + }) + return token } /** * * @param valueOverrides partial StyleDictionary.TransformedToken * @returns StyleDictionary.TransformedToken - a merge of {@link mockTokenDefaults} and any valid properties provided in the valueOverrides param */ -export const getMockToken = (valueOverrides: { - [key: keyof StyleDictionary.TransformedToken]: unknown -}): StyleDictionary.TransformedToken => ({ - ...mockTokenDefaults, - ...valueOverrides, -}) +export const getMockToken = ( + valueOverrides: { + [key: keyof TransformedToken]: unknown + }, + options?: getMockTokenOptions, +) => { + return { + ...removeProps(mockTokenDefaults, options?.remove), + ...valueOverrides, + } +}