-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.js
134 lines (122 loc) · 3.98 KB
/
test.js
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
125
126
127
128
129
130
131
132
133
134
'use strict';
require('mocha');
var capture = require('capture-stream');
var assemble = require('assemble');
var assert = require('assert');
var helpers = require('./');
var app;
describe('assemble-handlebars-helpers', function() {
beforeEach(function() {
app = assemble();
app.helpers(helpers);
});
it('should register helpers', function() {
assert.deepEqual(Object.keys(app._.helpers.sync), ['partials', 'layouts', 'pages', 'each', 'if', 'log', 'lookup', 'unless', 'with']);
});
describe('each', function() {
it('should render using {{each}} helper', function(cb) {
app.page('a.hbs', { content: '{{#each items}}{{this}}{{/each}}' });
var context = {
items: ['a', 'b', 'c']
};
app.render('a.hbs', context, function(err, results) {
if (err) return cb(err);
assert.equal(results.content, 'abc');
cb();
});
});
it('should render using {{if}} helper inside {{each}} helper without context loss', function(cb) {
app.page('a.hbs', { content: '{{#each items}}{{#if foo}}{{foo}}{{/if}}{{/each}}' });
var context = {
items: [{foo: 'a'}, {foo: false}, {foo: 'c'}]
};
app.render('a.hbs', context, function(err, results) {
if (err) return cb(err);
assert.equal(results.content, 'ac');
cb();
});
});
});
describe('if', function() {
it('should render using {{if}} helper', function(cb) {
app.page('a.hbs', { content: '{{#if foo}}{{foo}}{{/if}}' });
var context = {
foo: 'bar'
};
app.render('a.hbs', context, function(err, results) {
if (err) return cb(err);
assert.equal(results.content, 'bar');
cb();
});
});
});
describe('log', function() {
it('should render using {{log}} helper', function(cb) {
app.page('a.hbs', { content: '{{log items}}' });
var context = {
items: ['a', 'b', 'c']
};
var restore = capture(process.stdout);
app.render('a.hbs', context, function(err, results) {
var output = restore(true);
if (err) return cb(err);
assert.equal(results.content, '');
assert.equal(output.indexOf("[ 'a', 'b', 'c' ]"), 0);
cb();
});
});
});
describe('lookup', function() {
it('should render using {{lookup}} helper', function(cb) {
app.page('a.hbs', { content: '{{#each (lookup data "items")}}{{this}}{{/each}}' });
var context = {
data: { items: ['a', 'b', 'c'] }
};
app.render('a.hbs', context, function(err, results) {
if (err) return cb(err);
assert.equal(results.content, 'abc');
cb();
});
});
it('should render dynamic partials using {{lookup}} helper', function(cb) {
app.page('b.hbs', { content: '{{> (lookup data "partial") partialData }}'});
app.partial('partial.hbs', { content: '{{answer}}'});
var context = {
data: { partial: 'partial' },
partialData: { answer: '42' }
};
app.render('b.hbs', context, function(err, results) {
if (err) return cb(err);
assert.equal(results.content, '42');
cb();
});
});
});
describe('unless', function() {
it('should render using {{unless}} helper', function(cb) {
app.page('a.hbs', { content: '{{#unless foo}}{{bar}}{{/unless}}' });
var context = {
foo: false,
bar: 'baz'
};
app.render('a.hbs', context, function(err, results) {
if (err) return cb(err);
assert.equal(results.content, 'baz');
cb();
});
});
});
describe('with', function() {
it('should render using {{with}} helper', function(cb) {
app.page('a.hbs', { content: '{{#with data}}{{#each items}}{{this}}{{/each}}{{/with}}' });
var context = {
data: { items: ['a', 'b', 'c'] }
};
app.render('a.hbs', context, function(err, results) {
if (err) return cb(err);
assert.equal(results.content, 'abc');
cb();
});
});
});
});