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

Commit d85d2cf

Browse files
committed
fix(test): fix an ineffective assertion
`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.
1 parent c465402 commit d85d2cf

File tree

7 files changed

+225
-167
lines changed

7 files changed

+225
-167
lines changed

test/patch/HTMLImports.spec.js

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,47 @@ function supportsImports() {
66
supportsImports.message = 'HTML Imports';
77

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

1011
it('should work with addEventListener', function (done) {
11-
var link = document.createElement('link');
12-
link.rel = 'import';
13-
link.href = 'someUrl';
14-
link.addEventListener('error', function () {
15-
expect(window.zone.parent).toBeDefined();
16-
document.head.removeChild(link);
17-
done();
12+
testZone.run(function() {
13+
var link = document.createElement('link');
14+
link.rel = 'import';
15+
link.href = 'someUrl';
16+
link.addEventListener('error', function () {
17+
assertInChildOf(testZone);
18+
document.head.removeChild(link);
19+
done();
20+
});
21+
document.head.appendChild(link);
1822
});
19-
document.head.appendChild(link);
2023
});
2124

2225
it('should work with onerror', function (done) {
23-
var link = document.createElement('link');
24-
link.rel = 'import';
25-
link.href = 'anotherUrl';
26-
link.onerror = function () {
27-
expect(window.zone.parent).toBeDefined();
28-
document.head.removeChild(link);
29-
done();
30-
};
31-
document.head.appendChild(link);
26+
testZone.run(function() {
27+
var link = document.createElement('link');
28+
link.rel = 'import';
29+
link.href = 'anotherUrl';
30+
link.onerror = function () {
31+
assertInChildOf(testZone);
32+
document.head.removeChild(link);
33+
done();
34+
};
35+
document.head.appendChild(link);
36+
});
3237
});
3338

3439
it('should work with onload', function (done) {
35-
var link = document.createElement('link');
36-
link.rel = 'import';
37-
link.href = '/base/test/assets/import.html';
38-
link.onload = function () {
39-
expect(window.zone.parent).toBeDefined();
40-
document.head.removeChild(link);
41-
done();
42-
};
43-
document.head.appendChild(link);
40+
testZone.run(function() {
41+
var link = document.createElement('link');
42+
link.rel = 'import';
43+
link.href = '/base/test/assets/import.html';
44+
link.onload = function () {
45+
assertInChildOf(testZone);
46+
document.head.removeChild(link);
47+
done();
48+
};
49+
document.head.appendChild(link);
50+
});
4451
});
4552
}));

test/patch/MutationObserver.spec.js

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,25 @@
22

33
describe('MutationObserver', ifEnvSupports('MutationObserver', function () {
44
var elt;
5+
var testZone = window.zone.fork();
56

67
beforeEach(function () {
78
elt = document.createElement('div');
89
});
910

1011
it('should run observers within the zone', function (done) {
11-
var ob = new MutationObserver(function () {
12-
expect(window.zone.parent).toBeDefined();
13-
done();
14-
});
12+
testZone.run(function() {
13+
var ob = new MutationObserver(function () {
14+
assertInChildOf(testZone);
15+
done();
16+
});
1517

16-
ob.observe(elt, {
17-
childList: true
18-
});
18+
ob.observe(elt, {
19+
childList: true
20+
});
1921

20-
elt.innerHTML = '<p>hey</p>';
22+
elt.innerHTML = '<p>hey</p>';
23+
});
2124
});
2225

2326
it('should dequeue upon disconnect', function () {
@@ -77,18 +80,22 @@ describe('MutationObserver', ifEnvSupports('MutationObserver', function () {
7780
}));
7881

7982
describe('WebKitMutationObserver', ifEnvSupports('WebKitMutationObserver', function () {
83+
var testZone = window.zone.fork();
84+
8085
it('should run observers within the zone', function (done) {
81-
var elt = document.createElement('div');
86+
testZone.run(function() {
87+
var elt = document.createElement('div');
8288

83-
var ob = new WebKitMutationObserver(function () {
84-
expect(window.zone.parent).toBeDefined();
85-
done();
86-
});
89+
var ob = new WebKitMutationObserver(function () {
90+
assertInChildOf(testZone);
91+
done();
92+
});
8793

88-
ob.observe(elt, {
89-
childList: true
90-
});
94+
ob.observe(elt, {
95+
childList: true
96+
});
9197

92-
elt.innerHTML = '<p>hey</p>';
98+
elt.innerHTML = '<p>hey</p>';
99+
});
93100
});
94101
}));

test/patch/Promise.spec.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
'use strict';
22

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

56
it('should work with .then', function (done) {
6-
new Promise(function (resolve) {
7-
resolve();
8-
}).then(function () {
9-
expect(window.zone.parent).toBeDefined();
10-
done();
7+
testZone.run(function() {
8+
new Promise(function (resolve) {
9+
resolve();
10+
}).then(function () {
11+
assertInChildOf(testZone);
12+
done();
13+
});
1114
});
1215
});
1316

1417
it('should work with .catch', function (done) {
15-
new Promise(function (resolve, reject) {
16-
reject();
17-
}).catch(function () {
18-
expect(window.zone.parent).toBeDefined();
19-
done();
18+
testZone.run(function() {
19+
new Promise(function (resolve, reject) {
20+
reject();
21+
}).catch(function () {
22+
assertInChildOf(testZone);
23+
done();
24+
});
2025
});
2126
});
2227

test/patch/XMLHttpRequest.spec.js

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
11
'use strict';
22

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

56
it('should work with onreadystatechange', function (done) {
6-
var req = new XMLHttpRequest();
7-
var firstCall = true;
8-
req.onreadystatechange = function () {
9-
// Make sure that the callback will only be called once
10-
req.onreadystatechange = null;
11-
expect(window.zone.parent).toBeDefined();
12-
done();
13-
};
14-
req.open('get', '/', true);
15-
req.send();
7+
testZone.run(function() {
8+
var req = new XMLHttpRequest();
9+
var firstCall = true;
10+
req.onreadystatechange = function () {
11+
// Make sure that the callback will only be called once
12+
req.onreadystatechange = null;
13+
assertInChildOf(testZone);
14+
done();
15+
};
16+
req.open('get', '/', true);
17+
req.send();
18+
});
1619
});
1720

1821
it('should work with onprogress', function (done) {
19-
var req = new XMLHttpRequest();
20-
req.onprogress = function () {
21-
// Make sure that the callback will only be called once
22-
req.onprogress = null;
23-
expect(window.zone.parent).toBeDefined();
24-
done();
25-
};
26-
req.open('get', '/', true);
27-
req.send();
22+
testZone.run(function() {
23+
var req = new XMLHttpRequest();
24+
req.onprogress = function () {
25+
// Make sure that the callback will only be called once
26+
req.onprogress = null;
27+
assertInChildOf(testZone);
28+
done();
29+
};
30+
req.open('get', '/', true);
31+
req.send();
32+
});
2833
});
2934

3035
it('should preserve other setters', function () {

test/patch/registerElement.spec.js

Lines changed: 68 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ describe('document.registerElement', ifEnvSupports(registerElement, function ()
2222

2323
var callbacks = {};
2424

25+
var testZone = window.zone.fork();
26+
2527
var customElements = callbackNames.map(function (callbackName) {
2628
var fullCallbackName = callbackName + 'Callback';
2729
var proto = Object.create(HTMLElement.prototype);
@@ -33,85 +35,96 @@ describe('document.registerElement', ifEnvSupports(registerElement, function ()
3335
});
3436
});
3537

36-
3738
it('should work with createdCallback', function (done) {
38-
callbacks.created = function () {
39-
expect(window.zone.parent).toBeDefined();
40-
done();
41-
};
42-
var elt = document.createElement('x-created');
39+
testZone.run(function() {
40+
callbacks.created = function () {
41+
assertInChildOf(testZone);
42+
done();
43+
};
44+
var elt = document.createElement('x-created');
45+
});
4346
});
4447

4548

4649
it('should work with attachedCallback', function (done) {
47-
callbacks.attached = function () {
48-
expect(window.zone.parent).toBeDefined();
49-
done();
50-
};
51-
var elt = document.createElement('x-attached');
52-
document.body.appendChild(elt);
53-
document.body.removeChild(elt);
50+
testZone.run(function() {
51+
callbacks.attached = function () {
52+
assertInChildOf(testZone);
53+
done();
54+
};
55+
var elt = document.createElement('x-attached');
56+
document.body.appendChild(elt);
57+
document.body.removeChild(elt);
58+
});
5459
});
5560

5661

5762
it('should work with detachedCallback', function (done) {
58-
callbacks.detached = function () {
59-
expect(window.zone.parent).toBeDefined();
60-
done();
61-
};
62-
var elt = document.createElement('x-detached');
63-
document.body.appendChild(elt);
64-
document.body.removeChild(elt);
63+
testZone.run(function() {
64+
callbacks.detached = function () {
65+
assertInChildOf(testZone);
66+
done();
67+
};
68+
var elt = document.createElement('x-detached');
69+
document.body.appendChild(elt);
70+
document.body.removeChild(elt);
71+
});
6572
});
6673

6774

6875
it('should work with attributeChanged', function (done) {
69-
callbacks.attributeChanged = function () {
70-
expect(window.zone.parent).toBeDefined();
71-
done();
72-
};
73-
var elt = document.createElement('x-attributechanged');
74-
elt.id = 'bar';
76+
testZone.run(function() {
77+
callbacks.attributeChanged = function () {
78+
assertInChildOf(testZone);
79+
done();
80+
};
81+
var elt = document.createElement('x-attributechanged');
82+
elt.id = 'bar';
83+
});
7584
});
7685

7786

7887
it('should work with non-writable, non-configurable prototypes created with defineProperty', function (done) {
79-
var proto = Object.create(HTMLElement.prototype);
80-
Object.defineProperty(proto, 'createdCallback', {
81-
writeable: false,
82-
configurable: false,
83-
value: checkZone
84-
});
85-
document.registerElement('x-prop-desc', {
86-
prototype: proto
88+
testZone.run(function() {
89+
var proto = Object.create(HTMLElement.prototype);
90+
Object.defineProperty(proto, 'createdCallback', {
91+
writeable: false,
92+
configurable: false,
93+
value: checkZone
94+
});
95+
document.registerElement('x-prop-desc', {
96+
prototype: proto
97+
});
98+
var elt = document.createElement('x-prop-desc');
99+
100+
function checkZone() {
101+
assertInChildOf(testZone);
102+
done();
103+
}
87104
});
88-
var elt = document.createElement('x-prop-desc');
89-
90-
function checkZone() {
91-
expect(window.zone.parent).toBeDefined();
92-
done();
93-
}
94105
});
95106

96107

97108
it('should work with non-writable, non-configurable prototypes created with defineProperties', function (done) {
98-
var proto = Object.create(HTMLElement.prototype);
99-
Object.defineProperties(proto, {
100-
createdCallback: {
101-
writeable: false,
102-
configurable: false,
103-
value: checkZone
109+
testZone.run(function() {
110+
var proto = Object.create(HTMLElement.prototype);
111+
Object.defineProperties(proto, {
112+
createdCallback: {
113+
writeable: false,
114+
configurable: false,
115+
value: checkZone
116+
}
117+
});
118+
document.registerElement('x-props-desc', {
119+
prototype: proto
120+
});
121+
var elt = document.createElement('x-props-desc');
122+
123+
function checkZone() {
124+
assertInChildOf(testZone);
125+
done();
104126
}
105127
});
106-
document.registerElement('x-props-desc', {
107-
prototype: proto
108-
});
109-
var elt = document.createElement('x-props-desc');
110-
111-
function checkZone() {
112-
expect(window.zone.parent).toBeDefined();
113-
done();
114-
}
115128
});
116129

117130
}));

0 commit comments

Comments
 (0)