Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

partial window mock #1218

Merged
merged 6 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
isPreviewMode,
isSiteMode,
optionsMap,
windowWrapper,
} from 'pro-gallery-lib';
import { ItemsHelper } from 'pro-layouts';
import GalleryView from './galleryView';
Expand Down Expand Up @@ -135,6 +136,7 @@ export class GalleryContainer extends React.Component {
}

componentDidMount() {
windowWrapper.stopUsingMock();
const { body, documentElement: html } = document;
const viewportHeight = window.innerHeight;
const height = Math.max(
Expand Down
25 changes: 16 additions & 9 deletions packages/lib/src/common/window/window.mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const noop = () => ({});
const width = 2560;
const height = 1440;

const dims = {
export const dimsMock = {
y: 0,
x: 0,
width,
Expand All @@ -16,8 +16,8 @@ const dims = {
};

const elem = {
...dims,
getBoundingClientRect: () => dims,
...dimsMock,
getBoundingClientRect: () => dimsMock,
};

const documentMock = {
Expand All @@ -30,8 +30,8 @@ const documentMock = {
querySelector: () => [elem],
documentElement: elem,
activeElement: elem,
style: dims,
...dims,
style: dimsMock,
...dimsMock,
};

documentMock.body = documentMock;
Expand All @@ -46,18 +46,24 @@ const locationMock = {
search: '',
hash: '',
};

export const scrollMock = {
scrollTop: 0,
scrollY: 0,
};
const windowHydrateMock = {
...dimsMock,
...scrollMock,
};
const windowMock = {
isMock: true,
isSSR: true,
orientation: 0,
devicePixelRatio: 1,
scrollTop: 0,
addEventListener: noop,
removeEventListener: noop,
createEvent: noop,
CustomEvent: noop,
screen: dims,
screen: dimsMock,
open: noop,
petri: {},
search: {},
Expand All @@ -69,9 +75,10 @@ const windowMock = {
getComputedStyle: noop,
localStorage: {},
frames: [],
...dims,
...windowHydrateMock,
};

export const hydrateMockMap = new Map(Object.keys(windowHydrateMock).map((key) => [key, windowHydrateMock[key]]));
windowMock.parent = windowMock;

export default windowMock;
65 changes: 58 additions & 7 deletions packages/lib/src/common/window/windowWrapper.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import WindowMock from './window.mock';
import WindowMock, { hydrateMockMap } from './window.mock';

class WindowWrapper {
constructor() {
this.reset();
this.shouldUseMock = true;
this.initProxyWindow = this.initProxyWindow.bind(this);
if (this.windowIsAvailable()) {
// this will wrap the real window with partial mock for the dimensions
// once the gallery is mounted we will switch from the mocked properties to the real values
this.initProxyWindow();
} else {
this.initMockWindow();
}
}

windowIsAvailable() {
Expand All @@ -13,12 +21,55 @@ class WindowWrapper {
}
}

reset() {
this.isMock = !this.windowIsAvailable();
this.window = this.isMock ? WindowMock : window;
if (this.isMock) {
this.window.mockInstanceId = Math.floor(Math.random() * 100000);
initProxyWindow() {
console.log('locall pro-gallery ==>');
const handler = {
// here the proxy target is the global window object
get: function (target, property) {
if (hydrateMockMap.has(property) && this.shouldUseMock) {
return hydrateMockMap.get(property);
}
return target[property];
}.bind(this),
// here we push to the custom props Set to know later if we want to bind the prop
// reflect just assigns the proprty and returns boolean if the assign was successfull
set: function (target, property, value) {
return Reflect.set(target, property, value);
},
};
// eslint-disable-next-line no-undef
const windowProxy = new Proxy(window, handler);
const windowFuncHandler = {
get: function (target, property) {
if (!windowProxy.proGalleryCustomProps.has(property) && typeof windowProxy[property] === 'function') {
return windowProxy[property].bind(window);
}
return windowProxy[property];
},
set: function (target, property, value) {
windowProxy.proGalleryCustomProps.add(property);
return Reflect.set(windowProxy, property, value);
},
};
if (!windowProxy.proGalleryCustomProps) {
windowProxy.proGalleryCustomProps = new Set();
}
// this second proxy that returnes binded functions to avoid issues with non configurable proprties
// eslint-disable-next-line no-undef
this.window = new Proxy({}, windowFuncHandler);
}
initMockWindow() {
this.window = WindowMock;
this.window.mockInstanceId = Math.floor(Math.random() * 100000);
}
stopUsingMock() {
this.shouldUseMock = false;
}
get shouldUseMock() {
return this._shouldUseMock;
}
set shouldUseMock(shouldUseMock) {
this._shouldUseMock = shouldUseMock;
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/lib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export { NEW_PRESETS } from './core/presets/presets';
export { getLayoutName } from './core/presets/presets';
export { isInPreset } from './core/presets/presets';

export { default as window } from './common/window/windowWrapper';
export { default as window, windowWrapper } from './common/window/windowWrapper';
export { default as utils } from './common/utils';

export { viewModeWrapper } from './common/window/viewModeWrapper';
Expand Down
Loading