From 13c40a25d1b7183ecbe7dc9fd3b0908737dd0fa0 Mon Sep 17 00:00:00 2001 From: Tom Byers Date: Fri, 2 Aug 2019 17:00:26 +0100 Subject: [PATCH] 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. --- tests/javascripts/fullscreenTable.test.js | 2 +- tests/javascripts/support/helpers.js | 49 ++++++++++++++++++----- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/tests/javascripts/fullscreenTable.test.js b/tests/javascripts/fullscreenTable.test.js index c1130f1ce..968712134 100644 --- a/tests/javascripts/fullscreenTable.test.js +++ b/tests/javascripts/fullscreenTable.test.js @@ -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'); diff --git a/tests/javascripts/support/helpers.js b/tests/javascripts/support/helpers.js index ee56ae375..10a4445b0 100644 --- a/tests/javascripts/support/helpers.js +++ b/tests/javascripts/support/helpers.js @@ -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()); });