Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 3561f13

Browse files
gkalpakpetebacondarwin
authored andcommitted
fix(jqLite): add private jqLiteReadyLoaded function
Add a private jqLiteReadyLoaded function that can be used to execute a callback when the `load` event fires (or immediately if the page has already loaded, i.e. `document.readyState === 'complete'`).
1 parent 8a0dace commit 3561f13

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/jqLite.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,17 @@ function jqLiteRemove(element, keepData) {
453453
if (parent) parent.removeChild(element);
454454
}
455455

456+
// Note: This is a private method that should not be exposed unto jQLitePrototype
457+
function jqLiteReadyLoaded(trigger, win) {
458+
win = win || window;
459+
if (win.document.readyState === 'complete') {
460+
trigger();
461+
} else {
462+
// Note: No need to unregister as `load` fires only once.
463+
jqLite(win).on('load', trigger);
464+
}
465+
}
466+
456467
//////////////////////////////////////////
457468
// Functions which are declared directly.
458469
//////////////////////////////////////////

test/jqLiteSpec.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,4 +1993,66 @@ describe('jqLite', function() {
19931993
});
19941994
});
19951995

1996+
1997+
describe('jqLiteReadyLoaded', function() {
1998+
/* global jqLiteReadyLoaded: false */
1999+
var onLoadCallback = jasmine.createSpy('onLoadCallback');
2000+
var fakeWin;
2001+
2002+
function createFakeWin() {
2003+
fakeWin = {
2004+
document: {readyState: 'loading'},
2005+
addEventListener: jasmine.createSpy('fakeWinAddEventListener')
2006+
};
2007+
}
2008+
2009+
function spyOnJQLiteOn() {
2010+
spyOn(jqLite.prototype, 'on').andCallThrough();
2011+
}
2012+
2013+
function unspyOnJQLiteOn() {
2014+
jqLite.prototype.on = jqLite.prototype.on.originalValue;
2015+
}
2016+
2017+
function expectJQLiteOnCallsCount(callCount) {
2018+
expect(jqLite.prototype.on.calls.length).toBe(callCount);
2019+
}
2020+
2021+
2022+
beforeEach(function() {
2023+
onLoadCallback.reset();
2024+
createFakeWin();
2025+
spyOnJQLiteOn();
2026+
});
2027+
2028+
afterEach(function() {
2029+
unspyOnJQLiteOn();
2030+
fakeWin = null;
2031+
});
2032+
2033+
2034+
it('should immediatelly execute the callback if the document has already loaded', function() {
2035+
fakeWin.document.readyState = 'complete';
2036+
jqLiteReadyLoaded(onLoadCallback, fakeWin);
2037+
expectJQLiteOnCallsCount(0);
2038+
expect(fakeWin.addEventListener).not.toHaveBeenCalled();
2039+
expect(onLoadCallback).toHaveBeenCalledOnce();
2040+
});
2041+
2042+
2043+
it('should register a listener for the `load` event', function() {
2044+
jqLiteReadyLoaded(onLoadCallback, fakeWin);
2045+
expectJQLiteOnCallsCount(1);
2046+
expect(fakeWin.addEventListener).toHaveBeenCalledOnce();
2047+
expect(onLoadCallback).not.toHaveBeenCalled();
2048+
});
2049+
2050+
2051+
it('should execute the callback once the `load` event fires', function() {
2052+
jqLiteReadyLoaded(onLoadCallback, fakeWin);
2053+
expect(onLoadCallback).not.toHaveBeenCalled();
2054+
jqLite(fakeWin).triggerHandler('load');
2055+
expect(onLoadCallback).toHaveBeenCalledOnce();
2056+
});
2057+
});
19962058
});

0 commit comments

Comments
 (0)