ember-data-factory-guy 0.7.5 → 0.7.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +56 -17
- data/bower.json +6 -4
- data/dist/ember-data-factory-guy.js +9 -59
- data/dist/ember-data-factory-guy.min.js +1 -1
- data/ember-data-factory-guy.gemspec +1 -1
- data/package.json +1 -1
- data/src/factory_guy_test_mixin.js +9 -60
- data/tests/factory_guy_test_mixin_test.js +2 -4
- data/tests/index.html +8 -5
- data/tests/support/factories/profile_factory.js +3 -0
- data/tests/test_setup.js +3 -4888
- data/vendor/assets/javascripts/ember_data_factory_guy.js +9 -59
- metadata +2 -4
- data/tests/support/libs/mockjax.js +0 -600
- data/tests/support/libs/sinon.js +0 -4287
@@ -776,12 +776,10 @@ FactoryGuyTestMixin = Em.Mixin.create({
|
|
776
776
|
/**
|
777
777
|
Proxy to store's makeFixture method
|
778
778
|
|
779
|
-
@param {String} name name of fixture
|
780
|
-
@param {Object} options fixture options
|
781
|
-
@returns {Object|DS.Model} json or record depending on the adapter type
|
782
779
|
*/
|
783
|
-
make: function (
|
784
|
-
|
780
|
+
make: function () {
|
781
|
+
var store = this.getStore();
|
782
|
+
return store.makeFixture.apply(store, arguments);
|
785
783
|
},
|
786
784
|
|
787
785
|
getStore: function () {
|
@@ -821,57 +819,6 @@ FactoryGuyTestMixin = Em.Mixin.create({
|
|
821
819
|
},
|
822
820
|
|
823
821
|
|
824
|
-
/**
|
825
|
-
Build the json used for creating or finding a record.
|
826
|
-
|
827
|
-
@param {String} modelName model name like 'user'
|
828
|
-
@param {String} fixture the fixture data
|
829
|
-
@return {Object} json response used for mocking a request
|
830
|
-
*/
|
831
|
-
buildAjaxHttpResponse: function (modelName, fixture) {
|
832
|
-
if (this.usingActiveModelSerializer(modelName)) {
|
833
|
-
this.toSnakeCase(fixture);
|
834
|
-
}
|
835
|
-
var hash = {};
|
836
|
-
hash[modelName] = fixture;
|
837
|
-
return hash;
|
838
|
-
},
|
839
|
-
|
840
|
-
|
841
|
-
_collectArgs: function (args, fromMethod) {
|
842
|
-
var args = Array.prototype.slice.call(arguments);
|
843
|
-
var name = args.shift();
|
844
|
-
if (!name) {
|
845
|
-
throw new Error(fromMethod + " needs a factory name to build");
|
846
|
-
}
|
847
|
-
var succeed = true;
|
848
|
-
if (Ember.typeOf(args[args.length-1]) == 'boolean') {
|
849
|
-
succeed = args.pop();
|
850
|
-
}
|
851
|
-
var opts = {}
|
852
|
-
if (Ember.typeOf(args[args.length-1]) == 'object') {
|
853
|
-
opts = args.pop();
|
854
|
-
}
|
855
|
-
var traits = args; // whatever is left are traits
|
856
|
-
|
857
|
-
return {name: name, traits: traits, opts: opts, succeed: succeed}
|
858
|
-
},
|
859
|
-
|
860
|
-
/**
|
861
|
-
Convert Object's keys to snake case
|
862
|
-
|
863
|
-
@param {Object} fixture to convert
|
864
|
-
*/
|
865
|
-
toSnakeCase: function (fixture) {
|
866
|
-
for (key in fixture) {
|
867
|
-
if (key != Em.String.decamelize(key)) {
|
868
|
-
var value = fixture[key];
|
869
|
-
delete fixture[key];
|
870
|
-
fixture[Em.String.decamelize(key)] = value
|
871
|
-
}
|
872
|
-
}
|
873
|
-
},
|
874
|
-
|
875
822
|
/**
|
876
823
|
Build url for the mockjax call. Proxy to the adapters buildURL method.
|
877
824
|
|
@@ -892,7 +839,6 @@ FactoryGuyTestMixin = Em.Mixin.create({
|
|
892
839
|
@param {Number} number number of fixtures to create
|
893
840
|
@param {String} trait optional traits (one or more)
|
894
841
|
@param {Object} opts optional fixture options
|
895
|
-
@return {Object} json response
|
896
842
|
*/
|
897
843
|
handleFindMany: function () {
|
898
844
|
var store = this.getStore();
|
@@ -916,7 +862,8 @@ FactoryGuyTestMixin = Em.Mixin.create({
|
|
916
862
|
@param {String} name name of the fixture ( or model ) to create
|
917
863
|
@param {String} trait optional traits ( one or more )
|
918
864
|
@param {Object} opts optional fixture options
|
919
|
-
@
|
865
|
+
@param {Boolean} succeed optional flag to indicate if the request
|
866
|
+
should succeed ( default is true )
|
920
867
|
*/
|
921
868
|
handleCreate: function () {
|
922
869
|
var args = Array.prototype.slice.call(arguments);
|
@@ -934,8 +881,9 @@ FactoryGuyTestMixin = Em.Mixin.create({
|
|
934
881
|
|
935
882
|
if (succeed) {
|
936
883
|
var store = this.getStore();
|
937
|
-
// make the
|
884
|
+
// make the record and load it in the store
|
938
885
|
var model = store.makeFixture.apply(store,args);
|
886
|
+
// believe it or not .. this actually works
|
939
887
|
responseJson[modelName]=model;
|
940
888
|
} else {
|
941
889
|
httpOptions.status = 500;
|
@@ -983,8 +931,10 @@ FactoryGuyTestMixin = Em.Mixin.create({
|
|
983
931
|
|
984
932
|
teardown: function () {
|
985
933
|
FactoryGuy.resetModels(this.getStore());
|
934
|
+
$.mockjax.clear();
|
986
935
|
}
|
987
936
|
});
|
937
|
+
|
988
938
|
/*!
|
989
939
|
* MockJax - jQuery Plugin to Mock Ajax requests
|
990
940
|
*
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ember-data-factory-guy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Sudol
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-
|
12
|
+
date: 2014-11-06 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Easily create Fixtures for Ember Data
|
15
15
|
email:
|
@@ -53,8 +53,6 @@ files:
|
|
53
53
|
- tests/support/factories/project_factory.js
|
54
54
|
- tests/support/factories/property_factory.js
|
55
55
|
- tests/support/factories/user_factory.js
|
56
|
-
- tests/support/libs/mockjax.js
|
57
|
-
- tests/support/libs/sinon.js
|
58
56
|
- tests/support/models/company.js
|
59
57
|
- tests/support/models/group.js
|
60
58
|
- tests/support/models/hat.js
|
@@ -1,600 +0,0 @@
|
|
1
|
-
/*!
|
2
|
-
* MockJax - jQuery Plugin to Mock Ajax requests
|
3
|
-
*
|
4
|
-
* Version: 1.5.3
|
5
|
-
* Released:
|
6
|
-
* Home: http://github.com/appendto/jquery-mockjax
|
7
|
-
* Author: Jonathan Sharp (http://jdsharp.com)
|
8
|
-
* License: MIT,GPL
|
9
|
-
*
|
10
|
-
* Copyright (c) 2011 appendTo LLC.
|
11
|
-
* Dual licensed under the MIT or GPL licenses.
|
12
|
-
* http://appendto.com/open-source-licenses
|
13
|
-
*/
|
14
|
-
(function($) {
|
15
|
-
var _ajax = $.ajax,
|
16
|
-
mockHandlers = [],
|
17
|
-
mockedAjaxCalls = [],
|
18
|
-
CALLBACK_REGEX = /=\?(&|$)/,
|
19
|
-
jsc = (new Date()).getTime();
|
20
|
-
|
21
|
-
|
22
|
-
// Parse the given XML string.
|
23
|
-
function parseXML(xml) {
|
24
|
-
if ( window.DOMParser == undefined && window.ActiveXObject ) {
|
25
|
-
DOMParser = function() { };
|
26
|
-
|
27
|
-
DOMParser.prototype.parseFromString = function( xmlString ) {
|
28
|
-
var doc = new ActiveXObject('Microsoft.XMLDOM');
|
29
|
-
doc.async = 'false';
|
30
|
-
doc.loadXML( xmlString );
|
31
|
-
return doc;
|
32
|
-
};
|
33
|
-
}
|
34
|
-
|
35
|
-
try {
|
36
|
-
var xmlDoc = ( new DOMParser() ).parseFromString( xml, 'text/xml' );
|
37
|
-
if ( $.isXMLDoc( xmlDoc ) ) {
|
38
|
-
var err = $('parsererror', xmlDoc);
|
39
|
-
if ( err.length == 1 ) {
|
40
|
-
throw('Error: ' + $(xmlDoc).text() );
|
41
|
-
}
|
42
|
-
} else {
|
43
|
-
throw('Unable to parse XML');
|
44
|
-
}
|
45
|
-
return xmlDoc;
|
46
|
-
} catch( e ) {
|
47
|
-
var msg = ( e.name == undefined ? e : e.name + ': ' + e.message );
|
48
|
-
$(document).trigger('xmlParseError', [ msg ]);
|
49
|
-
return undefined;
|
50
|
-
}
|
51
|
-
}
|
52
|
-
|
53
|
-
// Trigger a jQuery event
|
54
|
-
function trigger(s, type, args) {
|
55
|
-
(s.context ? $(s.context) : $.event).trigger(type, args);
|
56
|
-
}
|
57
|
-
|
58
|
-
// Check if the data field on the mock handler and the request match. This
|
59
|
-
// can be used to restrict a mock handler to being used only when a certain
|
60
|
-
// set of data is passed to it.
|
61
|
-
function isMockDataEqual( mock, live ) {
|
62
|
-
var identical = true;
|
63
|
-
// Test for situations where the data is a querystring (not an object)
|
64
|
-
if (typeof live === 'string') {
|
65
|
-
// Querystring may be a regex
|
66
|
-
return $.isFunction( mock.test ) ? mock.test(live) : mock == live;
|
67
|
-
}
|
68
|
-
$.each(mock, function(k) {
|
69
|
-
if ( live[k] === undefined ) {
|
70
|
-
identical = false;
|
71
|
-
return identical;
|
72
|
-
} else {
|
73
|
-
// This will allow to compare Arrays
|
74
|
-
if ( typeof live[k] === 'object' && live[k] !== null ) {
|
75
|
-
identical = identical && isMockDataEqual(mock[k], live[k]);
|
76
|
-
} else {
|
77
|
-
if ( mock[k] && $.isFunction( mock[k].test ) ) {
|
78
|
-
identical = identical && mock[k].test(live[k]);
|
79
|
-
} else {
|
80
|
-
identical = identical && ( mock[k] == live[k] );
|
81
|
-
}
|
82
|
-
}
|
83
|
-
}
|
84
|
-
});
|
85
|
-
|
86
|
-
return identical;
|
87
|
-
}
|
88
|
-
|
89
|
-
// See if a mock handler property matches the default settings
|
90
|
-
function isDefaultSetting(handler, property) {
|
91
|
-
return handler[property] === $.mockjaxSettings[property];
|
92
|
-
}
|
93
|
-
|
94
|
-
// Check the given handler should mock the given request
|
95
|
-
function getMockForRequest( handler, requestSettings ) {
|
96
|
-
|
97
|
-
// If the mock was registered with a function, let the function decide if we
|
98
|
-
// want to mock this request
|
99
|
-
if ( $.isFunction(handler) ) {
|
100
|
-
return handler( requestSettings );
|
101
|
-
}
|
102
|
-
|
103
|
-
// Inspect the URL of the request and check if the mock handler's url
|
104
|
-
// matches the url for this ajax request
|
105
|
-
if ( $.isFunction(handler.url.test) ) {
|
106
|
-
// The user provided a regex for the url, test it
|
107
|
-
if ( !handler.url.test( requestSettings.url ) ) {
|
108
|
-
return null;
|
109
|
-
}
|
110
|
-
} else {
|
111
|
-
// Look for a simple wildcard '*' or a direct URL match
|
112
|
-
var star = handler.url.indexOf('*');
|
113
|
-
if (handler.url !== requestSettings.url && star === -1 ||
|
114
|
-
!new RegExp(handler.url.replace(/[-[\]{}()+?.,\\^$|#\s]/g, "\\$&").replace(/\*/g, '.+')).test(requestSettings.url)) {
|
115
|
-
return null;
|
116
|
-
}
|
117
|
-
}
|
118
|
-
// console.log(handler.url, handler.data, requestSettings.data)
|
119
|
-
// Inspect the data submitted in the request (either POST body or GET query string)
|
120
|
-
if ( handler.data && requestSettings.data ) {
|
121
|
-
if ( !isMockDataEqual(handler.data, requestSettings.data) ) {
|
122
|
-
// They're not identical, do not mock this request
|
123
|
-
return null;
|
124
|
-
}
|
125
|
-
}
|
126
|
-
// Inspect the request type
|
127
|
-
if ( handler && handler.type &&
|
128
|
-
handler.type.toLowerCase() != requestSettings.type.toLowerCase() ) {
|
129
|
-
// The request type doesn't match (GET vs. POST)
|
130
|
-
return null;
|
131
|
-
}
|
132
|
-
|
133
|
-
return handler;
|
134
|
-
}
|
135
|
-
|
136
|
-
// Process the xhr objects send operation
|
137
|
-
function _xhrSend(mockHandler, requestSettings, origSettings) {
|
138
|
-
|
139
|
-
// This is a substitute for < 1.4 which lacks $.proxy
|
140
|
-
var process = (function(that) {
|
141
|
-
return function() {
|
142
|
-
return (function() {
|
143
|
-
var onReady;
|
144
|
-
|
145
|
-
// The request has returned
|
146
|
-
this.status = mockHandler.status;
|
147
|
-
this.statusText = mockHandler.statusText;
|
148
|
-
this.readyState = 4;
|
149
|
-
|
150
|
-
// We have an executable function, call it to give
|
151
|
-
// the mock handler a chance to update it's data
|
152
|
-
if ( $.isFunction(mockHandler.response) ) {
|
153
|
-
mockHandler.response(origSettings);
|
154
|
-
}
|
155
|
-
// Copy over our mock to our xhr object before passing control back to
|
156
|
-
// jQuery's onreadystatechange callback
|
157
|
-
if ( requestSettings.dataType == 'json' && ( typeof mockHandler.responseText == 'object' ) ) {
|
158
|
-
this.responseText = JSON.stringify(mockHandler.responseText);
|
159
|
-
} else if ( requestSettings.dataType == 'xml' ) {
|
160
|
-
if ( typeof mockHandler.responseXML == 'string' ) {
|
161
|
-
this.responseXML = parseXML(mockHandler.responseXML);
|
162
|
-
//in jQuery 1.9.1+, responseXML is processed differently and relies on responseText
|
163
|
-
this.responseText = mockHandler.responseXML;
|
164
|
-
} else {
|
165
|
-
this.responseXML = mockHandler.responseXML;
|
166
|
-
}
|
167
|
-
} else {
|
168
|
-
this.responseText = mockHandler.responseText;
|
169
|
-
}
|
170
|
-
if( typeof mockHandler.status == 'number' || typeof mockHandler.status == 'string' ) {
|
171
|
-
this.status = mockHandler.status;
|
172
|
-
}
|
173
|
-
if( typeof mockHandler.statusText === "string") {
|
174
|
-
this.statusText = mockHandler.statusText;
|
175
|
-
}
|
176
|
-
// jQuery 2.0 renamed onreadystatechange to onload
|
177
|
-
onReady = this.onreadystatechange || this.onload;
|
178
|
-
|
179
|
-
// jQuery < 1.4 doesn't have onreadystate change for xhr
|
180
|
-
if ( $.isFunction( onReady ) ) {
|
181
|
-
if( mockHandler.isTimeout) {
|
182
|
-
this.status = -1;
|
183
|
-
}
|
184
|
-
onReady.call( this, mockHandler.isTimeout ? 'timeout' : undefined );
|
185
|
-
} else if ( mockHandler.isTimeout ) {
|
186
|
-
// Fix for 1.3.2 timeout to keep success from firing.
|
187
|
-
this.status = -1;
|
188
|
-
}
|
189
|
-
}).apply(that);
|
190
|
-
};
|
191
|
-
})(this);
|
192
|
-
|
193
|
-
if ( mockHandler.proxy ) {
|
194
|
-
// We're proxying this request and loading in an external file instead
|
195
|
-
_ajax({
|
196
|
-
global: false,
|
197
|
-
url: mockHandler.proxy,
|
198
|
-
type: mockHandler.proxyType,
|
199
|
-
data: mockHandler.data,
|
200
|
-
dataType: requestSettings.dataType === "script" ? "text/plain" : requestSettings.dataType,
|
201
|
-
complete: function(xhr) {
|
202
|
-
mockHandler.responseXML = xhr.responseXML;
|
203
|
-
mockHandler.responseText = xhr.responseText;
|
204
|
-
// Don't override the handler status/statusText if it's specified by the config
|
205
|
-
if (isDefaultSetting(mockHandler, 'status')) {
|
206
|
-
mockHandler.status = xhr.status;
|
207
|
-
}
|
208
|
-
if (isDefaultSetting(mockHandler, 'statusText')) {
|
209
|
-
mockHandler.statusText = xhr.statusText;
|
210
|
-
}
|
211
|
-
|
212
|
-
this.responseTimer = setTimeout(process, mockHandler.responseTime || 0);
|
213
|
-
}
|
214
|
-
});
|
215
|
-
} else {
|
216
|
-
// type == 'POST' || 'GET' || 'DELETE'
|
217
|
-
if ( requestSettings.async === false ) {
|
218
|
-
// TODO: Blocking delay
|
219
|
-
process();
|
220
|
-
} else {
|
221
|
-
this.responseTimer = setTimeout(process, mockHandler.responseTime || 50);
|
222
|
-
}
|
223
|
-
}
|
224
|
-
}
|
225
|
-
|
226
|
-
// Construct a mocked XHR Object
|
227
|
-
function xhr(mockHandler, requestSettings, origSettings, origHandler) {
|
228
|
-
// Extend with our default mockjax settings
|
229
|
-
mockHandler = $.extend(true, {}, $.mockjaxSettings, mockHandler);
|
230
|
-
|
231
|
-
if (typeof mockHandler.headers === 'undefined') {
|
232
|
-
mockHandler.headers = {};
|
233
|
-
}
|
234
|
-
if ( mockHandler.contentType ) {
|
235
|
-
mockHandler.headers['content-type'] = mockHandler.contentType;
|
236
|
-
}
|
237
|
-
|
238
|
-
return {
|
239
|
-
status: mockHandler.status,
|
240
|
-
statusText: mockHandler.statusText,
|
241
|
-
readyState: 1,
|
242
|
-
open: function() { },
|
243
|
-
send: function() {
|
244
|
-
origHandler.fired = true;
|
245
|
-
_xhrSend.call(this, mockHandler, requestSettings, origSettings);
|
246
|
-
},
|
247
|
-
abort: function() {
|
248
|
-
clearTimeout(this.responseTimer);
|
249
|
-
},
|
250
|
-
setRequestHeader: function(header, value) {
|
251
|
-
mockHandler.headers[header] = value;
|
252
|
-
},
|
253
|
-
getResponseHeader: function(header) {
|
254
|
-
// 'Last-modified', 'Etag', 'content-type' are all checked by jQuery
|
255
|
-
if ( mockHandler.headers && mockHandler.headers[header] ) {
|
256
|
-
// Return arbitrary headers
|
257
|
-
return mockHandler.headers[header];
|
258
|
-
} else if ( header.toLowerCase() == 'last-modified' ) {
|
259
|
-
return mockHandler.lastModified || (new Date()).toString();
|
260
|
-
} else if ( header.toLowerCase() == 'etag' ) {
|
261
|
-
return mockHandler.etag || '';
|
262
|
-
} else if ( header.toLowerCase() == 'content-type' ) {
|
263
|
-
return mockHandler.contentType || 'text/plain';
|
264
|
-
}
|
265
|
-
},
|
266
|
-
getAllResponseHeaders: function() {
|
267
|
-
var headers = '';
|
268
|
-
$.each(mockHandler.headers, function(k, v) {
|
269
|
-
headers += k + ': ' + v + "\n";
|
270
|
-
});
|
271
|
-
return headers;
|
272
|
-
}
|
273
|
-
};
|
274
|
-
}
|
275
|
-
|
276
|
-
// Process a JSONP mock request.
|
277
|
-
function processJsonpMock( requestSettings, mockHandler, origSettings ) {
|
278
|
-
// Handle JSONP Parameter Callbacks, we need to replicate some of the jQuery core here
|
279
|
-
// because there isn't an easy hook for the cross domain script tag of jsonp
|
280
|
-
|
281
|
-
processJsonpUrl( requestSettings );
|
282
|
-
|
283
|
-
requestSettings.dataType = "json";
|
284
|
-
if(requestSettings.data && CALLBACK_REGEX.test(requestSettings.data) || CALLBACK_REGEX.test(requestSettings.url)) {
|
285
|
-
createJsonpCallback(requestSettings, mockHandler, origSettings);
|
286
|
-
|
287
|
-
// We need to make sure
|
288
|
-
// that a JSONP style response is executed properly
|
289
|
-
|
290
|
-
var rurl = /^(\w+:)?\/\/([^\/?#]+)/,
|
291
|
-
parts = rurl.exec( requestSettings.url ),
|
292
|
-
remote = parts && (parts[1] && parts[1] !== location.protocol || parts[2] !== location.host);
|
293
|
-
|
294
|
-
requestSettings.dataType = "script";
|
295
|
-
if(requestSettings.type.toUpperCase() === "GET" && remote ) {
|
296
|
-
var newMockReturn = processJsonpRequest( requestSettings, mockHandler, origSettings );
|
297
|
-
|
298
|
-
// Check if we are supposed to return a Deferred back to the mock call, or just
|
299
|
-
// signal success
|
300
|
-
if(newMockReturn) {
|
301
|
-
return newMockReturn;
|
302
|
-
} else {
|
303
|
-
return true;
|
304
|
-
}
|
305
|
-
}
|
306
|
-
}
|
307
|
-
return null;
|
308
|
-
}
|
309
|
-
|
310
|
-
// Append the required callback parameter to the end of the request URL, for a JSONP request
|
311
|
-
function processJsonpUrl( requestSettings ) {
|
312
|
-
if ( requestSettings.type.toUpperCase() === "GET" ) {
|
313
|
-
if ( !CALLBACK_REGEX.test( requestSettings.url ) ) {
|
314
|
-
requestSettings.url += (/\?/.test( requestSettings.url ) ? "&" : "?") +
|
315
|
-
(requestSettings.jsonp || "callback") + "=?";
|
316
|
-
}
|
317
|
-
} else if ( !requestSettings.data || !CALLBACK_REGEX.test(requestSettings.data) ) {
|
318
|
-
requestSettings.data = (requestSettings.data ? requestSettings.data + "&" : "") + (requestSettings.jsonp || "callback") + "=?";
|
319
|
-
}
|
320
|
-
}
|
321
|
-
|
322
|
-
// Process a JSONP request by evaluating the mocked response text
|
323
|
-
function processJsonpRequest( requestSettings, mockHandler, origSettings ) {
|
324
|
-
// Synthesize the mock request for adding a script tag
|
325
|
-
var callbackContext = origSettings && origSettings.context || requestSettings,
|
326
|
-
newMock = null;
|
327
|
-
|
328
|
-
|
329
|
-
// If the response handler on the moock is a function, call it
|
330
|
-
if ( mockHandler.response && $.isFunction(mockHandler.response) ) {
|
331
|
-
mockHandler.response(origSettings);
|
332
|
-
} else {
|
333
|
-
|
334
|
-
// Evaluate the responseText javascript in a global context
|
335
|
-
if( typeof mockHandler.responseText === 'object' ) {
|
336
|
-
$.globalEval( '(' + JSON.stringify( mockHandler.responseText ) + ')');
|
337
|
-
} else {
|
338
|
-
$.globalEval( '(' + mockHandler.responseText + ')');
|
339
|
-
}
|
340
|
-
}
|
341
|
-
|
342
|
-
// Successful response
|
343
|
-
jsonpSuccess( requestSettings, callbackContext, mockHandler );
|
344
|
-
jsonpComplete( requestSettings, callbackContext, mockHandler );
|
345
|
-
|
346
|
-
// If we are running under jQuery 1.5+, return a deferred object
|
347
|
-
if($.Deferred){
|
348
|
-
newMock = new $.Deferred();
|
349
|
-
if(typeof mockHandler.responseText == "object"){
|
350
|
-
newMock.resolveWith( callbackContext, [mockHandler.responseText] );
|
351
|
-
}
|
352
|
-
else{
|
353
|
-
newMock.resolveWith( callbackContext, [$.parseJSON( mockHandler.responseText )] );
|
354
|
-
}
|
355
|
-
}
|
356
|
-
return newMock;
|
357
|
-
}
|
358
|
-
|
359
|
-
|
360
|
-
// Create the required JSONP callback function for the request
|
361
|
-
function createJsonpCallback( requestSettings, mockHandler, origSettings ) {
|
362
|
-
var callbackContext = origSettings && origSettings.context || requestSettings;
|
363
|
-
var jsonp = requestSettings.jsonpCallback || ("jsonp" + jsc++);
|
364
|
-
|
365
|
-
// Replace the =? sequence both in the query string and the data
|
366
|
-
if ( requestSettings.data ) {
|
367
|
-
requestSettings.data = (requestSettings.data + "").replace(CALLBACK_REGEX, "=" + jsonp + "$1");
|
368
|
-
}
|
369
|
-
|
370
|
-
requestSettings.url = requestSettings.url.replace(CALLBACK_REGEX, "=" + jsonp + "$1");
|
371
|
-
|
372
|
-
|
373
|
-
// Handle JSONP-style loading
|
374
|
-
window[ jsonp ] = window[ jsonp ] || function( tmp ) {
|
375
|
-
data = tmp;
|
376
|
-
jsonpSuccess( requestSettings, callbackContext, mockHandler );
|
377
|
-
jsonpComplete( requestSettings, callbackContext, mockHandler );
|
378
|
-
// Garbage collect
|
379
|
-
window[ jsonp ] = undefined;
|
380
|
-
|
381
|
-
try {
|
382
|
-
delete window[ jsonp ];
|
383
|
-
} catch(e) {}
|
384
|
-
|
385
|
-
if ( head ) {
|
386
|
-
head.removeChild( script );
|
387
|
-
}
|
388
|
-
};
|
389
|
-
}
|
390
|
-
|
391
|
-
// The JSONP request was successful
|
392
|
-
function jsonpSuccess(requestSettings, callbackContext, mockHandler) {
|
393
|
-
// If a local callback was specified, fire it and pass it the data
|
394
|
-
if ( requestSettings.success ) {
|
395
|
-
requestSettings.success.call( callbackContext, mockHandler.responseText || "", status, {} );
|
396
|
-
}
|
397
|
-
|
398
|
-
// Fire the global callback
|
399
|
-
if ( requestSettings.global ) {
|
400
|
-
trigger(requestSettings, "ajaxSuccess", [{}, requestSettings] );
|
401
|
-
}
|
402
|
-
}
|
403
|
-
|
404
|
-
// The JSONP request was completed
|
405
|
-
function jsonpComplete(requestSettings, callbackContext) {
|
406
|
-
// Process result
|
407
|
-
if ( requestSettings.complete ) {
|
408
|
-
requestSettings.complete.call( callbackContext, {} , status );
|
409
|
-
}
|
410
|
-
|
411
|
-
// The request was completed
|
412
|
-
if ( requestSettings.global ) {
|
413
|
-
trigger( "ajaxComplete", [{}, requestSettings] );
|
414
|
-
}
|
415
|
-
|
416
|
-
// Handle the global AJAX counter
|
417
|
-
if ( requestSettings.global && ! --$.active ) {
|
418
|
-
$.event.trigger( "ajaxStop" );
|
419
|
-
}
|
420
|
-
}
|
421
|
-
|
422
|
-
|
423
|
-
// The core $.ajax replacement.
|
424
|
-
function handleAjax( url, origSettings ) {
|
425
|
-
var mockRequest, requestSettings, mockHandler;
|
426
|
-
// console.log('handleAjax', url)
|
427
|
-
// If url is an object, simulate pre-1.5 signature
|
428
|
-
if ( typeof url === "object" ) {
|
429
|
-
origSettings = url;
|
430
|
-
url = undefined;
|
431
|
-
} else {
|
432
|
-
// work around to support 1.5 signature
|
433
|
-
origSettings.url = url;
|
434
|
-
}
|
435
|
-
|
436
|
-
// Extend the original settings for the request
|
437
|
-
requestSettings = $.extend(true, {}, $.ajaxSettings, origSettings);
|
438
|
-
|
439
|
-
// Iterate over our mock handlers (in registration order) until we find
|
440
|
-
// one that is willing to intercept the request
|
441
|
-
for(var k = 0; k < mockHandlers.length; k++) {
|
442
|
-
if ( !mockHandlers[k] ) {
|
443
|
-
continue;
|
444
|
-
}
|
445
|
-
|
446
|
-
mockHandler = getMockForRequest( mockHandlers[k], requestSettings );
|
447
|
-
if(!mockHandler) {
|
448
|
-
// No valid mock found for this request
|
449
|
-
continue;
|
450
|
-
}
|
451
|
-
|
452
|
-
mockedAjaxCalls.push(requestSettings);
|
453
|
-
|
454
|
-
// If logging is enabled, log the mock to the console
|
455
|
-
$.mockjaxSettings.log( mockHandler, requestSettings );
|
456
|
-
|
457
|
-
|
458
|
-
if ( requestSettings.dataType === "jsonp" ) {
|
459
|
-
if ((mockRequest = processJsonpMock( requestSettings, mockHandler, origSettings ))) {
|
460
|
-
// This mock will handle the JSONP request
|
461
|
-
return mockRequest;
|
462
|
-
}
|
463
|
-
}
|
464
|
-
|
465
|
-
|
466
|
-
// Removed to fix #54 - keep the mocking data object intact
|
467
|
-
//mockHandler.data = requestSettings.data;
|
468
|
-
|
469
|
-
mockHandler.cache = requestSettings.cache;
|
470
|
-
mockHandler.timeout = requestSettings.timeout;
|
471
|
-
mockHandler.global = requestSettings.global;
|
472
|
-
|
473
|
-
copyUrlParameters(mockHandler, origSettings);
|
474
|
-
|
475
|
-
(function(mockHandler, requestSettings, origSettings, origHandler) {
|
476
|
-
mockRequest = _ajax.call($, $.extend(true, {}, origSettings, {
|
477
|
-
// Mock the XHR object
|
478
|
-
xhr: function() { return xhr( mockHandler, requestSettings, origSettings, origHandler ); }
|
479
|
-
}));
|
480
|
-
})(mockHandler, requestSettings, origSettings, mockHandlers[k]);
|
481
|
-
|
482
|
-
return mockRequest;
|
483
|
-
}
|
484
|
-
|
485
|
-
// We don't have a mock request
|
486
|
-
if($.mockjaxSettings.throwUnmocked === true) {
|
487
|
-
throw('AJAX not mocked: ' + origSettings.url);
|
488
|
-
}
|
489
|
-
else { // trigger a normal request
|
490
|
-
return _ajax.apply($, [origSettings]);
|
491
|
-
}
|
492
|
-
}
|
493
|
-
|
494
|
-
/**
|
495
|
-
* Copies URL parameter values if they were captured by a regular expression
|
496
|
-
* @param {Object} mockHandler
|
497
|
-
* @param {Object} origSettings
|
498
|
-
*/
|
499
|
-
function copyUrlParameters(mockHandler, origSettings) {
|
500
|
-
//parameters aren't captured if the URL isn't a RegExp
|
501
|
-
if (!(mockHandler.url instanceof RegExp)) {
|
502
|
-
return;
|
503
|
-
}
|
504
|
-
//if no URL params were defined on the handler, don't attempt a capture
|
505
|
-
if (!mockHandler.hasOwnProperty('urlParams')) {
|
506
|
-
return;
|
507
|
-
}
|
508
|
-
var captures = mockHandler.url.exec(origSettings.url);
|
509
|
-
//the whole RegExp match is always the first value in the capture results
|
510
|
-
if (captures.length === 1) {
|
511
|
-
return;
|
512
|
-
}
|
513
|
-
captures.shift();
|
514
|
-
//use handler params as keys and capture resuts as values
|
515
|
-
var i = 0,
|
516
|
-
capturesLength = captures.length,
|
517
|
-
paramsLength = mockHandler.urlParams.length,
|
518
|
-
//in case the number of params specified is less than actual captures
|
519
|
-
maxIterations = Math.min(capturesLength, paramsLength),
|
520
|
-
paramValues = {};
|
521
|
-
for (i; i < maxIterations; i++) {
|
522
|
-
var key = mockHandler.urlParams[i];
|
523
|
-
paramValues[key] = captures[i];
|
524
|
-
}
|
525
|
-
origSettings.urlParams = paramValues;
|
526
|
-
}
|
527
|
-
|
528
|
-
|
529
|
-
// Public
|
530
|
-
|
531
|
-
$.extend({
|
532
|
-
ajax: handleAjax
|
533
|
-
});
|
534
|
-
|
535
|
-
$.mockjaxSettings = {
|
536
|
-
//url: null,
|
537
|
-
//type: 'GET',
|
538
|
-
log: function( mockHandler, requestSettings ) {
|
539
|
-
if ( mockHandler.logging === false ||
|
540
|
-
( typeof mockHandler.logging === 'undefined' && $.mockjaxSettings.logging === false ) ) {
|
541
|
-
return;
|
542
|
-
}
|
543
|
-
if ( window.console && console.log ) {
|
544
|
-
var message = 'MOCK ' + requestSettings.type.toUpperCase() + ': ' + requestSettings.url;
|
545
|
-
var request = $.extend({}, requestSettings);
|
546
|
-
|
547
|
-
if (typeof console.log === 'function') {
|
548
|
-
console.log(message, request);
|
549
|
-
} else {
|
550
|
-
try {
|
551
|
-
console.log( message + ' ' + JSON.stringify(request) );
|
552
|
-
} catch (e) {
|
553
|
-
console.log(message);
|
554
|
-
}
|
555
|
-
}
|
556
|
-
}
|
557
|
-
},
|
558
|
-
logging: true,
|
559
|
-
status: 200,
|
560
|
-
statusText: "OK",
|
561
|
-
responseTime: 500,
|
562
|
-
isTimeout: false,
|
563
|
-
throwUnmocked: false,
|
564
|
-
contentType: 'text/plain',
|
565
|
-
response: '',
|
566
|
-
responseText: '',
|
567
|
-
responseXML: '',
|
568
|
-
proxy: '',
|
569
|
-
proxyType: 'GET',
|
570
|
-
|
571
|
-
lastModified: null,
|
572
|
-
etag: '',
|
573
|
-
headers: {
|
574
|
-
etag: 'IJF@H#@923uf8023hFO@I#H#',
|
575
|
-
'content-type' : 'text/plain'
|
576
|
-
}
|
577
|
-
};
|
578
|
-
|
579
|
-
$.mockjax = function(settings) {
|
580
|
-
var i = mockHandlers.length;
|
581
|
-
mockHandlers[i] = settings;
|
582
|
-
return i;
|
583
|
-
};
|
584
|
-
$.mockjaxClear = function(i) {
|
585
|
-
if ( arguments.length == 1 ) {
|
586
|
-
mockHandlers[i] = null;
|
587
|
-
} else {
|
588
|
-
mockHandlers = [];
|
589
|
-
}
|
590
|
-
mockedAjaxCalls = [];
|
591
|
-
};
|
592
|
-
$.mockjax.handler = function(i) {
|
593
|
-
if ( arguments.length == 1 ) {
|
594
|
-
return mockHandlers[i];
|
595
|
-
}
|
596
|
-
};
|
597
|
-
$.mockjax.mockedAjaxCalls = function() {
|
598
|
-
return mockedAjaxCalls;
|
599
|
-
};
|
600
|
-
})(jQuery);
|