Skip to content

Commit 66d263a

Browse files
Miki Leskinendcneiner
authored andcommitted
Make sure jsonp calls are saved and can be retrieved with $.mockjax.mockedAjaxCalls()
1 parent 67d760a commit 66d263a

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
*.DS_Store
22
*.esproj
33
*.swp
4+
.idea/
5+
*.iml

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ checkout this list:
7373
* `$.mockjaxClear(id)`
7474
* Remove a single mockjax handler.
7575
* `id` is the string returned from `$.mockjax`.
76+
* `$.mockjax.mockedAjaxCalls()`
77+
* Returns all mocked ajax calls so you can e.g. check that expected data is sent to backend.
7678

7779
### Overview: Your First Mock
7880

jquery.mockjax.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,8 @@
435435
continue;
436436
}
437437

438+
mockedAjaxCalls.push(requestSettings);
439+
438440
// If logging is enabled, log the mock to the console
439441
$.mockjaxSettings.log( mockHandler, requestSettings );
440442

@@ -457,7 +459,6 @@
457459
copyUrlParameters(mockHandler, origSettings);
458460

459461
(function(mockHandler, requestSettings, origSettings, origHandler) {
460-
mockedAjaxCalls.push(requestSettings);
461462
mockRequest = _ajax.call($, $.extend(true, {}, origSettings, {
462463
// Mock the XHR object
463464
xhr: function() { return xhr( mockHandler, requestSettings, origSettings, origHandler ); }

test/test.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ asyncTest('Get mocked ajax calls', function() {
257257
url: '/api/example/*'
258258
});
259259
equals($.mockjax.mockedAjaxCalls().length, 0, 'Initially there are no saved ajax calls')
260+
// GET
260261
$.ajax({
261262
async: false,
262263
type: 'GET',
@@ -269,6 +270,7 @@ asyncTest('Get mocked ajax calls', function() {
269270
start();
270271
}
271272
});
273+
// POST with some data
272274
$.ajax({
273275
async: false,
274276
type: 'POST',
@@ -280,11 +282,27 @@ asyncTest('Get mocked ajax calls', function() {
280282
equals(actualCalls[1].type, 'POST', 'Second ajax call has expected method');
281283
equals(actualCalls[1].url, '/api/example/2', 'Second ajax call has expected url');
282284
deepEqual(actualCalls[1].data, {a: 1}, 'Second ajax call has expected data');
283-
$.mockjaxClear();
284285
start();
285286
}
286287
});
288+
// JSONP
289+
$.ajax({
290+
async: false,
291+
url: '/api/example/jsonp?callback=?',
292+
jsonpCallback: 'foo123',
293+
dataType: 'jsonp',
294+
complete: function() {
295+
var actualCalls = $.mockjax.mockedAjaxCalls();
296+
equals(actualCalls.length, 3, 'Three mocked ajax calls are saved');
297+
equals(actualCalls[2].url, '/api/example/jsonp?callback=foo123', 'Third ajax call has expected jsonp url');
298+
start();
299+
}
300+
});
301+
equals($.mockjax.mockedAjaxCalls().length, 3, 'Afterwords there should be three saved ajax calls')
302+
var mockedUrls = $.map($.mockjax.mockedAjaxCalls(), function(ajaxOptions) { return ajaxOptions.url })
303+
deepEqual(mockedUrls, ['/api/example/1', '/api/example/2', '/api/example/jsonp?callback=foo123'], 'Mocked ajax calls are saved in execution order')
287304
$.mockjaxClear();
305+
equals($.mockjax.mockedAjaxCalls().length, 0, 'After clearing there are no saved ajax calls')
288306
});
289307

290308
// These tests is only relevant in 1.5.2 and higher

0 commit comments

Comments
 (0)