Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update test utilities #1101

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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,
}
}
Loading