forked from pattern-lab/patternlab-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine_underscore_tests.js
More file actions
124 lines (98 loc) · 3.75 KB
/
engine_underscore_tests.js
File metadata and controls
124 lines (98 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"use strict";
var tap = require('tap');
var path = require('path');
var pa = require('../core/lib/pattern_assembler');
var PatternGraph = require('../core/lib/pattern_graph').PatternGraph;
var testPatternsPath = path.resolve(__dirname, 'files', '_underscore-test-patterns');
var eol = require('os').EOL;
// don't run tests unless underscore is installed
var engineLoader = require('../core/lib/pattern_engines');
if (!engineLoader.underscore) {
tap.test('Underscore engine not installed, skipping tests', function (test) {
test.end();
});
return;
}
// fake pattern lab constructor:
// sets up a fake patternlab object, which is needed by the pattern processing
// apparatus.
function fakePatternLab() {
var fpl = {
graph: PatternGraph.empty(),
partials: {},
patterns: [],
footer: '',
header: '',
listitems: {},
listItemArray: [],
data: {
link: {}
},
config: require('../patternlab-config.json'),
package: {}
};
// patch the pattern source so the pattern assembler can correctly determine
// the "subdir"
fpl.config.paths.source.patterns = testPatternsPath;
return fpl;
}
tap.test('hello world underscore pattern renders', function (test) {
test.plan(1);
var patternPath = path.resolve(
testPatternsPath,
'00-atoms',
'00-global',
'00-helloworld.html'
);
// do all the normal processing of the pattern
var patternlab = new fakePatternLab();
var assembler = new pa();
var helloWorldPattern = assembler.process_pattern_iterative(patternPath, patternlab);
assembler.process_pattern_recursive(patternPath, patternlab);
test.equals(helloWorldPattern.render(), 'Hello world!' + eol);
test.end();
});
tap.test('underscore partials can render JSON values', function (test) {
test.plan(1);
// pattern paths
var pattern1Path = path.resolve(
testPatternsPath,
'00-atoms',
'00-global',
'00-helloworld-withdata.html'
);
// set up environment
var patternlab = new fakePatternLab(); // environment
var assembler = new pa();
// do all the normal processing of the pattern
var helloWorldWithData = assembler.process_pattern_iterative(pattern1Path, patternlab);
assembler.process_pattern_recursive(pattern1Path, patternlab);
// test
test.equals(helloWorldWithData.render(), 'Hello world!' + eol + 'Yeah, we got the subtitle from the JSON.' + eol);
test.end();
});
tap.test('findPartial return the ID of the partial, given a whole partial call', function (test) {
var engineLoader = require('../core/lib/pattern_engines');
var underscoreEngine = engineLoader.underscore;
test.plan(1);
// do all the normal processing of the pattern
// test
test.equals(underscoreEngine.findPartial("<%= _.renderNamedPartial('molecules-details', obj) %>"), 'molecules-details');
test.end();
});
tap.test('hidden underscore patterns can be called by their nice names', function(test){
const util = require('./util/test_utils.js');
//arrange
const testPatternsPath = path.resolve(__dirname, 'files', '_underscore-test-patterns');
const pl = util.fakePatternLab(testPatternsPath);
var pattern_assembler = new pa();
var hiddenPatternPath = path.join('00-atoms', '00-global', '_00-hidden.html');
var hiddenPattern = pattern_assembler.process_pattern_iterative(hiddenPatternPath, pl);
pattern_assembler.process_pattern_recursive(hiddenPatternPath, pl);
var testPatternPath = path.join('00-molecules', '00-global', '00-hidden-pattern-tester.html');
var testPattern = pattern_assembler.process_pattern_iterative(testPatternPath, pl);
pattern_assembler.process_pattern_recursive(testPatternPath, pl);
//act
test.equals(util.sanitized(testPattern.render()), util.sanitized('Here\'s the hidden atom: [I\'m the hidden atom\n]\n'));
test.end();
});