summaryrefslogtreecommitdiffstats
path: root/spec/utils.js
blob: 7248ac4473c2d573e25248da78a72f2facc533fd (plain)
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
describe('utils', function() {
  describe('#SafeString', function() {
    it('constructing a safestring from a string and checking its type', function() {
      var safe = new Handlebars.SafeString('testing 1, 2, 3');
      if (!(safe instanceof Handlebars.SafeString)) {
        throw new Error('Must be instance of SafeString');
      }
      equals(safe.toString(), 'testing 1, 2, 3', 'SafeString is equivalent to its underlying string');
    });

    it('it should not escape SafeString properties', function() {
      var name = new Handlebars.SafeString('<em>Sean O&#x27;Malley</em>');

      shouldCompileTo('{{name}}', [{name: name}], '<em>Sean O&#x27;Malley</em>');
    });
  });

  describe('#escapeExpression', function() {
    it('shouhld escape html', function() {
      equals(Handlebars.Utils.escapeExpression('foo<&"\'>'), 'foo&lt;&amp;&quot;&#x27;&gt;');
      equals(Handlebars.Utils.escapeExpression('foo='), 'foo&#x3D;');
    });
    it('should not escape SafeString', function() {
      var string = new Handlebars.SafeString('foo<&"\'>');
      equals(Handlebars.Utils.escapeExpression(string), 'foo<&"\'>');

      var obj = {
        toHTML: function() {
          return 'foo<&"\'>';
        }
      };
      equals(Handlebars.Utils.escapeExpression(obj), 'foo<&"\'>');
    });
    it('should handle falsy', function() {
      equals(Handlebars.Utils.escapeExpression(''), '');
      equals(Handlebars.Utils.escapeExpression(undefined), '');
      equals(Handlebars.Utils.escapeExpression(null), '');

      equals(Handlebars.Utils.escapeExpression(false), 'false');
      equals(Handlebars.Utils.escapeExpression(0), '0');
    });
    it('should handle empty objects', function() {
      equals(Handlebars.Utils.escapeExpression({}), {}.toString());
      equals(Handlebars.Utils.escapeExpression([]), [].toString());
    });
  });

  describe('#isEmpty', function() {
    it('should not be empty', function() {
      equals(Handlebars.Utils.isEmpty(undefined), true);
      equals(Handlebars.Utils.isEmpty(null), true);
      equals(Handlebars.Utils.isEmpty(false), true);
      equals(Handlebars.Utils.isEmpty(''), true);
      equals(Handlebars.Utils.isEmpty([]), true);
    });

    it('should be empty', function() {
      equals(Handlebars.Utils.isEmpty(0), false);
      equals(Handlebars.Utils.isEmpty([1]), false);
      equals(Handlebars.Utils.isEmpty('foo'), false);
      equals(Handlebars.Utils.isEmpty({bar: 1}), false);
    });
  });

  describe('#extend', function() {
    it('should ignore prototype values', function() {
      function A() {
        this.a = 1;
      }
      A.prototype.b = 4;

      var b = {b: 2};

      Handlebars.Utils.extend(b, new A());

      equals(b.a, 1);
      equals(b.b, 2);
    });
  });
});