jasmine-ajax 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. data/.gitignore +2 -0
  2. data/.pairs +6 -0
  3. data/Gemfile +4 -0
  4. data/Gemfile.lock +35 -0
  5. data/MIT.LICENSE +20 -0
  6. data/README.markdown +168 -0
  7. data/Rakefile +3 -0
  8. data/examples/jquery/Rakefile +2 -0
  9. data/examples/jquery/public/css/master.css +59 -0
  10. data/examples/jquery/public/css/reset.css +47 -0
  11. data/examples/jquery/public/images/fail-whale.png +0 -0
  12. data/examples/jquery/public/index.html +60 -0
  13. data/examples/jquery/public/javascripts/Tweet.js +6 -0
  14. data/examples/jquery/public/javascripts/TwitSearch.js +32 -0
  15. data/examples/jquery/public/javascripts/TwitterApi.js +31 -0
  16. data/examples/jquery/spec/SpecRunner.html +32 -0
  17. data/examples/jquery/spec/javascripts/TweetSpec.js +24 -0
  18. data/examples/jquery/spec/javascripts/TwitterApiSpec.js +88 -0
  19. data/examples/jquery/spec/javascripts/helpers/test_responses/search.js +16 -0
  20. data/examples/jquery/spec/javascripts/helpers/tweets.js +3 -0
  21. data/examples/jquery/spec/javascripts/jasmine-0.11.1/jasmine-html.js +182 -0
  22. data/examples/jquery/spec/javascripts/jasmine-0.11.1/jasmine.css +166 -0
  23. data/examples/jquery/spec/javascripts/jasmine-0.11.1/jasmine.js +2343 -0
  24. data/examples/jquery/spec/javascripts/support/jasmine.yml +78 -0
  25. data/examples/jquery/spec/javascripts/support/jasmine_runner.rb +21 -0
  26. data/examples/prototype/Rakefile +2 -0
  27. data/examples/prototype/public/css/master.css +59 -0
  28. data/examples/prototype/public/css/reset.css +47 -0
  29. data/examples/prototype/public/images/fail-whale.png +0 -0
  30. data/examples/prototype/public/index.html +45 -0
  31. data/examples/prototype/public/javascripts/Tweet.js +6 -0
  32. data/examples/prototype/public/javascripts/TwitSearch.js +32 -0
  33. data/examples/prototype/public/javascripts/TwitterApi.js +24 -0
  34. data/examples/prototype/spec/SpecRunner.html +32 -0
  35. data/examples/prototype/spec/javascripts/TweetSpec.js +24 -0
  36. data/examples/prototype/spec/javascripts/TwitterApiSpec.js +89 -0
  37. data/examples/prototype/spec/javascripts/helpers/test_responses/search.js +16 -0
  38. data/examples/prototype/spec/javascripts/helpers/tweets.js +3 -0
  39. data/examples/prototype/spec/javascripts/jasmine-0.11.1/jasmine-html.js +182 -0
  40. data/examples/prototype/spec/javascripts/jasmine-0.11.1/jasmine.css +166 -0
  41. data/examples/prototype/spec/javascripts/jasmine-0.11.1/jasmine.js +2343 -0
  42. data/examples/prototype/spec/javascripts/support/jasmine.yml +78 -0
  43. data/examples/prototype/spec/javascripts/support/jasmine_runner.rb +21 -0
  44. data/frameworks/jquery.js +8176 -0
  45. data/frameworks/prototype.js +4874 -0
  46. data/lib/assets/javascripts/mock-ajax.js +207 -0
  47. data/lib/spec-helper.js +0 -0
  48. data/lib/version.rb +5 -0
  49. data/spec/javascripts/fake-xml-http-request-spec.js +45 -0
  50. data/spec/javascripts/helpers/spec-helper.js +5 -0
  51. data/spec/javascripts/mock-ajax-jquery-spec.js +374 -0
  52. data/spec/javascripts/mock-ajax-prototypejs-spec.js +287 -0
  53. data/spec/javascripts/mock-ajax-spec.js +193 -0
  54. data/spec/javascripts/support/jasmine.yml +81 -0
  55. data/spec/javascripts/support/jasmine_runner.rb +21 -0
  56. metadata +108 -0
@@ -0,0 +1,287 @@
1
+ describe("Jasmine Mock Ajax (for Prototype.js)", function() {
2
+ var request, anotherRequest, onSuccess, onFailure, onComplete;
3
+ var sharedContext = {};
4
+ var jquery = jQuery;
5
+
6
+ beforeEach(function() {
7
+ jQuery = undefined;
8
+ jasmine.Ajax.useMock();
9
+
10
+ onSuccess = jasmine.createSpy("onSuccess");
11
+ onFailure = jasmine.createSpy("onFailure");
12
+ onComplete = jasmine.createSpy("onComplete");
13
+ });
14
+
15
+ afterEach(function() {
16
+ jQuery = jquery;
17
+ });
18
+
19
+ describe("when making a request", function () {
20
+ beforeEach(function() {
21
+ request = new Ajax.Request("example.com/someApi", {
22
+ onSuccess: onSuccess,
23
+ onFailure: onFailure,
24
+ onComplete: onComplete
25
+ });
26
+ });
27
+
28
+ it("should store URL and transport", function() {
29
+ expect(request.url).toEqual("example.com/someApi");
30
+ expect(request.transport).toBeTruthy();
31
+ });
32
+
33
+ it("should queue the request", function() {
34
+ expect(ajaxRequests.length).toEqual(1);
35
+ });
36
+
37
+ it("should allow access to the queued request", function() {
38
+ expect(ajaxRequests[0]).toEqual(request);
39
+ });
40
+
41
+ describe("and then another request", function () {
42
+ beforeEach(function() {
43
+ anotherRequest = new Ajax.Request("example.com/someApi", {
44
+ onSuccess: onSuccess,
45
+ onFailure: onFailure,
46
+ onComplete: onComplete
47
+ });
48
+ });
49
+
50
+ it("should queue the next request", function() {
51
+ expect(ajaxRequests.length).toEqual(2);
52
+ });
53
+
54
+ it("should allow access to the other queued request", function() {
55
+ expect(ajaxRequests[1]).toEqual(anotherRequest);
56
+ });
57
+ });
58
+
59
+ describe("mostRecentAjaxRequest", function () {
60
+
61
+ describe("when there is one request queued", function () {
62
+ it("should return the request", function() {
63
+ expect(mostRecentAjaxRequest()).toEqual(request);
64
+ });
65
+ });
66
+
67
+ describe("when there is more than one request", function () {
68
+ beforeEach(function() {
69
+ anotherRequest = new Ajax.Request("balthazarurl", {
70
+ onSuccess: onSuccess,
71
+ onFailure: onFailure,
72
+ onComplete: onComplete
73
+ });
74
+ });
75
+
76
+ it("should return the most recent request", function() {
77
+ expect(mostRecentAjaxRequest()).toEqual(anotherRequest);
78
+ });
79
+ });
80
+
81
+ describe("when there are no requests", function () {
82
+ beforeEach(function() {
83
+ clearAjaxRequests();
84
+ });
85
+
86
+ it("should return null", function() {
87
+ expect(mostRecentAjaxRequest()).toEqual(null);
88
+ });
89
+ });
90
+ });
91
+
92
+ describe("clearAjaxRequests()", function () {
93
+ beforeEach(function() {
94
+ clearAjaxRequests();
95
+ });
96
+
97
+ it("should remove all requests", function() {
98
+ expect(ajaxRequests.length).toEqual(0);
99
+ expect(mostRecentAjaxRequest()).toEqual(null);
100
+ });
101
+ });
102
+ });
103
+
104
+ describe("when simulating a response with request.response", function () {
105
+ beforeEach(function() {
106
+ request = new Ajax.Request("idontcare", {
107
+ method: 'get',
108
+ onSuccess: onSuccess,
109
+ onFailure: onFailure,
110
+ onComplete: onComplete
111
+ });
112
+ });
113
+
114
+ describe("and the response is Success", function () {
115
+ beforeEach(function() {
116
+ var response = {status: 200, contentType: "text/html", responseText: "OK!"};
117
+ request.response(response);
118
+ sharedContext.responseCallback = onSuccess;
119
+ sharedContext.status = response.status;
120
+ sharedContext.contentType = response.contentType;
121
+ sharedContext.responseText = response.responseText;
122
+ });
123
+
124
+ it("should call the success handler", function() {
125
+ expect(onSuccess).toHaveBeenCalled();
126
+ });
127
+
128
+ it("should not call the failure handler", function() {
129
+ expect(onFailure).not.toHaveBeenCalled();
130
+ });
131
+
132
+ it("should call the complete handler", function() {
133
+ expect(onComplete).toHaveBeenCalled();
134
+ });
135
+
136
+ sharedAjaxResponseBehavior(sharedContext);
137
+ });
138
+
139
+ describe("and the response is Failure", function () {
140
+ beforeEach(function() {
141
+ var response = {status: 500, contentType: "text/html", responseText: "(._){"};
142
+ request.response(response);
143
+ sharedContext.responseCallback = onFailure;
144
+ sharedContext.status = response.status;
145
+ sharedContext.contentType = response.contentType;
146
+ sharedContext.responseText = response.responseText;
147
+ });
148
+
149
+ it("should not call the success handler", function() {
150
+ expect(onSuccess).not.toHaveBeenCalled();
151
+ });
152
+
153
+ it("should call the failure handler", function() {
154
+ expect(onFailure).toHaveBeenCalled();
155
+ });
156
+
157
+ it("should call the complete handler", function() {
158
+ expect(onComplete).toHaveBeenCalled();
159
+ });
160
+
161
+ sharedAjaxResponseBehavior(sharedContext);
162
+ });
163
+
164
+ describe("and the response is Success, but with JSON", function () {
165
+ var response;
166
+ beforeEach(function() {
167
+ var responseObject = {status: 200, contentType: "application/json", responseText: "{'foo':'bar'}"};
168
+
169
+ request.response(responseObject);
170
+
171
+ sharedContext.responseCallback = onSuccess;
172
+ sharedContext.status = responseObject.status;
173
+ sharedContext.contentType = responseObject.contentType;
174
+ sharedContext.responseText = responseObject.responseText;
175
+
176
+ response = onSuccess.mostRecentCall.args[0];
177
+ });
178
+
179
+ it("should call the success handler", function() {
180
+ expect(onSuccess).toHaveBeenCalled();
181
+ });
182
+
183
+ it("should not call the failure handler", function() {
184
+ expect(onFailure).not.toHaveBeenCalled();
185
+ });
186
+
187
+ it("should call the complete handler", function() {
188
+ expect(onComplete).toHaveBeenCalled();
189
+ });
190
+
191
+ it("should return a JavaScript object", function() {
192
+ window.response = response;
193
+ expect(response.responseJSON).toEqual({foo: "bar"});
194
+ });
195
+
196
+ sharedAjaxResponseBehavior(sharedContext);
197
+ });
198
+
199
+ describe("the content type defaults to application/json", function () {
200
+ beforeEach(function() {
201
+ var response = {status: 200, responseText: "OK!"};
202
+ request.response(response);
203
+
204
+ sharedContext.responseCallback = onSuccess;
205
+ sharedContext.status = response.status;
206
+ sharedContext.contentType = "application/json";
207
+ sharedContext.responseText = response.responseText;
208
+ });
209
+
210
+ it("should call the success handler", function() {
211
+ expect(onSuccess).toHaveBeenCalled();
212
+ });
213
+
214
+ it("should not call the failure handler", function() {
215
+ expect(onFailure).not.toHaveBeenCalled();
216
+ });
217
+
218
+ it("should call the complete handler", function() {
219
+ expect(onComplete).toHaveBeenCalled();
220
+ });
221
+
222
+ sharedAjaxResponseBehavior(sharedContext);
223
+ });
224
+
225
+ describe("and the status/response code is null", function () {
226
+ var on0;
227
+ beforeEach(function() {
228
+ on0 = jasmine.createSpy('on0');
229
+
230
+ request = new Ajax.Request("idontcare", {
231
+ method: 'get',
232
+ on0: on0,
233
+ onSuccess: onSuccess,
234
+ onFailure: onFailure,
235
+ onComplete: onComplete
236
+ });
237
+
238
+ var response = {status: null, responseText: "whoops!"};
239
+ request.response(response);
240
+
241
+ sharedContext.responseCallback = on0;
242
+ sharedContext.status = 0;
243
+ sharedContext.contentType = 'application/json';
244
+ sharedContext.responseText = response.responseText;
245
+ });
246
+
247
+ it("should not call the success handler", function() {
248
+ expect(onSuccess).not.toHaveBeenCalled();
249
+ });
250
+
251
+ it("should not call the failure handler", function() {
252
+ expect(onFailure).not.toHaveBeenCalled();
253
+ });
254
+
255
+ it("should call the on0 handler", function() {
256
+ expect(on0).toHaveBeenCalled();
257
+ });
258
+
259
+ it("should call the complete handler", function() {
260
+ expect(onComplete).toHaveBeenCalled();
261
+ });
262
+
263
+ sharedAjaxResponseBehavior(sharedContext);
264
+ });
265
+ });
266
+ });
267
+
268
+ function sharedAjaxResponseBehavior(context) {
269
+ describe("the response", function () {
270
+ var response;
271
+ beforeEach(function() {
272
+ response = context.responseCallback.mostRecentCall.args[0];
273
+ });
274
+
275
+ it("should have the expected status code", function() {
276
+ expect(response.status).toEqual(context.status);
277
+ });
278
+
279
+ it("should have the expected content type", function() {
280
+ expect(response.getHeader('Content-type')).toEqual(context.contentType);
281
+ });
282
+
283
+ it("should have the expected response text", function() {
284
+ expect(response.responseText).toEqual(context.responseText);
285
+ });
286
+ });
287
+ }
@@ -0,0 +1,193 @@
1
+ describe("jasmine.Ajax", function() {
2
+ beforeEach(function() {
3
+ jasmine.Ajax.reset();
4
+ });
5
+
6
+ describe("isInstalled", function() {
7
+ it("returns true if the mock has been installed", function() {
8
+ jasmine.Ajax.installed = true;
9
+ expect(jasmine.Ajax.isInstalled()).toBeTruthy();
10
+ });
11
+
12
+ it("returns false if the mock has not been installed", function() {
13
+ jasmine.Ajax.installed = false;
14
+ expect(jasmine.Ajax.isInstalled()).toBeFalsy();
15
+ });
16
+ });
17
+
18
+ describe("assertInstalled", function() {
19
+ it("doesn't raise an error if the mock is installed", function() {
20
+ jasmine.Ajax.installed = true;
21
+ expect(
22
+ function() {
23
+ jasmine.Ajax.assertInstalled()
24
+ }).not.toThrow("Mock ajax is not installed, use jasmine.Ajax.useMock()");
25
+ });
26
+
27
+ it("raises an error if the mock is not installed", function() {
28
+ jasmine.Ajax.installed = false;
29
+ expect(
30
+ function() {
31
+ jasmine.Ajax.assertInstalled()
32
+ }).toThrow("Mock ajax is not installed, use jasmine.Ajax.useMock()");
33
+ });
34
+ });
35
+
36
+ describe("installMock", function() {
37
+ describe("when using jQuery", function() {
38
+
39
+ it("installs the mock", function() {
40
+ withoutPrototype(function() {
41
+ jasmine.Ajax.installMock();
42
+ expect(jQuery.ajaxSettings.xhr).toBe(jasmine.Ajax.jQueryMock);
43
+ });
44
+ });
45
+
46
+ it("saves a reference to jQuery.ajaxSettings.xhr", function() {
47
+ withoutPrototype(function() {
48
+ var jqueryAjax = jQuery.ajaxSettings.xhr;
49
+ jasmine.Ajax.installMock();
50
+ expect(jasmine.Ajax.real).toBe(jqueryAjax);
51
+ });
52
+ });
53
+
54
+ it("sets mode to 'jQuery'", function() {
55
+ withoutPrototype(function() {
56
+ jasmine.Ajax.installMock();
57
+ expect(jasmine.Ajax.mode).toEqual("jQuery");
58
+ });
59
+ })
60
+ });
61
+
62
+ describe("when using Prototype", function() {
63
+ it("installs the mock", function() {
64
+ withoutJquery(function() {
65
+ jasmine.Ajax.installMock();
66
+ expect(Ajax.getTransport).toBe(jasmine.Ajax.prototypeMock);
67
+ });
68
+ });
69
+
70
+ it("stores a reference to Ajax.getTransport", function() {
71
+ withoutJquery(function(){
72
+ var prototypeAjax = Ajax.getTransport;
73
+
74
+ jasmine.Ajax.installMock();
75
+ expect(jasmine.Ajax.real).toBe(prototypeAjax);
76
+ });
77
+ });
78
+
79
+ it("sets mode to 'Prototype'", function() {
80
+ withoutJquery(function() {
81
+ jasmine.Ajax.installMock();
82
+ expect(jasmine.Ajax.mode).toEqual("Prototype");
83
+ });
84
+ });
85
+ });
86
+
87
+ describe("when using any other library", function() {
88
+ it("raises an exception", function() {
89
+ var jquery = jQuery;
90
+ var prototype = Prototype;
91
+ jQuery = undefined;
92
+ Prototype = undefined;
93
+
94
+ expect(function(){ jasmine.Ajax.installMock() }).toThrow("jasmine.Ajax currently only supports jQuery and Prototype");
95
+
96
+ jQuery = jquery;
97
+ Prototype = prototype;
98
+ });
99
+ });
100
+
101
+ it("sets the installed flag to true", function() {
102
+ jasmine.Ajax.installMock();
103
+ expect(jasmine.Ajax.installed).toBeTruthy();
104
+ });
105
+
106
+ });
107
+
108
+ describe("uninstallMock", function() {
109
+ describe("when using jQuery", function() {
110
+ it("returns ajax control to jQuery", function() {
111
+ withoutPrototype(function() {
112
+ var jqueryAjax = jQuery.ajaxSettings.xhr;
113
+
114
+ jasmine.Ajax.installMock();
115
+ jasmine.Ajax.uninstallMock();
116
+
117
+ expect(jQuery.ajaxSettings.xhr).toBe(jqueryAjax);
118
+ });
119
+
120
+
121
+ });
122
+
123
+ });
124
+
125
+ describe("when using Prototype", function() {
126
+ it("returns Ajax control to Ajax.getTransport", function() {
127
+ withoutJquery(function() {
128
+ var prototypeAjax = Ajax.getTransport;
129
+ jasmine.Ajax.installMock();
130
+ jasmine.Ajax.uninstallMock();
131
+
132
+ expect(Ajax.getTransport).toBe(prototypeAjax);
133
+ });
134
+ // so uninstallMock doesn't throw error when spec.after runs
135
+ jasmine.Ajax.installMock();
136
+ });
137
+ });
138
+
139
+ it("raises an exception if jasmine.Ajax is not installed", function() {
140
+ expect(function(){ jasmine.Ajax.uninstallMock() }).toThrow("Mock ajax is not installed, use jasmine.Ajax.useMock()");
141
+ });
142
+
143
+ it("sets the installed flag to false", function() {
144
+ jasmine.Ajax.installMock();
145
+ jasmine.Ajax.uninstallMock();
146
+ expect(jasmine.Ajax.installed).toBeFalsy();
147
+
148
+ // so uninstallMock doesn't throw error when spec.after runs
149
+ jasmine.Ajax.installMock();
150
+ });
151
+
152
+ it("sets the mode to null", function() {
153
+ jasmine.Ajax.installMock();
154
+ jasmine.Ajax.uninstallMock();
155
+ expect(jasmine.Ajax.mode).toEqual(null);
156
+ jasmine.Ajax.installMock();
157
+ });
158
+ });
159
+
160
+ describe("useMock", function() {
161
+ it("delegates to installMock", function() {
162
+ spyOn(jasmine.Ajax, 'installMock').andCallThrough();
163
+ jasmine.Ajax.useMock();
164
+ expect(jasmine.Ajax.installMock).toHaveBeenCalled();
165
+ });
166
+
167
+ it("ensures the mock is not already installed", function() {
168
+ jasmine.Ajax.useMock();
169
+
170
+ spyOn(jasmine.Ajax, 'installMock');
171
+
172
+ jasmine.Ajax.useMock();
173
+
174
+ expect(jasmine.Ajax.installMock).not.toHaveBeenCalled();
175
+ });
176
+
177
+ });
178
+
179
+ });
180
+
181
+ function withoutJquery(spec) {
182
+ var jqueryRef = jQuery;
183
+ jQuery = undefined;
184
+ spec.apply(this);
185
+ jQuery = jqueryRef;
186
+ }
187
+
188
+ function withoutPrototype(spec) {
189
+ var prototypeRef = Prototype;
190
+ Prototype = undefined;
191
+ spec.apply(this);
192
+ Prototype = prototypeRef;
193
+ }