-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
ajax_spec.js
211 lines (171 loc) · 7.72 KB
/
ajax_spec.js
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
describe('Ajax', function () {
var beforeSend, onSuccess, onComplete, onError, settings, request, xhrResponse;
describe('$.ajax', function () {
it('should be defined', function () {
expect($.ajax).toBeDefined();
});
it('should returns xhr object', function () {
expect($.ajax().onreadystatechange).toBeDefined();
});
describe('XHR request', function () {
var performAjax = function () {
$.ajax(settings);
request = mostRecentAjaxRequest();
request.response(xhrResponse);
};
beforeEach(function () {
jasmine.Ajax.useMock();
beforeSend = jasmine.createSpy('beforeSend');
onSuccess = jasmine.createSpy('onSuccess');
onError = jasmine.createSpy('onError');
onComplete = jasmine.createSpy('onComplete');
});
describe('on GET request', function () {
beforeEach(function () {
settings = {
url: '/example/url',
success: onSuccess,
error: onError,
complete: onComplete,
beforeSend: beforeSend
};
xhrResponse = {
status: 200,
responseText: 'Small is beautiful.'
};
});
describe('beforeSend callback', function () {
beforeEach(function () {
performAjax();
});
it('should call beforeSend callback', function () {
expect(beforeSend).toHaveBeenCalled();
});
it('should pass xhr and settings to beforeSend callback', function () {
var args = beforeSend.mostRecentCall.args;
expect(args.length).toEqual(2);
expect(args[0].constructor).toEqual(FakeXMLHttpRequest);
expect(args[1].constructor).toEqual(Object);
expect(args[1].beforeSend).toEqual(beforeSend);
});
});
describe('success callback', function () {
beforeEach(function () {
performAjax();
});
it('should call success callback', function () {
expect(onSuccess).toHaveBeenCalled();
});
it('should pass responseText, success string and xhr object to success callback', function () {
var args = onSuccess.mostRecentCall.args;
expect(args.length).toEqual(3);
expect(args[0]).toEqual('Small is beautiful.');
expect(args[1]).toEqual('success');
expect(args[2].constructor).toEqual(FakeXMLHttpRequest);
});
});
describe('error callback', function () {
beforeEach(function () {
xhrResponse.status = 500;
performAjax();
});
it('should call error callback', function () {
expect(onError).toHaveBeenCalled();
});
it('should pass xhr and error string to error callback', function () {
var args = onError.mostRecentCall.args;
expect(args.length).toEqual(2);
expect(args[0].constructor).toEqual(FakeXMLHttpRequest);
expect(args[1]).toEqual('error');
});
});
describe('complete callback', function () {
it('should call error callback on 200', function () {
performAjax();
expect(onComplete).toHaveBeenCalled();
});
it('should call error callback on 200', function () {
xhrResponse.status = 500;
performAjax();
expect(onComplete).toHaveBeenCalled();
});
it('should pass xhr and success string to complete callback', function () {
performAjax();
var args = onComplete.mostRecentCall.args;
expect(args.length).toEqual(2);
expect(args[0].constructor).toEqual(FakeXMLHttpRequest);
expect(args[1]).toEqual('success');
});
it('should pass xhr and error string to complete callback', function () {
xhrResponse.status = 500;
performAjax();
var args = onComplete.mostRecentCall.args;
expect(args[1]).toEqual('error');
});
});
});
});
});
describe('$.get', function () {
it('should be defined', function () {
expect($.get).toBeDefined();
});
it('should call $.ajax with url and success callback', function () {
var successCallback = function () {};
var zeptoAjax = $.ajax;
$.ajax = jasmine.createSpy('$.ajax');
$.get('/example/url', successCallback);
var options = $.ajax.mostRecentCall.args[0];
expect(options.constructor).toBe(Object);
expect(options.url).toBe('/example/url');
expect(options.success).toBe(successCallback);
$.ajax = zeptoAjax;
});
});
describe('$.post', function () {
it('should be defined', function () {
expect($.post).toBeDefined();
});
});
describe('$.getJSON', function () {
it('should be defined', function () {
expect($.post).toBeDefined();
});
});
describe('$.load', function () {
it('should be defined', function () {
expect($.post).toBeDefined();
});
});
describe('$.param', function () {
it('should be defined', function () {
expect($.post).toBeDefined();
});
});
describe('$.ajaxSettings', function () {
it('should be defined', function () {
expect($.ajaxSettings).toBeDefined();
});
it('should contain default ajax settings', function () {
expect($.ajaxSettings.constructor).toBe(Object);
expect($.ajaxSettings.type).toBe('GET');
expect($.ajaxSettings.beforeSend.constructor).toBe(Function);
expect($.ajaxSettings.success.constructor).toBe(Function);
expect($.ajaxSettings.error.constructor).toBe(Function);
expect($.ajaxSettings.complete.constructor).toBe(Function);
expect($.ajaxSettings.xhr().onreadystatechange).toBeDefined();
expect($.ajaxSettings.accepts.constructor).toBe(Object);
expect($.ajaxSettings.accepts.script).toBe('text/javascript, application/javascript');
expect($.ajaxSettings.accepts.json).toBe('application/json');
expect($.ajaxSettings.accepts.xml).toBe('application/xml, text/xml');
expect($.ajaxSettings.accepts.html).toBe('text/html');
expect($.ajaxSettings.accepts.text).toBe('text/plain');
expect($.ajaxSettings.timeout).toBe(0);
});
});
describe('$.ajaxJSONP', function () {
it('should be defined', function () {
expect($.ajaxJSONP).toBeDefined();
});
});
});