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

Commit 8a0dace

Browse files
gkalpakpetebacondarwin
authored andcommitted
test($anchorScroll): fix a test and cleanup dead code
1 parent 1d83f8d commit 8a0dace

File tree

1 file changed

+23
-35
lines changed

1 file changed

+23
-35
lines changed

test/ng/anchorScrollSpec.js

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
describe('$anchorScroll', function() {
44

55
var elmSpy;
6-
var docSpies;
6+
var jqLiteSpies;
77
var windowSpies;
88

99
function addElements() {
@@ -54,14 +54,11 @@ describe('$anchorScroll', function() {
5454

5555
function createMockDocument(initialReadyState) {
5656
var mockedDoc = {};
57-
docSpies = {};
5857

5958
var propsToPassThrough = ['body', 'documentElement'];
6059
var methodsToPassThrough = [
6160
'getElementById',
62-
'getElementsByName',
63-
'addEventListener',
64-
'removeEventListener'
61+
'getElementsByName'
6562
];
6663

6764
var document_ = document;
@@ -71,19 +68,9 @@ describe('$anchorScroll', function() {
7168
});
7269
methodsToPassThrough.forEach(function(method) {
7370
mockedDoc[method] = document_[method].bind(document_);
74-
docSpies[method] = spyOn(mockedDoc, method).andCallThrough();
7571
});
7672

7773
mockedDoc.readyState = initialReadyState || 'complete';
78-
mockedDoc.dispatchFakeReadyStateChangeEvent = function() {
79-
var evt = document_.createEvent('Event');
80-
evt.initEvent('readystatechange', false, false);
81-
document_.dispatchEvent(evt);
82-
};
83-
mockedDoc.updateReadyState = function(newState) {
84-
this.readyState = newState;
85-
this.dispatchFakeReadyStateChangeEvent();
86-
};
8774

8875
return mockedDoc;
8976
}
@@ -103,16 +90,13 @@ describe('$anchorScroll', function() {
10390
return getComputedStyle(elem);
10491
},
10592
addEventListener: function(eventType, callback, unsupported) {
106-
window.addEventListener(eventType, callback, unsupported);
93+
addEventListener(eventType, callback, unsupported);
10794
},
10895
removeEventListener: function(eventType, callback, unsupported) {
109-
window.removeEventListener(eventType, callback, unsupported);
96+
removeEventListener(eventType, callback, unsupported);
11097
}
11198
};
11299

113-
windowSpies.addEventListener = spyOn(mockedWin, 'addEventListener').andCallThrough();
114-
windowSpies.removeEventListener = spyOn(mockedWin, 'removeEventListener').andCallThrough();
115-
116100
$provide.value('$window', mockedWin);
117101
});
118102
};
@@ -158,7 +142,7 @@ describe('$anchorScroll', function() {
158142

159143
return function($window) {
160144
forEach(elmSpy, resetSpy);
161-
forEach(docSpies, resetSpy);
145+
forEach(jqLiteSpies, resetSpy);
162146
forEach(windowSpies, resetSpy);
163147
};
164148
}
@@ -235,16 +219,18 @@ describe('$anchorScroll', function() {
235219
};
236220
}
237221

238-
239222
function spyOnJQLiteOnOff() {
223+
jqLiteSpies = {};
224+
240225
return function() {
241-
spyOn(jqLite.prototype,'on').andCallThrough();
242-
spyOn(jqLite.prototype,'off').andCallThrough();
226+
jqLiteSpies.on = spyOn(jqLite.prototype,'on').andCallThrough();
227+
jqLiteSpies.off = spyOn(jqLite.prototype,'off').andCallThrough();
243228
};
244229
}
245230

246231
function unspyOnJQLiteOnOff() {
247232
return function() {
233+
jqLiteSpies = {};
248234
jqLite.prototype.on = jqLite.prototype.on.originalValue;
249235
jqLite.prototype.off = jqLite.prototype.off.originalValue;
250236
};
@@ -266,8 +252,8 @@ describe('$anchorScroll', function() {
266252
}
267253
});
268254

269-
expect(onCalls).toEqual(callCount);
270-
expect(offCalls).toEqual(callCount);
255+
expect(onCalls).toBe(callCount);
256+
expect(offCalls).toBe(callCount);
271257
};
272258
}
273259

@@ -320,17 +306,19 @@ describe('$anchorScroll', function() {
320306
});
321307

322308

323-
it('should scroll immediately if already `readyState === "complete"`', inject(
324-
addElements('id=some1'),
309+
it('should scroll immediately if already `readyState === "complete"`', function() {
310+
module(spyOnJQLiteOnOff());
311+
inject(
312+
addElements('id=some1'),
325313

326-
triggerLoadEvent(),
327-
changeHashTo('some1'),
314+
triggerLoadEvent(),
315+
changeHashTo('some1'),
328316

329-
expectScrollingTo('id=some1'),
330-
function() {
331-
expect(windowSpies.addEventListener.callCount).toBe(0);
332-
expect(windowSpies.removeEventListener.callCount).toBe(0);
333-
}));
317+
expectScrollingTo('id=some1'),
318+
expectJQLiteOnOffCallsToEqual(0),
319+
unspyOnJQLiteOnOff()
320+
);
321+
});
334322
});
335323

336324

0 commit comments

Comments
 (0)