diff options
-rw-r--r-- | spec/qunit_spec.js | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/spec/qunit_spec.js b/spec/qunit_spec.js index b1dd1ef..293c413 100644 --- a/spec/qunit_spec.js +++ b/spec/qunit_spec.js @@ -33,7 +33,13 @@ Handlebars.registerHelper('helperMissing', function(helper, context) { function shouldCompileTo(string, hashOrArray, expected, message) { shouldCompileToWithPartials(string, hashOrArray, false, expected, message); } + function shouldCompileToWithPartials(string, hashOrArray, partials, expected, message) { + var result = compileWithPartials(string, hashOrArray, partials); + equal(result, expected, "'" + expected + "' should === '" + result + "': " + message); +} + +function compileWithPartials(string, hashOrArray, partials) { var template = CompilerContext[partials ? 'compileWithPartial' : 'compile'](string), ary; if(Object.prototype.toString.call(hashOrArray) === "[object Array]") { var helpers = hashOrArray[1]; @@ -51,8 +57,7 @@ function shouldCompileToWithPartials(string, hashOrArray, partials, expected, me ary = [hashOrArray]; } - var result = template.apply(this, ary); - equal(result, expected, "'" + expected + "' should === '" + result + "': " + message); + return template.apply(this, ary); } function shouldThrow(fn, exception, message) { @@ -685,6 +690,22 @@ test("each", function() { "each with array argument ignores the contents when empty"); }); +test("each with an object and @key", function() { + var string = "{{#each goodbyes}}{{@key}}. {{text}}! {{/each}}cruel {{world}}!"; + var hash = {goodbyes: {"<b>#1</b>": {text: "goodbye"}, 2: {text: "GOODBYE"}}, 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!"; + + ok(actual === expected1 || actual === expected2, "each with object argument iterates over the contents when not empty"); + shouldCompileTo(string, {goodbyes: [], world: "world"}, "cruel world!", + "each with object argument ignores the contents when empty"); +}); + test("each with @index", function() { var string = "{{#each goodbyes}}{{@index}}. {{text}}! {{/each}}cruel {{world}}!"; var hash = {goodbyes: [{text: "goodbye"}, {text: "Goodbye"}, {text: "GOODBYE"}], world: "world"}; |