Skip to content

Commit

Permalink
restore and refactor DOMReady specs
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioCrisostomo committed Jan 19, 2015
1 parent e1a153c commit 9adadae
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 25 deletions.
97 changes: 73 additions & 24 deletions Specs/Utilities/DOMReady.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,79 @@ provides: ~
...
*/

/* todo
document.addListener = function(type, fn){
if (this.addEventListener) this.addEventListener(type, fn, false);
else this.attachEvent('on' + type, fn);
return this;
};
document.removeListener = function(type, fn){
if (this.removeEventListener) this.removeEventListener(type, fn, false);
else this.detachEvent('on' + type, fn);
return this;
};
window.fireEvent =
document.fireEvent = function(type){
if (type == 'domready')
for (var i = 0; i < domreadyCallbacks.length; ++i){
describe("DOMReady", function(){

var win, frame, cb, ready;

function checkStatus(){
ready = win && win.callbackFired;
if (ready) cb();
return ready;
}

function newFrame(url){
var iframe = new IFrame({
src: 'base/Tests/DOMReady/' + url
});
document.getElement('body').adopt(iframe);
return iframe;
}
domreadyCallbacks[i]();
};

window.addEvent = function(){};
beforeEach(function(){
cb = jasmine.createSpy('DOMReady!');
});

var Element = this.Element || {};
Element.Events = {};
*/
afterEach(function(){
frame.destroy();
win = cb = frame = ready = null;
});

it('should fire DOMReady when the DOM is ready', function(){
frame = newFrame('DOMReady.head.html');
frame.addEvent('load', function(){
win = frame.contentWindow;
});
waitsFor(function(){
return checkStatus();
}, "the iframe to load", 1500);
runs(function(){
expect(cb).toHaveBeenCalled();
});
});

it('should fire DOMReady when a new `addEvent("domready"` is added', function(){
frame = newFrame('DOMReady.onAdd.html');
frame.addEvent('load', function(){
win = frame.contentWindow;
win.addEvent('domready', win.callback);
});
waitsFor(function(){
return checkStatus();
}, "the iframe to load", 1500);
runs(function(){
expect(cb).toHaveBeenCalled();
});
});

it('should fire when MooTools was loaded into a already-ready page', function(){
frame = newFrame('DOMReady.delayed.html');
var ready;
frame.addEvent('load', function(){
win = frame.contentWindow;
expect(win.MooTools).toBeFalsy(); // because MooTools should not be loaded yet
var i = setInterval(function(){
if (win.addEvent && win.callback){
win.addEvent('domready', win.callback);
if (ready) clearInterval(i);
}
}, 50);
});
waitsFor(function(){
return checkStatus();
}, "the iframe to load and MooTools to be deployed", 6000);
runs(function(){
expect(cb).toHaveBeenCalled();
});
});

});
29 changes: 29 additions & 0 deletions Tests/DOMReady/DOMReady.delayed.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>Add MooTools when page is already ready</title>
</head>
<body>

<script>
function callback(){
window.callbackFired = true;
}

function addScript(path){
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = path;
document.getElementsByTagName('head')[0].appendChild(script);
}

window.callbackFired = false;
window.onload = function(){
addScript('/base/mootools-all.js');
addScript('/base/mootools-nocompat.js');
}
</script>
</body>
</html>

22 changes: 22 additions & 0 deletions Tests/DOMReady/DOMReady.head.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>Normal DOMReady scenario</title>

<script src="/base/mootools-all.js" type="text/javascript"></script>
<script src="/base/mootools-nocompat.js" type="text/javascript"></script>
</head>
<body>

<script>
function callback(){
window.callbackFired = true;
}

window.callbackFired = false;
window.addEvent('domready', callback);
</script>
</body>
</html>

21 changes: 21 additions & 0 deletions Tests/DOMReady/DOMReady.onAdd.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>DOMReady added when page is already loaded</title>

<script src="/base/mootools-all.js" type="text/javascript"></script>
<script src="/base/mootools-nocompat.js" type="text/javascript"></script>
</head>
<body>

<script>
function callback(){
window.callbackFired = true;
}

window.callbackFired = false;
</script>
</body>
</html>

7 changes: 6 additions & 1 deletion Tests/gruntfile-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ var karmaOptions = {
captureTimeout: 60000 * 2,
singleRun: true,
frameworks: ['jasmine', 'sinon'],
files: ['Tests/Utilities/*.js', 'mootools-*.js'],
files: [
'Tests/Utilities/*.js',
'mootools-*.js',
{pattern: 'Source/**/*.*', included: false, served: true},
{pattern: 'Tests/DOMReady/*.*', included: false, served: true}
],
sauceLabs: {
username: process.env.SAUCE_USERNAME,
accessKey: process.env.SAUCE_ACCESS_KEY,
Expand Down

0 comments on commit 9adadae

Please sign in to comment.