Changes to WindowMock helper

This includes the following fixes:

1. fix error in `WindowMock.setWidthTo`

   It was returning height, not width.

2. Fix for `WindowMock.reset`

   Changes to the scroll position need to go
   through the `scrollTo` method.

It also includes the following changes

1. Improve mocking of window scrollTop

   Increases the number of DOM API methods mocked
   to return the intended scrollTop value.

2. Change WindowMock.scrollBy to
   WindowMock.scrollTo

   Because you're not scrolling by an amount,
   you're scrolling to a position.

3. Give WindowMock getters for position/dimension

   It's useful to be able to get the
   position/dimension of the window in tests when
   you're resizing and scrolling it as part of the
   test.

4. Assign WindowMock spies on instantiation

   Assigning them whenever a dimension is set doesn't
   make sense. You're just setting a value, not
   changing how that value is accessed.
This commit is contained in:
Tom Byers
2019-08-02 17:00:26 +01:00
parent 983a1dc931
commit 13c40a25d1
2 changed files with 39 additions and 12 deletions

View File

@@ -181,7 +181,7 @@ describe('FullscreenTable', () => {
test("when the page has scrolled", () => {
// scroll the window so the table fills the height of the window (768px)
windowMock.scrollBy(500);
windowMock.scrollTo(500);
// the frames should crop to the window height
expect(window.getComputedStyle(tableFrame)['height']).toEqual('768px');

View File

@@ -173,27 +173,52 @@ class WindowMock {
height: window.innerHeight,
width: window.innerWidth
};
this._spies = {
this.spies = {
document: {}
};
this._jest = jest;
this._setSpies();
}
get top () {
return window.scrollY;
}
get bottom () {
return window.scrollY + window.innerHeight;
}
get height () {
return window.innerHeight;
}
get width () {
return window.innerWidth
}
get scrollPosition () {
return window.scrollY;
}
_setSpies () {
// 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(() => window.innerHeight);
// 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(() => window.innerWidth);
}
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);
}
@@ -205,9 +230,11 @@ class WindowMock {
}
scrollBy (scrollPosition) {
scrollTo (scrollPosition) {
document.documentElement.scrollTop = scrollPosition;
window.scrollY = scrollPosition;
window.pageYOffset = scrollPosition;
triggerEvent(window, 'scroll');
}
@@ -216,11 +243,11 @@ class WindowMock {
window.innerHeight = this._defaults.height;
window.innerWidth = this._defaults.width;
document.documentElement.scrollTop = 0;
this.scrollTo(0);
// reset all spies
Object.keys(this._spies).forEach(key => {
const objectSpies = this._spies[key];
Object.keys(this.spies).forEach(key => {
const objectSpies = this.spies[key];
Object.keys(objectSpies).forEach(key => objectSpies[key].mockClear());
});