Skip to content

Commit c92aec4

Browse files
Merge pull request #523 from alexbenic/twig-tests
Convert engine_twig_tests.js to use tape
2 parents 64a21c9 + 24901ec commit c92aec4

File tree

1 file changed

+138
-130
lines changed

1 file changed

+138
-130
lines changed

test/engine_twig_tests.js

+138-130
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
"use strict";
22
/*eslint-disable dot-notation*/
3+
/*eslint-disable no-shadow*/
34

5+
var tap = require('tap');
46
var path = require('path');
57
var pa = require('../core/lib/pattern_assembler');
68
var Pattern = require('../core/lib/object_factory').Pattern;
79
var eol = require('os').EOL;
810

11+
// don't run these tests unless twig is installed
12+
var engineLoader = require('../core/lib/pattern_engines');
13+
if (!engineLoader.twig) {
14+
tap.test('Twig engine not installed, skipping tests.', function (test) {
15+
test.end();
16+
});
17+
return;
18+
}
19+
920
// fake pattern lab constructor:
1021
// sets up a fake patternlab object, which is needed by the pattern processing
1122
// apparatus.
@@ -34,7 +45,7 @@ function fakePatternLab() {
3445

3546
// function for testing sets of partials
3647
function testFindPartials(test, partialTests) {
37-
test.expect(partialTests.length + 1);
48+
test.plan(partialTests.length + 1);
3849

3950
// setup current pattern from what we would have during execution
4051
// docs on partial syntax are here:
@@ -56,136 +67,133 @@ function testFindPartials(test, partialTests) {
5667
test.equals(results[index], testString);
5768
});
5869

59-
test.done();
70+
test.end();
6071
}
6172

62-
exports['engine_twig'] = {
63-
'button twig pattern renders': function (test) {
64-
test.expect(1);
65-
66-
var patternPath = path.join('00-atoms', '00-general', '08-button.twig');
67-
var expectedValue = '<style>' + eol + ' .btn {' + eol + ' padding: 10px;' + eol + ' border-radius: 10px;' + eol + ' display: inline-block;' + eol + ' text-align: center;' + eol + ' }' + eol + '</style>' + eol + eol + '<a href="#" class="btn">Button</a>' + eol;
68-
69-
// do all the normal processing of the pattern
70-
var patternlab = new fakePatternLab();
71-
var assembler = new pa();
72-
var helloWorldPattern = assembler.process_pattern_iterative(patternPath, patternlab);
73-
assembler.process_pattern_recursive(patternPath, patternlab);
74-
75-
test.equals(helloWorldPattern.render(), expectedValue);
76-
test.done();
77-
},
78-
'media object twig pattern can see the atoms-button and atoms-image partials and renders them': function (test) {
79-
test.expect(1);
80-
81-
// pattern paths
82-
var buttonPatternPath = path.join('00-atoms', '00-general', '08-button.twig');
83-
var imagePatternPath = path.join('00-atoms', '00-general', '09-image.twig');
84-
var mediaObjectPatternPath = path.join('00-molecules', '00-general', '00-media-object.twig');
85-
86-
var expectedValue = '<style>\n .Media {\n display: flex;\n align-items: flex-start;\n }\n\n .Media > img {\n margin-right: 1em;\n max-width: 200px;\n }\n\n .Media-body {\n flex: 1;\n }\n</style>\n\n\n\n\n<div class="Media">\n <img src="http://placeholdit.imgix.net/~text?txtsize=33&txt=280%C3%97220&w=280&h=220&fm=pjpg"\n srcset="http://placeholdit.imgix.net/~text?txtsize=33&txt=280%C3%97220&w=280&h=220&fm=pjpg 280w,\n http://placeholdit.imgix.net/~text?txtsize=33&txt=560%C3%97440&w=560&h=440&fm=pjpg 560w,\n http://placeholdit.imgix.net/~text?txtsize=33&txt=840%C3%97660&w=840&h=660&fm=pjpg 840w"\n sizes="100vw">\n\n <style>\n .btn {\n padding: 10px;\n border-radius: 10px;\n display: inline-block;\n text-align: center;\n }\n</style>\n\n<a href="#" class="btn">Button</a>\n\n\n <div class="Media-body">\n\n \n \n\n <p>Oh, hello world!</p>\n </div>\n</div>\n';
87-
88-
// set up environment
89-
var patternlab = new fakePatternLab(); // environment
90-
var assembler = new pa();
91-
92-
// do all the normal processing of the pattern
93-
assembler.process_pattern_iterative(buttonPatternPath, patternlab);
94-
assembler.process_pattern_iterative(imagePatternPath, patternlab);
95-
var mediaObjectPattern = assembler.process_pattern_iterative(mediaObjectPatternPath, patternlab);
96-
assembler.process_pattern_recursive(buttonPatternPath, patternlab);
97-
assembler.process_pattern_recursive(imagePatternPath, patternlab);
98-
assembler.process_pattern_recursive(mediaObjectPatternPath, patternlab);
99-
100-
// test
101-
// this pattern is too long - so just remove line endings on both sides and compare output
102-
test.equals(mediaObjectPattern.render().replace(/\r?\n|\r/gm, ""), expectedValue.replace(/\r?\n|\r/gm, ""));
103-
test.done();
104-
},
105-
// 'twig partials can render JSON values': function (test) {
106-
// test.expect(1);
107-
108-
// // pattern paths
109-
// var pattern1Path = path.resolve(
110-
// testPatternsPath,
111-
// '00-atoms',
112-
// '00-global',
113-
// '00-helloworld-withdata.hbs'
114-
// );
115-
116-
// // set up environment
117-
// var patternlab = new fakePatternLab(); // environment
118-
// var assembler = new pa();
119-
120-
// // do all the normal processing of the pattern
121-
// var helloWorldWithData = assembler.process_pattern_iterative(pattern1Path, patternlab);
122-
// assembler.process_pattern_recursive(pattern1Path, patternlab);
123-
124-
// // test
125-
// test.equals(helloWorldWithData.render(), 'Hello world!\nYeah, we got the subtitle from the JSON.\n');
126-
// test.done();
127-
// },
128-
// 'twig partials use the JSON environment from the calling pattern and can accept passed parameters': function (test) {
129-
// test.expect(1);
130-
131-
// // pattern paths
132-
// var atomPath = path.resolve(
133-
// testPatternsPath,
134-
// '00-atoms',
135-
// '00-global',
136-
// '00-helloworld-withdata.hbs'
137-
// );
138-
// var molPath = path.resolve(
139-
// testPatternsPath,
140-
// '00-molecules',
141-
// '00-global',
142-
// '00-call-atom-with-molecule-data.hbs'
143-
// );
144-
145-
// // set up environment
146-
// var patternlab = new fakePatternLab(); // environment
147-
// var assembler = new pa();
148-
149-
// // do all the normal processing of the pattern
150-
// var atom = assembler.process_pattern_iterative(atomPath, patternlab);
151-
// var mol = assembler.process_pattern_iterative(molPath, patternlab);
152-
// assembler.process_pattern_recursive(atomPath, patternlab);
153-
// assembler.process_pattern_recursive(molPath, patternlab);
154-
155-
// // test
156-
// test.equals(mol.render(), '<h2>Call with default JSON environment:</h2>\nThis is Hello world!\nfrom the default JSON.\n\n\n<h2>Call with passed parameter:</h2>\nHowever, this is Hello world!\nfrom a totally different blob.\n\n');
157-
// test.done();
158-
// },
159-
'find_pattern_partials finds partials': function (test) {
160-
testFindPartials(test, [
161-
'{% include "atoms-image" %}',
162-
"{% include 'atoms-image' %}",
163-
"{%include 'atoms-image'%}",
164-
"{% include 'molecules-template' only %}",
165-
"{% include 'organisms-sidebar' ignore missing %}",
166-
"{% include 'organisms-sidebar' ignore missing only %}"
167-
]);
168-
},
169-
'find_pattern_partials finds verbose partials': function (test) {
170-
testFindPartials(test, [
171-
"{% include '01-molecules/06-components/03-comment-header.twig' %}",
172-
"{% include '00-atoms/00-global/06-test' %}"
173-
]);
174-
},
175-
'find_pattern_partials finds partials with twig parameters': function (test) {
176-
testFindPartials(test, [
177-
"{% include 'molecules-template' with {'foo': 'bar'} %}",
178-
"{% include 'molecules-template' with vars %}",
179-
"{% include 'molecules-template.twig' with {'foo': 'bar'} only %}",
180-
"{% include 'organisms-sidebar' ignore missing with {'foo': 'bar'} %}"
181-
]);
182-
}
183-
};
73+
tap.test('button twig pattern renders', function (test) {
74+
test.plan(1);
75+
76+
var patternPath = path.join('00-atoms', '00-general', '08-button.twig');
77+
var expectedValue = '<style>' + eol + ' .btn {' + eol + ' padding: 10px;' + eol + ' border-radius: 10px;' + eol + ' display: inline-block;' + eol + ' text-align: center;' + eol + ' }' + eol + '</style>' + eol + eol + '<a href="#" class="btn">Button</a>' + eol;
78+
79+
// do all the normal processing of the pattern
80+
var patternlab = new fakePatternLab();
81+
var assembler = new pa();
82+
var helloWorldPattern = assembler.process_pattern_iterative(patternPath, patternlab);
83+
assembler.process_pattern_recursive(patternPath, patternlab);
84+
85+
test.equals(helloWorldPattern.render(), expectedValue);
86+
test.end();
87+
});
88+
89+
tap.test('media object twig pattern can see the atoms-button and atoms-image partials and renders them', function (test) {
90+
test.plan(1);
91+
92+
// pattern paths
93+
var buttonPatternPath = path.join('00-atoms', '00-general', '08-button.twig');
94+
var imagePatternPath = path.join('00-atoms', '00-general', '09-image.twig');
95+
var mediaObjectPatternPath = path.join('00-molecules', '00-general', '00-media-object.twig');
96+
97+
var expectedValue = '<style>\n .Media {\n display: flex;\n align-items: flex-start;\n }\n\n .Media > img {\n margin-right: 1em;\n max-width: 200px;\n }\n\n .Media-body {\n flex: 1;\n }\n</style>\n\n\n\n\n<div class="Media">\n <img src="http://placeholdit.imgix.net/~text?txtsize=33&txt=280%C3%97220&w=280&h=220&fm=pjpg"\n srcset="http://placeholdit.imgix.net/~text?txtsize=33&txt=280%C3%97220&w=280&h=220&fm=pjpg 280w,\n http://placeholdit.imgix.net/~text?txtsize=33&txt=560%C3%97440&w=560&h=440&fm=pjpg 560w,\n http://placeholdit.imgix.net/~text?txtsize=33&txt=840%C3%97660&w=840&h=660&fm=pjpg 840w"\n sizes="100vw">\n\n <style>\n .btn {\n padding: 10px;\n border-radius: 10px;\n display: inline-block;\n text-align: center;\n }\n</style>\n\n<a href="#" class="btn">Button</a>\n\n\n <div class="Media-body">\n\n \n \n\n <p>Oh, hello world!</p>\n </div>\n</div>\n';
98+
99+
// set up environment
100+
var patternlab = new fakePatternLab(); // environment
101+
var assembler = new pa();
102+
103+
// do all the normal processing of the pattern
104+
assembler.process_pattern_iterative(buttonPatternPath, patternlab);
105+
assembler.process_pattern_iterative(imagePatternPath, patternlab);
106+
var mediaObjectPattern = assembler.process_pattern_iterative(mediaObjectPatternPath, patternlab);
107+
assembler.process_pattern_recursive(buttonPatternPath, patternlab);
108+
assembler.process_pattern_recursive(imagePatternPath, patternlab);
109+
assembler.process_pattern_recursive(mediaObjectPatternPath, patternlab);
110+
111+
// test
112+
// this pattern is too long - so just remove line endings on both sides and compare output
113+
test.equals(mediaObjectPattern.render().replace(/\r?\n|\r/gm, ""), expectedValue.replace(/\r?\n|\r/gm, ""));
114+
test.end();
115+
});
116+
117+
tap.test('twig partials can render JSON values', {skip: true}, function (test) {
118+
test.plan(1);
119+
120+
// pattern paths
121+
var pattern1Path = path.resolve(
122+
testPatternsPath,
123+
'00-atoms',
124+
'00-global',
125+
'00-helloworld-withdata.hbs'
126+
);
184127

128+
// set up environment
129+
var patternlab = new fakePatternLab(); // environment
130+
var assembler = new pa();
131+
132+
// do all the normal processing of the pattern
133+
var helloWorldWithData = assembler.process_pattern_iterative(pattern1Path, patternlab);
134+
assembler.process_pattern_recursive(pattern1Path, patternlab);
135+
136+
// test
137+
test.equals(helloWorldWithData.render(), 'Hello world!\nYeah, we got the subtitle from the JSON.\n');
138+
test.end();
139+
});
140+
141+
tap.test('twig partials use the JSON environment from the calling pattern and can accept passed parameters', {skip: true}, function (test) {
142+
test.plan(1);
143+
144+
// pattern paths
145+
var atomPath = path.resolve(
146+
testPatternsPath,
147+
'00-atoms',
148+
'00-global',
149+
'00-helloworld-withdata.hbs'
150+
);
151+
var molPath = path.resolve(
152+
testPatternsPath,
153+
'00-molecules',
154+
'00-global',
155+
'00-call-atom-with-molecule-data.hbs'
156+
);
157+
158+
// set up environment
159+
var patternlab = new fakePatternLab(); // environment
160+
var assembler = new pa();
161+
162+
// do all the normal processing of the pattern
163+
var atom = assembler.process_pattern_iterative(atomPath, patternlab);
164+
var mol = assembler.process_pattern_iterative(molPath, patternlab);
165+
assembler.process_pattern_recursive(atomPath, patternlab);
166+
assembler.process_pattern_recursive(molPath, patternlab);
167+
168+
// test
169+
test.equals(mol.render(), '<h2>Call with default JSON environment:</h2>\nThis is Hello world!\nfrom the default JSON.\n\n\n<h2>Call with passed parameter:</h2>\nHowever, this is Hello world!\nfrom a totally different blob.\n\n');
170+
test.end();
171+
});
172+
173+
tap.test('find_pattern_partials finds partials', function (test) {
174+
testFindPartials(test, [
175+
'{% include "atoms-image" %}',
176+
"{% include 'atoms-image' %}",
177+
"{%include 'atoms-image'%}",
178+
"{% include 'molecules-template' only %}",
179+
"{% include 'organisms-sidebar' ignore missing %}",
180+
"{% include 'organisms-sidebar' ignore missing only %}"
181+
]);
182+
});
183+
184+
tap.test('find_pattern_partials finds verbose partials', function (test) {
185+
testFindPartials(test, [
186+
"{% include '01-molecules/06-components/03-comment-header.twig' %}",
187+
"{% include '00-atoms/00-global/06-test' %}"
188+
]);
189+
});
190+
191+
tap.test('find_pattern_partials finds partials with twig parameters', function (test) {
192+
testFindPartials(test, [
193+
"{% include 'molecules-template' with {'foo': 'bar'} %}",
194+
"{% include 'molecules-template' with vars %}",
195+
"{% include 'molecules-template.twig' with {'foo': 'bar'} only %}",
196+
"{% include 'organisms-sidebar' ignore missing with {'foo': 'bar'} %}"
197+
]);
198+
});
185199

186-
// don't run these tests unless twig is installed
187-
var engineLoader = require('../core/lib/pattern_engines');
188-
if (!engineLoader.twig) {
189-
console.log("Twig engine not installed, skipping tests.");
190-
delete exports.engine_twig;
191-
}

0 commit comments

Comments
 (0)