mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-07-18 13:39:57 -04:00
Move window mock functions into helper class
This commit is contained in:
committed by
Chris Hill-Scott
parent
1082a37750
commit
1a5d40312a
@@ -75,6 +75,66 @@ class ElementQuery {
|
||||
}
|
||||
};
|
||||
|
||||
class WindowMock {
|
||||
constructor (jest) {
|
||||
this._defaults = {
|
||||
height: window.innerHeight,
|
||||
width: window.innerWidth
|
||||
};
|
||||
this._spies = {
|
||||
document: {}
|
||||
};
|
||||
this._jest = jest;
|
||||
}
|
||||
|
||||
setHeightTo (height) {
|
||||
|
||||
// mock DOM calls for window height
|
||||
window.innerHeight = height;
|
||||
// remove calls to document.documentElement.clientHeight when jQuery is gone. It's called to support older browsers like IE8
|
||||
this._spies.document.clientHeight = this._jest.spyOn(document.documentElement, 'clientHeight', 'get').mockImplementation(() => height);
|
||||
|
||||
}
|
||||
|
||||
setWidthTo (width) {
|
||||
|
||||
// mock DOM calls for window width
|
||||
window.innerWidth = width;
|
||||
// remove calls to document.documentElement.clientWidth when jQuery is gone. It's called to support older browsers like IE8
|
||||
this._spies.document.clientWidth = this._jest.spyOn(document.documentElement, 'clientWidth', 'get').mockImplementation(() => height);
|
||||
|
||||
}
|
||||
|
||||
resizeTo (dimensions) {
|
||||
|
||||
this.setHeightTo(dimensions.height);
|
||||
this.setWidthTo(dimensions.width);
|
||||
triggerEvent(window, 'resize');
|
||||
|
||||
}
|
||||
|
||||
scrollBy (scrollPosition) {
|
||||
|
||||
document.documentElement.scrollTop = scrollPosition;
|
||||
triggerEvent(window, 'scroll');
|
||||
|
||||
}
|
||||
|
||||
reset () {
|
||||
|
||||
window.innerHeight = this._defaults.height;
|
||||
window.innerWidth = this._defaults.width;
|
||||
document.documentElement.scrollTop = 0;
|
||||
|
||||
// reset all spies
|
||||
Object.keys(this._spies).forEach(key => {
|
||||
const objectSpies = this._spies[key];
|
||||
Object.keys(objectSpies).forEach(key => objectSpies[key].mockClear());
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// function to ask certain questions of a DOM Element
|
||||
const element = function (el) {
|
||||
return new ElementQuery(el);
|
||||
@@ -82,3 +142,4 @@ const element = function (el) {
|
||||
|
||||
exports.triggerEvent = triggerEvent;
|
||||
exports.element = element;
|
||||
exports.WindowMock = WindowMock;
|
||||
|
||||
Reference in New Issue
Block a user