Skip to content

Commit

Permalink
Fix mock of window for server side tests (#11681)
Browse files Browse the repository at this point in the history
  • Loading branch information
LazyLahtak authored Jan 24, 2020
1 parent 946049a commit c71c837
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
12 changes: 5 additions & 7 deletions js/core/utils/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@

const domAdapter = require('../dom_adapter');

const hasWindow = typeof window !== 'undefined';
const hasWindow = () => typeof window !== 'undefined';

let windowObject = hasWindow && window;
let windowObject = hasWindow() && window;

if(!windowObject) {
windowObject = {};
windowObject.window = windowObject;
}

const hasWindowFn = () => hasWindow;

const getWindow = () => windowObject;

const hasProperty = (prop) => hasWindowFn() && prop in windowObject;
const hasProperty = (prop) => hasWindow() && prop in windowObject;

const defaultScreenFactorFunc = (width) => {
if(width < 768) {
Expand All @@ -36,10 +34,10 @@ const getCurrentScreenFactor = (screenFactorCallback) => {
return screenFactorFunc(windowWidth);
};

const getNavigator = () => hasWindowFn() ? windowObject.navigator : { userAgent: '' };
const getNavigator = () => hasWindow() ? windowObject.navigator : { userAgent: '' };

module.exports = {
hasWindow: hasWindowFn,
hasWindow,
getWindow,
hasProperty,
defaultScreenFactorFunc,
Expand Down
10 changes: 4 additions & 6 deletions testing/helpers/ssrEmulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,10 @@ for(const field in window) {
}

const makeWindowEmpty = function() {
windowUtils.hasWindow = function() {
return false;
};
windowUtils.getWindow = function() {
return windowMock;
};
windowUtils.hasWindow = () => false;
windowUtils.getWindow = () => windowMock;
windowUtils.getNavigator = () => ({ userAgent: '' });
windowUtils.hasProperty = () => false;
};

// Ensure domAdapter is not used on scripts loading stage (until the integration is not injected)
Expand Down

0 comments on commit c71c837

Please sign in to comment.