aboutsummaryrefslogtreecommitdiffstats
path: root/test/webcals/request/abstract_test.js
blob: 23bfa8c35c29d6798931f6e93f7ee3961ed6d78c (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
requireLib('xhr');
requireLib('sax');
requireLib('request/abstract');
requireSupport('fake_xhr');

suite('webcals/request/abstract.js', function() {
  var subject;
  var Abstract;
  var Xhr;
  var FakeXhr;
  var SAX;
  var oldXhrClass;
  var url = 'http://google.com/';
  var options = {
    configOpt: true
  };

  suiteSetup(function() {
    Abstract = Webcals.require('request/abstract');
    FakeXhr = Webcals.require('support/fake_xhr');
    Xhr = Webcals.require('xhr');
    SAX = Webcals.require('sax');

    oldXhrClass = Xhr.prototype.xhrClass;
    Xhr.prototype.xhrClass = FakeXhr;
  });

  suiteTeardown(function() {
    Xhr.prototype.xhrClass = oldXhrClass;
  });

  setup(function() {
    subject = new Abstract(url, options);
    FakeXhr.instances.length = 0;
  });

  test('#_createXhr', function() {
    var result = subject._createXhr();
    assert.instanceOf(result, Xhr);
    assert.equal(result.url, url);
  });

  test('#_createPayload', function() {
    assert.equal(subject._createPayload(), '');
  });

  test('#initializer', function() {
    assert.equal(subject.url, url);
    assert.equal(subject.configOpt, options.configOpt);
    assert.instanceOf(subject.sax, SAX);
  });

  suite('#send', function(done) {
    var xhr;

    function getXhr() {
      return FakeXhr.instances.pop();
    }

    suite('error', function() {
      var calledWith;

      setup(function(done) {
        subject.send(function() {
          calledWith = arguments;
          done();
        });


        xhr = getXhr();
        xhr.respond('NOT XML <div>', 500);
      });

      test('on response', function() {
        assert.match(calledWith[0].message, /http error/);
      });
    });

    suite('success', function() {
      var calledWith;
      var xml = '<el><item>value</item></el>';

      setup(function(done) {
        subject.send(function() {
          calledWith = arguments;
          done();
        });

        xhr = getXhr();
        xhr.respond(xml, 200);
      });

      test('on response', function() {
        assert.equal(calledWith[0], null);
        assert.deepEqual(calledWith[1], {
          el: {
            item: { value: 'value' }
          }
        });
        assert.equal(calledWith[2].xhr, xhr);
      });
    });
  });

});