Skip to content

Commit d37d624

Browse files
committed
Add more unit tests and bugfixes.
1 parent f7cee29 commit d37d624

File tree

2 files changed

+291
-6
lines changed

2 files changed

+291
-6
lines changed

specs/chrome/helpers-spec.js

Lines changed: 277 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
'use strict';
55

66
GLOBAL.window = GLOBAL;
7+
window.func = function() {};
78
window.Handlebars = {
89
helpers: {},
910
registerHelper: function(name, handler) {
@@ -158,7 +159,6 @@ describe('helpers.default', function() {
158159
});
159160

160161
it('should return function on function default value', function() {
161-
var func = function() {};
162162
expect(Handlebars.helpers.default(undefined, func)).toEqual(func);
163163
});
164164

@@ -203,7 +203,282 @@ describe('helpers.default', function() {
203203
});
204204

205205
it('should return function on function value', function() {
206-
var func = function() {};
207206
expect(Handlebars.helpers.default(func, 'a')).toEqual(func);
208207
});
209208
});
209+
210+
describe('helpers.equals', function() {
211+
it('should return logic result on equals', function() {
212+
var options = {
213+
fn: function(item) { return 'yes'; },
214+
inverse: function(item) { return 'no'; }
215+
};
216+
var inverse = spyOn(options, 'inverse').and.callThrough();
217+
var logic = spyOn(options, 'fn').and.callThrough();
218+
expect(Handlebars.helpers.equals('a', 'a', options)).toEqual('yes');
219+
expect(inverse).not.toHaveBeenCalled();
220+
expect(logic).toHaveBeenCalled();
221+
expect(logic.calls.count()).toEqual(1);
222+
});
223+
224+
it('should return inverse result on unequals', function() {
225+
var options = {
226+
fn: function(item) { return 'yes'; },
227+
inverse: function(item) { return 'no'; }
228+
};
229+
var inverse = spyOn(options, 'inverse').and.callThrough();
230+
var logic = spyOn(options, 'fn').and.callThrough();
231+
expect(Handlebars.helpers.equals('a', 'b', options)).toEqual('no');
232+
expect(inverse).toHaveBeenCalled();
233+
expect(inverse.calls.count()).toEqual(1);
234+
expect(logic).not.toHaveBeenCalled();
235+
});
236+
});
237+
238+
describe('helpers.fill', function() {
239+
it('should return empty on hash without definitions', function() {
240+
var isEmpty = spyOn(window.Handlebars.Utils, 'isEmpty').and.callThrough();
241+
expect(Handlebars.helpers.fill({ data: { root: {} } })).toEqual('');
242+
expect(isEmpty).toHaveBeenCalledWith(undefined);
243+
});
244+
245+
it('should return expected value without negate definitions', function() {
246+
var definitions = [
247+
{ attribute: { name: 'b' }, negate: 0, sourceIndex: 1, type: 'checkbox' },
248+
{ attribute: { name: 'b' }, negate: 0, sourceIndex: 1, type: 'radio' },
249+
{ attribute: { name: 'a' }, negate: 1, sourceIndex: 0, type: 'radio' },
250+
{ attribute: { name: 'a' }, negate: 0, sourceIndex: 0, type: 'select' },
251+
{ attribute: { name: 'c' }, negate: 0, sourceIndex: 2, type: 'text' }];
252+
var options = {
253+
data: { root: { definitions: definitions, fill: { separator: '-' } } },
254+
fn: function(item) { return item.attribute.name; } };
255+
var filter = spyOn(Array, 'filter').and.callThrough();
256+
var isEmpty = spyOn(window.Handlebars.Utils, 'isEmpty').and.callThrough();
257+
var join = spyOn(Array.prototype, 'join').and.callThrough();
258+
var logic = spyOn(options, 'fn').and.callThrough();
259+
var sort = spyOn(Array.prototype, 'sort').and.callThrough();
260+
expect(Handlebars.helpers.fill(options)).toEqual('a-b-b-c');
261+
expect(filter).toHaveBeenCalled();
262+
expect(filter.calls.count()).toEqual(1);
263+
expect(isEmpty).toHaveBeenCalledWith(definitions);
264+
expect(isEmpty.calls.count()).toEqual(1);
265+
expect(join).toHaveBeenCalledWith('-');
266+
expect(join.calls.count()).toEqual(1);
267+
expect(logic).toHaveBeenCalled();
268+
expect(logic.calls.count()).toEqual(4);
269+
expect(sort).toHaveBeenCalled();
270+
expect(sort.calls.count()).toEqual(1);
271+
});
272+
273+
it('should return expected value with separator', function() {
274+
var definitions = [
275+
{ attribute: { name: 'b' }, negate: 0, sourceIndex: 1, type: 'checkbox' },
276+
{ attribute: { name: 'b' }, negate: 0, sourceIndex: 1, type: 'radio' },
277+
{ attribute: { name: 'a' }, negate: 1, sourceIndex: 0, type: 'radio' },
278+
{ attribute: { name: 'a' }, negate: 0, sourceIndex: 0, type: 'select' },
279+
{ attribute: { name: 'c' }, negate: 0, sourceIndex: 2, type: 'text' }];
280+
var options = {
281+
data: { root: { definitions: definitions, fill: { separator: '$$$' } } },
282+
fn: function(item) { return item.attribute.name; } };
283+
var filter = spyOn(Array, 'filter').and.callThrough();
284+
var isEmpty = spyOn(window.Handlebars.Utils, 'isEmpty').and.callThrough();
285+
var join = spyOn(Array.prototype, 'join').and.callThrough();
286+
var logic = spyOn(options, 'fn').and.callThrough();
287+
var sort = spyOn(Array.prototype, 'sort').and.callThrough();
288+
expect(Handlebars.helpers.fill(options)).toEqual('a$$$b$$$b$$$c');
289+
expect(filter).toHaveBeenCalled();
290+
expect(filter.calls.count()).toEqual(1);
291+
expect(isEmpty).toHaveBeenCalledWith(definitions);
292+
expect(isEmpty.calls.count()).toEqual(1);
293+
expect(join).toHaveBeenCalledWith('$$$');
294+
expect(join.calls.count()).toEqual(1);
295+
expect(logic).toHaveBeenCalled();
296+
expect(logic.calls.count()).toEqual(4);
297+
expect(sort).toHaveBeenCalled();
298+
expect(sort.calls.count()).toEqual(1);
299+
});
300+
});
301+
302+
describe('helpers.lower', function() {
303+
it('should return empty string on undefined value', function() {
304+
expect(Handlebars.helpers.lower()).toEqual('');
305+
});
306+
307+
it('should return empty string on null value', function() {
308+
expect(Handlebars.helpers.lower(null)).toEqual('');
309+
});
310+
311+
it('should return false on boolean false value', function() {
312+
expect(Handlebars.helpers.lower(false)).toBeFalsy();
313+
});
314+
315+
it('should return true on boolean true value', function() {
316+
expect(Handlebars.helpers.lower(true)).toBeTruthy();
317+
});
318+
319+
it('should return zero on zero value', function() {
320+
expect(Handlebars.helpers.lower(0)).toEqual(0);
321+
});
322+
323+
it('should return number on number value', function() {
324+
expect(Handlebars.helpers.lower(1)).toEqual(1);
325+
});
326+
327+
it('should return empty string on empty string value', function() {
328+
expect(Handlebars.helpers.lower('')).toEqual('');
329+
});
330+
331+
it('should return lower case string on string value', function() {
332+
expect(Handlebars.helpers.lower('AABBCC')).toEqual('aabbcc');
333+
});
334+
335+
it('should return array on array value', function() {
336+
expect(Handlebars.helpers.lower([0])).toEqual([0]);
337+
});
338+
339+
it('should return hash on hash value', function() {
340+
expect(Handlebars.helpers.lower({'key': 'a'})).toEqual({'key': 'a'});
341+
});
342+
343+
it('should return function on function value', function() {
344+
expect(Handlebars.helpers.lower(func)).toEqual(func);
345+
});
346+
});
347+
348+
describe('helpers.operations', function() {
349+
it('should return empty on hash without definitions', function() {
350+
var isEmpty = spyOn(window.Handlebars.Utils, 'isEmpty').and.callThrough();
351+
expect(Handlebars.helpers.operations({ data: { root: {} } })).toEqual('');
352+
expect(isEmpty).toHaveBeenCalledWith(undefined);
353+
});
354+
355+
it('should return expected value with operation name', function() {
356+
var definitions = [
357+
{ operation: { name: 'b' } },
358+
{ operation: { name: 'b' } },
359+
{ operation: { target: 'a' } },
360+
{ operation: { name: 'a' } },
361+
{ operation: { name: 'c' } }];
362+
var options = {
363+
data: { root: { definitions: definitions, operations: { separator: '-' } } },
364+
fn: function(item) { return item.operation.name; } };
365+
var filter = spyOn(Array, 'filter').and.callThrough();
366+
var isEmpty = spyOn(window.Handlebars.Utils, 'isEmpty').and.callThrough();
367+
var join = spyOn(Array.prototype, 'join').and.callThrough();
368+
var logic = spyOn(options, 'fn').and.callThrough();
369+
var sort = spyOn(Array.prototype, 'sort').and.callThrough();
370+
expect(Handlebars.helpers.operations(options)).toEqual('a-b-b-c');
371+
expect(filter).toHaveBeenCalled();
372+
expect(filter.calls.count()).toEqual(1);
373+
expect(isEmpty).toHaveBeenCalledWith(definitions);
374+
expect(isEmpty.calls.count()).toEqual(1);
375+
expect(join).toHaveBeenCalledWith('-');
376+
expect(join.calls.count()).toEqual(1);
377+
expect(logic).toHaveBeenCalled();
378+
expect(logic.calls.count()).toEqual(4);
379+
expect(sort).toHaveBeenCalled();
380+
expect(sort.calls.count()).toEqual(1);
381+
});
382+
383+
it('should return expected value with separator', function() {
384+
var definitions = [
385+
{ operation: { name: 'b' } },
386+
{ operation: { name: 'b' } },
387+
{ operation: { target: 'a' } },
388+
{ operation: { name: 'a' } },
389+
{ operation: { name: 'c' } }];
390+
var options = {
391+
data: { root: { definitions: definitions, operations: { separator: '$$$' } } },
392+
fn: function(item) { return item.operation.name; } };
393+
var filter = spyOn(Array, 'filter').and.callThrough();
394+
var isEmpty = spyOn(window.Handlebars.Utils, 'isEmpty').and.callThrough();
395+
var join = spyOn(Array.prototype, 'join').and.callThrough();
396+
var logic = spyOn(options, 'fn').and.callThrough();
397+
var sort = spyOn(Array.prototype, 'sort').and.callThrough();
398+
expect(Handlebars.helpers.operations(options)).toEqual('a$$$b$$$b$$$c');
399+
expect(filter).toHaveBeenCalled();
400+
expect(filter.calls.count()).toEqual(1);
401+
expect(isEmpty).toHaveBeenCalledWith(definitions);
402+
expect(isEmpty.calls.count()).toEqual(1);
403+
expect(join).toHaveBeenCalledWith('$$$');
404+
expect(join.calls.count()).toEqual(1);
405+
expect(logic).toHaveBeenCalled();
406+
expect(logic.calls.count()).toEqual(4);
407+
expect(sort).toHaveBeenCalled();
408+
expect(sort.calls.count()).toEqual(1);
409+
});
410+
});
411+
412+
describe('helpers.proper', function() {
413+
it('should return empty string on undefined value', function() {
414+
expect(Handlebars.helpers.proper()).toEqual('');
415+
});
416+
417+
it('should return empty string on null value', function() {
418+
expect(Handlebars.helpers.proper(null)).toEqual('');
419+
});
420+
421+
it('should return false on boolean false value', function() {
422+
expect(Handlebars.helpers.proper(false)).toBeFalsy();
423+
});
424+
425+
it('should return true on boolean true value', function() {
426+
expect(Handlebars.helpers.proper(true)).toBeTruthy();
427+
});
428+
429+
it('should return zero on zero value', function() {
430+
expect(Handlebars.helpers.proper(0)).toEqual(0);
431+
});
432+
433+
it('should return number on number value', function() {
434+
expect(Handlebars.helpers.proper(1)).toEqual(1);
435+
});
436+
437+
it('should return empty string on empty string value', function() {
438+
expect(Handlebars.helpers.proper('')).toEqual('');
439+
});
440+
441+
it('should return proper case string on string value', function() {
442+
expect(Handlebars.helpers.proper('ma ME mu Mi mO')).toEqual('Ma Me Mu Mi Mo');
443+
});
444+
445+
it('should return array on array value', function() {
446+
expect(Handlebars.helpers.proper([0])).toEqual([0]);
447+
});
448+
449+
it('should return hash on hash value', function() {
450+
expect(Handlebars.helpers.proper({'key': 'a'})).toEqual({'key': 'a'});
451+
});
452+
453+
it('should return function on function value', function() {
454+
expect(Handlebars.helpers.proper(func)).toEqual(func);
455+
});
456+
});
457+
458+
describe('helpers.unequals', function() {
459+
it('should return logic result on unequals', function() {
460+
var options = {
461+
fn: function(item) { return 'yes'; },
462+
inverse: function(item) { return 'no'; }
463+
};
464+
var inverse = spyOn(options, 'inverse').and.callThrough();
465+
var logic = spyOn(options, 'fn').and.callThrough();
466+
expect(Handlebars.helpers.unequals('a', 'b', options)).toEqual('yes');
467+
expect(inverse).not.toHaveBeenCalled();
468+
expect(logic).toHaveBeenCalled();
469+
expect(logic.calls.count()).toEqual(1);
470+
});
471+
472+
it('should return inverse result on equals', function() {
473+
var options = {
474+
fn: function(item) { return 'yes'; },
475+
inverse: function(item) { return 'no'; }
476+
};
477+
var inverse = spyOn(options, 'inverse').and.callThrough();
478+
var logic = spyOn(options, 'fn').and.callThrough();
479+
expect(Handlebars.helpers.unequals('a', 'a', options)).toEqual('no');
480+
expect(inverse).toHaveBeenCalled();
481+
expect(inverse.calls.count()).toEqual(1);
482+
expect(logic).not.toHaveBeenCalled();
483+
});
484+
});

src/chrome/assets/js/helpers.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,12 @@ Handlebars.registerHelper('fill', function(options) {
7474
});
7575

7676
Handlebars.registerHelper('lower', function(value) {
77-
return (value || '').toLowerCase();
77+
var response = value;
78+
var type = typeof(value);
79+
if ('|string|undefined|'.indexOf('|' + type + '|') > -1 || value === null) {
80+
response = (value || '').toLowerCase();
81+
}
82+
return response;
7883
});
7984

8085
Handlebars.registerHelper('operations', function(options) {
@@ -102,10 +107,15 @@ Handlebars.registerHelper('operations', function(options) {
102107
});
103108

104109
Handlebars.registerHelper('proper', function(value) {
105-
return (value || '').replace(/[,.!?-]+/g, ' ').replace(/\s\s+/g, ' ').
110+
var response = value;
111+
var type = typeof(value);
112+
if ('|string|undefined|'.indexOf('|' + type + '|') > -1 || value === null) {
113+
response = (value || '').replace(/[,.!?-]+/g, ' ').replace(/\s\s+/g, ' ').
106114
replace(/\w\S*/g, function(word) {
107-
return word.charAt(0).toUpperCase() + word.substr(1).toLowerCase();
108-
});
115+
return word.charAt(0).toUpperCase() + word.substr(1).toLowerCase();
116+
});
117+
}
118+
return response;
109119
});
110120

111121
Handlebars.registerHelper('unequals', function(operand1, operand2, options) {

0 commit comments

Comments
 (0)