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

Commit

Permalink
test: strengthen assertions by testing *direct* child where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb committed May 10, 2015
1 parent bf77917 commit 9918957
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 101 deletions.
16 changes: 10 additions & 6 deletions test/microtasks.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ describe('Microtasks', function () {
it('should executed Promise callback in the zone where they are scheduled', function(done) {
var resolvedPromise = Promise.resolve(null);

zone.fork({_name: 'foo'}).run(function() {
var testZone = window.zone.fork();

testZone.run(function() {
resolvedPromise.then(function() {
expect(zone._name).toBe('foo');
expect(window.zone).toBeDirectChildOf(testZone);
done();
});
});
Expand All @@ -65,16 +67,18 @@ describe('Microtasks', function () {
var resolve;
var promise = new Promise(function (rs) {
resolve = rs;
})
});

var testZone = window.zone.fork();

zone.fork({_name: 'foo'}).run(function() {
testZone.run(function() {
promise.then(function() {
expect(zone._name).toBe('foo');
expect(window.zone).toBeDirectChildOf(testZone);
done();
});
});

zone.fork({_name: 'bar'}).run(function() {
window.zone.fork().run(function() {
resolve(null);
});
});
Expand Down
6 changes: 3 additions & 3 deletions test/patch/HTMLImports.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('HTML Imports', ifEnvSupports(supportsImports, function () {
link.rel = 'import';
link.href = 'someUrl';
link.addEventListener('error', function () {
assertInChildOf(testZone);
expect(window.zone).toBeDirectChildOf(testZone);
document.head.removeChild(link);
done();
});
Expand All @@ -42,7 +42,7 @@ describe('HTML Imports', ifEnvSupports(supportsImports, function () {
link.rel = 'import';
link.href = 'anotherUrl';
link.onerror = function () {
assertInChildOf(testZone);
expect(window.zone).toBeDirectChildOf(testZone);
document.head.removeChild(link);
done();
};
Expand All @@ -59,7 +59,7 @@ describe('HTML Imports', ifEnvSupports(supportsImports, function () {
link.rel = 'import';
link.href = '/base/test/assets/import.html';
link.onload = function () {
assertInChildOf(testZone);
expect(window.zone).toBeDirectChildOf(testZone);
document.head.removeChild(link);
done();
};
Expand Down
4 changes: 2 additions & 2 deletions test/patch/MutationObserver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('MutationObserver', ifEnvSupports('MutationObserver', function () {

testZone.run(function() {
ob = new MutationObserver(function () {
assertInChildOf(testZone);
expect(window.zone).toBeDirectChildOf(testZone);
done();
});

Expand Down Expand Up @@ -91,7 +91,7 @@ describe('WebKitMutationObserver', ifEnvSupports('WebKitMutationObserver', funct
elt = document.createElement('div');

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

Expand Down
4 changes: 2 additions & 2 deletions test/patch/Promise.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('Promise', ifEnvSupports('Promise', function () {
new Promise(function (resolveFn) {
resolve = resolveFn;
}).then(function () {
assertInChildOf(testZone);
expect(window.zone).toBeDirectChildOf(testZone);
done();
});
});
Expand All @@ -25,7 +25,7 @@ describe('Promise', ifEnvSupports('Promise', function () {
new Promise(function (resolveFn, rejectFn) {
reject = rejectFn;
}).catch(function () {
assertInChildOf(testZone);
expect(window.zone).toBeDirectChildOf(testZone);
done();
});
});
Expand Down
4 changes: 2 additions & 2 deletions test/patch/WebSocket.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('WebSocket', function () {
it('should work with addEventListener', function (done) {
testZone.run(function() {
socket.addEventListener('message', function (event) {
assertInChildOf(testZone);
expect(window.zone).toBeDirectChildOf(testZone);
expect(event.data).toBe('hi');
done();
});
Expand Down Expand Up @@ -60,7 +60,7 @@ describe('WebSocket', function () {
it('should work with onmessage', function (done) {
testZone.run(function() {
socket.onmessage = function (contents) {
assertInChildOf(testZone);
expect(window.zone).toBeDirectChildOf(testZone);
expect(contents.data).toBe('hi');
done();
};
Expand Down
4 changes: 2 additions & 2 deletions test/patch/XMLHttpRequest.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('XMLHttpRequest', function () {
req.onreadystatechange = function () {
// Make sure that the callback will only be called once
req.onreadystatechange = null;
assertInChildOf(testZone);
expect(window.zone).toBeDirectChildOf(testZone);
done();
};
req.open('get', '/', true);
Expand All @@ -29,7 +29,7 @@ describe('XMLHttpRequest', function () {
req.onprogress = function () {
// Make sure that the callback will only be called once
req.onprogress = null;
assertInChildOf(testZone);
expect(window.zone).toBeDirectChildOf(testZone);
done();
};
req.open('get', '/', true);
Expand Down
10 changes: 2 additions & 8 deletions test/patch/element.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,13 @@ describe('element', function () {
});

it('should work with addEventListener', function () {
var childTestZone;

testZone.run(function() {
button.addEventListener('click', function () {
childTestZone = window.zone;
expect(window.zone).toBeDirectChildOf(testZone);
});
});

button.click();
expect(childTestZone.parent).toBe(testZone);
});

it('should respect removeEventListener', function () {
Expand Down Expand Up @@ -61,16 +58,13 @@ describe('element', function () {

ifEnvSupports(supportsOnClick, function() {
it('should spawn new child zones', function () {
var childTestZone;

testZone.run(function() {
button.onclick = function () {
childTestZone = window.zone;
expect(window.zoneA).toBeDirectChildOf(testZone);
};
});

button.click();
expect(childTestZone.parent).toBe(testZone);
});
});

Expand Down
12 changes: 6 additions & 6 deletions test/patch/registerElement.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('document.registerElement', ifEnvSupports(registerElement, function ()

it('should work with createdCallback', function (done) {
callbacks.created = function () {
assertInChildOf(testZone);
expect(window.zone).toBeDirectChildOf(testZone);
done();
};

Expand All @@ -51,7 +51,7 @@ describe('document.registerElement', ifEnvSupports(registerElement, function ()

it('should work with attachedCallback', function (done) {
callbacks.attached = function () {
assertInChildOf(testZone);
expect(window.zone).toBeDirectChildOf(testZone);
done();
};

Expand All @@ -63,7 +63,7 @@ describe('document.registerElement', ifEnvSupports(registerElement, function ()

it('should work with detachedCallback', function (done) {
callbacks.detached = function () {
assertInChildOf(testZone);
expect(window.zone).toBeDirectChildOf(testZone);
done();
};

Expand All @@ -75,7 +75,7 @@ describe('document.registerElement', ifEnvSupports(registerElement, function ()

it('should work with attributeChanged', function (done) {
callbacks.attributeChanged = function () {
assertInChildOf(testZone);
expect(window.zone).toBeDirectChildOf(testZone);
done();
};

Expand All @@ -97,7 +97,7 @@ describe('document.registerElement', ifEnvSupports(registerElement, function ()
document.registerElement('x-prop-desc', { prototype: proto });

function checkZone() {
assertInChildOf(testZone);
expect(window.zone).toBeDirectChildOf(testZone);
done();
}
});
Expand All @@ -121,7 +121,7 @@ describe('document.registerElement', ifEnvSupports(registerElement, function ()
document.registerElement('x-props-desc', { prototype: proto });

function checkZone() {
assertInChildOf(testZone);
expect(window.zone).toBeDirectChildOf(testZone);
done();
}
});
Expand Down
6 changes: 3 additions & 3 deletions test/patch/requestAnimationFrame.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('requestAnimationFrame', function () {
// assume the patch works if setTimeout works, since they are mechanically
// the same
window.requestAnimationFrame(function () {
assertInChildOf(testZone);
expect(window.zone).toBeDirectChildOf(testZone);
done();
});
});
Expand All @@ -36,7 +36,7 @@ describe('requestAnimationFrame', function () {
// assume the patch works if setTimeout works, since they are mechanically
// the same
window.mozRequestAnimationFrame(function () {
assertInChildOf(testZone);
expect(window.zone).toBeDirectChildOf(testZone);
done();
});
});
Expand All @@ -56,7 +56,7 @@ describe('requestAnimationFrame', function () {
// assume the patch works if setTimeout works, since they are mechanically
// the same
window.webkitRequestAnimationFrame(function () {
assertInChildOf(testZone);
expect(window.zone).toBeDirectChildOf(testZone);
done();
});
});
Expand Down
23 changes: 3 additions & 20 deletions test/patch/setInterval.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,18 @@

describe('setInterval', function () {

beforeEach(function () {
zone.mark = 'root';
});

it('should work with setInterval', function (done) {
var childZone = window.zone.fork({
mark: 'child'
});

expect(zone.mark).toEqual('root');

childZone.run(function() {
expect(zone.mark).toEqual('child');
expect(zone).toEqual(childZone);
var testZone = window.zone.fork();

testZone.run(function() {
var cancelId = window.setInterval(function() {
// creates implied zone in all callbacks.
expect(zone).not.toBe(childZone);
expect(zone.parent).toBe(childZone);
expect(zone.mark).toBe('child'); // proto inherited
expect(window.zone).toBeDirectChildOf(testZone);

clearInterval(cancelId);
done();
}, 10);

expect(zone.mark).toEqual('child');
});

expect(zone.mark).toEqual('root');
});

});
22 changes: 3 additions & 19 deletions test/patch/setTimeout.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,18 @@

describe('setTimeout', function () {

beforeEach(function () {
zone.mark = 'root';
});

it('should work with setTimeout', function (done) {

var childZone = window.zone.fork({
mark: 'child'
});

expect(zone.mark).toEqual('root');
var testZone = window.zone.fork();

childZone.run(function() {
expect(zone.mark).toEqual('child');
expect(zone).toEqual(childZone);
testZone.run(function() {

window.setTimeout(function() {
// creates implied zone in all callbacks.
expect(zone).not.toEqual(childZone);
expect(zone.parent).toEqual(childZone);
expect(zone.mark).toEqual('child'); // proto inherited
expect(window.zone).toBeDirectChildOf(testZone);
done();
}, 0);

expect(zone.mark).toEqual('child');
});

expect(zone.mark).toEqual('root');
});

it('should allow canceling of fns registered with setTimeout', function (done) {
Expand Down
4 changes: 0 additions & 4 deletions test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ function ifEnvSupports(test, block) {
};
};

function assertInChildOf(parentZone) {
expect(window.zone).toBeChildOf(parentZone);
}

var customMatchers = {
// Assert that a zone is a child of an other zone
// usage: `expect(childZone).toBeChildOf(parentZone);`
Expand Down
Loading

0 comments on commit 9918957

Please sign in to comment.