aboutsummaryrefslogtreecommitdiffstats
path: root/test/helper.js
blob: 64346bd154062f84addddf7e3c082743086aeab3 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
(function() {

  // lazy defined navigator causes global leak warnings...

  var requireBak;
  var specialRequires = {
    'chai': requireChai
  };

  testSupport = {
    isNode: (typeof(window) === 'undefined')
  };


  /* stream hack for SAX */

  if (!testSupport.isNode) {
    window.navigator;
    requireBak = require;

    require = function require_shim(type) {
      if (type === 'stream') {
        throw new Error('this is not node');
      }

      requireBak.apply(this, arguments);
    }
  }

  /* cross require */

  testSupport.require = function cross_require(file, callback) {
    if (file in specialRequires) {
      return specialRequires[file](file, callback);
    }

    if (!(/\.js$/.test(file))) {
      file += '.js';
    }

    if (typeof(window) === 'undefined') {
      var lib = require(__dirname + '/../' + file);
      if (typeof(callback) !== 'undefined') {
        callback(lib);
      }
    } else {
      window.require(file, callback);
    }
  }

  function setupChai(chai) {
    chai.Assertion.includeStack = true;
    assert = chai.assert;
  }

  function requireChai(file, callback) {
    var path;
    if (testSupport.isNode) {
      setupChai(require('chai'));
    } else {
      require('/vendor/chai.js', function() {
        setupChai(chai);
      });
    }
  }

  testSupport.require('chai');

  if (!testSupport.isNode) {
    testSupport.require('/vendor/sax');
  }

  testSupport.loadSample = function(file, cb) {
    if (testSupport.isNode) {
      var root = __dirname + '/../samples/';
      require('fs').readFile(root + file, 'utf8', function(err, contents) {
        cb(err, contents);
      });
    } else {
      var xhr = new XMLHttpRequest();
      xhr.open('GET', '/samples/' + file, true);
      xhr.onreadystatechange = function() {
        if (xhr.readyState === 4) {
          if (xhr.status !== 200) {
            cb(new Error('file not found or other error', xhr));
          } else {
            cb(null, xhr.responseText);
          }
        }
      }
      xhr.send(null);
    }
  };

  testSupport.defineSample = function(file, cb) {
    suiteSetup(function(done) {
      testSupport.loadSample(file, function(err, data) {
        if (err) {
          done(err);
        }
        cb(data);
        done();
      });
    });
  };

  testSupport.mock = {

    /**
     * Mocks out a method
     *
     *    var called = testSupport.mock.method(subject, 'myMethod');
     *
     *    subject.myMethod('foo', 'bar');
     *
     *    called.args[0] === 'foo'; // true
     *    called.args[1] === 'bar'; // true
     *
     *
     * @return {Object} reference object.
     */
    method: function(obj, method, times) {
      var calledWith = {};
      var calls = 0;

      if (typeof(times) === 'undefined') {
        times = 1;
      }

      obj[method] = function() {
        if (calls.length > times) {
          throw new Error(
            method + ' called more then ' + times + 'time(s)'
          );
        }

        calledWith.args = arguments;
        calls++;
      };

      return calledWith;
    }

  };

  testSupport.lib = function(lib, callback) {
     testSupport.require('/lib/caldav/' + lib, callback);
  };

  testSupport.helper = function(lib) {
    testSupport.require('/test/support/' + lib);
  }

  Caldav = require('../lib/caldav/caldav.js');

  if (typeof(window) === 'undefined') {
    //in node we need to hack Caldav to do the right thing.
    var oldRequire = Caldav.require;

    Caldav.require = function exportRequireDev(path) {
      if (path.indexOf('support') === 0) {
        path = __dirname + '/' + path;
        return require(path);
      }
      return oldRequire(path);
    }
  }

  requireRequest = function(callback) {
    testSupport.lib('responder');
    testSupport.lib('xhr');
    testSupport.lib('connection');
    testSupport.lib('sax');
    testSupport.lib('sax/base');
    testSupport.lib('sax/dav_response');
    testSupport.lib('request/abstract');
    testSupport.lib('template');
    testSupport.helper('fake_xhr');
    testSupport.lib('request/propfind');

    //in the future we need a callback for browser support.
    if (typeof(callback) !== 'undefined') {
      callback();
    }
  };

}());