From 6bbd09c2f9174772a4b63b1c5b5996867039e089 Mon Sep 17 00:00:00 2001 From: Guilherme I F L Weizenmann Date: Wed, 28 Sep 2016 12:40:05 -0300 Subject: [PATCH] Add tests to #each/@value syntax on array/objects --- spec/builtins.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/spec/builtins.js b/spec/builtins.js index dc1df0a3e..d620ddf09 100644 --- a/spec/builtins.js +++ b/spec/builtins.js @@ -115,6 +115,67 @@ describe('builtin helpers', function() { shouldCompileTo(string, {goodbyes: {}, world: 'world'}, 'cruel world!'); }); + it('each with an object and @key/@value', function() { + var string = '{{#each goodbyes}}{{@key}}. {{@value.text}}! {{/each}}cruel {{world}}!'; + + function Clazz() { + this['#1'] = {text: 'goodbye'}; + this[2] = {text: 'GOODBYE'}; + } + Clazz.prototype.foo = 'fail'; + var hash = {goodbyes: new Clazz(), world: 'world'}; + + // Object property iteration order is undefined according to ECMA spec, + // so we need to check both possible orders + // @see http://stackoverflow.com/questions/280713/elements-order-in-a-for-in-loop + var actual = compileWithPartials(string, hash); + var expected1 = '<b>#1</b>. goodbye! 2. GOODBYE! cruel world!'; + var expected2 = '2. GOODBYE! <b>#1</b>. goodbye! cruel world!'; + + equals(actual === expected1 || actual === expected2, true, 'each with object argument iterates over the contents when not empty'); + shouldCompileTo(string, {goodbyes: {}, world: 'world'}, 'cruel world!'); + }); + + it('each with an object and @key/@value that has key/value properties itself', function() { + var string = '{{#each goodbyes}}{{@key}}. [{{@value.key}}] {{@value.value}}! {{/each}}cruel {{world}}!'; + + function Clazz() { + this['#1'] = {value: 'goodbye', key: 'html'}; + this[2] = {value: 'GOODBYE', key: 'number'}; + } + Clazz.prototype.foo = 'fail'; + var hash = {goodbyes: new Clazz(), world: 'world'}; + + // Object property iteration order is undefined according to ECMA spec, + // so we need to check both possible orders + // @see http://stackoverflow.com/questions/280713/elements-order-in-a-for-in-loop + var actual = compileWithPartials(string, hash); + var expected1 = '<b>#1</b>. [html] goodbye! 2. [number] GOODBYE! cruel world!'; + var expected2 = '2. [number] GOODBYE! <b>#1</b>. [html] goodbye! cruel world!'; + equals(actual === expected1 || actual === expected2, true, 'each with object argument iterates over the contents when not empty'); + shouldCompileTo(string, {goodbyes: {}, world: 'world'}, 'cruel world!'); + }); + + it('each with @key', function() { + var string = '{{#each goodbyes}}{{@key}}. {{text}}! {{/each}}cruel {{world}}!'; + var hash = {goodbyes: [{text: 'goodbye'}, {text: 'Goodbye'}, {text: 'GOODBYE'}], world: 'world'}; + + var template = CompilerContext.compile(string); + var result = template(hash); + + equal(result, '0. goodbye! 1. Goodbye! 2. GOODBYE! cruel world!', 'The @key variable is used'); + }); + + it('each with @key/@value', function() { + var string = '{{#each goodbyes}}{{@key}}. {{@value.text}}! {{/each}}cruel {{world}}!'; + var hash = {goodbyes: [{text: 'goodbye'}, {text: 'Goodbye'}, {text: 'GOODBYE'}], world: 'world'}; + + var template = CompilerContext.compile(string); + var result = template(hash); + + equal(result, '0. goodbye! 1. Goodbye! 2. GOODBYE! cruel world!', 'The @key/@value variables are used'); + }); + it('each with @index', function() { var string = '{{#each goodbyes}}{{@index}}. {{text}}! {{/each}}cruel {{world}}!'; var hash = {goodbyes: [{text: 'goodbye'}, {text: 'Goodbye'}, {text: 'GOODBYE'}], world: 'world'};