Skip to content

Commit

Permalink
update test utitlities (#1101)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasoppermann authored Dec 5, 2024
1 parent 813e375 commit 029bf43
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
28 changes: 21 additions & 7 deletions src/test-utilities/getMockDictionary.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
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, 'name')) {
flatTokens.push(token as TransformedToken)
continue
}
getToken(token, flatTokens)
}
}

getToken(tokenTree, output)

return output
}

const mockDictionaryDefault = {
tokens: {
Expand All @@ -14,11 +32,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 => [],
})
33 changes: 26 additions & 7 deletions src/test-utilities/getMockToken.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type StyleDictionary from 'style-dictionary'
import {TransformedToken} from 'style-dictionary/types'

Check warning on line 1 in src/test-utilities/getMockToken.ts

View workflow job for this annotation

GitHub Actions / Test & Lint

All imports in the declaration are only used as types. Use `import type`

const mockTokenDefaults = {
name: 'tokenName',
Expand All @@ -11,15 +11,34 @@ const mockTokenDefaults = {
isSource: true,
$value: 'transformedValue',
attributes: {},
} as const

type getMockTokenOptions = {
remove: Array<keyof typeof mockTokenDefaults>
}

const removeProps = (
token: Record<string, unknown>,
props: getMockTokenOptions['remove'] = [],
): Partial<TransformedToken> => {
for (const prop of props) {
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,
}
}

0 comments on commit 029bf43

Please sign in to comment.