UNPKG

13.1 kBJavaScriptView Raw
1(function() {
2 var Assemble, Handlebars, context;
3
4 require('should');
5
6 Handlebars = require('handlebars');
7
8 Assemble = require('../lib/helpers-lib');
9
10 context = {
11 collection: ['Amy Wong', 'Bender', 'Dr. Zoidberg', 'Fry', 'Hermes Conrad', 'Leela', 'Professor Farnsworth', 'Scruffy']
12 };
13
14 describe('first', function() {
15 describe('{{first collection}}', function() {
16 return it('should return the first item in a collection.', function() {
17 var source, template;
18
19 source = '{{first collection}}';
20 template = Handlebars.compile(source);
21 return template(context).should.equal('Amy Wong');
22 });
23 });
24 return describe('{{first collection 2}}', function() {
25 return it('should return an array with the first two items in a collection.', function() {
26 var source, template;
27
28 source = '{{first collection 2}}';
29 template = Handlebars.compile(source);
30 return template(context).should.eql(['Amy Wong', 'Bender']);
31 });
32 });
33 });
34
35 describe('withFirst', function() {
36 describe('{{#withFirst collection}} \n\
37 <p>{{this}} is smart.</p> \n\
38 {{/withFirst}}', function() {
39 return it('should use the first item in a collection inside a block.', function() {
40 var source, template;
41
42 source = '{{#withFirst collection}}<p>{{this}} is smart.</p>{{/withFirst}}';
43 template = Handlebars.compile(source);
44 return template(context).should.equal('<p>Amy Wong is smart.</p>');
45 });
46 });
47 return describe('{{#withFirst collection 2}} \n\
48 <p>{{this}} is smart.</p> \n\
49 {{/withFirst}}', function() {
50 return it('should use the first two items in a collection inside a block.', function() {
51 var source, template;
52
53 source = '{{#withFirst collection 2}}<p>{{this}} is smart.</p>{{/withFirst}}';
54 template = Handlebars.compile(source);
55 return template(context).should.equal('<p>Amy Wong is smart.</p><p>Bender is smart.</p>');
56 });
57 });
58 });
59
60 describe('last', function() {
61 describe('{{last collection}}', function() {
62 return it('should return the last item in a collection.', function() {
63 var source, template;
64
65 source = '{{last collection}}';
66 template = Handlebars.compile(source);
67 return template(context).should.equal('Scruffy');
68 });
69 });
70 return describe('{{last collection 2}}', function() {
71 return it('should return an array with the last two items in a collection.', function() {
72 var source, template;
73
74 source = '{{last collection 2}}';
75 template = Handlebars.compile(source);
76 return template(context).should.eql(['Professor Farnsworth', 'Scruffy']);
77 });
78 });
79 });
80
81 describe('withLast', function() {
82 describe('{{#withLast collection}} \n\
83 <p>{{this}} is dumb.</p> \n\
84 {{/withLast}}', function() {
85 return it('should use the last item in a collection inside a block.', function() {
86 var source, template;
87
88 source = '{{#withLast collection}}<p>{{this}} is dumb.</p>{{/withLast}}';
89 template = Handlebars.compile(source);
90 return template(context).should.equal('<p>Scruffy is dumb.</p>');
91 });
92 });
93 return describe('{{#withLast collection 2}} \n\
94 <p>{{this}} is dumb.</p> \n\
95 {{/withLast}}', function() {
96 return it('should use the last two items in a collection inside a block.', function() {
97 var source, template;
98
99 source = '{{#withLast collection 2}}<p>{{this}} is dumb.</p>{{/withLast}}';
100 template = Handlebars.compile(source);
101 return template(context).should.equal('<p>Professor Farnsworth is dumb.</p><p>Scruffy is dumb.</p>');
102 });
103 });
104 });
105
106 describe('after', function() {
107 return describe('{{after collection 5}}', function() {
108 return it('should return all of the items in a collection after the specified count.', function() {
109 var source, template;
110
111 source = '{{after collection 5}}';
112 template = Handlebars.compile(source);
113 return template(context).should.eql(['Leela', 'Professor Farnsworth', 'Scruffy']);
114 });
115 });
116 });
117
118 describe('withAfter', function() {
119 return describe('{{#withAfter collection 5}} \n\
120 <{{this}}> \n\
121 {{/withAfter}}', function() {
122 return it('should use all of the items in a collection after the specified count inside a block.', function() {
123 var source, template;
124
125 source = '{{#withAfter collection 5}}<{{this}}>{{/withAfter}}';
126 template = Handlebars.compile(source);
127 return template(context).should.equal('<Leela><Professor Farnsworth><Scruffy>');
128 });
129 });
130 });
131
132 describe('before', function() {
133 return describe('{{before collection 5}}', function() {
134 return it('should return all of the items in a collection before the specified count.', function() {
135 var source, template;
136
137 source = '{{before collection 5}}';
138 template = Handlebars.compile(source);
139 return template(context).should.eql(['Amy Wong', 'Bender', 'Dr. Zoidberg']);
140 });
141 });
142 });
143
144 describe('withBefore', function() {
145 return describe('{{#withBefore collection 5}} \n\
146 <{{this}}> \n\
147 {{/withBefore}}', function() {
148 return it('should use all of the items in a collection before the specified count inside a block.', function() {
149 var source, template;
150
151 source = '{{#withBefore collection 5}}<{{this}}>{{/withBefore}}';
152 template = Handlebars.compile(source);
153 return template(context).should.equal('<Amy Wong><Bender><Dr. Zoidberg>');
154 });
155 });
156 });
157
158 describe('join', function() {
159 return describe('{{join collection " | "}}', function() {
160 return it('should return all items in a collection joined by a separator if specified.', function() {
161 var source, template;
162
163 source = '{{join collection " | "}}';
164 template = Handlebars.compile(source);
165 return template(context).should.equal('Amy Wong | Bender | Dr. Zoidberg | Fry | Hermes Conrad | Leela | Professor Farnsworth | Scruffy');
166 });
167 });
168 });
169
170 describe('sort', function() {
171 describe('{{sort collection}}', function() {
172 return it('should return all items in a collection sorted in lexicographical order.', function() {
173 var source, template;
174
175 source = '{{sort collection}}';
176 template = Handlebars.compile(source);
177 return template(context).should.eql(['Amy Wong', 'Bender', 'Dr. Zoidberg', 'Fry', 'Hermes Conrad', 'Leela', 'Professor Farnsworth', 'Scruffy']);
178 });
179 });
180 return describe('{{sort collection "name"}}', function() {
181 return it('should return all items in a collection sorted in by name.', function() {
182 var source, template, _context;
183
184 source = '{{sort collection "name"}}';
185 template = Handlebars.compile(source);
186 _context = {
187 collection: [
188 {
189 name: 'Leela',
190 deliveries: 8021
191 }, {
192 name: 'Bender',
193 deliveries: 239
194 }, {
195 name: 'Fry',
196 deliveries: -12
197 }
198 ]
199 };
200 return template(_context).should.eql([
201 {
202 name: 'Bender',
203 deliveries: 239
204 }, {
205 name: 'Fry',
206 deliveries: -12
207 }, {
208 name: 'Leela',
209 deliveries: 8021
210 }
211 ]);
212 });
213 });
214 });
215
216 describe('withSort', function() {
217 describe('{{#withSort collection}} \n\
218 <p>{{this}}</p> \n\
219 {{/withSort}}', function() {
220 return it('should sort the collection in lexicographical order and use it in a block.', function() {
221 var source, template;
222
223 source = '{{#withSort collection}}<p>{{this}}</p>{{/withSort}}';
224 template = Handlebars.compile(source);
225 return template(context).should.equal('<p>Amy Wong</p><p>Bender</p><p>Dr. Zoidberg</p><p>Fry</p><p>Hermes Conrad</p><p>Leela</p><p>Professor Farnsworth</p><p>Scruffy</p>');
226 });
227 });
228 return describe('{{#withSort collection "deliveries"}} \n\
229 {{name}}: {{deliveries}} <br> \n\
230 {{/withSort}}', function() {
231 return it('should sort the collection by deliveries and use it in a block.', function() {
232 var source, template, _context;
233
234 source = '{{#withSort collection "deliveries"}}{{name}}: {{deliveries}} <br>{{/withSort}}';
235 template = Handlebars.compile(source);
236 _context = {
237 collection: [
238 {
239 name: 'Leela',
240 deliveries: 8021
241 }, {
242 name: 'Bender',
243 deliveries: 239
244 }, {
245 name: 'Fry',
246 deliveries: -12
247 }
248 ]
249 };
250 return template(_context).should.equal('Fry: -12 <br>Bender: 239 <br>Leela: 8021 <br>');
251 });
252 });
253 });
254
255 describe('length', function() {
256 return describe('{{length collection}}', function() {
257 return it('should return the length of the collection', function() {
258 var source, template;
259
260 source = '{{length collection}}';
261 template = Handlebars.compile(source);
262 return template(context).should.equal(8);
263 });
264 });
265 });
266
267 describe('lengthEqual', function() {
268 return describe('{{#lengthEqual collection 3}} \n\
269 There are 3 people in Planet Express. \n\
270 {{else}} \n\
271 This is not Planet Express. \n\
272 {{/lengthEqual}}', function() {
273 return it('should conditionally render a block based on the length of a collection.', function() {
274 var source, template;
275
276 source = '{{#lengthEqual collection 3}}There are 3 people in Planet Express.{{else}}This is not Planet Express.{{/lengthEqual}}';
277 template = Handlebars.compile(source);
278 return template(context).should.equal('This is not Planet Express.');
279 });
280 });
281 });
282
283 describe('empty', function() {
284 return describe('{{#empty collection}} \n\
285 Bad news everyone! \n\
286 {{else}} \n\
287 Good news everyone! \n\
288 {{/empty}}', function() {
289 return it('should conditionally render a block the collection is empty.', function() {
290 var source, template;
291
292 source = '{{#empty collection}}Bad news everyone!{{else}}Good news everyone!{{/empty}}';
293 template = Handlebars.compile(source);
294 return template(context).should.equal('Good news everyone!');
295 });
296 });
297 });
298
299 describe('any', function() {
300 return describe('{{#any collection}} \n\
301 Bad news everyone! \n\
302 {{else}} \n\
303 Good news everyone! \n\
304 {{/any}}', function() {
305 return it('should conditionally render a block the collection isn\'t empty.', function() {
306 var source, template;
307
308 source = '{{#any collection}}Bad news everyone!{{else}}Good news everyone!{{/any}}';
309 template = Handlebars.compile(source);
310 return template(context).should.equal('Bad news everyone!');
311 });
312 });
313 });
314
315 describe('inArray', function() {
316 return describe('{{#inArray collection "Fry"}} \n\
317 I\'m walking on sunshine! \n\
318 {{else}} \n\
319 I\'m walking in darkness. \n\
320 {{/inArray}}', function() {
321 return it('should conditionally render a block if a specified value is in the collection.', function() {
322 var source, template;
323
324 source = '{{#inArray collection "Fry"}}I\'m walking on sunshine!{{else}}I\'m walking in darkness.{{/inArray}}';
325 template = Handlebars.compile(source);
326 return template(context).should.equal('I\'m walking on sunshine!');
327 });
328 });
329 });
330
331 describe('eachIndex', function() {
332 return describe('{{#eachIndex collection}} \n\
333 {{item}} is {{index}} \n\
334 {{/eachIndex}}', function() {
335 return it('should render the block using the array and each item\'s index.', function() {
336 var source, template;
337
338 source = '{{#eachIndex collection}} {{item}} is {{index}} {{/eachIndex}}';
339 template = Handlebars.compile(source);
340 return template(context).should.equal(' Amy Wong is 0 Bender is 1 Dr. Zoidberg is 2 Fry is 3 Hermes Conrad is 4 Leela is 5 Professor Farnsworth is 6 Scruffy is 7 ');
341 });
342 });
343 });
344
345 describe('eachProperty', function() {
346 return describe('{{#eachProperty collection}} \n\
347 {{key}}: {{value}} \n\
348 {{/eachProperty}}', function() {
349 return it('should use the key and value of each property in an object inside a block.', function() {
350 var source, template, _context;
351
352 source = '{{#eachProperty collection}}{{key}}: {{value}} {{/eachProperty}}';
353 template = Handlebars.compile(source);
354 _context = {
355 collection: {
356 fry: 3,
357 bender: 120
358 }
359 };
360 return template(_context).should.equal('fry: 3 bender: 120 ');
361 });
362 });
363 });
364
365}).call(this);