Skip to content

Commit

Permalink
fix: check the existence of the window object in places where is not …
Browse files Browse the repository at this point in the history
…yet verified (#86)
  • Loading branch information
comanV authored Jun 21, 2022
1 parent 7f5439c commit 316aeb6
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 15 deletions.
12 changes: 8 additions & 4 deletions src/ModelRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,13 @@ export class RouterModes {
* @return
*/
export function getModelPath(url?: string | null | URL): string | null {
const localUrl = (url || window.location.pathname) as string;
const localUrl = (url || (PathUtils.isBrowser() && window.location.pathname)) as string;

return PathUtils.sanitize(localUrl);
if (localUrl) {
return PathUtils.sanitize(localUrl);
}

return null;
}

/**
Expand Down Expand Up @@ -179,7 +183,7 @@ export function routeModel(url?: string | undefined | null | URL): void {

export function initModelRouter(): void {
// Activate the model router
if (isModelRouterEnabled() && typeof window !== 'undefined') {
if (isModelRouterEnabled() && PathUtils.isBrowser()) {
// Encapsulate the history.pushState and history.replaceState functions to prefetch the page model for the current route
const pushState = window.history.pushState;
const replaceState = window.history.replaceState;
Expand All @@ -202,4 +206,4 @@ export function initModelRouter(): void {
return replaceState.apply(history, [ state, title, url ]);
};
}
}
}
2 changes: 1 addition & 1 deletion src/PathUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ export class PathUtils {
* @returns Updated path to match against
*/
public static toAEMPath(path: string, aemHost: string, rootPath: string): string {
const isLoadedInAEM = window.location.origin === aemHost;
const isLoadedInAEM = this.isBrowser() && window.location.origin === aemHost;

if (isLoadedInAEM) {
// Remove leading and trailing slashes, if any
Expand Down
11 changes: 5 additions & 6 deletions test/AuthoringUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
*/

import * as assert from 'assert';
import {AuthoringUtils} from '../src/AuthoringUtils';
import {PathUtils} from '../src/PathUtils';
import {AEM_MODE} from '../src/Constants';
import MetaProperty from "../src/MetaProperty";
import { AuthoringUtils } from '../src/AuthoringUtils';
import { PathUtils } from '../src/PathUtils';
import { AEM_MODE } from '../src/Constants';
import MetaProperty from '../src/MetaProperty';

describe('AuthoringUtils ->', () => {
let authoringUtils: AuthoringUtils;
Expand Down Expand Up @@ -94,7 +94,7 @@ describe('AuthoringUtils ->', () => {

// then
Object.entries(AuthoringUtils.AUTHORING_LIBRARIES.META).forEach((entry) => {
const [key, val] = entry;
const [ key, val ] = entry;

assert.ok(libs.querySelectorAll('meta[property="' + key + '"][content="' + val + '"]'));
});
Expand Down Expand Up @@ -231,7 +231,6 @@ describe('AuthoringUtils ->', () => {
});
});


describe('isPreviewMode', () => {
it('should be false if both indicators are falsy', () => {
jest.spyOn(PathUtils, 'getMetaPropertyValue').mockReturnValue(AEM_MODE.EDIT);
Expand Down
9 changes: 7 additions & 2 deletions test/ModelManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import EventType from '../src/EventType';
import InternalConstants from '../src/InternalConstants';
import MetaProperty from '../src/MetaProperty';
import { ModelClient } from '../src/ModelClient';
import ModelManager, {ModelManagerConfiguration} from '../src/ModelManager';
import ModelManager, { ModelManagerConfiguration } from '../src/ModelManager';
import { isRouteExcluded } from '../src/ModelRouter';
import { PathUtils } from '../src/PathUtils';
import { content_test_page_root_child0000_child0010, PAGE_MODEL, ERROR_PAGE_MODEL_404, ERROR_PAGE_MODEL_500 } from './data/MainPageData';
Expand All @@ -44,7 +44,7 @@ jest.mock('../src/ModelRouter');

const REQUEST_MAP:{[key:string]:any} = {};

fetchMock.mockResponse( (req) => {
fetchMock.mockResponse((req) => {
if (REQUEST_MAP[req.url]) {
return Promise.resolve({
body: JSON.stringify(REQUEST_MAP[req.url])
Expand Down Expand Up @@ -231,6 +231,7 @@ describe('ModelManager ->', () => {

it('should fetch data once on initialization when sub page route path is excluded', () => {
isRouteExcludedSpy.mockReturnValue(true);

return ModelManager.initialize({ path: PAGE_PATH, modelClient: modelClient }).then((data) => {
expectPageModelLoadedEventFired();
verify(ModelClientMock.fetch(anyString())).times(1);
Expand Down Expand Up @@ -345,10 +346,12 @@ describe('ModelManager ->', () => {
errorPageRoot: ERROR_PAGE_ROOT
};
const data:Model = await ModelManager.initialize(configuration);

verify(modelClient.fetch(anyString()));
assert.deepEqual(data, PAGE_MODEL, 'data should be correct');

const nonExistingData = await ModelManager._fetchData(NON_EXISTING_PATH);

assert.deepEqual(nonExistingData, ERROR_PAGE_MODEL_404, 'data should be correct');

});
Expand All @@ -361,10 +364,12 @@ describe('ModelManager ->', () => {
errorPageRoot: ERROR_PAGE_ROOT
};
const data:Model = await ModelManager.initialize(configuration);

verify(modelClient.fetch(anyString()));
assert.deepEqual(data, PAGE_MODEL, 'data should be correct');

const nonExistingData = await ModelManager._fetchData(PAGE_WITH_ERROR_PATH);

assert.deepEqual(nonExistingData, ERROR_PAGE_MODEL_500, 'data should be correct');

});
Expand Down
8 changes: 7 additions & 1 deletion test/ModelRouter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import MetaProperty from '../src/MetaProperty';
import { Model } from '../src/Model';
import ModelManager from '../src/ModelManager';
import {
dispatchRouteChanged, getModelPath, getRouteFilters, initModelRouter, isModelRouterEnabled, isRouteExcluded, routeModel, RouterModes,
dispatchRouteChanged, getModelPath, getRouteFilters, initModelRouter, isModelRouterEnabled, isRouteExcluded, routeModel, RouterModes
} from '../src/ModelRouter';
import { PathUtils } from '../src/PathUtils';

Expand Down Expand Up @@ -49,6 +49,12 @@ describe('ModelRouter ->', () => {
it('should get the current window URL', () => {
expect(getModelPath('/zyx/abc?date=03.10.2021')).toEqual('/zyx/abc');
});
it('should return null', () => {
const isBrowserSpy = jest.spyOn(PathUtils, 'isBrowser').mockImplementation(() => false);

expect(getModelPath()).toBeNull();
isBrowserSpy.mockRestore();
});
});

describe('dispatchRouteChanged ->', () => {
Expand Down
5 changes: 5 additions & 0 deletions test/ModelStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ describe('ModelStore ->', () => {
describe('setData', () => {
it('should set data', () => {
let item: any = modelStore.getData('/content/test/child_page_1/jcr:content/root/child1000');

item[TEST_FIELD] = TEST_FIELD_DATA;

const val = {
key: 'child1000',
value: item
Expand All @@ -204,11 +206,14 @@ describe('ModelStore ->', () => {

it('should set empty string on removed data', () => {
let item: any = modelStore.getData('/content/test/child_page_1/jcr:content/root/child1000');

item[TEST_FIELD] = TEST_FIELD_DATA;

const val = {
key: 'child1000',
value: item
};

modelStore.setData('/content/test/child_page_1/jcr:content/root/child1000', val);

delete item[TEST_FIELD];
Expand Down
1 change: 0 additions & 1 deletion test/data/MainPageData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ export const ERROR_PAGE_MODEL_404: PageModel = {
':type': 'we-retail-react/components/structure/page'
};


export const ERROR_PAGE_MODEL_500: PageModel = {
'designPath': '/libs/settings/wcm/designs/default',
'title': 'Example Error Page 500',
Expand Down

0 comments on commit 316aeb6

Please sign in to comment.