Skip to content

Commit

Permalink
feat(attribute/cti): attribute/cti should respect manually set attrib…
Browse files Browse the repository at this point in the history
…utes (#415)

fix #414
  • Loading branch information
tonyjwalt authored and dbanksdesign committed Sep 28, 2020
1 parent 6f0ac4d commit 8ab28cc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
43 changes: 43 additions & 0 deletions __tests__/common/transforms.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,49 @@ describe('common', () => {
});
});

describe('transform', () => {
describe('attribute/cti', () => {

const prop = {
"path": ["color", "background", "button", "primary", "active", "extra"],
};
const propShort = { "path": ["color", "primary"] };
const propOverride = {
"path": ["button", "primary", "border", "width"],
"attributes": { "category": "size", "component": "button" }
};

const attrs = transforms["attribute/cti"].transformer(prop);
const attrsShort = transforms["attribute/cti"].transformer(propShort);
const attrsOverride = transforms["attribute/cti"].transformer(propOverride);

it('should assign attributes correctly', () => {
expect(attrs).toEqual({
"category": "color",
"type": "background",
"item": "button",
"subitem": "primary",
"state": "active"
});
});

it('should not assign path props when path is short' , () => {
expect(attrsShort).toEqual({
"category": "color",
"type": "primary"
});
});

it('should leave other attributes alone', () => {
expect(attrsOverride).toHaveProperty('component', 'button');
});

it('should not override previously assigned path attributes', () => {
expect(attrsOverride).toHaveProperty('category', 'size');
});
});
});

describe('color/hex', () => {
it('should handle hex colors', () => {
var value = transforms["color/hex"].transformer({
Expand Down
15 changes: 9 additions & 6 deletions lib/common/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,16 @@ module.exports = {
'attribute/cti': {
type: 'attribute',
transformer: function(prop) {
return {
category: prop.path[0],
type: prop.path[1],
item: prop.path[2],
subitem: prop.path[3],
state: prop.path[4]

const attrNames = ['category', 'type', 'item', 'subitem', 'state'];
const originalAttrs = prop.attributes || {};
const generatedAttrs = {}

for(let i=0; i<prop.path.length && i<attrNames.length; i++) {
generatedAttrs[attrNames[i]] = prop.path[i];
}

return Object.assign(generatedAttrs, originalAttrs);
}
},

Expand Down

0 comments on commit 8ab28cc

Please sign in to comment.