Completed
Push — GildedRose/js ( a01e2a )
by
unknown
05:41 queued 03:38
created

lib/jasmine-ajax/mock-ajax.js   B

Complexity

Total Complexity 36
Complexity/F 1.5

Size

Lines of Code 173
Function Count 24

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
nc 2
dl 0
loc 173
rs 8.8
wmc 36
mnd 2
bc 36
fnc 24
bpm 1.5
cpm 1.5
noi 15

15 Functions

Rating   Name   Duplication   Size   Complexity  
A jasmine.Ajax.uninstallMock 0 9 3
A mock-ajax.js ➔ FakeXMLHttpRequest 0 67 1
A jasmine.Ajax.reset 0 5 1
A jasmine.Ajax.useMock 0 8 2
A jasmine.Ajax.assertInstalled 0 5 2
A jasmine.Ajax.prototypeMock 0 3 1
A jasmine.Ajax.isInstalled 0 3 1
A jasmine.Ajax.installMock 0 10 3
A mock-ajax.js ➔ clearAjaxRequests 0 3 1
A mock-ajax.js ➔ mostRecentAjaxRequest 0 7 2
A jasmine.Ajax.installPrototype 0 6 1
A jasmine.Ajax.jQueryMock 0 5 1
A jasmine.Ajax.installJquery 0 6 1
A Ajax.Request.response 0 3 1
A Ajax.Request.request 0 4 1
1
/*
2
 Jasmine-Ajax : a set of helpers for testing AJAX requests under the Jasmine
3
 BDD framework for JavaScript.
4
5
 Supports both Prototype.js and jQuery.
6
7
 http://github.com/pivotal/jasmine-ajax
8
9
 Jasmine Home page: http://pivotal.github.com/jasmine
10
11
 Copyright (c) 2008-2010 Pivotal Labs
12
13
 Permission is hereby granted, free of charge, to any person obtaining
14
 a copy of this software and associated documentation files (the
15
 "Software"), to deal in the Software without restriction, including
16
 without limitation the rights to use, copy, modify, merge, publish,
17
 distribute, sublicense, and/or sell copies of the Software, and to
18
 permit persons to whom the Software is furnished to do so, subject to
19
 the following conditions:
20
21
 The above copyright notice and this permission notice shall be
22
 included in all copies or substantial portions of the Software.
23
24
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28
 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29
 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30
 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
32
 */
33
34
// Jasmine-Ajax interface
35
var ajaxRequests = [];
36
37
function mostRecentAjaxRequest() {
38
  if (ajaxRequests.length > 0) {
39
    return ajaxRequests[ajaxRequests.length - 1];
40
  } else {
41
    return null;
42
  }
43
}
44
45
function clearAjaxRequests() {
46
  ajaxRequests = [];
47
}
48
49
// Fake XHR for mocking Ajax Requests & Responses
50
function FakeXMLHttpRequest() {
51
  var extend = Object.extend || $.extend;
52
  extend(this, {
53
    requestHeaders: {},
54
55
    open: function() {
56
      this.method = arguments[0];
57
      this.url = arguments[1];
58
      this.readyState = 1;
59
    },
60
61
    setRequestHeader: function(header, value) {
62
      this.requestHeaders[header] = value;
63
    },
64
65
    abort: function() {
66
      this.readyState = 0;
67
    },
68
69
    readyState: 0,
70
71
    onreadystatechange: function(isTimeout) {
0 ignored issues
show
Unused Code introduced by
The parameter isTimeout is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
72
    },
73
74
    status: null,
75
76
    send: function(data) {
77
      this.params = data;
78
      this.readyState = 2;
79
    },
80
81
    getResponseHeader: function(name) {
82
      return this.responseHeaders[name];
83
    },
84
85
    getAllResponseHeaders: function() {
86
      var responseHeaders = [];
87
      for (var i in this.responseHeaders) {
88
        if (this.responseHeaders.hasOwnProperty(i)) {
89
          responseHeaders.push(i + ': ' + this.responseHeaders[i]);
90
        }
91
      }
92
      return responseHeaders.join('\r\n');
93
    },
94
95
    responseText: null,
96
97
    response: function(response) {
98
      this.status = response.status;
99
      this.responseText = response.responseText || "";
100
      this.readyState = 4;
101
      this.responseHeaders = response.responseHeaders ||
102
      {"Content-type": response.contentType || "application/json" };
103
      // uncomment for jquery 1.3.x support
104
      // jasmine.Clock.tick(20);
105
106
      this.onreadystatechange();
107
    },
108
    responseTimeout: function() {
109
      this.readyState = 4;
110
      jasmine.Clock.tick(jQuery.ajaxSettings.timeout || 30000);
0 ignored issues
show
Bug introduced by
The variable jasmine seems to be never declared. If this is a global, consider adding a /** global: jasmine */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
111
      this.onreadystatechange('timeout');
112
    }
113
  });
114
115
  return this;
116
}
117
118
119
jasmine.Ajax = {
120
121
  isInstalled: function() {
122
    return jasmine.Ajax.installed == true;
0 ignored issues
show
Bug introduced by
The variable jasmine seems to be never declared. If this is a global, consider adding a /** global: jasmine */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
123
  },
124
125
  assertInstalled: function() {
126
    if (!jasmine.Ajax.isInstalled()) {
0 ignored issues
show
Bug introduced by
The variable jasmine seems to be never declared. If this is a global, consider adding a /** global: jasmine */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
127
      throw new Error("Mock ajax is not installed, use jasmine.Ajax.useMock()")
128
    }
129
  },
130
131
  useMock: function() {
132
    if (!jasmine.Ajax.isInstalled()) {
0 ignored issues
show
Bug introduced by
The variable jasmine seems to be never declared. If this is a global, consider adding a /** global: jasmine */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
133
      var spec = jasmine.getEnv().currentSpec;
134
      spec.after(jasmine.Ajax.uninstallMock);
135
136
      jasmine.Ajax.installMock();
137
    }
138
  },
139
140
  installMock: function() {
141
    if (typeof jQuery != 'undefined') {
142
      jasmine.Ajax.installJquery();
0 ignored issues
show
Bug introduced by
The variable jasmine seems to be never declared. If this is a global, consider adding a /** global: jasmine */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
143
    } else if (typeof Prototype != 'undefined') {
0 ignored issues
show
Bug introduced by
The variable Prototype seems to be never declared. If this is a global, consider adding a /** global: Prototype */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
144
      jasmine.Ajax.installPrototype();
145
    } else {
146
      throw new Error("jasmine.Ajax currently only supports jQuery and Prototype");
147
    }
148
    jasmine.Ajax.installed = true;
149
  },
150
151
  installJquery: function() {
152
    jasmine.Ajax.mode = 'jQuery';
0 ignored issues
show
Bug introduced by
The variable jasmine seems to be never declared. If this is a global, consider adding a /** global: jasmine */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
153
    jasmine.Ajax.real = jQuery.ajaxSettings.xhr;
154
    jQuery.ajaxSettings.xhr = jasmine.Ajax.jQueryMock;
155
156
  },
157
158
  installPrototype: function() {
159
    jasmine.Ajax.mode = 'Prototype';
0 ignored issues
show
Bug introduced by
The variable jasmine seems to be never declared. If this is a global, consider adding a /** global: jasmine */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
160
    jasmine.Ajax.real = Ajax.getTransport;
0 ignored issues
show
Bug introduced by
The variable Ajax seems to be never declared. If this is a global, consider adding a /** global: Ajax */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
161
162
    Ajax.getTransport = jasmine.Ajax.prototypeMock;
163
  },
164
165
  uninstallMock: function() {
166
    jasmine.Ajax.assertInstalled();
0 ignored issues
show
Bug introduced by
The variable jasmine seems to be never declared. If this is a global, consider adding a /** global: jasmine */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
167
    if (jasmine.Ajax.mode == 'jQuery') {
168
      jQuery.ajaxSettings.xhr = jasmine.Ajax.real;
169
    } else if (jasmine.Ajax.mode == 'Prototype') {
170
      Ajax.getTransport = jasmine.Ajax.real;
0 ignored issues
show
Bug introduced by
The variable Ajax seems to be never declared. If this is a global, consider adding a /** global: Ajax */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
171
    }
172
    jasmine.Ajax.reset();
173
  },
174
175
  reset: function() {
176
    jasmine.Ajax.installed = false;
0 ignored issues
show
Bug introduced by
The variable jasmine seems to be never declared. If this is a global, consider adding a /** global: jasmine */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
177
    jasmine.Ajax.mode = null;
178
    jasmine.Ajax.real = null;
179
  },
180
181
  jQueryMock: function() {
182
    var newXhr = new FakeXMLHttpRequest();
183
    ajaxRequests.push(newXhr);
184
    return newXhr;
185
  },
186
187
  prototypeMock: function() {
188
    return new FakeXMLHttpRequest();
189
  },
190
191
  installed: false,
192
  mode: null
193
}
194
195
196
// Jasmine-Ajax Glue code for Prototype.js
197
if (typeof Prototype != 'undefined' && Ajax && Ajax.Request) {
0 ignored issues
show
Best Practice introduced by
If you intend to check if the variable Ajax is declared in the current environment, consider using typeof Ajax === "undefined" instead. This is safe if the variable is not actually declared.
Loading history...
Bug introduced by
The variable Prototype seems to be never declared. If this is a global, consider adding a /** global: Prototype */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
198
  Ajax.Request.prototype.originalRequest = Ajax.Request.prototype.request;
199
  Ajax.Request.prototype.request = function(url) {
200
    this.originalRequest(url);
201
    ajaxRequests.push(this);
202
  };
203
204
  Ajax.Request.prototype.response = function(responseOptions) {
205
    return this.transport.response(responseOptions);
206
  };
207
}
208