From 9adadae39e82466880141920e9feb3611284e946 Mon Sep 17 00:00:00 2001 From: SergioCrisostomo Date: Mon, 19 Jan 2015 23:19:23 +0100 Subject: [PATCH] restore and refactor DOMReady specs --- Specs/Utilities/DOMReady.js | 97 +++++++++++++++++++++------- Tests/DOMReady/DOMReady.delayed.html | 29 +++++++++ Tests/DOMReady/DOMReady.head.html | 22 +++++++ Tests/DOMReady/DOMReady.onAdd.html | 21 ++++++ Tests/gruntfile-options.js | 7 +- 5 files changed, 151 insertions(+), 25 deletions(-) create mode 100644 Tests/DOMReady/DOMReady.delayed.html create mode 100644 Tests/DOMReady/DOMReady.head.html create mode 100644 Tests/DOMReady/DOMReady.onAdd.html diff --git a/Specs/Utilities/DOMReady.js b/Specs/Utilities/DOMReady.js index 5a8d15fc5..098aa65e0 100644 --- a/Specs/Utilities/DOMReady.js +++ b/Specs/Utilities/DOMReady.js @@ -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(); + }); + }); + +}); diff --git a/Tests/DOMReady/DOMReady.delayed.html b/Tests/DOMReady/DOMReady.delayed.html new file mode 100644 index 000000000..ebda3479f --- /dev/null +++ b/Tests/DOMReady/DOMReady.delayed.html @@ -0,0 +1,29 @@ + + + + + Add MooTools when page is already ready + + + + + + + diff --git a/Tests/DOMReady/DOMReady.head.html b/Tests/DOMReady/DOMReady.head.html new file mode 100644 index 000000000..4c326ce7a --- /dev/null +++ b/Tests/DOMReady/DOMReady.head.html @@ -0,0 +1,22 @@ + + + + + Normal DOMReady scenario + + + + + + + + + + diff --git a/Tests/DOMReady/DOMReady.onAdd.html b/Tests/DOMReady/DOMReady.onAdd.html new file mode 100644 index 000000000..278c18a33 --- /dev/null +++ b/Tests/DOMReady/DOMReady.onAdd.html @@ -0,0 +1,21 @@ + + + + + DOMReady added when page is already loaded + + + + + + + + + + diff --git a/Tests/gruntfile-options.js b/Tests/gruntfile-options.js index 094a18383..4a05bb00f 100644 --- a/Tests/gruntfile-options.js +++ b/Tests/gruntfile-options.js @@ -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,