From 8ab28cc53b3c3af0bcbc3f2b40f9ed142ce15215 Mon Sep 17 00:00:00 2001 From: Tony Walt Date: Mon, 20 Apr 2020 15:11:35 -0600 Subject: [PATCH] feat(attribute/cti): attribute/cti should respect manually set attributes (#415) fix #414 --- __tests__/common/transforms.test.js | 43 +++++++++++++++++++++++++++++ lib/common/transforms.js | 15 ++++++---- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/__tests__/common/transforms.test.js b/__tests__/common/transforms.test.js index d5b95ba61..38457ec3d 100644 --- a/__tests__/common/transforms.test.js +++ b/__tests__/common/transforms.test.js @@ -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({ diff --git a/lib/common/transforms.js b/lib/common/transforms.js index d8b36e55c..98834d6fd 100644 --- a/lib/common/transforms.js +++ b/lib/common/transforms.js @@ -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