summaryrefslogtreecommitdiffstats
path: root/spec/blocks.js
diff options
context:
space:
mode:
Diffstat (limited to 'spec/blocks.js')
-rw-r--r--spec/blocks.js138
1 files changed, 138 insertions, 0 deletions
diff --git a/spec/blocks.js b/spec/blocks.js
index 7622634..2fbaee7 100644
--- a/spec/blocks.js
+++ b/spec/blocks.js
@@ -168,6 +168,144 @@ describe('blocks', function() {
});
describe('decorators', function() {
+ it('should apply mustache decorators', function() {
+ var helpers = {
+ helper: function(options) {
+ return options.fn.run;
+ }
+ };
+ var decorators = {
+ decorator: function(fn) {
+ fn.run = 'success';
+ return fn;
+ }
+ };
+ shouldCompileTo(
+ '{{#helper}}{{*decorator}}{{/helper}}',
+ {hash: {}, helpers: helpers, decorators: decorators},
+ 'success');
+ });
+ it('should apply allow undefined return', function() {
+ var helpers = {
+ helper: function(options) {
+ return options.fn() + options.fn.run;
+ }
+ };
+ var decorators = {
+ decorator: function(fn) {
+ fn.run = 'cess';
+ }
+ };
+ shouldCompileTo(
+ '{{#helper}}{{*decorator}}suc{{/helper}}',
+ {hash: {}, helpers: helpers, decorators: decorators},
+ 'success');
+ });
+
+ it('should apply block decorators', function() {
+ var helpers = {
+ helper: function(options) {
+ return options.fn.run;
+ }
+ };
+ var decorators = {
+ decorator: function(fn, props, container, options) {
+ fn.run = options.fn();
+ return fn;
+ }
+ };
+ shouldCompileTo(
+ '{{#helper}}{{#*decorator}}success{{/decorator}}{{/helper}}',
+ {hash: {}, helpers: helpers, decorators: decorators},
+ 'success');
+ });
+ it('should support nested decorators', function() {
+ var helpers = {
+ helper: function(options) {
+ return options.fn.run;
+ }
+ };
+ var decorators = {
+ decorator: function(fn, props, container, options) {
+ fn.run = options.fn.nested + options.fn();
+ return fn;
+ },
+ nested: function(fn, props, container, options) {
+ props.nested = options.fn();
+ }
+ };
+ shouldCompileTo(
+ '{{#helper}}{{#*decorator}}{{#*nested}}suc{{/nested}}cess{{/decorator}}{{/helper}}',
+ {hash: {}, helpers: helpers, decorators: decorators},
+ 'success');
+ });
+
+ it('should apply multiple decorators', function() {
+ var helpers = {
+ helper: function(options) {
+ return options.fn.run;
+ }
+ };
+ var decorators = {
+ decorator: function(fn, props, container, options) {
+ fn.run = (fn.run || '') + options.fn();
+ return fn;
+ }
+ };
+ shouldCompileTo(
+ '{{#helper}}{{#*decorator}}suc{{/decorator}}{{#*decorator}}cess{{/decorator}}{{/helper}}',
+ {hash: {}, helpers: helpers, decorators: decorators},
+ 'success');
+ });
+
+ it('should access parent variables', function() {
+ var helpers = {
+ helper: function(options) {
+ return options.fn.run;
+ }
+ };
+ var decorators = {
+ decorator: function(fn, props, container, options) {
+ fn.run = options.args;
+ return fn;
+ }
+ };
+ shouldCompileTo(
+ '{{#helper}}{{*decorator foo}}{{/helper}}',
+ {hash: {'foo': 'success'}, helpers: helpers, decorators: decorators},
+ 'success');
+ });
+ it('should work with root program', function() {
+ var run;
+ var decorators = {
+ decorator: function(fn, props, container, options) {
+ equals(options.args[0], 'success');
+ run = true;
+ return fn;
+ }
+ };
+ shouldCompileTo(
+ '{{*decorator "success"}}',
+ {hash: {'foo': 'success'}, decorators: decorators},
+ '');
+ equals(run, true);
+ });
+ it('should fail when accessing variables from root', function() {
+ var run;
+ var decorators = {
+ decorator: function(fn, props, container, options) {
+ equals(options.args[0], undefined);
+ run = true;
+ return fn;
+ }
+ };
+ shouldCompileTo(
+ '{{*decorator foo}}',
+ {hash: {'foo': 'fail'}, decorators: decorators},
+ '');
+ equals(run, true);
+ });
+
describe('registration', function() {
it('unregisters', function() {
handlebarsEnv.decorators = {};