diff --git a/Gruntfile.js b/Gruntfile.js index 956113503..1870a3134 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -1,4 +1,14 @@ "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] == null || process.argv[2] == 'all' ? 'all' : 'nocompat'; +})(); +require('./Tests/httpServer.js')(build); module.exports = function(grunt) { diff --git a/Specs/Utilities/DOMReady.js b/Specs/Utilities/DOMReady.js index 5a8d15fc5..193509e44 100644 --- a/Specs/Utilities/DOMReady.js +++ b/Specs/Utilities/DOMReady.js @@ -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; + 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 = {}; -*/ diff --git a/Tests/DOMReady/DOMReady.delayed.html b/Tests/DOMReady/DOMReady.delayed.html new file mode 100644 index 000000000..09e644609 --- /dev/null +++ b/Tests/DOMReady/DOMReady.delayed.html @@ -0,0 +1,27 @@ + + +
+ +