Skip to content

Commit

Permalink
Merge pull request #158 from canjs/clone
Browse files Browse the repository at this point in the history
Make can-fixture work within steal-clone
  • Loading branch information
matthewp authored Oct 25, 2018
2 parents c67816b + 96ed08e commit ef09793
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
},
"devDependencies": {
"can-set-legacy": "<2.0.0",
"can-test-helpers": "^1.1.2",
"detect-cyclic-packages": "^1.1.0",
"jquery": "^3.1.1",
"jshint": "^2.7.0",
Expand Down
21 changes: 21 additions & 0 deletions test/fixture_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ var fixture = require("can-fixture");
var set = require("can-set-legacy");
var $ = require("jquery");
var canDev = require('can-log/dev/dev');
var clone = require("steal-clone");
var dataFromUrl = require("../data-from-url");
var canReflect = require("can-reflect");
var matches = require("../matches");
var QueryLogic = require("can-query-logic");
var testHelpers = require("can-test-helpers");


var errorCallback = function(xhr, status, error){
Expand Down Expand Up @@ -1945,4 +1947,23 @@ if ("onabort" in XMLHttpRequest._XHR.prototype) {
xhr.open('GET', '/onload');
xhr.send();
});

testHelpers.dev.devOnlyTest("Works with steal-clone", function() {
clone({})["import"]("can-fixture").then(function(fixture){
fixture('/onload', function(req, res) {
res(400);
});

var xhr = new XMLHttpRequest();
xhr.addEventListener('load', function() {
fixture('/onload', null);
QUnit.ok(true, "Got to the load event without throwing");
start();
});
xhr.open('GET', '/onload');
xhr.send();
});

stop();
});
} // END onabort check
15 changes: 12 additions & 3 deletions xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,25 @@ function callEvents(xhr, ev) {
}
}

function defineNonEnumerable(obj, prop, value) {
Object.defineProperty(obj, prop, {
enumerable: false,
configurable: true,
writable: true,
value: value
});
}

GLOBAL.XMLHttpRequest = function() {
var mockXHR = this;
var realXHR = new XHR();

// store real xhr on mockXHR
this._xhr = realXHR;
defineNonEnumerable(this, "_xhr", realXHR);

// create other properties needed by prototype functions
this._requestHeaders = {};
this.__events = {};
defineNonEnumerable(this, "_requestHeaders", {});
defineNonEnumerable(this, "__events", {});

// wire up events to forward from real xhr to fake xhr
events.forEach(function(eventName) {
Expand Down

0 comments on commit ef09793

Please sign in to comment.