Skip to content

Commit c50484d

Browse files
committed
fix(unit tests): Fix more tests. Since we no longer render, no need to look for escaped values
1 parent 4072b61 commit c50484d

File tree

7 files changed

+50
-78
lines changed

7 files changed

+50
-78
lines changed

core/lib/replaceParameter.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,27 @@ module.exports = function (template, prop, data) {
1212
if (typeof data === 'boolean') {
1313
const startRE = new RegExp(`{{\\s?#[${prop}]+\\s?}}`);
1414
const endRE = new RegExp(`{{\\s?/[${prop}]+\\s?}}`);
15+
16+
const bIdx = t.search(startRE);
17+
const eIdxStart = t.search(endRE);
18+
19+
// try to determine if this is a {{#section}}
20+
// if it is, this looks like a boolean value meant to be a mere {{value}}
21+
if (bIdx === -1) {
22+
return t.replace(`{{${prop}}}`, data);
23+
}
24+
1525
if (data) {
1626
t = t.replace(startRE, '');
1727
t = t.replace(endRE, '');
1828
} else {
19-
const bIdx = t.search(startRE);
20-
const eIdxStart = t.search(endRE);
29+
// data is falsey
2130
const eIdxEnd = t.indexOf('}}', eIdxStart) + 2;
2231
t = t.substring(0, bIdx) + t.substring(eIdxEnd, t.length);
2332
}
2433
return t;
2534
}
2635

27-
console.log(`Could not replace ${prop} with ${data} inside ${template}`);
2836
logger.warning(`Could not replace ${prop} with ${data} inside ${template}`);
2937
return t;
3038
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>{{{ tag1 }}}</p><p>{{{ tag2 }}}</p><p>{{{ tag3 }}}</p>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{> test-link(url: 'link.test-comment') }}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{> test-comment-tag(tag1: '<strong>Single-quoted</strong>', tag2: \"<em>Double-quoted</em>\", tag3: '<strong class=\\\"foo\\\" id=\\\'bar\\\'>With attributes</strong>') }}

test/index_tests.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ tap.test('getDefaultConfig - should return the default config object', function
4545
test.end();
4646
});
4747

48-
tap.test('buildPatterns', function () {
48+
tap.only('buildPatterns', function () {
4949
//arrange
5050

5151
var patternExporterMock = {
@@ -63,7 +63,7 @@ tap.test('buildPatterns', function () {
6363
test.end();
6464
});
6565

66-
tap.test('parameter hunter finds partials with their own parameters and renders them too', function (test) {
66+
tap.test('finds partials with their own parameters and renders them too', function (test) {
6767
var pattern = get('test-c', patternlab);
6868
test.equals(util.sanitized(pattern.extendedTemplate), util.sanitized(`<b>c</b>
6969
<b>b</b>
@@ -73,11 +73,24 @@ tap.test('buildPatterns', function () {
7373
test.end();
7474
});
7575

76-
tap.test('parameter hunter finds and extends templates with mixed parameter and global data', function (test) {
76+
tap.test('finds and extends templates with mixed parameter and global data', function (test) {
7777
var pattern = get('test-sticky-comment', patternlab);
7878
test.equals(util.sanitized(pattern.patternPartialCode), util.sanitized(`<h1>Bar</h1><p>A life is like a garden. Perfect moments can be had, but not preserved, except in memory.</p>`));
7979
test.end();
8080
});
81+
82+
tap.test('expands links inside parameters', function (test) {
83+
var pattern = get('test-linkInParameter', patternlab);
84+
test.equals(util.sanitized(pattern.patternPartialCode), util.sanitized(`<a href="/patterns/00-test-comment/00-test-comment.rendered.html">Cool Dude</a>`));
85+
test.end();
86+
});
87+
88+
// From issue #145 https://github.com/pattern-lab/patternlab-node/issues/145
89+
tap.test(' parses parameters containing html tags', function (test) {
90+
var pattern = get('test-parameterTags', patternlab);
91+
test.equals(util.sanitized(pattern.patternPartialCode), util.sanitized(`<p><strong>Single-quoted</strong></p><p><em>Double-quoted</em></p><p><strong class="foo" id=\'bar\'>With attributes</strong></p>`));
92+
test.end();
93+
});
8194
}
8295
};
8396

test/parameter_hunter_tests.js

Lines changed: 5 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ tap.test('parameter hunter parses parameters with unquoted keys and unquoted val
8686
//act
8787
parameter_hunter.find_parameters(testPattern, pl).then(() => {
8888
//assert
89-
test.equals(util.sanitized(testPattern.extendedTemplate), util.sanitized('<h1></h1><p>true</p>'));
89+
test.equals(util.sanitized(testPattern.extendedTemplate), util.sanitized('<h1>{{foo}}</h1><p>true</p>'));
9090
test.end();
9191
});
9292
});
@@ -173,7 +173,7 @@ tap.test('parameter hunter parses parameters with single-quoted keys and single-
173173
//act
174174
parameter_hunter.find_parameters(testPattern, pl).then(() => {
175175
//assert
176-
test.equals(util.sanitized(testPattern.extendedTemplate), util.sanitized('<h1>{{foo}}</h1><p>true not,&#39;true&#39;</p>'));
176+
test.equals(util.sanitized(testPattern.extendedTemplate), util.sanitized(`<h1>{{foo}}</h1><p>true not,'true'</p>`));
177177
test.end();
178178
});
179179
});
@@ -201,7 +201,7 @@ tap.test('parameter hunter parses parameters with single-quoted keys and double-
201201
//act
202202
parameter_hunter.find_parameters(testPattern, pl).then(() => {
203203
//assert
204-
test.equals(util.sanitized(testPattern.extendedTemplate), util.sanitized('<h1>{{foo}}</h1><p>true not:&#39;true&#39;</p>'));
204+
test.equals(util.sanitized(testPattern.extendedTemplate), util.sanitized(`<h1>{{foo}}</h1><p>true not:'true'</p>`));
205205
test.end();
206206
});
207207
});
@@ -257,7 +257,7 @@ tap.test('parameter hunter parses parameters with double-quoted keys and single-
257257
//act
258258
parameter_hunter.find_parameters(testPattern, pl).then(() => {
259259
//assert
260-
test.equals(util.sanitized(testPattern.extendedTemplate), util.sanitized('<h1>{{foo}}</h1><p>true not{&quot;true&quot;</p>'));
260+
test.equals(util.sanitized(testPattern.extendedTemplate), util.sanitized('<h1>{{foo}}</h1><p>true not{"true"</p>'));
261261
test.end();
262262
});
263263
});
@@ -286,7 +286,7 @@ tap.test('parameter hunter parses parameters with double-quoted keys and double-
286286
//act
287287
parameter_hunter.find_parameters(testPattern, pl).then(() => {
288288
//assert
289-
test.equals(util.sanitized(testPattern.extendedTemplate), util.sanitized('<h1>{{foo}}</h1><p>true not}&quot;true&quot;</p>'));
289+
test.equals(util.sanitized(testPattern.extendedTemplate), util.sanitized('<h1>{{foo}}</h1><p>true not}"true"</p>'));
290290
test.end();
291291
});
292292
});
@@ -378,67 +378,3 @@ tap.test('parameter hunter skips malformed parameters', function (test) {
378378
});
379379
});
380380
});
381-
382-
// From issue #145 https://github.com/pattern-lab/patternlab-node/issues/145
383-
tap.only('parameter hunter parses parameters containing html tags', function (test){
384-
385-
const pl = util.fakePatternLab(testPatternsPath);
386-
387-
var commentPath = path.join('00-test', 'comment.mustache');
388-
var commentPattern = loadPattern(commentPath, pl);
389-
390-
var testPatternPath = path.join('00-test', 'sticky-comment.mustache');
391-
var testPattern = loadPattern(testPatternPath, pl);
392-
393-
//override the commentTemplate - i dont really want to create another file
394-
pl.patterns[0].template = "<p>{{{ tag1 }}}</p><p>{{{ tag2 }}}</p><p>{{{ tag3 }}}</p>";
395-
pl.patterns[0].extendedTemplate = pl.patterns[0].template;
396-
397-
//override the file
398-
testPattern.template = "{{> test-comment(tag1: '<strong>Single-quoted</strong>', tag2: \"<em>Double-quoted</em>\", tag3: '<strong class=\\\"foo\\\" id=\\\'bar\\\'>With attributes</strong>') }}";
399-
testPattern.extendedTemplate = testPattern.template;
400-
testPattern.parameteredPartials[0] = testPattern.template;
401-
402-
var p1 = processIterative(commentPattern, pl);
403-
var p2 = processIterative(testPattern, pl);
404-
405-
Promise.all([p1, p2]).then(() => {
406-
//act
407-
parameter_hunter.find_parameters(testPattern, pl).then(() => {
408-
//assert
409-
test.equals(util.sanitized(testPattern.extendedTemplate), util.sanitized('<p><strong>Single-quoted</strong></p><p><em>Double-quoted</em></p><p><strong class="foo" id=\'bar\'>With attributes</strong></p>'));
410-
test.end();
411-
});
412-
});
413-
});
414-
415-
tap.only('parameter hunter expands links inside parameters', function (test) {
416-
const pl = util.fakePatternLab(testPatternsPath);
417-
418-
var commentPath = path.join('00-test', 'comment.mustache');
419-
var commentPattern = loadPattern(commentPath, pl);
420-
421-
var testPatternPath = path.join('00-test', 'sticky-comment.mustache');
422-
var testPattern = loadPattern(testPatternPath, pl);
423-
424-
//override the commentTemplate - i dont really want to create another file
425-
pl.patterns[0].template = '<a href="{{{ url }}}">{{ description }}</a>';
426-
pl.patterns[0].extendedTemplate = pl.patterns[0].template;
427-
428-
//override the file
429-
testPattern.template = "{{> test-comment(url: 'link.test-comment', description: 'Link to single comment') }}";
430-
testPattern.extendedTemplate = testPattern.template;
431-
testPattern.parameteredPartials[0] = testPattern.template;
432-
433-
var p1 = processIterative(commentPattern, pl);
434-
var p2 = processIterative(testPattern, pl);
435-
436-
Promise.all([p1, p2]).then(() => {
437-
//act
438-
parameter_hunter.find_parameters(testPattern, pl).then(() => {
439-
//assert
440-
test.equals(util.sanitized(testPattern.extendedTemplate), util.sanitized('<a href="/patterns/00-test-comment/00-test-comment.rendered.html">Link to single comment</a>'));
441-
test.end();
442-
});
443-
});
444-
});

test/replaceParameter_tests.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,31 @@ tap.test('replaces simple value', function (test) {
1212
test.end();
1313
});
1414

15-
tap.test('replaces boolean true', function (test) {
15+
tap.test('replaces simple boolean true value', function (test) {
16+
const result = replaceParameter('{{key}}', 'key', true);
17+
test.equals(result, 'true');
18+
test.end();
19+
});
20+
21+
tap.test('replaces simple boolean false value', function (test) {
22+
const result = replaceParameter('{{key}}', 'key', false);
23+
test.equals(result, 'false');
24+
test.end();
25+
});
26+
27+
tap.test('replaces boolean true section', function (test) {
1628
const result = replaceParameter('1{{#key}}value{{/key}}2', 'key', true);
1729
test.equals(result, '1value2');
1830
test.end();
1931
});
2032

21-
tap.only('replaces boolean true with spaces', function (test) {
33+
tap.only('replaces boolean true section with spaces', function (test) {
2234
const result = replaceParameter('1{{ #key }}value{{ /key }}2', 'key', true);
2335
test.equals(result, '1value2');
2436
test.end();
2537
});
2638

27-
tap.test('replaces boolean false', function (test) {
39+
tap.test('replaces boolean section false', function (test) {
2840
const result = replaceParameter('1{{#key}}value{{/key}}2', 'key', false);
2941
test.equals(result, '12');
3042
test.end();

0 commit comments

Comments
 (0)