Skip to content

Commit ad3c415

Browse files
#1199: Add tests and fix bugs
1 parent b0a880a commit ad3c415

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed

packages/core/src/lib/pseudopattern_hunter.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,14 @@ pseudopattern_hunter.prototype.find_pseudopatterns = function(
6767
(objValue, srcValue) => {
6868
if (
6969
_.isArray(objValue) &&
70+
// If the parameter is not available after updating pattern lab but
71+
// not the patternlab-config it should not override arrays.
72+
patternlab.config.hasOwnProperty('patternMergeVariantArrays') &&
7073
!patternlab.config.patternMergeVariantArrays
7174
) {
7275
return srcValue;
76+
} else if (_.isArray(objValue)) {
77+
return objValue.concat(srcValue);
7378
}
7479
// Lodash will only check for "undefined" and eslint needs a consistent
7580
// return so do not remove
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"a": 1,
3+
"b": [2, 3],
4+
"c": {
5+
"d": [4, 5],
6+
"e": 8,
7+
"f": {"a": ["a"], "b": ["b"], "c": ["c"]}
8+
}
9+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{a}}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"a": 2,
3+
"b": [8],
4+
"c": {
5+
"d": [6, 7],
6+
"f": {"b": ["x"]}
7+
}
8+
}

packages/core/test/pseudopattern_hunter_tests.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,72 @@ tap.test(
133133
});
134134
}
135135
);
136+
137+
tap.test('pseudo pattern variant data should merge arrays', function(test) {
138+
const pl = stubPatternlab();
139+
pl.config.patternMergeVariantArrays = true;
140+
141+
const pattern = loadPattern('00-test/475-variant-test.mustache', pl);
142+
143+
addPattern(pattern, pl);
144+
145+
return pph.find_pseudopatterns(pattern, pl).then(() => {
146+
test.equals(pl.patterns[1].patternPartial, 'test-variant-test-merge');
147+
test.equals(
148+
JSON.stringify(pl.patterns[1].jsonFileData),
149+
JSON.stringify({
150+
a: 2,
151+
b: [2, 3, 8],
152+
c: { d: [4, 5, 6, 7], e: 8, f: { a: ['a'], b: ['b', 'x'], c: ['c'] } },
153+
})
154+
);
155+
});
156+
});
157+
158+
tap.test(
159+
'pseudo pattern variant data should merge arrays if config "patternMergeVariantArrays" is not available as default behavior',
160+
function(test) {
161+
const pl = stubPatternlab();
162+
163+
const pattern = loadPattern('00-test/475-variant-test.mustache', pl);
164+
165+
addPattern(pattern, pl);
166+
167+
return pph.find_pseudopatterns(pattern, pl).then(() => {
168+
test.equals(pl.patterns[1].patternPartial, 'test-variant-test-merge');
169+
test.equals(
170+
JSON.stringify(pl.patterns[1].jsonFileData),
171+
JSON.stringify({
172+
a: 2,
173+
b: [2, 3, 8],
174+
c: {
175+
d: [4, 5, 6, 7],
176+
e: 8,
177+
f: { a: ['a'], b: ['b', 'x'], c: ['c'] },
178+
},
179+
})
180+
);
181+
});
182+
}
183+
);
184+
185+
tap.test('pseudo pattern variant data should override arrays', function(test) {
186+
const pl = stubPatternlab();
187+
pl.config.patternMergeVariantArrays = false;
188+
189+
const pattern = loadPattern('00-test/475-variant-test.mustache', pl);
190+
191+
addPattern(pattern, pl);
192+
193+
return pph.find_pseudopatterns(pattern, pl).then(() => {
194+
test.equals(pl.patterns[1].patternPartial, 'test-variant-test-merge');
195+
test.equals(
196+
JSON.stringify(pl.patterns[1].jsonFileData),
197+
JSON.stringify({
198+
a: 2,
199+
b: [8],
200+
c: { d: [6, 7], e: 8, f: { a: ['a'], b: ['x'], c: ['c'] } },
201+
})
202+
);
203+
});
204+
});

0 commit comments

Comments
 (0)