Skip to content

Commit 9adadae

Browse files
restore and refactor DOMReady specs
1 parent e1a153c commit 9adadae

File tree

5 files changed

+151
-25
lines changed

5 files changed

+151
-25
lines changed

Specs/Utilities/DOMReady.js

+73-24
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,79 @@ provides: ~
66
...
77
*/
88

9-
/* todo
10-
document.addListener = function(type, fn){
11-
if (this.addEventListener) this.addEventListener(type, fn, false);
12-
else this.attachEvent('on' + type, fn);
13-
return this;
14-
};
15-
16-
document.removeListener = function(type, fn){
17-
if (this.removeEventListener) this.removeEventListener(type, fn, false);
18-
else this.detachEvent('on' + type, fn);
19-
return this;
20-
};
21-
22-
23-
window.fireEvent =
24-
document.fireEvent = function(type){
25-
if (type == 'domready')
26-
for (var i = 0; i < domreadyCallbacks.length; ++i){
9+
describe("DOMReady", function(){
10+
11+
var win, frame, cb, ready;
12+
13+
function checkStatus(){
14+
ready = win && win.callbackFired;
15+
if (ready) cb();
16+
return ready;
17+
}
18+
19+
function newFrame(url){
20+
var iframe = new IFrame({
21+
src: 'base/Tests/DOMReady/' + url
22+
});
23+
document.getElement('body').adopt(iframe);
24+
return iframe;
2725
}
28-
domreadyCallbacks[i]();
29-
};
3026

31-
window.addEvent = function(){};
27+
beforeEach(function(){
28+
cb = jasmine.createSpy('DOMReady!');
29+
});
3230

33-
var Element = this.Element || {};
34-
Element.Events = {};
35-
*/
31+
afterEach(function(){
32+
frame.destroy();
33+
win = cb = frame = ready = null;
34+
});
35+
36+
it('should fire DOMReady when the DOM is ready', function(){
37+
frame = newFrame('DOMReady.head.html');
38+
frame.addEvent('load', function(){
39+
win = frame.contentWindow;
40+
});
41+
waitsFor(function(){
42+
return checkStatus();
43+
}, "the iframe to load", 1500);
44+
runs(function(){
45+
expect(cb).toHaveBeenCalled();
46+
});
47+
});
48+
49+
it('should fire DOMReady when a new `addEvent("domready"` is added', function(){
50+
frame = newFrame('DOMReady.onAdd.html');
51+
frame.addEvent('load', function(){
52+
win = frame.contentWindow;
53+
win.addEvent('domready', win.callback);
54+
});
55+
waitsFor(function(){
56+
return checkStatus();
57+
}, "the iframe to load", 1500);
58+
runs(function(){
59+
expect(cb).toHaveBeenCalled();
60+
});
61+
});
62+
63+
it('should fire when MooTools was loaded into a already-ready page', function(){
64+
frame = newFrame('DOMReady.delayed.html');
65+
var ready;
66+
frame.addEvent('load', function(){
67+
win = frame.contentWindow;
68+
expect(win.MooTools).toBeFalsy(); // because MooTools should not be loaded yet
69+
var i = setInterval(function(){
70+
if (win.addEvent && win.callback){
71+
win.addEvent('domready', win.callback);
72+
if (ready) clearInterval(i);
73+
}
74+
}, 50);
75+
});
76+
waitsFor(function(){
77+
return checkStatus();
78+
}, "the iframe to load and MooTools to be deployed", 6000);
79+
runs(function(){
80+
expect(cb).toHaveBeenCalled();
81+
});
82+
});
83+
84+
});

Tests/DOMReady/DOMReady.delayed.html

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
5+
<title>Add MooTools when page is already ready</title>
6+
</head>
7+
<body>
8+
9+
<script>
10+
function callback(){
11+
window.callbackFired = true;
12+
}
13+
14+
function addScript(path){
15+
var script = document.createElement('script');
16+
script.type = 'text/javascript';
17+
script.src = path;
18+
document.getElementsByTagName('head')[0].appendChild(script);
19+
}
20+
21+
window.callbackFired = false;
22+
window.onload = function(){
23+
addScript('/base/mootools-all.js');
24+
addScript('/base/mootools-nocompat.js');
25+
}
26+
</script>
27+
</body>
28+
</html>
29+

Tests/DOMReady/DOMReady.head.html

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
5+
<title>Normal DOMReady scenario</title>
6+
7+
<script src="/base/mootools-all.js" type="text/javascript"></script>
8+
<script src="/base/mootools-nocompat.js" type="text/javascript"></script>
9+
</head>
10+
<body>
11+
12+
<script>
13+
function callback(){
14+
window.callbackFired = true;
15+
}
16+
17+
window.callbackFired = false;
18+
window.addEvent('domready', callback);
19+
</script>
20+
</body>
21+
</html>
22+

Tests/DOMReady/DOMReady.onAdd.html

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
5+
<title>DOMReady added when page is already loaded</title>
6+
7+
<script src="/base/mootools-all.js" type="text/javascript"></script>
8+
<script src="/base/mootools-nocompat.js" type="text/javascript"></script>
9+
</head>
10+
<body>
11+
12+
<script>
13+
function callback(){
14+
window.callbackFired = true;
15+
}
16+
17+
window.callbackFired = false;
18+
</script>
19+
</body>
20+
</html>
21+

Tests/gruntfile-options.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@ var karmaOptions = {
6666
captureTimeout: 60000 * 2,
6767
singleRun: true,
6868
frameworks: ['jasmine', 'sinon'],
69-
files: ['Tests/Utilities/*.js', 'mootools-*.js'],
69+
files: [
70+
'Tests/Utilities/*.js',
71+
'mootools-*.js',
72+
{pattern: 'Source/**/*.*', included: false, served: true},
73+
{pattern: 'Tests/DOMReady/*.*', included: false, served: true}
74+
],
7075
sauceLabs: {
7176
username: process.env.SAUCE_USERNAME,
7277
accessKey: process.env.SAUCE_ACCESS_KEY,

0 commit comments

Comments
 (0)