aboutsummaryrefslogtreecommitdiffstats
path: root/test/caldav/template_test.js
blob: 3853c6e75da6ec3c06dc460df0d02c8aeea29b84 (plain) (blame)
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
testSupport.lib('template');

suite('templates', function() {

  var Template,
      subject;

  suiteSetup(function() {
    Template = Caldav.require('template');
  });

  setup(function() {
    subject = new Template('propget', { prop: 'val' });
  });

  test('initializer', function() {
    assert.equal(subject.rootTag, 'propget');
    assert.deepEqual(subject.activeNamespaces, {});
    assert.deepEqual(subject.rootAttrs, {
      prop: 'val'
    });
  });

  suite('#_registerTag', function() {
    var tag = 'foo';
    var prefix = 'dav';
    var firstOut;

    setup(function() {
      firstOut = subject._registerTag(prefix, tag);
    });

    test('same tag', function() {
      assert.deepEqual(
        subject.activeNamespaces['DAV:'],
        'N0',
        'should register namespace'
      );

      assert.equal(
        subject.rootAttrs['xmlns:N0'],
        'DAV:',
        'should add xmlns to root attrs'
      );

      assert.equal(
        firstOut,
        subject._registerTag(prefix, tag),
        'tag should render the same each time'
      );
    });

    test('second tag', function() {
      var result = subject._registerTag('ical', 'baz');

      assert.equal(
        result,
        'N1:baz'
      );
    });
  });

  suite('#tag', function() {

    test('when given a non-existant namespace', function() {
      var out = subject.tag(['foo', 'bar'], 'content');

      assert.deepEqual(
        subject.activeNamespaces['foo'],
        'N0'
      );

      assert.equal(out, '<N0:bar>content</N0:bar>');
    });

    test('self closing', function() {
      var out = subject.tag('baz', { type: 'text' });
      assert.equal(
        out,
        '<N0:baz type="text" />'
      );
    });

    test('when given a an object /w .render in content', function() {
      var obj = {
        render: function(template) {
          return template.tag('baz');
        }
      };

      var out = subject.tag('foo', obj);

      assert.equal(
        out,
        '<N0:foo><N0:baz /></N0:foo>'
      );
    });

    test('with attrs', function() {
      var out = subject.tag('bar', {
        value: 'val'
      }, 'foo');

      assert.deepEqual(
        subject.activeNamespaces['DAV:'],
        'N0'
      );

      assert.equal(
        out,
        '<N0:bar value="val">foo</N0:bar>'
      );
    });

    test('given an array', function() {
      var out = subject.tag(
        ['bar', 'baz'],
        'foo'
      );

      assert.equal(
        out,
        '<N0:baz>foo</N0:baz>'
      );
    });

    test('given a string', function() {
      var out = subject.tag('baz', 'foo');
      assert.equal(
        out,
        '<N0:baz>foo</N0:baz>'
      );
    });
  });

  test('#render', function() {
    var tag = subject.tag('href', 'value');
    var out = subject.render(tag);
    var output = '';

    output += subject.doctype;
    output += '<N0:propget prop="val" xmlns:N0="DAV:">';
    output += tag;
    output += '</N0:propget>';

    assert.equal(subject.render(tag), output);
  });

});