Skip to content

Commit

Permalink
chore: formatting createPropertyFormatter test/code
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenbroekema committed Sep 29, 2023
1 parent c5a9592 commit d043139
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 33 deletions.
69 changes: 40 additions & 29 deletions __tests__/common/formatHelpers/createPropertyFormatter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,33 @@ const dictionary = createDictionary({
foo: {
original: {
value: '5px',
type: 'spacing'
type: 'spacing',
},
attributes: {
category: 'tokens',
type: 'foo'
type: 'foo',
},
name: 'tokens-foo',
path: ['tokens', 'foo'],
value: '5px',
type: 'spacing'
type: 'spacing',
},
bar: {
original: {
value: '{tokens.foo}',
type: 'spacing'
type: 'spacing',
},
attributes: {
category: 'tokens',
type: 'bar'
type: 'bar',
},
name: 'tokens-bar',
path: ['tokens', 'bar'],
value: '5px',
type: 'spacing'
type: 'spacing',
},
}
}
},
},
});

const transformedDictionary = createDictionary({
Expand All @@ -54,52 +54,63 @@ const transformedDictionary = createDictionary({
foo: {
original: {
value: '5px',
type: 'spacing'
type: 'spacing',
},
attributes: {
category: 'tokens',
type: 'foo'
type: 'foo',
},
name: 'tokens-foo',
path: ['tokens', 'foo'],
value: '5px',
type: 'spacing'
type: 'spacing',
},
bar: {
original: {
value: '{tokens.foo}',
type: 'spacing'
type: 'spacing',
},
attributes: {
category: 'tokens',
type: 'bar'
type: 'bar',
},
name: 'tokens-bar',
path: ['tokens', 'bar'],
value: 'changed by transitive transform',
type: 'spacing'
type: 'spacing',
},
}
}
},
},
});




describe('common', () => {
describe('formatHelpers', () => {
describe('createPropertyFormatter', () => {
it('should support outputReferences', () => {
const propFormatter = createPropertyFormatter({ outputReferences: true, dictionary, format: 'css' })
const propFormatter = createPropertyFormatter({
outputReferences: true,
dictionary,
format: 'css',
});
expect(propFormatter(dictionary.tokens.tokens.foo)).toEqual(' --tokens-foo: 5px;');
expect(propFormatter(dictionary.tokens.tokens.bar)).toEqual(' --tokens-bar: var(--tokens-foo);');
})
expect(propFormatter(dictionary.tokens.tokens.bar)).toEqual(
' --tokens-bar: var(--tokens-foo);',
);
});

it('should support outputReferences when values are transformed by (transitive) "value" transforms', () => {
const propFormatter = createPropertyFormatter({ outputReferences: true, dictionary, format: 'css' })
expect(propFormatter(transformedDictionary.tokens.tokens.foo)).toEqual(' --tokens-foo: 5px;');
expect(propFormatter(transformedDictionary.tokens.tokens.bar)).toEqual(' --tokens-bar: var(--tokens-foo);');
})
})
})
})
const propFormatter = createPropertyFormatter({
outputReferences: true,
dictionary,
format: 'css',
});
expect(propFormatter(transformedDictionary.tokens.tokens.foo)).toEqual(
' --tokens-foo: 5px;',
);
expect(propFormatter(transformedDictionary.tokens.tokens.bar)).toEqual(
' --tokens-bar: var(--tokens-foo);',
);
});
});
});
});
11 changes: 7 additions & 4 deletions lib/common/formatHelpers/createPropertyFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ function createPropertyFormatter({
value = prop.original.value;
}

refs.forEach(ref => {
refs.forEach((ref) => {
// value should be a string that contains the resolved reference
// because Style Dictionary resolved this in the resolution step.
// Here we are undoing that by replacing the value with
// the reference's name
if (ref.value && ref.name) {
const replaceFunc = function() {
const replaceFunc = function () {
if (format === 'css') {
if (outputReferenceFallbacks) {
return `var(${prefix}${ref.name}, ${ref.value})`;
Expand All @@ -135,11 +135,14 @@ function createPropertyFormatter({
} else {
return `${prefix}${ref.name}`;
}
}
};
// when original is object value, we replace value by matching ref.value and putting a var instead
// when original is string value, we replace value by matching original.value and putting a var instead
// this is more friendly to transitive transforms that transform the string values
value = value.replace(originalIsString ? new RegExp(`{${ref.path.join('.')}(.value)?}`, 'g') : ref.value, replaceFunc);
value = value.replace(
originalIsString ? new RegExp(`{${ref.path.join('.')}(.value)?}`, 'g') : ref.value,
replaceFunc,
);
}
});
}
Expand Down

0 comments on commit d043139

Please sign in to comment.