-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync_test.js
135 lines (130 loc) · 5 KB
/
async_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
describe('Asynchronous controller execution detection', function(){
var $controller, $rootScope;
beforeEach(module('controllerTrainer'));
beforeEach(module('$timeout'));
beforeEach(inject(function(_$controller_, _$rootScope_) {
$controller = _$controller_;
$rootScope = _$rootScope_;
}));
beforeEach(function() {
patchServices.listener = function() {
console.log('For tests, just print this error.');
}
});
it('should handle asynchronous DOM manipulations', inject(function($rootScope) {
var timeoutCompleted = false;
runs(function() {
spyOn(patchServices, 'listener');
expect(patchServices.listener).not.toHaveBeenCalled();
var controllerMock = function($timeout) {
$timeout(function() {
var element = document.createElement('a');
element.innerHTML = 'testValue';
timeoutCompleted = true;
}, 0);
};
try{
var ctrl = $controller(controllerMock);
}
catch(e){
console.log('Caught an error making controller: ' + e);
}
$rootScope.$apply();
});
waitsFor(function() {
return timeoutCompleted;
}, 'controller execution', 100);
runs(function() {
expect(patchServices.listener).toHaveBeenCalled();
});
}));
it('should unpatch elements after asynchronous execution', inject(function($rootScope) {
var timeoutCompleted = false;
var objectProperties = Object.getOwnPropertyNames(Element.prototype);
var objectPropertiesNode = Object.getOwnPropertyNames(Node.prototype);
var objectPropertiesEventTarget = Object.getOwnPropertyNames(EventTarget.prototype);
var objectPropertiesDocument = Object.getOwnPropertyNames(Document.prototype);
var testProperty = objectProperties[0];
var originalFunction = Element.prototype[testProperty];
var originalFunctionNode = Node.prototype[testProperty];
var originalFunctionEventTarget = EventTarget.prototype[testProperty];
var originalFunctionDocument = Document.prototype[testProperty];
expect(Element.prototype[testProperty]).toBe(originalFunction);
var elem = document.createElement('div');
elem.setAttribute('id', 'testElement');
document.body.appendChild(elem);
runs(function() {
spyOn(patchServices, 'listener');
var controllerMock = function($timeout) {
$timeout(function() {
var element = document.getElementById('testElement');
element.getAttribute('id');
timeoutCompleted = true;
}, 0);
};
try{
var element = document.getElementById('testElement');
element.getAttribute('id');
var ctrl = $controller(controllerMock);
}
catch(e){
console.log('Caught an error making controller: ' + e);
}
var element = document.getElementById('testElement');
element.getAttribute('id');
});
waitsFor(function() {
elem = document.getElementById('testElement');
elem.getAttribute('id');
return timeoutCompleted;
}, 'controller execution', 100);
runs(function() {
elem.getAttribute('id');
expect(Element.prototype[testProperty]).toBe(originalFunction);
expect(Node.prototype[testProperty]).toBe(originalFunctionNode);
expect(EventTarget.prototype[testProperty]).toBe(originalFunctionEventTarget);
expect(Document.prototype[testProperty]).toBe(originalFunctionDocument);
expect(patchServices.listener.callCount).toBe(2);
});
}));
it('should use zone.js to enter and exit at the right times', inject(function($rootScope) {
var timeoutCompleted = false;
spyOn(patchServices, 'listener');
runs(function() {
var testZone = zone.fork(Zone.longStackTraceZone).fork({
beforeTask: function() {
console.log('before');
patchServices.addManipulationListener();
},
afterTask: function() {
console.log('after');
patchServices.removeManipulationListener();
}
});
var ctrlFunction = function() {
var controllerMock = function($timeout) {
$timeout(function() {
document.createElement('test');
timeoutCompleted = true;
}, 0);
};
document.createElement('test1');
var myScope = $rootScope.$new();
var ctrl = $controller(controllerMock, {$scope: myScope});
ctrl.testAttribute = 'testAttribute';
console.log(ctrl.testAttribute);
document.createElement('test2');
};
testZone.bind(ctrlFunction);
document.createElement('test3');
ctrlFunction();
});
waitsFor(function() {
document.createElement('test');
return timeoutCompleted;
}, 'controller execution', 100);
runs(function() {
expect(patchServices.listener.callCount).toBe(1);
});
}));
});