From 6bcaeb1de4da216d12f81341e3df7c40445df526 Mon Sep 17 00:00:00 2001 From: Jon Snyder Date: Wed, 18 Oct 2023 16:50:02 -0600 Subject: [PATCH] Extract redirect into its own util --- .../Personalization/dom-actions/createRedirect.js | 13 +++++++++++++ src/components/Personalization/index.js | 3 ++- .../dom-actions/createRedirect.spec.js | 14 ++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 src/components/Personalization/dom-actions/createRedirect.js create mode 100644 test/unit/specs/components/Personalization/dom-actions/createRedirect.spec.js diff --git a/src/components/Personalization/dom-actions/createRedirect.js b/src/components/Personalization/dom-actions/createRedirect.js new file mode 100644 index 000000000..3d6c692ac --- /dev/null +++ b/src/components/Personalization/dom-actions/createRedirect.js @@ -0,0 +1,13 @@ +/* +Copyright 2019 Adobe. All rights reserved. +This file is licensed to you under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. You may obtain a copy +of the License at http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under +the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS +OF ANY KIND, either express or implied. See the License for the specific language +governing permissions and limitations under the License. +*/ + +export default window => url => window.location.replace(url); diff --git a/src/components/Personalization/index.js b/src/components/Personalization/index.js index 46280e6ec..dadec5c6d 100644 --- a/src/components/Personalization/index.js +++ b/src/components/Personalization/index.js @@ -38,6 +38,7 @@ import createProcessDomAction from "./handlers/createProcessDomAction"; import createProcessHtmlContent from "./handlers/createProcessHtmlContent"; import createProcessRedirect from "./handlers/createProcessRedirect"; import createProcessPropositions from "./handlers/createProcessPropositions"; +import createRedirect from "./dom-actions/createRedirect"; const createPersonalization = ({ config, logger, eventManager }) => { const { targetMigrationEnabled, prehidingStyle } = config; @@ -58,7 +59,7 @@ const createPersonalization = ({ config, logger, eventManager }) => { }); const viewCache = createViewCacheManager({ createProposition }); - const executeRedirect = url => window.location.replace(url); + const executeRedirect = createRedirect(window); const schemaProcessors = { [schema.DEFAULT_CONTENT_ITEM]: processDefaultContent, [schema.DOM_ACTION]: createProcessDomAction({ diff --git a/test/unit/specs/components/Personalization/dom-actions/createRedirect.spec.js b/test/unit/specs/components/Personalization/dom-actions/createRedirect.spec.js new file mode 100644 index 000000000..0818f0680 --- /dev/null +++ b/test/unit/specs/components/Personalization/dom-actions/createRedirect.spec.js @@ -0,0 +1,14 @@ +import createRedirect from "../../../../../../src/components/Personalization/dom-actions/createRedirect"; + +describe("createRedirect", () => { + it("redirects", () => { + const window = { + location: { + replace: jasmine.createSpy() + } + }; + const redirect = createRedirect(window); + redirect("myurl"); + expect(window.location.replace).toHaveBeenCalledWith("myurl"); + }); +});