summaryrefslogtreecommitdiffstats
path: root/spec/qunit_spec.js
diff options
context:
space:
mode:
Diffstat (limited to 'spec/qunit_spec.js')
-rw-r--r--spec/qunit_spec.js77
1 files changed, 76 insertions, 1 deletions
diff --git a/spec/qunit_spec.js b/spec/qunit_spec.js
index 5c53e97..aca8fcb 100644
--- a/spec/qunit_spec.js
+++ b/spec/qunit_spec.js
@@ -616,7 +616,7 @@ test("if with function argument", function() {
});
test("each", function() {
- var string = "{{#each goodbyes}}{{text}}! {{/each}}cruel {{world}}!"
+ var string = "{{#each goodbyes}}{{text}}! {{/each}}cruel {{world}}!";
var hash = {goodbyes: [{text: "goodbye"}, {text: "Goodbye"}, {text: "GOODBYE"}], world: "world"};
shouldCompileTo(string, hash, "goodbye! Goodbye! GOODBYE! cruel world!",
"each with array argument iterates over the contents when not empty");
@@ -624,6 +624,16 @@ test("each", function() {
"each with array 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"};
+
+ var template = CompilerContext.compile(string);
+ var result = template(hash);
+
+ equal(result, "0. goodbye! 1. Goodbye! 2. GOODBYE! cruel world!", "The @index variable is used");
+});
+
test("log", function() {
var string = "{{log blah}}"
var hash = { blah: "whee" };
@@ -655,6 +665,71 @@ test("passing in data to a compiled function that expects data - works with help
equals("happy cat", result, "Data output by helper");
});
+test("data can be looked up via @foo", function() {
+ var template = CompilerContext.compile("{{@hello}}");
+ var result = template({}, { data: { hello: "hello" } });
+ equals("hello", result, "@foo retrieves template data");
+});
+
+var objectCreate = Handlebars.createFrame;
+
+test("deep @foo triggers automatic top-level data", function() {
+ var template = CompilerContext.compile('{{#let world="world"}}{{#if foo}}{{#if foo}}Hello {{@world}}{{/if}}{{/if}}{{/let}}');
+
+ var helpers = objectCreate(Handlebars.helpers);
+
+ helpers.let = function(options) {
+ var frame = Handlebars.createFrame(options.data);
+
+ for (var prop in options.hash) {
+ frame[prop] = options.hash[prop];
+ }
+ return options.fn(this, { data: frame });
+ };
+
+ var result = template({ foo: true }, { helpers: helpers });
+ equals("Hello world", result, "Automatic data was triggered");
+});
+
+test("parameter data can be looked up via @foo", function() {
+ var template = CompilerContext.compile("{{hello @world}}");
+ var helpers = {
+ hello: function(noun) {
+ return "Hello " + noun;
+ }
+ };
+
+ var result = template({}, { helpers: helpers, data: { world: "world" } });
+ equals("Hello world", result, "@foo as a parameter retrieves template data");
+});
+
+test("hash values can be looked up via @foo", function() {
+ var template = CompilerContext.compile("{{hello noun=@world}}");
+ var helpers = {
+ hello: function(options) {
+ return "Hello " + options.hash.noun;
+ }
+ };
+
+ var result = template({}, { helpers: helpers, data: { world: "world" } });
+ equals("Hello world", result, "@foo as a parameter retrieves template data");
+});
+
+test("data is inherited downstream", function() {
+ var template = CompilerContext.compile("{{#let foo=bar.baz}}{{@foo}}{{/let}}", { data: true });
+ var helpers = {
+ let: function(options) {
+ for (var prop in options.hash) {
+ options.data[prop] = options.hash[prop];
+ }
+ return options.fn(this);
+ }
+ };
+
+ var result = template({ bar: { baz: "hello world" } }, { helpers: helpers, data: {} });
+ equals("hello world", result, "data variables are inherited downstream");
+});
+
test("passing in data to a compiled function that expects data - works with helpers in partials", function() {
var template = CompilerContext.compile("{{>my_partial}}", {data: true});