Skip to content

Commit

Permalink
Merge branch '3.0' into custom-filters
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanksdesign authored Nov 14, 2020
2 parents ba6fae7 + a8bb832 commit 353588f
Show file tree
Hide file tree
Showing 65 changed files with 3,819 additions and 2,055 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
language: node_js
node_js:
- "8"
- "10"
- "12"
- "14"
2 changes: 2 additions & 0 deletions __tests__/__json_files/yaml.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
foo: "bar"
bar: "{foo}"
10 changes: 10 additions & 0 deletions __tests__/__properties/colors.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@

"font": {
"link": { "value": "{color.base.blue.100.value}" }
},

"button": {
"base": { "value": "{color.base.blue.100.value}" },
"active": { "value": "{color.button.base.value}", "transformColor": {
"darken": 0.5
} },
"hover": { "value": "{color.button.active.value}", "transformColor": {
"darken": 0.2
} }
}
}
}
24 changes: 24 additions & 0 deletions __tests__/__properties/paddings.1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"size": {
"padding": {
"tiny": {
"value": "3"
},
"small": {
"value": "5"
},
"base": {
"value": "10"
},
"large": {
"value": "15"
},
"xl": {
"value": "20"
},
"xxl": {
"value": "30"
}
}
}
}
26 changes: 16 additions & 10 deletions __tests__/buildFile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,30 @@ describe('buildFile', () => {

it('should error if format doesnt exist or isnt a function', () => {
expect(
buildFile.bind(null, '__tests__/__output/test.txt', {}, {}, {})
buildFile.bind(null, {destination: '__tests__/__output/test.txt'}, {}, {})
).toThrow('Please enter a valid file format');
expect(
buildFile.bind(null, '__tests__/__output/test.txt', [], {}, {})
buildFile.bind(null, {destination: '__tests__/__output/test.txt', format: {}}, {}, {})
).toThrow('Please enter a valid file format');
expect(
buildFile.bind(null, '__tests__/__output/test.txt', null, {}, {})
buildFile.bind(null, {destination: '__tests__/__output/test.txt', format: []}, {}, {})
).toThrow('Please enter a valid file format');
});

it('should error if destination doesnt exist or isnt a string', () => {
expect(
buildFile.bind(null, {}, format, {}, {})
buildFile.bind(null, {format}, {}, {})
).toThrow('Please enter a valid destination');
expect(
buildFile.bind(null, [], format, {}, {})
buildFile.bind(null, {format, destination: []}, {}, {})
).toThrow('Please enter a valid destination');
expect(
buildFile.bind(null, null, format, {}, {})
buildFile.bind(null, {format, destination: {}}, {}, {})
).toThrow('Please enter a valid destination');
});

let dest = './__tests__/__output/test.collisions';
var PROPERTY_NAME_COLLISION_WARNINGS = GroupMessages.GROUP.PropertyNameCollisionWarnings + ":" + dest;
let destination = './__tests__/__output/test.collisions';
var PROPERTY_NAME_COLLISION_WARNINGS = GroupMessages.GROUP.PropertyNameCollisionWarnings + ":" + destination;
it('should generate warning messages for output name collisions', () => {
let name = 'someName';
let properties = {
Expand All @@ -71,7 +71,7 @@ describe('buildFile', () => {
};

GroupMessages.clear(PROPERTY_NAME_COLLISION_WARNINGS);
buildFile(dest, format, {}, properties);
buildFile({destination, format}, {}, properties);

let collisions = properties.allProperties.map((properties) => {
let propertyPathText = chalk.keyword('orangered')(properties.path.join('.'));
Expand All @@ -86,7 +86,13 @@ describe('buildFile', () => {
});

it('should write to a file properly', () => {
buildFile('test.txt', format, {buildPath: '__tests__/__output/'}, {});
buildFile({
destination: 'test.txt',
format
},{
buildPath: '__tests__/__output/'
},{}
);
expect(helpers.fileExists('./__tests__/__output/test.txt')).toBeTruthy();
});
});
6 changes: 3 additions & 3 deletions __tests__/cleanDir.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ describe('cleanDir', () => {
});

it('should delete a dir properly', () => {
buildFile('test.txt', format, {buildPath: '__tests__/__output/extradir1/extradir2/'}, {});
cleanFile('test.txt', format, {buildPath: '__tests__/__output/extradir1/extradir2/'}, {});
cleanDir('test.txt', format, {buildPath: '__tests__/__output/extradir1/extradir2/'}, {});
buildFile({destination:'test.txt', format}, {buildPath: '__tests__/__output/extradir1/extradir2/'}, {});
cleanFile({destination:'test.txt', format}, {buildPath: '__tests__/__output/extradir1/extradir2/'}, {});
cleanDir({destination:'test.txt', format}, {buildPath: '__tests__/__output/extradir1/extradir2/'}, {});
expect(helpers.dirDoesNotExist('./__tests__/__output/extradir1/extradir2')).toBeTruthy();
expect(helpers.dirDoesNotExist('./__tests__/__output/extradir1')).toBeTruthy();
});
Expand Down
6 changes: 3 additions & 3 deletions __tests__/cleanFile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ describe('cleanFile', () => {
});

it('should delete a file properly', () => {
buildFile('test.txt', format, {buildPath: '__tests__/__output/'}, {});
cleanFile('test.txt', format, {buildPath: '__tests__/__output/'}, {});
buildFile({destination:'test.txt', format}, {buildPath: '__tests__/__output/'}, {});
cleanFile({destination:'test.txt', format}, {buildPath: '__tests__/__output/'}, {});
expect(helpers.fileDoesNotExist('./__tests__/__output/test.txt')).toBeTruthy();
});

describe('if a file does not exist', () => {
it('should not throw', () => {
expect(() => cleanFile('non-existent.txt', format, { buildPath: '__tests__/__output/' }, {})).not.toThrow();
expect(() => cleanFile({destination: 'non-existent.txt', format}, { buildPath: '__tests__/__output/' }, {})).not.toThrow();
})
})

Expand Down
23 changes: 23 additions & 0 deletions __tests__/exportPlatform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,29 @@ describe('exportPlatform', () => {
expect(dictionary.size.padding.base.value.indexOf('px')).toBeGreaterThan(0);
});

it('should have applied transforms for props with refs in it', () => {
const StyleDictionaryExtended = StyleDictionary.extend({
platforms: {
test: {
transforms: ['color/css','color/darken']
}
}
});

StyleDictionaryExtended.registerTransform({
type: 'value',
name: 'color/darken',
transitive: true,
matcher: function(prop) { return !!prop.original.transformColor; },
transformer: function(prop) { return prop.value + '-darker'; }
});

const dictionary = StyleDictionaryExtended.exportPlatform('test');

expect(dictionary.color.button.active.value).toEqual('#0077CC-darker');
expect(dictionary.color.button.hover.value).toEqual('#0077CC-darker-darker');
});

it('should not have mutated the original properties', () => {
var dictionary = StyleDictionary.exportPlatform('web');
expect(dictionary.color.font.link.value).not.toEqual(StyleDictionary.properties.color.font.link.value);
Expand Down
67 changes: 54 additions & 13 deletions __tests__/extend.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ var helpers = require('./__helpers');
var StyleDictionary = require('../index');
var _ = require('lodash');

function traverseObj(obj, fn) {
for (let key in obj) {
fn.apply(this, [obj, key, obj[key]]);
if (obj[key] && typeof obj[key] === 'object') {
traverseObj(obj[key], fn);
}
}
}

var test_props = {
size: {
padding: {
Expand All @@ -24,7 +33,6 @@ var test_props = {
};

describe('extend', () => {

describe('method signature', () => {
it('should accept a string as a path to a JSON file', () => {
var StyleDictionaryExtended = StyleDictionary.extend(__dirname + '/__configs/test.json');
Expand Down Expand Up @@ -77,17 +85,31 @@ describe('extend', () => {

it('should build the properties object if an include is given', () => {
var StyleDictionaryExtended = StyleDictionary.extend({
"include": [__dirname + "/__properties/paddings.json"]
include: [__dirname + "/__properties/paddings.json"]
});
expect(StyleDictionaryExtended.properties).toEqual(helpers.fileToJSON(__dirname + "/__properties/paddings.json"));
var output = helpers.fileToJSON(__dirname + "/__properties/paddings.json");
traverseObj(output, (obj) => {
if (obj.value && !obj.filePath) {
obj.filePath = __dirname + "/__properties/paddings.json";
obj.isSource = false;
}
});
expect(StyleDictionaryExtended.properties).toEqual(output);
});

it('should override existing properties if include is given', () => {
var StyleDictionaryExtended = StyleDictionary.extend({
properties: test_props,
include: [__dirname + "/__properties/paddings.json"]
"include": [__dirname + "/__properties/paddings.json"]
});
expect(StyleDictionaryExtended.properties).toEqual(helpers.fileToJSON(__dirname + "/__properties/paddings.json"));
var output = helpers.fileToJSON(__dirname + "/__properties/paddings.json");
traverseObj(output, (obj) => {
if (obj.value && !obj.filePath) {
obj.filePath = __dirname + "/__properties/paddings.json";
obj.isSource = false;
}
});
expect(StyleDictionaryExtended.properties).toEqual(output);
});

it('should update properties if there are includes', () => {
Expand All @@ -106,7 +128,6 @@ describe('extend', () => {
});
});


describe('source', () => {
it('should throw if source isnt an array', () => {
expect(
Expand All @@ -125,34 +146,54 @@ describe('extend', () => {
var StyleDictionaryExtended = StyleDictionary.extend({
"source": [__dirname + "/__properties/paddings.json"]
});
expect(StyleDictionaryExtended.properties).toEqual(helpers.fileToJSON(__dirname + "/__properties/paddings.json"));
var output = helpers.fileToJSON(__dirname + "/__properties/paddings.json");
traverseObj(output, (obj) => {
if (obj.value && !obj.filePath) {
obj.filePath = __dirname + "/__properties/paddings.json";
obj.isSource = true;
}
});
expect(StyleDictionaryExtended.properties).toEqual(output);
});

it('should override existing properties source is given', () => {
var StyleDictionaryExtended = StyleDictionary.extend({
properties: test_props,
source: [__dirname + "/__properties/paddings.json"]
});
expect(StyleDictionaryExtended.properties).toEqual(helpers.fileToJSON(__dirname + "/__properties/paddings.json"));
var output = helpers.fileToJSON(__dirname + "/__properties/paddings.json");
traverseObj(output, (obj) => {
if (obj.value && !obj.filePath) {
obj.filePath = __dirname + "/__properties/paddings.json";
obj.isSource = true;
}
});
expect(StyleDictionaryExtended.properties).toEqual(output);
});
});


// This is to allow style dictionaries to depend on other style dictionaries and
// override properties. Useful for skinning
it('should not throw a collision error if a source file collides with an include', () => {
var StyleDictionaryExtended = StyleDictionary.extend({
include: [__dirname + "/__properties/paddings.json"],
include: [__dirname + "/__properties/paddings.1.json"],
source: [__dirname + "/__properties/paddings.json"],
log: 'error'
});
expect(StyleDictionaryExtended.properties).toEqual(helpers.fileToJSON(__dirname + "/__properties/paddings.json"));
var output = helpers.fileToJSON(__dirname + "/__properties/paddings.json");
traverseObj(output, (obj) => {
if (obj.value && !obj.filePath) {
obj.filePath = __dirname + "/__properties/paddings.json";
obj.isSource = true;
}
});
expect(StyleDictionaryExtended.properties).toEqual(output);
});

it('should throw a error if the collision is in source files and log is set to error', () => {
expect(
StyleDictionary.extend.bind(null, {
source: [__dirname + "/__properties/paddings.json", __dirname + "/__properties/paddings.json"],
source: [__dirname + "/__properties/paddings.1.json", __dirname + "/__properties/paddings.json"],
log: 'error'
})
).toThrow('Collisions detected');
Expand All @@ -171,7 +212,7 @@ describe('extend', () => {
var StyleDictionaryExtended = StyleDictionary.extend(__dirname + '/__configs/test.json5');
expect(StyleDictionaryExtended).toHaveProperty('platforms.web');
});

it('should allow for chained extends and not mutate the original', function() {
var StyleDictionary1 = StyleDictionary.extend({
foo: 'bar'
Expand Down
11 changes: 11 additions & 0 deletions __tests__/formats/__snapshots__/all.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,17 @@ module.exports = {
};"
`;
exports[`formats all should match javascript/module-flat snapshot 1`] = `
"/**
* Do not edit directly
* Generated on Sat, 01 Jan 2000 00:00:00 GMT
*/
module.exports = {
\\"color_red\\": \\"#FF0000\\"
};"
`;
exports[`formats all should match javascript/object snapshot 1`] = `
"/**
* Do not edit directly
Expand Down
2 changes: 1 addition & 1 deletion __tests__/formats/all.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('formats', () => {
global.Date = function() { return constantDate };

var formatter = formats[key].bind(file);
var output = formatter(dictionary, file);
var output = formatter(dictionary, {}, file);

// reset the global Date object
global.Date = globalDate;
Expand Down
2 changes: 1 addition & 1 deletion __tests__/formats/es6Constants.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('formats', () => {
});

it('should be a valid JS file', () => {
fs.writeFileSync('./__tests__/__output/output.js', formatter(dictionary) );
fs.writeFileSync('./__tests__/__output/output.js', formatter(dictionary, {}, file) );
var test = require('../__output/output.js');
expect(test.red).toEqual(dictionary.allProperties[0].value);
});
Expand Down
2 changes: 1 addition & 1 deletion __tests__/formats/javascriptModule.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('formats', () => {
});

it('should be a valid JS file', () => {
fs.writeFileSync('./__tests__/__output/output.js', formatter(dictionary) );
fs.writeFileSync('./__tests__/__output/output.js', formatter(dictionary, {}, file) );
var test = require('../__output/output.js');
expect(test.color.red.value).toEqual(dictionary.properties.color.red.value);
});
Expand Down
Loading

0 comments on commit 353588f

Please sign in to comment.