Skip to content

Commit

Permalink
fixup! Feat(variables-scss): Formatting color tokens #DS-1461
Browse files Browse the repository at this point in the history
  • Loading branch information
curdaj committed Sep 10, 2024
1 parent 1e7d6ab commit fabf49b
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const dataProvider = [
token: {
id: '3',
name: 'unsupportedToken',
tokenType: TokenType.color,
tokenType: TokenType.duration,
} as Token,
expectedCss: null,
hasParentPrefix: true,
Expand All @@ -53,9 +53,16 @@ describe('cssGenerator', () => {

describe('generateCssFromTokens', () => {
it('should generate CSS from tokens', () => {
const css = generateCssFromTokens(Array.from(exampleMockedTokens.values()), mappedTokens, tokenGroups, true);
const css = generateCssFromTokens(
Array.from(exampleMockedTokens.values()),
mappedTokens,
tokenGroups,
'Grid',
true,
false,
);

expect(css).toBe('$grid-spacing-desktop: 32px !default;\n$grid-columns: 12 !default;');
expect(css).toBe('$grid-columns: 12 !default;\n\n$grid-spacing-desktop: 32px !default;');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('fileGenerator', () => {
{ fileName: '_other.scss', content: mockedExpectedResult },
{ fileName: '_radii.scss', content: emptyFile },
{ fileName: '_spacing.scss', content: emptyFile },
{ fileName: '_colors.scss', content: emptyFile },
]);
});
});
44 changes: 44 additions & 0 deletions exporters/variables-scss/src/helpers/__tests__/colorHelper.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { canHexBeShortened, normalizeColor, shortHex } from '../colorHelper';

const dataProviderItems = [
{
originalColor: 'ffffffff',
expectedColor: '#fff',
},
{
originalColor: '96969',
expectedColor: '#96969',
},
{
originalColor: '835ea1',
expectedColor: '#835ea1',
},
{
originalColor: '00000040',
expectedColor: '#00000040',
},
];

describe('colorHelper', () => {
describe.each(dataProviderItems)('normalizeColor', ({ originalColor, expectedColor }) => {
it('should normalize color', () => {
expect(normalizeColor(originalColor)).toBe(expectedColor);
});
});

describe('canHexBeShortened', () => {
it('should return true if hex can be shortened', () => {
expect(canHexBeShortened('ffffff')).toBe(true);
});

it('should return false if hex cannot be shortened', () => {
expect(canHexBeShortened('00000040')).toBe(false);
});
});

describe('shortHex', () => {
it('should shorten hex', () => {
expect(shortHex('ffffff')).toBe('fff');
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { convertToScss, mergeObjects } from '../cssObjectHelper';

const object1 = {
$grids: {
spacing: {
desktop: '$grid-spacing-desktop',
mobile: '$grid-spacing-mobile',
tablet: '$grid-spacing-tablet',
},
},
};

const object2 = {
$grids: { columns: '$grid-columns' },
};

const mergedObject = {
$grids: {
spacing: {
desktop: '$grid-spacing-desktop',
mobile: '$grid-spacing-mobile',
tablet: '$grid-spacing-tablet',
},
columns: '$grid-columns',
},
};

const scssObject = `$grids: (
spacing: (
desktop: $grid-spacing-desktop,
mobile: $grid-spacing-mobile,
tablet: $grid-spacing-tablet,
),
columns: $grid-columns,
),`;

describe('cssObjectHelper', () => {
describe('mergeObjects', () => {
it('should merge objects', () => {
const result = mergeObjects(object1, object2);

expect(result).toStrictEqual(mergedObject);
});
});

describe('convertToScss', () => {
it('should convert object to SCSS', () => {
const result = convertToScss(mergedObject);

expect(result).toBe(scssObject);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Token, TokenGroup } from '@supernovaio/sdk-exporters';
import { formatTokenName, tokenVariableName } from '../tokenHelper';
import { addEmptyLineBetweenTokenGroups, formatTokenName, sortTokens, tokenVariableName } from '../tokenHelper';
import { exampleMockedGroups, exampleMockedTokens } from '../../formatters/__fixtures__/mockedExampleTokens';

const dataProvider = [
Expand Down Expand Up @@ -47,4 +47,32 @@ describe('tokenHelper', () => {
expect(result).toBe('$grid-columns: 12 !default;');
});
});

describe('sortTokens', () => {
it('should sort tokens by variable name', () => {
const tokens = Array.from(exampleMockedTokens.values());
const tokenGroups = exampleMockedGroups;
const hasParentPrefix = true;
const group = 'Grid';
const sortByNumValue = false;

const result = sortTokens(tokens, tokenGroups, hasParentPrefix, group, sortByNumValue);

expect(result[0]?.origin?.name).toBe('Grid/Columns');
expect(result[1]?.origin?.name).toBe('Grid/spacing/desktop');
});
});

describe('addEmptyLineBetweenTokenGroups', () => {
it('should add empty line between token groups', () => {
const cssTokens = [
{ css: '$grid-columns: 12 !default;', parentGroupId: '1' },
{ css: '$grid-spacing-desktop: 32px !default;', parentGroupId: '2' },
];

const result = addEmptyLineBetweenTokenGroups(cssTokens);

expect(result).toBe('$grid-columns: 12 !default;\n\n$grid-spacing-desktop: 32px !default;');
});
});
});
4 changes: 2 additions & 2 deletions exporters/variables-scss/src/helpers/colorHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ const SHORT_HEX_WITHOUT_ALPHA_LENGTH = 3;
const SHORT_HEX_WITH_ALPHA_LENGTH = 4;
const LONG_HEX_WITH_ALPHA_LENGTH = 8;

const canHexBeShortened = (hex: string) => {
export const canHexBeShortened = (hex: string) => {
return hex.length % 2 === 0 && hex.split('').every((ch, i, arr) => (i % 2 === 0 ? ch === arr[i + 1] : true));
};

const shortHex = (hex: string): string => {
export const shortHex = (hex: string): string => {
return hex
.split('')
.map((char, index) => (index % 2 === 0 ? char : ''))
Expand Down

0 comments on commit fabf49b

Please sign in to comment.