Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 2682 #2693

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
"use strict";
var fs = require('fs');
var path = require('path');
var http = require('http');
var build = (function(){
// travis testing
if (process.env && process.env.BUILD) return process.env.BUILD == 'default' ? 'all' : 'nocompat';
// local testing
else return process.argv[2] == 'default' || process.argv[2] == null ? 'all' : 'nocompat';
})();

module.exports = function(grunt) {
module.exports = function(grunt){

grunt.loadNpmTasks('grunt-contrib-uglify');
require('load-grunt-tasks')(grunt);
Expand Down Expand Up @@ -49,11 +58,14 @@ module.exports = function(grunt) {
}
});

var compatBuild = ['clean:specs', 'packager:all', 'packager:specs'];
var nocompatBuild = ['clean:specs', 'packager:nocompat', 'packager:specs-nocompat'];
var compatBuild = ['clean:specs', 'packager:all', 'packager:specs', 'specsserver'];
var nocompatBuild = ['clean:specs', 'packager:nocompat', 'packager:specs-nocompat', 'specsserver'];
var tasks = options.travis.build == 'default' ? compatBuild : nocompatBuild;
tasks = usePhantom ? tasks.concat('karma:continuous') : tasks.concat('karma:sauceTask');

grunt.registerTask('specsserver', function(){
require('./Tests/httpServer.js')(build);
});
grunt.registerTask('default', compatBuild.concat('karma:continuous')); // local testing - compat build
grunt.registerTask('nocompat', nocompatBuild.concat('karma:continuous')); // local testing - no compat build
grunt.registerTask('default:travis', tasks); // Travis & Sauce Labs
Expand Down
6 changes: 5 additions & 1 deletion Source/Utilities/DOMReady.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ if (document.readyState) checks.push(function(){
return (state == 'loaded' || state == 'complete');
});

if ('onreadystatechange' in document) document.addListener('readystatechange', check);
if ('onreadystatechange' in document){
document.addListener('readystatechange', check);
var state = document.readyState;
if (state == 'loaded' || state == 'complete') domready();
}
else shouldPoll = true;

if (shouldPoll) poll();
Expand Down
114 changes: 90 additions & 24 deletions Specs/Utilities/DOMReady.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,96 @@ 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a fan of abbreviated variable names. code should be readable; let the minimizer worry about brevity. So, callback here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reading a big more I see this is the spec file, so I'm less pedantic about it... can leave it.

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

window.addEvent = function(){};
function newFrame(url){
var iframe = new IFrame({
src: 'specsserver/' + url
});
document.getElement('body').adopt(iframe);
return iframe;
}

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

afterEach(function(){
frame.destroy();
win = cb = frame = ready = null;
});

it('should fire DOMReady, after flushing, when the DOM is ready', function(){
frame = newFrame('foo?assets=flush');
frame.addEvent('load', function(){
win = frame.contentWindow;
expect(win.moments[0]).toEqual('loading');
expect(win.moments[1]).toEqual('loading');
expect(win.moments[2] == 'interactive' || win.moments[2] == 'complete').toBeTruthy();
});

waitsFor(function(){
return checkStatus();
}, "the iframe to load", 8000);
runs(function(){
expect(cb).toHaveBeenCalled();
});
});

it('should fire DOMReady when the DOM is ready', function(){
frame = newFrame('DOMReady/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/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/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();
});
});

});

var Element = this.Element || {};
Element.Events = {};
*/
27 changes: 27 additions & 0 deletions Tests/DOMReady/DOMReady.delayed.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!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('mootoolsPath.js');
}
</script>
</body>
</html>
21 changes: 21 additions & 0 deletions Tests/DOMReady/DOMReady.head.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>Normal DOMReady scenario</title>

<script src="mootoolsPath.js" type="text/javascript"></script>
</head>
<body>

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

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

19 changes: 19 additions & 0 deletions Tests/DOMReady/DOMReady.onAdd.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!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="mootoolsPath.js" type="text/javascript"></script>
</head>
<body>

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

window.callbackFired = false;
</script>
</body>
</html>
45 changes: 45 additions & 0 deletions Tests/assets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"use strict";

// custom behaviours to test
var assets = {

flush: function(req, res, src){

var headString = '' +
'<html>' +
'<head>' +
'<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />' +
'<title>Flushed page scenario</title>' +
'<script src="' + src + '.js" type="text/javascript"></script>' +
'<script>' +
'window.moments = [];' +
'moments.push(document.readyState);' +
'function callback(){' +
'window.callbackFired = true;' +
'moments.push(document.readyState);' +
'}' +
'window.callbackFired = false;' +
'window.addEvent("domready", callback);' +
'</script>' +
'</head>';
var bodyString = '' +
'<body>' +
'<div>body added...</div>' +
'<script>' +
'moments.push(document.readyState);' +
'</script>' +
'</body>';
var endString = '</html>';

res.writeHead(200, {'Content-Type': 'text/html'});
res.write(headString);
setTimeout(function() {
res.write(bodyString);
setTimeout(function() {
res.end(endString);
}, 2000);
}, 2000);
}
}

module.exports = assets;
10 changes: 9 additions & 1 deletion Tests/gruntfile-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,15 @@ 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/**/*.*', included: false, served: true}
],
proxies: {
'/specsserver/': 'http://localhost:9000/'
},
sauceLabs: {
username: process.env.SAUCE_USERNAME,
accessKey: process.env.SAUCE_ACCESS_KEY,
Expand Down
27 changes: 27 additions & 0 deletions Tests/httpServer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use strict";

var fs = require('fs');
var path = require('path');
var http = require('http');
var assets = require('./assets.js');

function getQuery(path){
var match = path.match(/=(\w+)/);
return match ? match[1] : null;
}

module.exports = function(build){
http.createServer(function(req, res){

var src = '/base/mootools-' + build;
var customFunction = getQuery(req.url);

if (customFunction) return assets[customFunction].call(null, req, res, src);
var filePath = path.join(__dirname, req.url);
fs.readFile(filePath, 'utf-8', function (err, content){
if (err) return console.log(err);
content = content.replace('mootoolsPath', src);
res.end(content);
});
}).listen(9000);
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"karma-firefox-launcher": "~0.1.3",
"karma-ie-launcher": "~0.1",
"karma-safari-launcher": "~0.1",
"path": "^0.11.14",
"js-yaml": "^3.0.2"
}
}