Skip to content

Commit be1bbdf

Browse files
Fix patterns are not sorted, when there is a nested pattern (#1283)
* Fix patterns are not sorted, when there is a nested pattern * #1283 Add order test cases
1 parent e40c454 commit be1bbdf

File tree

10 files changed

+79
-3
lines changed

10 files changed

+79
-3
lines changed

packages/core/src/lib/readDocumentation.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ module.exports = function(pattern, patternlab, isVariant) {
8383
// Read Documentation for Pattern-Group
8484
// Use this approach, since pattern lab is a pattern driven software
8585
try {
86+
const groupRelPath = pattern.relPath.split(path.sep);
8687
const markdownFileNameGroup = path.resolve(
8788
patternlab.config.paths.source.patterns,
88-
path.parse(pattern.subdir).dir || pattern.subdir,
89+
groupRelPath[0] || pattern.subdir,
8990
GROUP_DOC_PREFIX + pattern.patternGroup + FILE_EXTENSION
9091
);
9192
const markdownFileContentsGroup = fs.readFileSync(
@@ -115,10 +116,11 @@ module.exports = function(pattern, patternlab, isVariant) {
115116

116117
// Read Documentation for Pattern-Subgroup
117118
try {
119+
const subgroupRelPath = pattern.relPath.split(path.sep);
118120
const markdownFileNameSubgroup = path.resolve(
119121
patternlab.config.paths.source.patterns,
120-
path.parse(pattern.subdir).dir,
121-
path.parse(pattern.subdir).base,
122+
subgroupRelPath[0],
123+
subgroupRelPath[1],
122124
GROUP_DOC_PREFIX + pattern.patternSubgroup + FILE_EXTENSION
123125
);
124126
const markdownFileContentsSubgroup = fs.readFileSync(
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
order: 1
3+
---

packages/core/test/files/_patterns/orderTest/a/a-test.mustache

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
order: 2
3+
---

packages/core/test/files/_patterns/orderTest/b/b-test.mustache

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
order: -1
3+
---

packages/core/test/files/_patterns/orderTest/c/c-test.mustache

Whitespace-only changes.

packages/core/test/files/_patterns/orderTest/c/subfolder/subfolder.mustache

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
order: 1
3+
---

packages/core/test/loadPattern_tests.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,65 @@ tap.test(
111111
test.end();
112112
}
113113
);
114+
115+
tap.test(
116+
'loadPattern - group and subgroup ordering will be taken from markdown files',
117+
test => {
118+
//arrange
119+
const patternlab = util.fakePatternLab(patterns_dir);
120+
121+
const basePatternAPath = path.join('orderTest', 'a', 'a-test.mustache');
122+
const basePatternBPath = path.join('orderTest', 'b', 'b-test.mustache');
123+
const basePatternCPath = path.join(
124+
'orderTest',
125+
'c',
126+
'subfolder',
127+
'subfolder.mustache'
128+
);
129+
130+
//act
131+
const resultPatternA = loadPattern(basePatternAPath, patternlab);
132+
const resultPatternB = loadPattern(basePatternBPath, patternlab);
133+
const resultPatternC = loadPattern(basePatternCPath, patternlab);
134+
135+
//assert
136+
console.log(resultPatternA.patternGroupData.order);
137+
test.same(
138+
resultPatternA.patternGroupData.order,
139+
1,
140+
'Pattern group should be loaded as 1'
141+
);
142+
console.log(resultPatternA.patternSubgroupData.order || 0);
143+
test.same(
144+
resultPatternA.patternSubgroupData.order || 0,
145+
0,
146+
'Pattern Subgroup not be availabe and default to 0'
147+
);
148+
149+
console.log(resultPatternB.patternGroupData.order);
150+
test.same(
151+
resultPatternB.patternGroupData.order,
152+
1,
153+
'Pattern group should be loaded as 1'
154+
);
155+
console.log(resultPatternB.patternSubgroupData.order);
156+
test.same(
157+
resultPatternB.patternSubgroupData.order,
158+
2,
159+
'Pattern Subgroup should be loaded as 2'
160+
);
161+
162+
test.same(
163+
resultPatternC.patternGroupData.order,
164+
1,
165+
'Pattern group should be loaded as 1'
166+
);
167+
test.same(
168+
resultPatternC.patternSubgroupData.order,
169+
-1,
170+
'Pattern Subgroup should be loaded as -1'
171+
);
172+
173+
test.end();
174+
}
175+
);

0 commit comments

Comments
 (0)