jasmine-ajax
Advanced tools
Comparing version 3.1.1 to 3.2.0
{ | ||
"name": "jasmine-ajax", | ||
"description": "A library for faking Ajax responses in your Jasmine suite.", | ||
"version": "3.1.1", | ||
"version": "3.2.0", | ||
"main": "lib/mock-ajax.js", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
/* | ||
Jasmine-Ajax - v3.1.0: a set of helpers for testing AJAX requests under the Jasmine | ||
Jasmine-Ajax - v3.2.0: a set of helpers for testing AJAX requests under the Jasmine | ||
BDD framework for JavaScript. | ||
@@ -40,3 +40,4 @@ | ||
$ajax.ParamParser = jRequire.AjaxParamParser(); | ||
$ajax.eventBus = jRequire.AjaxEventBus(); | ||
$ajax.event = jRequire.AjaxEvent(); | ||
$ajax.eventBus = jRequire.AjaxEventBus($ajax.event); | ||
$ajax.fakeRequest = jRequire.AjaxFakeRequest($ajax.eventBus); | ||
@@ -48,5 +49,58 @@ $ajax.MockAjax = jRequire.MockAjax($ajax); | ||
getJasmineRequireObj().AjaxEventBus = function() { | ||
function EventBus() { | ||
getJasmineRequireObj().AjaxEvent = function() { | ||
function now() { | ||
return new Date().getTime(); | ||
} | ||
function noop() { | ||
} | ||
// Event object | ||
// https://dom.spec.whatwg.org/#concept-event | ||
function XMLHttpRequestEvent(xhr, type) { | ||
this.type = type; | ||
this.bubbles = false; | ||
this.cancelable = false; | ||
this.timeStamp = now(); | ||
this.isTrusted = false; | ||
this.defaultPrevented = false; | ||
// Event phase should be "AT_TARGET" | ||
// https://dom.spec.whatwg.org/#dom-event-at_target | ||
this.eventPhase = 2; | ||
this.target = xhr; | ||
this.currentTarget = xhr; | ||
} | ||
XMLHttpRequestEvent.prototype.preventDefault = noop; | ||
XMLHttpRequestEvent.prototype.stopPropagation = noop; | ||
XMLHttpRequestEvent.prototype.stopImmediatePropagation = noop; | ||
function XMLHttpRequestProgressEvent() { | ||
XMLHttpRequestEvent.apply(this, arguments); | ||
this.lengthComputable = false; | ||
this.loaded = 0; | ||
this.total = 0; | ||
} | ||
// Extend prototype | ||
XMLHttpRequestProgressEvent.prototype = XMLHttpRequestEvent.prototype; | ||
return { | ||
event: function(xhr, type) { | ||
return new XMLHttpRequestEvent(xhr, type); | ||
}, | ||
progressEvent: function(xhr, type) { | ||
return new XMLHttpRequestProgressEvent(xhr, type); | ||
} | ||
}; | ||
}; | ||
getJasmineRequireObj().AjaxEventBus = function(eventFactory) { | ||
function EventBus(source) { | ||
this.eventList = {}; | ||
this.source = source; | ||
} | ||
@@ -86,7 +140,18 @@ | ||
EventBus.prototype.trigger = function(event) { | ||
var evt; | ||
// Event 'readystatechange' is should be a simple event. | ||
// Others are progress event. | ||
// https://xhr.spec.whatwg.org/#events | ||
if (event === 'readystatechange') { | ||
evt = eventFactory.event(this.source, event); | ||
} else { | ||
evt = eventFactory.progressEvent(this.source, event); | ||
} | ||
var eventListeners = this.eventList[event]; | ||
if(eventListeners){ | ||
for(var i = 0; i < eventListeners.length; i++){ | ||
eventListeners[i](); | ||
if (eventListeners) { | ||
for (var i = 0; i < eventListeners.length; i++) { | ||
eventListeners[i].call(this.source, evt); | ||
} | ||
@@ -96,6 +161,7 @@ } | ||
return function() { | ||
return new EventBus(); | ||
return function(source) { | ||
return new EventBus(source); | ||
}; | ||
}; | ||
getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) { | ||
@@ -124,3 +190,3 @@ function extend(destination, source, propertiesToSkip) { | ||
if (xhr[eventName]) { | ||
xhr[eventName](); | ||
xhr[eventName].apply(xhr, arguments); | ||
} | ||
@@ -131,2 +197,3 @@ }; | ||
function initializeEvents(xhr) { | ||
xhr.eventBus.addEventListener('readystatechange', wrapProgressEvent(xhr, 'onreadystatechange')); | ||
xhr.eventBus.addEventListener('loadstart', wrapProgressEvent(xhr, 'onloadstart')); | ||
@@ -154,3 +221,3 @@ xhr.eventBus.addEventListener('load', wrapProgressEvent(xhr, 'onload')); | ||
requestTracker.track(this); | ||
this.eventBus = eventBusFactory(); | ||
this.eventBus = eventBusFactory(this); | ||
initializeEvents(this); | ||
@@ -221,3 +288,3 @@ this.requestHeaders = {}; | ||
this.readyState = 1; | ||
this.onreadystatechange(); | ||
this.eventBus.trigger('readystatechange'); | ||
}, | ||
@@ -241,3 +308,3 @@ | ||
this.statusText = "abort"; | ||
this.onreadystatechange(); | ||
this.eventBus.trigger('readystatechange'); | ||
this.eventBus.trigger('progress'); | ||
@@ -257,10 +324,8 @@ this.eventBus.trigger('abort'); | ||
onloadend: null, | ||
onreadystatechange: null, | ||
onreadystatechange: function(isTimeout) { | ||
}, | ||
addEventListener: function() { | ||
this.eventBus.addEventListener.apply(this.eventBus, arguments); | ||
}, | ||
removeEventListener: function(event, callback) { | ||
@@ -274,9 +339,13 @@ this.eventBus.removeEventListener.apply(this.eventBus, arguments); | ||
this.params = data; | ||
this.readyState = 2; | ||
this.eventBus.trigger('loadstart'); | ||
this.onreadystatechange(); | ||
var stub = stubTracker.findStub(this.url, data, this.method); | ||
if (stub) { | ||
this.respondWith(stub); | ||
if (stub.isReturn()) { | ||
this.respondWith(stub); | ||
} else if (stub.isError()) { | ||
this.responseError(); | ||
} else if (stub.isTimeout()) { | ||
this.responseTimeout(); | ||
} | ||
} | ||
@@ -348,8 +417,12 @@ }, | ||
} | ||
this.status = response.status; | ||
this.statusText = response.statusText || ""; | ||
this.responseHeaders = normalizeHeaders(response.responseHeaders, response.contentType); | ||
this.readyState = 2; | ||
this.eventBus.trigger('readystatechange'); | ||
this.responseText = response.responseText || ""; | ||
this.responseType = response.responseType || ""; | ||
this.readyState = 4; | ||
this.responseHeaders = normalizeHeaders(response.responseHeaders, response.contentType); | ||
this.responseXML = getResponseXml(response.responseText, this.getResponseHeader('content-type') || ''); | ||
@@ -366,3 +439,3 @@ if (this.responseXML) { | ||
this.onreadystatechange(); | ||
this.eventBus.trigger('readystatechange'); | ||
this.eventBus.trigger('progress'); | ||
@@ -379,3 +452,3 @@ this.eventBus.trigger('load'); | ||
jasmine.clock().tick(30000); | ||
this.onreadystatechange('timeout'); | ||
this.eventBus.trigger('readystatechange'); | ||
this.eventBus.trigger('progress'); | ||
@@ -391,3 +464,3 @@ this.eventBus.trigger('timeout'); | ||
this.readyState = 4; | ||
this.onreadystatechange(); | ||
this.eventBus.trigger('readystatechange'); | ||
this.eventBus.trigger('progress'); | ||
@@ -513,2 +586,6 @@ this.eventBus.trigger('error'); | ||
getJasmineRequireObj().AjaxRequestStub = function() { | ||
var RETURN = 0, | ||
ERROR = 1, | ||
TIMEOUT = 2; | ||
function RequestStub(url, stubData, method) { | ||
@@ -528,6 +605,7 @@ var normalizeQuery = function(query) { | ||
this.data = normalizeQuery(stubData); | ||
this.data = (stubData instanceof RegExp) ? stubData : normalizeQuery(stubData); | ||
this.method = method; | ||
this.andReturn = function(options) { | ||
this.action = RETURN; | ||
this.status = options.status || 200; | ||
@@ -541,7 +619,27 @@ | ||
this.isReturn = function() { | ||
return this.action === RETURN; | ||
}; | ||
this.andError = function() { | ||
this.action = ERROR; | ||
}; | ||
this.isError = function() { | ||
return this.action === ERROR; | ||
}; | ||
this.andTimeout = function() { | ||
this.action = TIMEOUT; | ||
}; | ||
this.isTimeout = function() { | ||
return this.action === TIMEOUT; | ||
}; | ||
this.matches = function(fullUrl, data, method) { | ||
var matches = false; | ||
var urlMatches = false; | ||
fullUrl = fullUrl.toString(); | ||
if (this.url instanceof RegExp) { | ||
matches = this.url.test(fullUrl); | ||
urlMatches = this.url.test(fullUrl); | ||
} else { | ||
@@ -551,5 +649,11 @@ var urlSplit = fullUrl.split('?'), | ||
query = urlSplit[1]; | ||
matches = this.url === url && this.query === normalizeQuery(query); | ||
urlMatches = this.url === url && this.query === normalizeQuery(query); | ||
} | ||
return matches && (!this.data || this.data === normalizeQuery(data)) && (!this.method || this.method === method); | ||
var dataMatches = false; | ||
if (this.data instanceof RegExp) { | ||
dataMatches = this.data.test(data); | ||
} else { | ||
dataMatches = !this.data || this.data === normalizeQuery(data); | ||
} | ||
return urlMatches && dataMatches && (!this.method || this.method === method); | ||
}; | ||
@@ -556,0 +660,0 @@ } |
{ | ||
"name": "jasmine-ajax", | ||
"description": "A library for faking Ajax responses in your Jasmine suite", | ||
"version": "3.1.1", | ||
"version": "3.2.0", | ||
"main": "lib/mock-ajax.js", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
41488
11
644
290