11"use strict" ;
22/*eslint-disable dot-notation*/
3+ /*eslint-disable no-shadow*/
34
5+ var test = require ( 'tape' ) ;
46var path = require ( 'path' ) ;
57var pa = require ( '../core/lib/pattern_assembler' ) ;
68var Pattern = require ( '../core/lib/object_factory' ) . Pattern ;
79var 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+ test . only ( 'Twig engine not installed, skipping tests.' , function ( test ) {
15+ test . end ( ) ;
16+ } ) ;
17+ }
18+
919// fake pattern lab constructor:
1020// sets up a fake patternlab object, which is needed by the pattern processing
1121// apparatus.
@@ -34,7 +44,7 @@ function fakePatternLab() {
3444
3545// function for testing sets of partials
3646function testFindPartials ( test , partialTests ) {
37- test . expect ( partialTests . length + 1 ) ;
47+ test . plan ( partialTests . length + 1 ) ;
3848
3949 // setup current pattern from what we would have during execution
4050 // docs on partial syntax are here:
@@ -56,136 +66,133 @@ function testFindPartials(test, partialTests) {
5666 test . equals ( results [ index ] , testString ) ;
5767 } ) ;
5868
59- test . done ( ) ;
69+ test . end ( ) ;
6070}
6171
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- } ;
72+ test ( 'button twig pattern renders' , function ( test ) {
73+ test . plan ( 1 ) ;
74+
75+ var patternPath = path . join ( '00-atoms' , '00-general' , '08-button.twig' ) ;
76+ 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 ;
77+
78+ // do all the normal processing of the pattern
79+ var patternlab = new fakePatternLab ( ) ;
80+ var assembler = new pa ( ) ;
81+ var helloWorldPattern = assembler . process_pattern_iterative ( patternPath , patternlab ) ;
82+ assembler . process_pattern_recursive ( patternPath , patternlab ) ;
83+
84+ test . equals ( helloWorldPattern . render ( ) , expectedValue ) ;
85+ test . end ( ) ;
86+ } ) ;
87+
88+ test ( 'media object twig pattern can see the atoms-button and atoms-image partials and renders them' , function ( test ) {
89+ test . plan ( 1 ) ;
90+
91+ // pattern paths
92+ var buttonPatternPath = path . join ( '00-atoms' , '00-general' , '08-button.twig' ) ;
93+ var imagePatternPath = path . join ( '00-atoms' , '00-general' , '09-image.twig' ) ;
94+ var mediaObjectPatternPath = path . join ( '00-molecules' , '00-general' , '00-media-object.twig' ) ;
95+
96+ 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' ;
97+
98+ // set up environment
99+ var patternlab = new fakePatternLab ( ) ; // environment
100+ var assembler = new pa ( ) ;
101+
102+ // do all the normal processing of the pattern
103+ assembler . process_pattern_iterative ( buttonPatternPath , patternlab ) ;
104+ assembler . process_pattern_iterative ( imagePatternPath , patternlab ) ;
105+ var mediaObjectPattern = assembler . process_pattern_iterative ( mediaObjectPatternPath , patternlab ) ;
106+ assembler . process_pattern_recursive ( buttonPatternPath , patternlab ) ;
107+ assembler . process_pattern_recursive ( imagePatternPath , patternlab ) ;
108+ assembler . process_pattern_recursive ( mediaObjectPatternPath , patternlab ) ;
109+
110+ // test
111+ // this pattern is too long - so just remove line endings on both sides and compare output
112+ test . equals ( mediaObjectPattern . render ( ) . replace ( / \r ? \n | \r / gm, "" ) , expectedValue . replace ( / \r ? \n | \r / gm, "" ) ) ;
113+ test . end ( ) ;
114+ } ) ;
115+
116+ test . skip ( 'twig partials can render JSON values' , function ( test ) {
117+ test . plan ( 1 ) ;
118+
119+ // pattern paths
120+ var pattern1Path = path . resolve (
121+ testPatternsPath ,
122+ '00-atoms' ,
123+ '00-global' ,
124+ '00-helloworld-withdata.hbs'
125+ ) ;
184126
127+ // set up environment
128+ var patternlab = new fakePatternLab ( ) ; // environment
129+ var assembler = new pa ( ) ;
130+
131+ // do all the normal processing of the pattern
132+ var helloWorldWithData = assembler . process_pattern_iterative ( pattern1Path , patternlab ) ;
133+ assembler . process_pattern_recursive ( pattern1Path , patternlab ) ;
134+
135+ // test
136+ test . equals ( helloWorldWithData . render ( ) , 'Hello world!\nYeah, we got the subtitle from the JSON.\n' ) ;
137+ test . end ( ) ;
138+ } ) ;
139+
140+ test . skip ( 'twig partials use the JSON environment from the calling pattern and can accept passed parameters' , function ( test ) {
141+ test . plan ( 1 ) ;
142+
143+ // pattern paths
144+ var atomPath = path . resolve (
145+ testPatternsPath ,
146+ '00-atoms' ,
147+ '00-global' ,
148+ '00-helloworld-withdata.hbs'
149+ ) ;
150+ var molPath = path . resolve (
151+ testPatternsPath ,
152+ '00-molecules' ,
153+ '00-global' ,
154+ '00-call-atom-with-molecule-data.hbs'
155+ ) ;
156+
157+ // set up environment
158+ var patternlab = new fakePatternLab ( ) ; // environment
159+ var assembler = new pa ( ) ;
160+
161+ // do all the normal processing of the pattern
162+ var atom = assembler . process_pattern_iterative ( atomPath , patternlab ) ;
163+ var mol = assembler . process_pattern_iterative ( molPath , patternlab ) ;
164+ assembler . process_pattern_recursive ( atomPath , patternlab ) ;
165+ assembler . process_pattern_recursive ( molPath , patternlab ) ;
166+
167+ // test
168+ 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' ) ;
169+ test . end ( ) ;
170+ } ) ;
171+
172+ test ( 'find_pattern_partials finds partials' , function ( test ) {
173+ testFindPartials ( test , [
174+ '{% include "atoms-image" %}' ,
175+ "{% include 'atoms-image' %}" ,
176+ "{%include 'atoms-image'%}" ,
177+ "{% include 'molecules-template' only %}" ,
178+ "{% include 'organisms-sidebar' ignore missing %}" ,
179+ "{% include 'organisms-sidebar' ignore missing only %}"
180+ ] ) ;
181+ } ) ;
182+
183+ test ( 'find_pattern_partials finds verbose partials' , function ( test ) {
184+ testFindPartials ( test , [
185+ "{% include '01-molecules/06-components/03-comment-header.twig' %}" ,
186+ "{% include '00-atoms/00-global/06-test' %}"
187+ ] ) ;
188+ } ) ;
189+
190+ test ( 'find_pattern_partials finds partials with twig parameters' , function ( test ) {
191+ testFindPartials ( test , [
192+ "{% include 'molecules-template' with {'foo': 'bar'} %}" ,
193+ "{% include 'molecules-template' with vars %}" ,
194+ "{% include 'molecules-template.twig' with {'foo': 'bar'} only %}" ,
195+ "{% include 'organisms-sidebar' ignore missing with {'foo': 'bar'} %}"
196+ ] ) ;
197+ } ) ;
185198
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