From 316aeb667d0ab704b77ebe5840c11f70cab33fb8 Mon Sep 17 00:00:00 2001 From: comanV <79856745+comanV@users.noreply.github.com> Date: Tue, 21 Jun 2022 10:30:44 +0300 Subject: [PATCH] fix: check the existence of the window object in places where is not yet verified (#86) --- src/ModelRouter.ts | 12 ++++++++---- src/PathUtils.ts | 2 +- test/AuthoringUtils.test.ts | 11 +++++------ test/ModelManager.test.ts | 9 +++++++-- test/ModelRouter.test.ts | 8 +++++++- test/ModelStore.test.ts | 5 +++++ test/data/MainPageData.ts | 1 - 7 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/ModelRouter.ts b/src/ModelRouter.ts index 6ac67d5..975b3b8 100644 --- a/src/ModelRouter.ts +++ b/src/ModelRouter.ts @@ -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; } /** @@ -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; @@ -202,4 +206,4 @@ export function initModelRouter(): void { return replaceState.apply(history, [ state, title, url ]); }; } -} \ No newline at end of file +} diff --git a/src/PathUtils.ts b/src/PathUtils.ts index 9489319..3d698f3 100644 --- a/src/PathUtils.ts +++ b/src/PathUtils.ts @@ -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 diff --git a/test/AuthoringUtils.test.ts b/test/AuthoringUtils.test.ts index 57a5b13..3de6124 100644 --- a/test/AuthoringUtils.test.ts +++ b/test/AuthoringUtils.test.ts @@ -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; @@ -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 + '"]')); }); @@ -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); diff --git a/test/ModelManager.test.ts b/test/ModelManager.test.ts index 4926b2f..b705016 100644 --- a/test/ModelManager.test.ts +++ b/test/ModelManager.test.ts @@ -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'; @@ -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]) @@ -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); @@ -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'); }); @@ -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'); }); diff --git a/test/ModelRouter.test.ts b/test/ModelRouter.test.ts index 2ba88be..bbe8d1f 100644 --- a/test/ModelRouter.test.ts +++ b/test/ModelRouter.test.ts @@ -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'; @@ -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 ->', () => { diff --git a/test/ModelStore.test.ts b/test/ModelStore.test.ts index e4eb2d4..90475ac 100644 --- a/test/ModelStore.test.ts +++ b/test/ModelStore.test.ts @@ -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 @@ -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]; diff --git a/test/data/MainPageData.ts b/test/data/MainPageData.ts index 1994b59..b775fe0 100644 --- a/test/data/MainPageData.ts +++ b/test/data/MainPageData.ts @@ -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',