Skip to content

Commit 54369c1

Browse files
committed
basic unit test coverage for list_item_hunter
1 parent b3f5969 commit 54369c1

File tree

3 files changed

+162
-6
lines changed

3 files changed

+162
-6
lines changed

builder/list_item_hunter.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,17 @@
4949
var thisBlockTemplate = repeatedBlockTemplate[i];
5050
var thisBlockHTML = "";
5151

52+
//check for a local listitems.json file
53+
// var patternSpecificListJson = {};
54+
// try {
55+
// patternSpecificListJson = abspath.substr(0, abspath.lastIndexOf(".")) + "listitems.json";
56+
// currentPattern.jsonFileData = fs.readJSONSync(jsonFilename);
57+
// }
58+
// catch(e) {
59+
// }
60+
5261
//combine listItem data with pattern data with global data
53-
var itemData = patternlab.listitems['' + items.indexOf(loopNumberString)]; //this is a propety
62+
var itemData = patternlab.listitems['' + items.indexOf(loopNumberString)]; //this is a property
5463
var globalData = JSON.parse(JSON.stringify(patternlab.data));
5564
var localData = JSON.parse(JSON.stringify(pattern.jsonFileData));
5665

@@ -61,19 +70,16 @@
6170
//check for partials within the repeated block
6271
var foundPartials = pattern_assembler.find_pattern_partials({ 'template' : thisBlockTemplate });
6372

64-
if(foundPartials.length > 0){
73+
if(foundPartials && foundPartials.length > 0){
6574

6675
for(var j = 0; j < foundPartials.length; j++){
67-
//add the lineage once for the parent pattern
68-
//TODO
6976

7077
//get the partial
7178
var partialName = foundPartials[j].match(/([a-z-]+)/ig)[0];
7279
var partialPattern = pattern_assembler.get_pattern_by_key(partialName, patternlab);
7380

7481
//replace its reference within the block with the extended template
7582
thisBlockTemplate = thisBlockTemplate.replace(foundPartials[j], partialPattern.extendedTemplate);
76-
7783
}
7884

7985
//render with data

builder/pattern_assembler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565

6666
//extract some information
6767
var abspath = file.substring(2);
68-
var subdir = path.dirname(path.relative('./source/_patterns', file)).replace('\\', '/');
68+
var subdir = path.dirname(path.relative(patternlab.config.patterns.source, file)).replace('\\', '/');
6969
var filename = path.basename(file);
7070

7171
//ignore _underscored patterns, json (for now), and dotfiles

test/list_item_hunter_tests.js

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
(function () {
2+
"use strict";
3+
4+
var lih = require('../builder/list_item_hunter');
5+
6+
exports['list_item_hunter'] = {
7+
'process_list_item_partials finds and outputs basic repeating blocks' : function(test){
8+
//arrange
9+
//setup current pattern from what we would have during execution
10+
var currentPattern = {
11+
"template": "{{#listItems.two}}{{ title }}{{/listItems.two}}",
12+
"extendedTemplate" : "{{#listItems.two}}{{ title }}{{/listItems.two}}",
13+
"key": "test-patternName",
14+
"jsonFileData" : {}
15+
};
16+
17+
var patternlab = {
18+
"listitems": {
19+
"1": [
20+
{
21+
"title": "Foo"
22+
}
23+
],
24+
"2": [
25+
{
26+
"title": "Foo"
27+
},
28+
{
29+
"title": "Bar"
30+
}
31+
]
32+
},
33+
"data": {
34+
"link": {},
35+
"partials": []
36+
},
37+
"config": {"debug": false}
38+
};
39+
40+
var list_item_hunter = new lih();
41+
42+
//act
43+
list_item_hunter.process_list_item_partials(currentPattern, patternlab);
44+
45+
//assert
46+
test.equals(currentPattern.extendedTemplate, "FooBar" );
47+
48+
test.done();
49+
},
50+
51+
'process_list_item_partials finds partials and outputs repeated renders' : function(test){
52+
//arrange
53+
//setup current pattern from what we would have during execution
54+
var currentPattern = {
55+
"template": "{{#listItems.two}}{{ title }}{{/listItems.two}}",
56+
"extendedTemplate" : "{{#listItems.two}}{{> test-simple }}{{/listItems.two}}",
57+
"key": "test-patternName",
58+
"jsonFileData" : {}
59+
};
60+
61+
var patternlab = {
62+
"listitems": {
63+
"1": [
64+
{
65+
"title": "Foo"
66+
}
67+
],
68+
"2": [
69+
{
70+
"title": "Foo"
71+
},
72+
{
73+
"title": "Bar"
74+
}
75+
]
76+
},
77+
"data": {
78+
"link": {},
79+
"partials": []
80+
},
81+
"config": {"debug": false},
82+
"patterns": [
83+
{
84+
"template": "{{ title }}",
85+
"extendedTemplate" : "{{ title }}",
86+
"key": "test-simple",
87+
"jsonFileData" : {}
88+
}
89+
]
90+
};
91+
92+
var list_item_hunter = new lih();
93+
94+
//act
95+
list_item_hunter.process_list_item_partials(currentPattern, patternlab);
96+
97+
//assert
98+
test.equals(currentPattern.extendedTemplate, "FooBar" );
99+
100+
test.done();
101+
},
102+
103+
//TODO
104+
//'process_list_item_partials overwrites listItem data if local .listitem.json is found' : function(test){
105+
// //arrange
106+
// //setup current pattern from what we would have during execution
107+
// var currentPattern = {
108+
// "template": "{{#listItems.two}}{{ title }}{{/listItems.two}}",
109+
// "extendedTemplate" : "{{#listItems.two}}{{ title }}{{/listItems.two}}",
110+
// "key": "test-patternName",
111+
// "jsonFileData" : {}
112+
// };
113+
114+
// var patternlab = {
115+
// "listitems": {
116+
// "1": [
117+
// {
118+
// "title": "Foo"
119+
// }
120+
// ],
121+
// "2": [
122+
// {
123+
// "title": "Foo"
124+
// },
125+
// {
126+
// "title": "Bar"
127+
// }
128+
// ]
129+
// },
130+
// "data": {
131+
// "link": {},
132+
// "partials": []
133+
// },
134+
// "config": {"debug": true}
135+
// };
136+
137+
// var list_item_hunter = new lih();
138+
139+
// //act
140+
// list_item_hunter.process_list_item_partials(currentPattern, patternlab);
141+
142+
// //assert
143+
// test.equals(currentPattern.extendedTemplate, "FooBar" );
144+
145+
// test.done();
146+
//},
147+
148+
};
149+
150+
}());

0 commit comments

Comments
 (0)