Code coverage report for spec/track-ids.js

Statements: 27.38% (23 / 84)      Branches: 100% (0 / 0)      Functions: 16.67% (5 / 30)      Lines: 27.38% (23 / 84)      Ignored: none     

All files » spec/ » track-ids.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164  3 3 3       3                           3                           3                           3                           3                             3                             3                             3 3           3 3         3         3           3 3         3           3 3         3         3                
/*global CompilerContext */
describe('track ids', function() {
  var context;
  beforeEach(function() {
    context = {is: {a: 'foo'}, slave: {driver: 'bar'}};
  });
 
  it('should not include anything without the flag', function() {
    var template = CompilerContext.compile('{{wycats is.a slave.driver}}');
 
    var helpers = {
      wycats: function(passiveVoice, noun, options) {
        equal(options.ids, undefined);
        equal(options.hashIds, undefined);
 
        return 'success';
      }
    };
 
    equals(template({}, {helpers: helpers}), 'success');
  });
  it('should include argument ids', function() {
    var template = CompilerContext.compile('{{wycats is.a slave.driver}}', {trackIds: true});
 
    var helpers = {
      wycats: function(passiveVoice, noun, options) {
        equal(options.ids[0], 'is.a');
        equal(options.ids[1], 'slave.driver');
 
        return "HELP ME MY BOSS " + options.ids[0] + ':' + passiveVoice + ' ' + options.ids[1] + ':' + noun;
      }
    };
 
    equals(template(context, {helpers: helpers}), 'HELP ME MY BOSS is.a:foo slave.driver:bar');
  });
  it('should include hash ids', function() {
    var template = CompilerContext.compile('{{wycats bat=is.a baz=slave.driver}}', {trackIds: true});
 
    var helpers = {
      wycats: function(options) {
        equal(options.hashIds.bat, 'is.a');
        equal(options.hashIds.baz, 'slave.driver');
 
        return "HELP ME MY BOSS " + options.hashIds.bat + ':' + options.hash.bat + ' ' + options.hashIds.baz + ':' + options.hash.baz;
      }
    };
 
    equals(template(context, {helpers: helpers}), 'HELP ME MY BOSS is.a:foo slave.driver:bar');
  });
  it('should note ../ and ./ references', function() {
    var template = CompilerContext.compile('{{wycats ./is.a ../slave.driver}}', {trackIds: true});
 
    var helpers = {
      wycats: function(passiveVoice, noun, options) {
        equal(options.ids[0], 'is.a');
        equal(options.ids[1], '../slave.driver');
 
        return "HELP ME MY BOSS " + options.ids[0] + ':' + passiveVoice + ' ' + options.ids[1] + ':' + noun;
      }
    };
 
    equals(template(context, {helpers: helpers}), 'HELP ME MY BOSS is.a:foo ../slave.driver:undefined');
  });
  it('should note @data references', function() {
    var template = CompilerContext.compile('{{wycats @is.a @slave.driver}}', {trackIds: true});
 
    var helpers = {
      wycats: function(passiveVoice, noun, options) {
        equal(options.ids[0], '@is.a');
        equal(options.ids[1], '@slave.driver');
 
        return "HELP ME MY BOSS " + options.ids[0] + ':' + passiveVoice + ' ' + options.ids[1] + ':' + noun;
      }
    };
 
    equals(template({}, {helpers: helpers, data:context}), 'HELP ME MY BOSS @is.a:foo @slave.driver:bar');
  });
 
  it('should return null for constants', function() {
    var template = CompilerContext.compile('{{wycats 1 "foo" key=false}}', {trackIds: true});
 
    var helpers = {
      wycats: function(passiveVoice, noun, options) {
        equal(options.ids[0], null);
        equal(options.ids[1], null);
        equal(options.hashIds.key, null);
 
        return "HELP ME MY BOSS " + passiveVoice + ' ' + noun + ' ' + options.hash.key;
      }
    };
 
    equals(template(context, {helpers: helpers}), 'HELP ME MY BOSS 1 foo false');
  });
  it('should return true for subexpressions', function() {
    var template = CompilerContext.compile('{{wycats (sub)}}', {trackIds: true});
 
    var helpers = {
      sub: function() { return 1; },
      wycats: function(passiveVoice, options) {
        equal(options.ids[0], true);
 
        return "HELP ME MY BOSS " + passiveVoice;
      }
    };
 
    equals(template(context, {helpers: helpers}), 'HELP ME MY BOSS 1');
  });
 
  describe('builtin helpers', function() {
    var helpers = {
      wycats: function(name, options) {
        return name + ':' + options.data.contextPath + '\n';
      }
    };
 
    describe('#each', function() {
      it('should track contextPath for arrays', function() {
        var template = CompilerContext.compile('{{#each array}}{{wycats name}}{{/each}}', {trackIds: true});
 
        equals(template({array: [{name: 'foo'}, {name: 'bar'}]}, {helpers: helpers}), 'foo:array.0\nbar:array.1\n');
      });
      it('should track contextPath for keys', function() {
        var template = CompilerContext.compile('{{#each object}}{{wycats name}}{{/each}}', {trackIds: true});
 
        equals(template({object: {foo: {name: 'foo'}, bar: {name: 'bar'}}}, {helpers: helpers}), 'foo:object.foo\nbar:object.bar\n');
      });
      it('should handle nesting', function() {
        var template = CompilerContext.compile('{{#each .}}{{#each .}}{{wycats name}}{{/each}}{{/each}}', {trackIds: true});
 
        equals(template({array: [{name: 'foo'}, {name: 'bar'}]}, {helpers: helpers}), 'foo:.array..0\nbar:.array..1\n');
      });
    });
    describe('#with', function() {
      it('should track contextPath', function() {
        var template = CompilerContext.compile('{{#with field}}{{wycats name}}{{/with}}', {trackIds: true});
 
        equals(template({field: {name: 'foo'}}, {helpers: helpers}), 'foo:field\n');
      });
      it('should handle nesting', function() {
        var template = CompilerContext.compile('{{#with bat}}{{#with field}}{{wycats name}}{{/with}}{{/with}}', {trackIds: true});
 
        equals(template({bat: {field: {name: 'foo'}}}, {helpers: helpers}), 'foo:bat.field\n');
      });
    });
    describe('#blockHelperMissing', function() {
      it('should track contextPath for arrays', function() {
        var template = CompilerContext.compile('{{#field}}{{wycats name}}{{/field}}', {trackIds: true});
 
        equals(template({field: [{name: 'foo'}]}, {helpers: helpers}), 'foo:field.0\n');
      });
      it('should track contextPath for keys', function() {
        var template = CompilerContext.compile('{{#field}}{{wycats name}}{{/field}}', {trackIds: true});
 
        equals(template({field: {name: 'foo'}}, {helpers: helpers}), 'foo:field\n');
      });
      it('should handle nesting', function() {
        var template = CompilerContext.compile('{{#bat}}{{#field}}{{wycats name}}{{/field}}{{/bat}}', {trackIds: true});
 
        equals(template({bat: {field: {name: 'foo'}}}, {helpers: helpers}), 'foo:bat.field\n');
      });
    });
  });
});