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

feat(attribute/cti): make attribute/cti respect manually set attributes #415

Merged
merged 2 commits into from
Apr 20, 2020
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
43 changes: 43 additions & 0 deletions __tests__/common/transforms.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,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 @@ -71,13 +71,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