Skip to content

Commit

Permalink
#1199: Add tests and fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
JosefBredereck committed May 8, 2020
1 parent b0a880a commit ad3c415
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/core/src/lib/pseudopattern_hunter.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,14 @@ pseudopattern_hunter.prototype.find_pseudopatterns = function(
(objValue, srcValue) => {
if (
_.isArray(objValue) &&
// If the parameter is not available after updating pattern lab but
// not the patternlab-config it should not override arrays.
patternlab.config.hasOwnProperty('patternMergeVariantArrays') &&
!patternlab.config.patternMergeVariantArrays
) {
return srcValue;
} else if (_.isArray(objValue)) {
return objValue.concat(srcValue);
}
// Lodash will only check for "undefined" and eslint needs a consistent
// return so do not remove
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"a": 1,
"b": [2, 3],
"c": {
"d": [4, 5],
"e": 8,
"f": {"a": ["a"], "b": ["b"], "c": ["c"]}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{a}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"a": 2,
"b": [8],
"c": {
"d": [6, 7],
"f": {"b": ["x"]}
}
}
69 changes: 69 additions & 0 deletions packages/core/test/pseudopattern_hunter_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,72 @@ tap.test(
});
}
);

tap.test('pseudo pattern variant data should merge arrays', function(test) {
const pl = stubPatternlab();
pl.config.patternMergeVariantArrays = true;

const pattern = loadPattern('00-test/475-variant-test.mustache', pl);

addPattern(pattern, pl);

return pph.find_pseudopatterns(pattern, pl).then(() => {
test.equals(pl.patterns[1].patternPartial, 'test-variant-test-merge');
test.equals(
JSON.stringify(pl.patterns[1].jsonFileData),
JSON.stringify({
a: 2,
b: [2, 3, 8],
c: { d: [4, 5, 6, 7], e: 8, f: { a: ['a'], b: ['b', 'x'], c: ['c'] } },
})
);
});
});

tap.test(
'pseudo pattern variant data should merge arrays if config "patternMergeVariantArrays" is not available as default behavior',
function(test) {
const pl = stubPatternlab();

const pattern = loadPattern('00-test/475-variant-test.mustache', pl);

addPattern(pattern, pl);

return pph.find_pseudopatterns(pattern, pl).then(() => {
test.equals(pl.patterns[1].patternPartial, 'test-variant-test-merge');
test.equals(
JSON.stringify(pl.patterns[1].jsonFileData),
JSON.stringify({
a: 2,
b: [2, 3, 8],
c: {
d: [4, 5, 6, 7],
e: 8,
f: { a: ['a'], b: ['b', 'x'], c: ['c'] },
},
})
);
});
}
);

tap.test('pseudo pattern variant data should override arrays', function(test) {
const pl = stubPatternlab();
pl.config.patternMergeVariantArrays = false;

const pattern = loadPattern('00-test/475-variant-test.mustache', pl);

addPattern(pattern, pl);

return pph.find_pseudopatterns(pattern, pl).then(() => {
test.equals(pl.patterns[1].patternPartial, 'test-variant-test-merge');
test.equals(
JSON.stringify(pl.patterns[1].jsonFileData),
JSON.stringify({
a: 2,
b: [8],
c: { d: [6, 7], e: 8, f: { a: ['a'], b: ['x'], c: ['c'] } },
})
);
});
});

0 comments on commit ad3c415

Please sign in to comment.