Skip to content

Commit 450b586

Browse files
committed
fix(tests): Fix tests across many files
why you so broke synchronous pattern lab?
1 parent 38b96ca commit 450b586

17 files changed

+870
-826
lines changed

core/lib/decompose.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function expandPartials(foundPatternPartials, patternlab, currentPattern) {
2121

2222
// determine if the template contains any pattern parameters. if so they
2323
// must be immediately consumed
24-
parameter_hunter.find_parameters(currentPattern, patternlab).then(() => {
24+
return parameter_hunter.find_parameters(currentPattern, patternlab).then(() => {
2525

2626
//do something with the regular old partials
2727
for (var i = 0; i < foundPatternPartials.length; i++) {
@@ -37,20 +37,22 @@ function expandPartials(foundPatternPartials, patternlab, currentPattern) {
3737
}
3838
}
3939

40-
//recurse through nested partials to fill out this extended template.
41-
processRecursive(partialPath, patternlab);
42-
43-
//complete assembly of extended template
44-
//create a copy of the partial so as to not pollute it after the getPartial call.
45-
var partialPattern = getPartial(partial, patternlab);
46-
var cleanPartialPattern = jsonCopy(partialPattern, `partial pattern ${partial}`);
40+
console.log(processRecursive);
4741

48-
//if partial has style modifier data, replace the styleModifier value
49-
if (currentPattern.stylePartials && currentPattern.stylePartials.length > 0) {
50-
style_modifier_hunter.consume_style_modifier(cleanPartialPattern, foundPatternPartials[i], patternlab);
51-
}
42+
//recurse through nested partials to fill out this extended template.
43+
processRecursive(partialPath, patternlab).then(() => { //eslint-disable-line no-loop-func
44+
//complete assembly of extended template
45+
//create a copy of the partial so as to not pollute it after the getPartial call.
46+
var partialPattern = getPartial(partial, patternlab);
47+
var cleanPartialPattern = jsonCopy(partialPattern, `partial pattern ${partial}`);
48+
49+
//if partial has style modifier data, replace the styleModifier value
50+
if (currentPattern.stylePartials && currentPattern.stylePartials.length > 0) {
51+
style_modifier_hunter.consume_style_modifier(cleanPartialPattern, foundPatternPartials[i], patternlab);
52+
}
5253

53-
currentPattern.extendedTemplate = currentPattern.extendedTemplate.replace(foundPatternPartials[i], cleanPartialPattern.extendedTemplate);
54+
currentPattern.extendedTemplate = currentPattern.extendedTemplate.replace(foundPatternPartials[i], cleanPartialPattern.extendedTemplate);
55+
});
5456
}
5557
});
5658
}
@@ -94,9 +96,7 @@ module.exports = function (pattern, patternlab, ignoreLineage) {
9496

9597
//find pattern lineage
9698
if (!ignoreLineage) {
97-
lineagePromise.resolve(() => {
98-
lineage_hunter.find_lineage(pattern, patternlab);
99-
});
99+
lineagePromise = Promise.resolve(lineage_hunter.find_lineage(pattern, patternlab));
100100
} else {
101101
lineagePromise = Promise.resolve();
102102
}

core/lib/lineage_hunter.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
'use strict';
22
const extend = require("util")._extend;
3+
const getPartial = require('./get');
34
const logger = require('./log');
45

56
const lineage_hunter = function () {
67

78
function findlineage(pattern, patternlab) {
89

9-
const pa = require('./pattern_assembler');
10-
const pattern_assembler = new pa();
11-
1210
// As we are adding edges from pattern to ancestor patterns, ensure it is known to the graph
1311
patternlab.graph.add(pattern);
1412

@@ -18,7 +16,7 @@ const lineage_hunter = function () {
1816
if (matches !== null) {
1917
matches.forEach(function (match) {
2018
//get the ancestorPattern
21-
const ancestorPattern = pattern_assembler.getPartial(pattern.findPartial(match), patternlab);
19+
const ancestorPattern = getPartial(pattern.findPartial(match), patternlab);
2220

2321
if (ancestorPattern && pattern.lineageIndex.indexOf(ancestorPattern.patternPartial) === -1) {
2422
//add it since it didnt exist
@@ -99,8 +97,6 @@ const lineage_hunter = function () {
9997

10098
//find all lineage - patterns being consumed by this one
10199
for (let h = 0; h < lineage.length; h++) {
102-
// Not needed, the graph already knows the concrete pattern
103-
// let lineagePattern = pattern_assembler.getPartial(lineageIndex[h], patternlab);
104100
setPatternState('fromFuture', lineage[h], pattern, patternlab.graph);
105101
}
106102
}

core/lib/list_item_hunter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ const list_item_hunter = function () {
122122
}
123123

124124
//replace the block with our generated HTML
125-
const repeatingBlock = pattern.extendedTemplate.substring(pattern.extendedTemplate.indexOf(liMatch), pattern.extendedTemplate.indexOf(end) + end.length);
125+
//const repeatingBlock = pattern.extendedTemplate.substring(pattern.extendedTemplate.indexOf(liMatch), pattern.extendedTemplate.indexOf(end) + end.length);
126126

127127
//pattern.extendedTemplate = pattern.extendedTemplate.replace(repeatingBlock, repeatedBlockHtml); //todo
128128

core/lib/pattern_assembler.js

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const ch = require('./changes_hunter');
1212
const da = require('./data_loader');
1313
const addPattern = require('./addPattern');
1414
const parseLink = require('./parseLink');
15+
const readDocumentation = require('./readDocumentation');
1516

1617
const markdown_parser = new mp();
1718
const changes_hunter = new ch();
@@ -46,59 +47,6 @@ const pattern_assembler = function () {
4647
}
4748
}
4849

49-
function parsePatternMarkdown(currentPattern, patternlab) {
50-
51-
try {
52-
var markdownFileName = path.resolve(patternlab.config.paths.source.patterns, currentPattern.subdir, currentPattern.fileName + ".md");
53-
changes_hunter.checkLastModified(currentPattern, markdownFileName);
54-
55-
var markdownFileContents = fs.readFileSync(markdownFileName, 'utf8');
56-
57-
var markdownObject = markdown_parser.parse(markdownFileContents);
58-
if (!_.isEmpty(markdownObject)) {
59-
//set keys and markdown itself
60-
currentPattern.patternDescExists = true;
61-
currentPattern.patternDesc = markdownObject.markdown;
62-
63-
//Add all markdown to the currentPattern, including frontmatter
64-
currentPattern.allMarkdown = markdownObject;
65-
66-
//consider looping through all keys eventually. would need to blacklist some properties and whitelist others
67-
if (markdownObject.state) {
68-
currentPattern.patternState = markdownObject.state;
69-
}
70-
if (markdownObject.order) {
71-
currentPattern.order = markdownObject.order;
72-
}
73-
if (markdownObject.hidden) {
74-
currentPattern.hidden = markdownObject.hidden;
75-
}
76-
if (markdownObject.excludeFromStyleguide) {
77-
currentPattern.excludeFromStyleguide = markdownObject.excludeFromStyleguide;
78-
}
79-
if (markdownObject.tags) {
80-
currentPattern.tags = markdownObject.tags;
81-
}
82-
if (markdownObject.title) {
83-
currentPattern.patternName = markdownObject.title;
84-
}
85-
if (markdownObject.links) {
86-
currentPattern.links = markdownObject.links;
87-
}
88-
} else {
89-
logger.warning(`error processing markdown for ${currentPattern.patternPartial}`);
90-
}
91-
logger.debug(`found pattern-specific markdown for ${currentPattern.patternPartial}`);
92-
}
93-
catch (err) {
94-
// do nothing when file not found
95-
if (err.code !== 'ENOENT') {
96-
logger.warning(`'there was an error setting pattern keys after markdown parsing of the companion file for pattern ${currentPattern.patternPartial}`);
97-
logger.warning(err);
98-
}
99-
}
100-
}
101-
10250
// loads a pattern from disk, creates a Pattern object from it and
10351
// all its associated files, and records it in patternlab.patterns[]
10452
function loadPatternIterative(relPath, patternlab) {
@@ -199,7 +147,7 @@ const pattern_assembler = function () {
199147
}
200148

201149
//look for a markdown file for this template
202-
parsePatternMarkdown(currentPattern, patternlab);
150+
readDocumentation(currentPattern, patternlab);
203151

204152
//add the raw template to memory
205153
var templatePath = path.resolve(patternsPath, currentPattern.relPath);
@@ -304,9 +252,6 @@ const pattern_assembler = function () {
304252
},
305253
parse_data_links: function (patternlab) {
306254
parseDataLinks(patternlab);
307-
},
308-
parse_pattern_markdown: function (pattern, patternlab) {
309-
parsePatternMarkdown(pattern, patternlab);
310255
}
311256
};
312257

core/lib/pseudopattern_hunter.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ const _ = require('lodash');
77
const lh = require('./lineage_hunter');
88
const Pattern = require('./object_factory').Pattern;
99
const path = require('path');
10+
const addPattern = require('./addPattern');
1011
const logger = require('./log');
12+
const readDocumentation = require('./readDocumentation');
1113
const lineage_hunter = new lh();
1214
const changes_hunter = new ch();
1315

1416
const pseudopattern_hunter = function () {};
1517

1618
pseudopattern_hunter.prototype.find_pseudopatterns = function (currentPattern, patternlab) {
17-
const pa = require('./pattern_assembler');
18-
const pattern_assembler = new pa();
1919

2020
const paths = patternlab.config.paths;
2121

@@ -70,13 +70,13 @@ pseudopattern_hunter.prototype.find_pseudopatterns = function (currentPattern, p
7070
patternlab.graph.link(patternVariant, currentPattern);
7171

7272
//process the companion markdown file if it exists
73-
pattern_assembler.parse_pattern_markdown(patternVariant, patternlab);
73+
readDocumentation(patternVariant, patternlab);
7474

7575
//find pattern lineage
7676
lineage_hunter.find_lineage(patternVariant, patternlab);
7777

7878
//add to patternlab object so we can look these up later.
79-
pattern_assembler.addPattern(patternVariant, patternlab);
79+
addPattern(patternVariant, patternlab);
8080
}
8181

8282
// GTP: this is to emulate the behavior of the stale asynced

core/lib/readDocumentation.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"use strict";
2+
3+
const path = require('path');
4+
const _ = require('lodash');
5+
6+
const ch = require('./changes_hunter');
7+
const logger = require('./log');
8+
const mp = require('./markdown_parser');
9+
10+
const changes_hunter = new ch();
11+
const markdown_parser = new mp();
12+
13+
let fs = require('fs-extra'); //eslint-disable-line prefer-const
14+
15+
module.exports = function (pattern, patternlab) {
16+
17+
try {
18+
var markdownFileName = path.resolve(patternlab.config.paths.source.patterns, pattern.subdir, pattern.fileName + ".md");
19+
changes_hunter.checkLastModified(pattern, markdownFileName);
20+
21+
var markdownFileContents = fs.readFileSync(markdownFileName, 'utf8');
22+
23+
var markdownObject = markdown_parser.parse(markdownFileContents);
24+
if (!_.isEmpty(markdownObject)) {
25+
//set keys and markdown itself
26+
pattern.patternDescExists = true;
27+
pattern.patternDesc = markdownObject.markdown;
28+
29+
//Add all markdown to the pattern, including frontmatter
30+
pattern.allMarkdown = markdownObject;
31+
32+
//consider looping through all keys eventually. would need to blacklist some properties and whitelist others
33+
if (markdownObject.state) {
34+
pattern.patternState = markdownObject.state;
35+
}
36+
if (markdownObject.order) {
37+
pattern.order = markdownObject.order;
38+
}
39+
if (markdownObject.hidden) {
40+
pattern.hidden = markdownObject.hidden;
41+
}
42+
if (markdownObject.excludeFromStyleguide) {
43+
pattern.excludeFromStyleguide = markdownObject.excludeFromStyleguide;
44+
}
45+
if (markdownObject.tags) {
46+
pattern.tags = markdownObject.tags;
47+
}
48+
if (markdownObject.title) {
49+
pattern.patternName = markdownObject.title;
50+
}
51+
if (markdownObject.links) {
52+
pattern.links = markdownObject.links;
53+
}
54+
} else {
55+
logger.warning(`error processing markdown for ${pattern.patternPartial}`);
56+
}
57+
logger.debug(`found pattern-specific markdown for ${pattern.patternPartial}`);
58+
}
59+
catch (err) {
60+
// do nothing when file not found
61+
if (err.code !== 'ENOENT') {
62+
logger.warning(`'there was an error setting pattern keys after markdown parsing of the companion file for pattern ${pattern.patternPartial}`);
63+
logger.warning(err);
64+
}
65+
}
66+
67+
};

core/lib/ui_builder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const ae = require('./annotation_exporter');
99
const of = require('./object_factory');
1010
const Pattern = of.Pattern;
1111
const logger = require('./log');
12-
const renderSync = require('./lib/renderSync');
12+
const renderSync = require('./renderSync');
1313

1414
//these are mocked in unit tests, so let them be overridden
1515
let fs = require('fs-extra'); //eslint-disable-line prefer-const

test/addPattern_tests.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"use strict";
2+
3+
const tap = require('tap');
4+
5+
const addPattern = require('../core/lib/addPattern');
6+
var Pattern = require('../core/lib/object_factory').Pattern;
7+
const util = require('./util/test_utils.js');
8+
9+
const patterns_dir = './test/files/_patterns';
10+
11+
tap.test('addPattern - adds pattern extended template to patternlab partial object', function (test) {
12+
//arrange
13+
const patternlab = util.fakePatternLab(patterns_dir);
14+
15+
var pattern = new Pattern('00-test/01-bar.mustache');
16+
pattern.extendedTemplate = 'barExtended';
17+
pattern.template = 'bar';
18+
19+
//act
20+
addPattern(pattern, patternlab);
21+
22+
//assert
23+
test.equals(patternlab.patterns.length, 1);
24+
test.equals(patternlab.partials['test-bar'] !== undefined, true);
25+
test.equals(patternlab.partials['test-bar'], 'barExtended');
26+
test.end();
27+
});
28+
29+
tap.test('addPattern - adds pattern template to patternlab partial object if extendedtemplate does not exist yet', function (test){
30+
//arrange
31+
const patternlab = util.fakePatternLab(patterns_dir);
32+
33+
var pattern = new Pattern('00-test/01-bar.mustache');
34+
pattern.extendedTemplate = undefined;
35+
pattern.template = 'bar';
36+
37+
//act
38+
addPattern(pattern, patternlab);
39+
40+
//assert
41+
test.equals(patternlab.patterns.length, 1);
42+
test.equals(patternlab.partials['test-bar'] !== undefined, true);
43+
test.equals(patternlab.partials['test-bar'], 'bar');
44+
test.end();
45+
});

0 commit comments

Comments
 (0)