Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
fix(test): fix an ineffective assertion
Browse files Browse the repository at this point in the history
`expect(window.zone.parent).toBeDefined();` is always true since the
root zone parent is set to `null`.

What we actually want to assert is that the code is executed in a fork
of the enclosing zone.

- The code should be executed in a fork of the root zone (`bind()` will
not cause the root zone to fork),
- An `assertInChildOf()` helper has been added to the the utils.
  • Loading branch information
vicb committed May 8, 2015
1 parent c465402 commit d85d2cf
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 167 deletions.
59 changes: 33 additions & 26 deletions test/patch/HTMLImports.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,47 @@ function supportsImports() {
supportsImports.message = 'HTML Imports';

describe('HTML Imports', ifEnvSupports(supportsImports, function () {
var testZone = window.zone.fork();

it('should work with addEventListener', function (done) {
var link = document.createElement('link');
link.rel = 'import';
link.href = 'someUrl';
link.addEventListener('error', function () {
expect(window.zone.parent).toBeDefined();
document.head.removeChild(link);
done();
testZone.run(function() {
var link = document.createElement('link');
link.rel = 'import';
link.href = 'someUrl';
link.addEventListener('error', function () {
assertInChildOf(testZone);
document.head.removeChild(link);
done();
});
document.head.appendChild(link);
});
document.head.appendChild(link);
});

it('should work with onerror', function (done) {
var link = document.createElement('link');
link.rel = 'import';
link.href = 'anotherUrl';
link.onerror = function () {
expect(window.zone.parent).toBeDefined();
document.head.removeChild(link);
done();
};
document.head.appendChild(link);
testZone.run(function() {
var link = document.createElement('link');
link.rel = 'import';
link.href = 'anotherUrl';
link.onerror = function () {
assertInChildOf(testZone);
document.head.removeChild(link);
done();
};
document.head.appendChild(link);
});
});

it('should work with onload', function (done) {
var link = document.createElement('link');
link.rel = 'import';
link.href = '/base/test/assets/import.html';
link.onload = function () {
expect(window.zone.parent).toBeDefined();
document.head.removeChild(link);
done();
};
document.head.appendChild(link);
testZone.run(function() {
var link = document.createElement('link');
link.rel = 'import';
link.href = '/base/test/assets/import.html';
link.onload = function () {
assertInChildOf(testZone);
document.head.removeChild(link);
done();
};
document.head.appendChild(link);
});
});
}));
41 changes: 24 additions & 17 deletions test/patch/MutationObserver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@

describe('MutationObserver', ifEnvSupports('MutationObserver', function () {
var elt;
var testZone = window.zone.fork();

beforeEach(function () {
elt = document.createElement('div');
});

it('should run observers within the zone', function (done) {
var ob = new MutationObserver(function () {
expect(window.zone.parent).toBeDefined();
done();
});
testZone.run(function() {
var ob = new MutationObserver(function () {
assertInChildOf(testZone);
done();
});

ob.observe(elt, {
childList: true
});
ob.observe(elt, {
childList: true
});

elt.innerHTML = '<p>hey</p>';
elt.innerHTML = '<p>hey</p>';
});
});

it('should dequeue upon disconnect', function () {
Expand Down Expand Up @@ -77,18 +80,22 @@ describe('MutationObserver', ifEnvSupports('MutationObserver', function () {
}));

describe('WebKitMutationObserver', ifEnvSupports('WebKitMutationObserver', function () {
var testZone = window.zone.fork();

it('should run observers within the zone', function (done) {
var elt = document.createElement('div');
testZone.run(function() {
var elt = document.createElement('div');

var ob = new WebKitMutationObserver(function () {
expect(window.zone.parent).toBeDefined();
done();
});
var ob = new WebKitMutationObserver(function () {
assertInChildOf(testZone);
done();
});

ob.observe(elt, {
childList: true
});
ob.observe(elt, {
childList: true
});

elt.innerHTML = '<p>hey</p>';
elt.innerHTML = '<p>hey</p>';
});
});
}));
25 changes: 15 additions & 10 deletions test/patch/Promise.spec.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
'use strict';

describe('Promise', ifEnvSupports('Promise', function () {
var testZone = window.zone.fork();

it('should work with .then', function (done) {
new Promise(function (resolve) {
resolve();
}).then(function () {
expect(window.zone.parent).toBeDefined();
done();
testZone.run(function() {
new Promise(function (resolve) {
resolve();
}).then(function () {
assertInChildOf(testZone);
done();
});
});
});

it('should work with .catch', function (done) {
new Promise(function (resolve, reject) {
reject();
}).catch(function () {
expect(window.zone.parent).toBeDefined();
done();
testZone.run(function() {
new Promise(function (resolve, reject) {
reject();
}).catch(function () {
assertInChildOf(testZone);
done();
});
});
});

Expand Down
43 changes: 24 additions & 19 deletions test/patch/XMLHttpRequest.spec.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
'use strict';

describe('XMLHttpRequest', function () {
var testZone = window.zone.fork();

it('should work with onreadystatechange', function (done) {
var req = new XMLHttpRequest();
var firstCall = true;
req.onreadystatechange = function () {
// Make sure that the callback will only be called once
req.onreadystatechange = null;
expect(window.zone.parent).toBeDefined();
done();
};
req.open('get', '/', true);
req.send();
testZone.run(function() {
var req = new XMLHttpRequest();
var firstCall = true;
req.onreadystatechange = function () {
// Make sure that the callback will only be called once
req.onreadystatechange = null;
assertInChildOf(testZone);
done();
};
req.open('get', '/', true);
req.send();
});
});

it('should work with onprogress', function (done) {
var req = new XMLHttpRequest();
req.onprogress = function () {
// Make sure that the callback will only be called once
req.onprogress = null;
expect(window.zone.parent).toBeDefined();
done();
};
req.open('get', '/', true);
req.send();
testZone.run(function() {
var req = new XMLHttpRequest();
req.onprogress = function () {
// Make sure that the callback will only be called once
req.onprogress = null;
assertInChildOf(testZone);
done();
};
req.open('get', '/', true);
req.send();
});
});

it('should preserve other setters', function () {
Expand Down
123 changes: 68 additions & 55 deletions test/patch/registerElement.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ describe('document.registerElement', ifEnvSupports(registerElement, function ()

var callbacks = {};

var testZone = window.zone.fork();

var customElements = callbackNames.map(function (callbackName) {
var fullCallbackName = callbackName + 'Callback';
var proto = Object.create(HTMLElement.prototype);
Expand All @@ -33,85 +35,96 @@ describe('document.registerElement', ifEnvSupports(registerElement, function ()
});
});


it('should work with createdCallback', function (done) {
callbacks.created = function () {
expect(window.zone.parent).toBeDefined();
done();
};
var elt = document.createElement('x-created');
testZone.run(function() {
callbacks.created = function () {
assertInChildOf(testZone);
done();
};
var elt = document.createElement('x-created');
});
});


it('should work with attachedCallback', function (done) {
callbacks.attached = function () {
expect(window.zone.parent).toBeDefined();
done();
};
var elt = document.createElement('x-attached');
document.body.appendChild(elt);
document.body.removeChild(elt);
testZone.run(function() {
callbacks.attached = function () {
assertInChildOf(testZone);
done();
};
var elt = document.createElement('x-attached');
document.body.appendChild(elt);
document.body.removeChild(elt);
});
});


it('should work with detachedCallback', function (done) {
callbacks.detached = function () {
expect(window.zone.parent).toBeDefined();
done();
};
var elt = document.createElement('x-detached');
document.body.appendChild(elt);
document.body.removeChild(elt);
testZone.run(function() {
callbacks.detached = function () {
assertInChildOf(testZone);
done();
};
var elt = document.createElement('x-detached');
document.body.appendChild(elt);
document.body.removeChild(elt);
});
});


it('should work with attributeChanged', function (done) {
callbacks.attributeChanged = function () {
expect(window.zone.parent).toBeDefined();
done();
};
var elt = document.createElement('x-attributechanged');
elt.id = 'bar';
testZone.run(function() {
callbacks.attributeChanged = function () {
assertInChildOf(testZone);
done();
};
var elt = document.createElement('x-attributechanged');
elt.id = 'bar';
});
});


it('should work with non-writable, non-configurable prototypes created with defineProperty', function (done) {
var proto = Object.create(HTMLElement.prototype);
Object.defineProperty(proto, 'createdCallback', {
writeable: false,
configurable: false,
value: checkZone
});
document.registerElement('x-prop-desc', {
prototype: proto
testZone.run(function() {
var proto = Object.create(HTMLElement.prototype);
Object.defineProperty(proto, 'createdCallback', {
writeable: false,
configurable: false,
value: checkZone
});
document.registerElement('x-prop-desc', {
prototype: proto
});
var elt = document.createElement('x-prop-desc');

function checkZone() {
assertInChildOf(testZone);
done();
}
});
var elt = document.createElement('x-prop-desc');

function checkZone() {
expect(window.zone.parent).toBeDefined();
done();
}
});


it('should work with non-writable, non-configurable prototypes created with defineProperties', function (done) {
var proto = Object.create(HTMLElement.prototype);
Object.defineProperties(proto, {
createdCallback: {
writeable: false,
configurable: false,
value: checkZone
testZone.run(function() {
var proto = Object.create(HTMLElement.prototype);
Object.defineProperties(proto, {
createdCallback: {
writeable: false,
configurable: false,
value: checkZone
}
});
document.registerElement('x-props-desc', {
prototype: proto
});
var elt = document.createElement('x-props-desc');

function checkZone() {
assertInChildOf(testZone);
done();
}
});
document.registerElement('x-props-desc', {
prototype: proto
});
var elt = document.createElement('x-props-desc');

function checkZone() {
expect(window.zone.parent).toBeDefined();
done();
}
});

}));
Loading

0 comments on commit d85d2cf

Please sign in to comment.