jasmine-ajax 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/.pairs +6 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +35 -0
- data/MIT.LICENSE +20 -0
- data/README.markdown +168 -0
- data/Rakefile +3 -0
- data/examples/jquery/Rakefile +2 -0
- data/examples/jquery/public/css/master.css +59 -0
- data/examples/jquery/public/css/reset.css +47 -0
- data/examples/jquery/public/images/fail-whale.png +0 -0
- data/examples/jquery/public/index.html +60 -0
- data/examples/jquery/public/javascripts/Tweet.js +6 -0
- data/examples/jquery/public/javascripts/TwitSearch.js +32 -0
- data/examples/jquery/public/javascripts/TwitterApi.js +31 -0
- data/examples/jquery/spec/SpecRunner.html +32 -0
- data/examples/jquery/spec/javascripts/TweetSpec.js +24 -0
- data/examples/jquery/spec/javascripts/TwitterApiSpec.js +88 -0
- data/examples/jquery/spec/javascripts/helpers/test_responses/search.js +16 -0
- data/examples/jquery/spec/javascripts/helpers/tweets.js +3 -0
- data/examples/jquery/spec/javascripts/jasmine-0.11.1/jasmine-html.js +182 -0
- data/examples/jquery/spec/javascripts/jasmine-0.11.1/jasmine.css +166 -0
- data/examples/jquery/spec/javascripts/jasmine-0.11.1/jasmine.js +2343 -0
- data/examples/jquery/spec/javascripts/support/jasmine.yml +78 -0
- data/examples/jquery/spec/javascripts/support/jasmine_runner.rb +21 -0
- data/examples/prototype/Rakefile +2 -0
- data/examples/prototype/public/css/master.css +59 -0
- data/examples/prototype/public/css/reset.css +47 -0
- data/examples/prototype/public/images/fail-whale.png +0 -0
- data/examples/prototype/public/index.html +45 -0
- data/examples/prototype/public/javascripts/Tweet.js +6 -0
- data/examples/prototype/public/javascripts/TwitSearch.js +32 -0
- data/examples/prototype/public/javascripts/TwitterApi.js +24 -0
- data/examples/prototype/spec/SpecRunner.html +32 -0
- data/examples/prototype/spec/javascripts/TweetSpec.js +24 -0
- data/examples/prototype/spec/javascripts/TwitterApiSpec.js +89 -0
- data/examples/prototype/spec/javascripts/helpers/test_responses/search.js +16 -0
- data/examples/prototype/spec/javascripts/helpers/tweets.js +3 -0
- data/examples/prototype/spec/javascripts/jasmine-0.11.1/jasmine-html.js +182 -0
- data/examples/prototype/spec/javascripts/jasmine-0.11.1/jasmine.css +166 -0
- data/examples/prototype/spec/javascripts/jasmine-0.11.1/jasmine.js +2343 -0
- data/examples/prototype/spec/javascripts/support/jasmine.yml +78 -0
- data/examples/prototype/spec/javascripts/support/jasmine_runner.rb +21 -0
- data/frameworks/jquery.js +8176 -0
- data/frameworks/prototype.js +4874 -0
- data/lib/assets/javascripts/mock-ajax.js +207 -0
- data/lib/spec-helper.js +0 -0
- data/lib/version.rb +5 -0
- data/spec/javascripts/fake-xml-http-request-spec.js +45 -0
- data/spec/javascripts/helpers/spec-helper.js +5 -0
- data/spec/javascripts/mock-ajax-jquery-spec.js +374 -0
- data/spec/javascripts/mock-ajax-prototypejs-spec.js +287 -0
- data/spec/javascripts/mock-ajax-spec.js +193 -0
- data/spec/javascripts/support/jasmine.yml +81 -0
- data/spec/javascripts/support/jasmine_runner.rb +21 -0
- metadata +108 -0
@@ -0,0 +1,207 @@
|
|
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) {
|
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);
|
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;
|
123
|
+
},
|
124
|
+
|
125
|
+
assertInstalled: function() {
|
126
|
+
if (!jasmine.Ajax.isInstalled()) {
|
127
|
+
throw new Error("Mock ajax is not installed, use jasmine.Ajax.useMock()")
|
128
|
+
}
|
129
|
+
},
|
130
|
+
|
131
|
+
useMock: function() {
|
132
|
+
if (!jasmine.Ajax.isInstalled()) {
|
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();
|
143
|
+
} else if (typeof Prototype != 'undefined') {
|
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';
|
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';
|
160
|
+
jasmine.Ajax.real = Ajax.getTransport;
|
161
|
+
|
162
|
+
Ajax.getTransport = jasmine.Ajax.prototypeMock;
|
163
|
+
},
|
164
|
+
|
165
|
+
uninstallMock: function() {
|
166
|
+
jasmine.Ajax.assertInstalled();
|
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;
|
171
|
+
}
|
172
|
+
jasmine.Ajax.reset();
|
173
|
+
},
|
174
|
+
|
175
|
+
reset: function() {
|
176
|
+
jasmine.Ajax.installed = false;
|
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) {
|
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
|
+
}
|
data/lib/spec-helper.js
ADDED
File without changes
|
data/lib/version.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
describe("FakeXMLHttpRequest", function() {
|
2
|
+
var xhr;
|
3
|
+
beforeEach(function() {
|
4
|
+
xhr = new FakeXMLHttpRequest();
|
5
|
+
});
|
6
|
+
it("should have an initial readyState of 0 (uninitialized)", function() {
|
7
|
+
expect(xhr.readyState).toEqual(0);
|
8
|
+
});
|
9
|
+
describe("when opened", function() {
|
10
|
+
beforeEach(function() {
|
11
|
+
xhr.open("GET", "http://example.com")
|
12
|
+
});
|
13
|
+
it("should have a readyState of 1 (open)", function() {
|
14
|
+
expect(xhr.readyState).toEqual(1);
|
15
|
+
});
|
16
|
+
|
17
|
+
describe("when sent", function() {
|
18
|
+
it("should have a readyState of 2 (sent)", function() {
|
19
|
+
xhr.send(null);
|
20
|
+
expect(xhr.readyState).toEqual(2);
|
21
|
+
});
|
22
|
+
});
|
23
|
+
|
24
|
+
describe("when a response comes in", function() {
|
25
|
+
it("should have a readyState of 4 (loaded)", function() {
|
26
|
+
xhr.response({status: 200});
|
27
|
+
expect(xhr.readyState).toEqual(4);
|
28
|
+
});
|
29
|
+
});
|
30
|
+
|
31
|
+
describe("when aborted", function() {
|
32
|
+
it("should have a readyState of 0 (uninitialized)", function() {
|
33
|
+
xhr.abort();
|
34
|
+
expect(xhr.readyState).toEqual(0);
|
35
|
+
});
|
36
|
+
});
|
37
|
+
});
|
38
|
+
|
39
|
+
it("can be extended", function(){
|
40
|
+
FakeXMLHttpRequest.prototype.foo = function(){
|
41
|
+
return "foo";
|
42
|
+
}
|
43
|
+
expect(new FakeXMLHttpRequest().foo()).toEqual("foo");
|
44
|
+
});
|
45
|
+
});
|
@@ -0,0 +1,374 @@
|
|
1
|
+
describe("Jasmine Mock Ajax (for jQuery)", function() {
|
2
|
+
var request, anotherRequest, response;
|
3
|
+
var success, error, complete;
|
4
|
+
var sharedContext = {};
|
5
|
+
var prototype = Prototype;
|
6
|
+
|
7
|
+
beforeEach(function() {
|
8
|
+
Prototype = undefined;
|
9
|
+
jasmine.Ajax.useMock();
|
10
|
+
|
11
|
+
success = jasmine.createSpy("onSuccess");
|
12
|
+
error = jasmine.createSpy("onFailure");
|
13
|
+
complete = jasmine.createSpy("onComplete");
|
14
|
+
});
|
15
|
+
|
16
|
+
afterEach(function() {
|
17
|
+
Prototype = prototype;
|
18
|
+
});
|
19
|
+
|
20
|
+
describe("when making a request", function () {
|
21
|
+
beforeEach(function() {
|
22
|
+
jQuery.ajax({
|
23
|
+
url: "example.com/someApi",
|
24
|
+
type: "GET",
|
25
|
+
success: success,
|
26
|
+
complete: complete,
|
27
|
+
error: error
|
28
|
+
});
|
29
|
+
request = mostRecentAjaxRequest();
|
30
|
+
});
|
31
|
+
|
32
|
+
it("should store URL and transport", function() {
|
33
|
+
expect(request.url).toEqual("example.com/someApi");
|
34
|
+
});
|
35
|
+
|
36
|
+
it("should queue the request", function() {
|
37
|
+
expect(ajaxRequests.length).toEqual(1);
|
38
|
+
});
|
39
|
+
|
40
|
+
it("should allow access to the queued request", function() {
|
41
|
+
expect(ajaxRequests[0]).toEqual(request);
|
42
|
+
});
|
43
|
+
|
44
|
+
describe("and then another request", function () {
|
45
|
+
beforeEach(function() {
|
46
|
+
jQuery.ajax({
|
47
|
+
url: "example.com/someApi",
|
48
|
+
type: "GET",
|
49
|
+
success: success,
|
50
|
+
complete: complete,
|
51
|
+
error: error
|
52
|
+
});
|
53
|
+
anotherRequest = mostRecentAjaxRequest();
|
54
|
+
});
|
55
|
+
|
56
|
+
it("should queue the next request", function() {
|
57
|
+
expect(ajaxRequests.length).toEqual(2);
|
58
|
+
});
|
59
|
+
|
60
|
+
it("should allow access to the other queued request", function() {
|
61
|
+
expect(ajaxRequests[1]).toEqual(anotherRequest);
|
62
|
+
});
|
63
|
+
});
|
64
|
+
|
65
|
+
describe("mostRecentAjaxRequest", function () {
|
66
|
+
|
67
|
+
describe("when there is one request queued", function () {
|
68
|
+
it("should return the request", function() {
|
69
|
+
expect(mostRecentAjaxRequest()).toEqual(request);
|
70
|
+
});
|
71
|
+
});
|
72
|
+
|
73
|
+
describe("when there is more than one request", function () {
|
74
|
+
beforeEach(function() {
|
75
|
+
jQuery.ajax({
|
76
|
+
url: "example.com/someApi",
|
77
|
+
type: "GET",
|
78
|
+
success: success,
|
79
|
+
complete: complete,
|
80
|
+
error: error
|
81
|
+
});
|
82
|
+
anotherRequest = mostRecentAjaxRequest();
|
83
|
+
});
|
84
|
+
|
85
|
+
it("should return the most recent request", function() {
|
86
|
+
expect(mostRecentAjaxRequest()).toEqual(anotherRequest);
|
87
|
+
});
|
88
|
+
});
|
89
|
+
|
90
|
+
describe("when there are no requests", function () {
|
91
|
+
beforeEach(function() {
|
92
|
+
clearAjaxRequests();
|
93
|
+
});
|
94
|
+
|
95
|
+
it("should return null", function() {
|
96
|
+
expect(mostRecentAjaxRequest()).toEqual(null);
|
97
|
+
});
|
98
|
+
});
|
99
|
+
});
|
100
|
+
|
101
|
+
describe("clearAjaxRequests()", function () {
|
102
|
+
beforeEach(function() {
|
103
|
+
clearAjaxRequests();
|
104
|
+
});
|
105
|
+
|
106
|
+
it("should remove all requests", function() {
|
107
|
+
expect(ajaxRequests.length).toEqual(0);
|
108
|
+
expect(mostRecentAjaxRequest()).toEqual(null);
|
109
|
+
});
|
110
|
+
});
|
111
|
+
});
|
112
|
+
|
113
|
+
describe("when simulating a response with request.response", function () {
|
114
|
+
describe("and the response is Success", function () {
|
115
|
+
beforeEach(function() {
|
116
|
+
jQuery.ajax({
|
117
|
+
url: "example.com/someApi",
|
118
|
+
type: "GET",
|
119
|
+
dataType: 'text',
|
120
|
+
success: success,
|
121
|
+
complete: complete,
|
122
|
+
error: error
|
123
|
+
});
|
124
|
+
request = mostRecentAjaxRequest();
|
125
|
+
response = {status: 200, contentType: "text/html", responseText: "OK!"};
|
126
|
+
request.response(response);
|
127
|
+
|
128
|
+
sharedContext.responseCallback = success;
|
129
|
+
sharedContext.status = response.status;
|
130
|
+
sharedContext.contentType = response.contentType;
|
131
|
+
sharedContext.responseText = response.responseText;
|
132
|
+
});
|
133
|
+
|
134
|
+
it("should call the success handler", function() {
|
135
|
+
expect(success).toHaveBeenCalled();
|
136
|
+
});
|
137
|
+
|
138
|
+
it("should not call the failure handler", function() {
|
139
|
+
expect(error).not.toHaveBeenCalled();
|
140
|
+
});
|
141
|
+
|
142
|
+
it("should call the complete handler", function() {
|
143
|
+
expect(complete).toHaveBeenCalled();
|
144
|
+
});
|
145
|
+
|
146
|
+
sharedAjaxResponseBehaviorForJQuery_Success(sharedContext);
|
147
|
+
});
|
148
|
+
|
149
|
+
describe("and the response is Success, but with JSON", function () {
|
150
|
+
beforeEach(function() {
|
151
|
+
jQuery.ajax({
|
152
|
+
url: "example.com/someApi",
|
153
|
+
type: "GET",
|
154
|
+
dataType: 'json',
|
155
|
+
success: success,
|
156
|
+
complete: complete,
|
157
|
+
error: error
|
158
|
+
});
|
159
|
+
request = mostRecentAjaxRequest();
|
160
|
+
var responseObject = {status: 200, contentType: "application/json", responseText: '{"foo":"bar"}'};
|
161
|
+
|
162
|
+
request.response(responseObject);
|
163
|
+
|
164
|
+
sharedContext.responseCallback = success;
|
165
|
+
sharedContext.status = responseObject.status;
|
166
|
+
sharedContext.contentType = responseObject.contentType;
|
167
|
+
sharedContext.responseText = responseObject.responseText;
|
168
|
+
|
169
|
+
response = success.mostRecentCall.args[2];
|
170
|
+
});
|
171
|
+
|
172
|
+
it("should call the success handler", function() {
|
173
|
+
expect(success).toHaveBeenCalled();
|
174
|
+
});
|
175
|
+
|
176
|
+
it("should not call the failure handler", function() {
|
177
|
+
expect(error).not.toHaveBeenCalled();
|
178
|
+
});
|
179
|
+
|
180
|
+
it("should call the complete handler", function() {
|
181
|
+
expect(complete).toHaveBeenCalled();
|
182
|
+
});
|
183
|
+
|
184
|
+
it("should return a JavaScript object", function() {
|
185
|
+
expect(success.mostRecentCall.args[0]).toEqual({foo: "bar"});
|
186
|
+
});
|
187
|
+
|
188
|
+
sharedAjaxResponseBehaviorForJQuery_Success(sharedContext);
|
189
|
+
});
|
190
|
+
|
191
|
+
describe("the content type defaults to application/json", function () {
|
192
|
+
beforeEach(function() {
|
193
|
+
jQuery.ajax({
|
194
|
+
url: "example.com/someApi",
|
195
|
+
type: "GET",
|
196
|
+
dataType: 'json',
|
197
|
+
success: success,
|
198
|
+
complete: complete,
|
199
|
+
error: error
|
200
|
+
});
|
201
|
+
request = mostRecentAjaxRequest();
|
202
|
+
response = {status: 200, responseText: '{"foo": "valid JSON, dammit."}'};
|
203
|
+
request.response(response);
|
204
|
+
|
205
|
+
sharedContext.responseCallback = success;
|
206
|
+
sharedContext.status = response.status;
|
207
|
+
sharedContext.contentType = "application/json";
|
208
|
+
sharedContext.responseText = response.responseText;
|
209
|
+
});
|
210
|
+
|
211
|
+
it("should call the success handler", function() {
|
212
|
+
expect(success).toHaveBeenCalled();
|
213
|
+
});
|
214
|
+
|
215
|
+
it("should not call the failure handler", function() {
|
216
|
+
expect(error).not.toHaveBeenCalled();
|
217
|
+
});
|
218
|
+
|
219
|
+
it("should call the complete handler", function() {
|
220
|
+
expect(complete).toHaveBeenCalled();
|
221
|
+
});
|
222
|
+
|
223
|
+
sharedAjaxResponseBehaviorForJQuery_Success(sharedContext);
|
224
|
+
});
|
225
|
+
|
226
|
+
describe("and the status/response code is 0", function () {
|
227
|
+
beforeEach(function() {
|
228
|
+
jQuery.ajax({
|
229
|
+
url: "example.com/someApi",
|
230
|
+
type: "GET",
|
231
|
+
dataType: "text",
|
232
|
+
success: success,
|
233
|
+
complete: complete,
|
234
|
+
error: error
|
235
|
+
});
|
236
|
+
request = mostRecentAjaxRequest();
|
237
|
+
response = {status: 0, responseText: '{"foo": "whoops!"}'};
|
238
|
+
request.response(response);
|
239
|
+
|
240
|
+
sharedContext.responseCallback = success;
|
241
|
+
sharedContext.status = 304; /* jQuery detects status code zero as 304 when headers are present */
|
242
|
+
sharedContext.contentType = 'application/json';
|
243
|
+
sharedContext.responseText = response.responseText;
|
244
|
+
});
|
245
|
+
|
246
|
+
it("should call the success handler", function() {
|
247
|
+
expect(success).toHaveBeenCalled();
|
248
|
+
});
|
249
|
+
|
250
|
+
it("should not call the failure handler", function() {
|
251
|
+
expect(error).not.toHaveBeenCalled();
|
252
|
+
});
|
253
|
+
|
254
|
+
it("should call the complete handler", function() {
|
255
|
+
expect(complete).toHaveBeenCalled();
|
256
|
+
});
|
257
|
+
|
258
|
+
sharedAjaxResponseBehaviorForJQuery_Success(sharedContext);
|
259
|
+
});
|
260
|
+
});
|
261
|
+
|
262
|
+
describe("and the response is error", function () {
|
263
|
+
beforeEach(function() {
|
264
|
+
jQuery.ajax({
|
265
|
+
url: "example.com/someApi",
|
266
|
+
type: "GET",
|
267
|
+
dataType: "text",
|
268
|
+
success: success,
|
269
|
+
complete: complete,
|
270
|
+
error: error
|
271
|
+
});
|
272
|
+
request = mostRecentAjaxRequest();
|
273
|
+
response = {status: 500, contentType: "text/html", responseText: "(._){"};
|
274
|
+
request.response(response);
|
275
|
+
|
276
|
+
sharedContext.responseCallback = error;
|
277
|
+
sharedContext.status = response.status;
|
278
|
+
sharedContext.contentType = response.contentType;
|
279
|
+
sharedContext.responseText = response.responseText;
|
280
|
+
});
|
281
|
+
|
282
|
+
it("should not call the success handler", function() {
|
283
|
+
expect(success).not.toHaveBeenCalled();
|
284
|
+
});
|
285
|
+
|
286
|
+
it("should call the failure handler", function() {
|
287
|
+
expect(error).toHaveBeenCalled();
|
288
|
+
});
|
289
|
+
|
290
|
+
it("should call the complete handler", function() {
|
291
|
+
expect(complete).toHaveBeenCalled();
|
292
|
+
});
|
293
|
+
|
294
|
+
sharedAjaxResponseBehaviorForJQuery_Failure(sharedContext);
|
295
|
+
});
|
296
|
+
|
297
|
+
describe('when simulating a response with request.responseTimeout', function() {
|
298
|
+
beforeEach(function() {
|
299
|
+
jasmine.Clock.useMock();
|
300
|
+
|
301
|
+
jQuery.ajax({
|
302
|
+
url: "example.com/someApi",
|
303
|
+
type: "GET",
|
304
|
+
dataType: "text",
|
305
|
+
success: success,
|
306
|
+
complete: complete,
|
307
|
+
error: error
|
308
|
+
});
|
309
|
+
request = mostRecentAjaxRequest();
|
310
|
+
response = {contentType: "text/html", responseText: "(._){"};
|
311
|
+
request.responseTimeout(response);
|
312
|
+
|
313
|
+
sharedContext.responseCallback = error;
|
314
|
+
sharedContext.status = response.status;
|
315
|
+
sharedContext.contentType = response.contentType;
|
316
|
+
sharedContext.responseText = response.responseText;
|
317
|
+
});
|
318
|
+
|
319
|
+
it("should not call the success handler", function() {
|
320
|
+
expect(success).not.toHaveBeenCalled();
|
321
|
+
});
|
322
|
+
|
323
|
+
it("should call the failure handler", function() {
|
324
|
+
expect(error).toHaveBeenCalled();
|
325
|
+
});
|
326
|
+
|
327
|
+
it("should call the complete handler", function() {
|
328
|
+
expect(complete).toHaveBeenCalled();
|
329
|
+
});
|
330
|
+
});
|
331
|
+
});
|
332
|
+
|
333
|
+
|
334
|
+
function sharedAjaxResponseBehaviorForJQuery_Success(context) {
|
335
|
+
describe("the success response", function () {
|
336
|
+
var xhr;
|
337
|
+
beforeEach(function() {
|
338
|
+
xhr = context.responseCallback.mostRecentCall.args[2];
|
339
|
+
});
|
340
|
+
|
341
|
+
it("should have the expected status code", function() {
|
342
|
+
expect(xhr.status).toEqual(context.status);
|
343
|
+
});
|
344
|
+
|
345
|
+
it("should have the expected content type", function() {
|
346
|
+
expect(xhr.getResponseHeader('Content-type')).toEqual(context.contentType);
|
347
|
+
});
|
348
|
+
|
349
|
+
it("should have the expected response text", function() {
|
350
|
+
expect(xhr.responseText).toEqual(context.responseText);
|
351
|
+
});
|
352
|
+
});
|
353
|
+
}
|
354
|
+
|
355
|
+
function sharedAjaxResponseBehaviorForJQuery_Failure(context) {
|
356
|
+
describe("the failure response", function () {
|
357
|
+
var xhr;
|
358
|
+
beforeEach(function() {
|
359
|
+
xhr = context.responseCallback.mostRecentCall.args[0];
|
360
|
+
});
|
361
|
+
|
362
|
+
it("should have the expected status code", function() {
|
363
|
+
expect(xhr.status).toEqual(context.status);
|
364
|
+
});
|
365
|
+
|
366
|
+
it("should have the expected content type", function() {
|
367
|
+
expect(xhr.getResponseHeader('Content-type')).toEqual(context.contentType);
|
368
|
+
});
|
369
|
+
|
370
|
+
it("should have the expected response text", function() {
|
371
|
+
expect(xhr.responseText).toEqual(context.responseText);
|
372
|
+
});
|
373
|
+
});
|
374
|
+
}
|