-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest.js
148 lines (130 loc) · 5.43 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
'use strict';
require('mocha');
var assert = require('assert');
var Templates = require('templates');
var helper = require('./');
var handlebars;
var app;
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
describe('handlebars', function() {
beforeEach(function() {
handlebars = require('handlebars');
handlebars.registerHelper(require('handlebars-helpers').comparison());
handlebars.registerHelper('repeat', helper);
handlebars.registerPartial('button', '<button>{{text}}</button>');
handlebars.registerPartial('outter', '<button>{{> inner }}</button>');
handlebars.registerPartial('inner', '<button>{{zzz}}</button>');
});
it('should repeat a block n times:', function() {
var ctx = {text: 'foo'};
var actual = handlebars.compile('{{#repeat 2}}{{> button }}\n{{/repeat}}')(ctx);
assert.equal(actual, '<button>foo</button>\n<button>foo</button>\n');
});
it('should output the inverse when no number is specified:', function() {
var ctx = {text: 'foo'};
var actual = handlebars.compile('{{#repeat}}{{> button }}\n{{else}}Nothing :({{/repeat}}')(ctx);
assert.equal(actual, 'Nothing :(');
});
it('should repeat a string', function() {
const actual = handlebars.compile('{{#repeat "foo" count=2}}{{.}}{{/repeat}}')();
assert.equal(actual, 'foofoo');
});
it('should work as a non-block helper', function() {
const actual = handlebars.compile('{{repeat "foo" count=2}}')();
assert.equal(actual, 'foofoo');
});
it('should work as a subexpression', function() {
handlebars.registerHelper('upper', str => str.toUpperCase());
const actual = handlebars.compile('{{upper (repeat "foo" count=2)}}')();
assert.equal(actual, 'FOOFOO');
});
it('should allow the count to be specified on the hash:', function() {
var ctx = {text: 'foo'};
var actual = handlebars.compile('{{#repeat count=2}}{{> button }}\n{{else}}Nothing :({{/repeat}}')(ctx);
assert.equal(actual, '<button>foo</button>\n<button>foo</button>\n');
});
it('should expose hash variables as private variables:', function() {
var ctx = {text: 'foo'};
var a = handlebars.compile('{{#repeat count=2}}{{@count}}{{> button }}\n{{else}}Nothing :({{/repeat}}')(ctx);
assert.equal(a, '2<button>foo</button>\n2<button>foo</button>\n');
var b = handlebars.compile('{{#repeat count=2}}{{@index}}{{> button }}\n{{else}}Nothing :({{/repeat}}')(ctx);
assert.equal(b, '0<button>foo</button>\n1<button>foo</button>\n');
});
it('should start the index with the given number:', function() {
var ctx = {text: 'foo'};
var actual = handlebars.compile('{{#repeat count=2 start=17}}{{@index}}{{> button }}\n{{else}}Nothing :({{/repeat}}')(ctx);
assert.equal(actual, '17<button>foo</button>\n18<button>foo</button>\n');
});
it('should support nested repeats with block params', function() {
var ctx = {sep: ', '};
var template =
'{{#repeat count=2 start=17 step=2 as |outer_index outer_context|}}' +
'{{#repeat 2 as |inner_index inner_context|}}' +
'{{outer_index}} {{@index}} {{inner_index}}' +
'{{#unless (and outer_context.last inner_context.last)}}' +
'{{sep}}' +
'{{/unless}}' +
'{{/repeat}}' +
'{{/repeat}}';
var expected =
'17 0 0, ' +
'17 1 1, ' +
'19 0 0, ' +
'19 1 1';
var actual = handlebars.compile(template)(ctx);
assert.equal(actual, expected);
});
it('should support last and first in nested repeats', function() {
var ctx = {
sep: '\n',
count: 3,
random_start1: getRandomInt(),
random_start2: getRandomInt(),
random_step1: getRandomInt(),
random_step2: getRandomInt()
};
var template =
'{{#repeat count=count start=random_start1 step=random_step1 as |outer_index outer_context|}}' +
'{{#repeat count=count start=random_start2 step=random_step2 as |inner_index inner_context|}}' +
'first: {{outer_context.first}} {{inner_context.first}}, last: {{outer_context.last}} {{inner_context.last}}' +
'{{#unless (and outer_context.last inner_context.last)}}' +
'{{sep}}' +
'{{/unless}}' +
'{{/repeat}}' +
'{{/repeat}}';
var expected =
'first: true true, last: false false\n' +
'first: true false, last: false false\n' +
'first: true false, last: false true\n' +
'first: false true, last: false false\n' +
'first: false false, last: false false\n' +
'first: false false, last: false true\n' +
'first: false true, last: true false\n' +
'first: false false, last: true false\n' +
'first: false false, last: true true';
var actual = handlebars.compile(template)(ctx);
assert.equal(actual, expected);
});
});
describe('Templates', function() {
beforeEach(function() {
app = new Templates();
handlebars = require('engine-handlebars');
app.create('partial', {viewType: ['partial']});
app.create('page');
app.engine('hbs', handlebars);
app.helper('repeat', helper);
app.partial('button.hbs', '<button>{{text}}</button>');
app.page('fixture.hbs', '{{#repeat 2}}{{> button }}\n{{/repeat}}');
app.data({text: 'foo'});
});
it('should work with Templates:', function(cb) {
app.render('fixture.hbs', function(err, view) {
if (err) return cb(err);
assert.equal(view.contents.toString(), '<button>foo</button>\n<button>foo</button>\n');
cb();
});
});
});