From 63a598aaca6af6c00b71d9f9d6a38e8a9f81f8f3 Mon Sep 17 00:00:00 2001 From: Happy Shandilya Date: Mon, 16 Oct 2023 16:40:52 -0700 Subject: [PATCH 01/12] use existing Alloy DOM utils --- .../actions/displayIframeContent.js | 37 ++++++++++++------- .../in-app-message-actions/utils.js | 12 ------ .../in-app-message-actions/utils.spec.js | 29 --------------- 3 files changed, 24 insertions(+), 54 deletions(-) diff --git a/src/components/Personalization/in-app-message-actions/actions/displayIframeContent.js b/src/components/Personalization/in-app-message-actions/actions/displayIframeContent.js index 4a768eb26..2478bbb63 100644 --- a/src/components/Personalization/in-app-message-actions/actions/displayIframeContent.js +++ b/src/components/Personalization/in-app-message-actions/actions/displayIframeContent.js @@ -11,22 +11,31 @@ governing permissions and limitations under the License. */ import { getNonce } from "../../dom-actions/dom"; -import { createElement, parseAnchor, removeElementById } from "../utils"; +import { parseAnchor } from "../utils"; import { TEXT_HTML } from "../../constants/contentType"; import { assign } from "../../../../utils"; import { getEventType } from "../../../../constants/propositionEventType"; +import { createNode, removeNode } from "../../../../utils/dom"; const ALLOY_MESSAGING_CONTAINER_ID = "alloy-messaging-container"; const ALLOY_OVERLAY_CONTAINER_ID = "alloy-overlay-container"; const ALLOY_IFRAME_ID = "alloy-content-iframe"; -const dismissMessage = () => - [ALLOY_MESSAGING_CONTAINER_ID, ALLOY_OVERLAY_CONTAINER_ID].forEach( - removeElementById - ); +export const dismissMessage = () => { + const elementIdsToRemove = [ + ALLOY_MESSAGING_CONTAINER_ID, + ALLOY_OVERLAY_CONTAINER_ID + ]; + elementIdsToRemove.forEach(id => { + const element = document.getElementById(id); + if (element) { + removeNode(element); + } + }); +}; const setWindowLocationHref = link => { - window.location.assign(link); + window.location.href = link; }; export const createIframeClickHandler = ( @@ -73,11 +82,13 @@ export const createIframe = (htmlContent, clickHandler) => { if (scriptTag) { scriptTag.setAttribute("nonce", getNonce()); } - const element = document.createElement("iframe"); - element.src = URL.createObjectURL( - new Blob([htmlDocument.documentElement.outerHTML], { type: TEXT_HTML }) - ); - element.id = ALLOY_IFRAME_ID; + const element = createNode("iframe", { + src: URL.createObjectURL( + new Blob([htmlDocument.documentElement.outerHTML], { type: "text/html" }) + ), + id: ALLOY_IFRAME_ID + }); + element.addEventListener("load", () => { const { addEventListener } = element.contentDocument || element.contentWindow.document; @@ -271,9 +282,9 @@ export const displayHTMLContentInIframe = (settings = {}, interact) => { return; } - const container = createElement(ALLOY_MESSAGING_CONTAINER_ID); + const container = createNode("div", { id: ALLOY_MESSAGING_CONTAINER_ID }); const iframe = createIframe(content, createIframeClickHandler(interact)); - const overlay = createElement(ALLOY_OVERLAY_CONTAINER_ID); + const overlay = createNode("div", { id: ALLOY_OVERLAY_CONTAINER_ID }); if (!isValidWebParameters(webParameters)) { webParameters = generateWebParameters(mobileParameters); diff --git a/src/components/Personalization/in-app-message-actions/utils.js b/src/components/Personalization/in-app-message-actions/utils.js index aa4f42567..618f821a9 100644 --- a/src/components/Personalization/in-app-message-actions/utils.js +++ b/src/components/Personalization/in-app-message-actions/utils.js @@ -33,13 +33,6 @@ export const removeElements = cssClassName => { }); }; -export const removeElementById = id => { - const element = document.getElementById(id); - if (element) { - element.remove(); - } -}; - export const parseAnchor = anchor => { const nothing = {}; @@ -74,8 +67,3 @@ export const parseAnchor = anchor => { uuid }; }; -export const createElement = elementTagId => { - const element = document.createElement("div"); - element.id = elementTagId; - return element; -}; diff --git a/test/unit/specs/components/Personalization/in-app-message-actions/utils.spec.js b/test/unit/specs/components/Personalization/in-app-message-actions/utils.spec.js index 28e375686..26ef92385 100644 --- a/test/unit/specs/components/Personalization/in-app-message-actions/utils.spec.js +++ b/test/unit/specs/components/Personalization/in-app-message-actions/utils.spec.js @@ -9,32 +9,3 @@ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTA OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ - -import { removeElementById } from "../../../../../../src/components/Personalization/in-app-message-actions/utils"; - -describe("removeElementById", () => { - beforeEach(() => { - document.body.innerHTML = ` -
-`; - }); - - it("should remove an element when it exists", () => { - const elementId = "test-element"; - const element = document.getElementById(elementId); - - expect(element).toBeTruthy(); - - removeElementById(elementId); - - expect(document.getElementById(elementId)).toBeNull(); - }); - - it("should do nothing when the element does not exist", () => { - const nonExistentId = "non-existent-element"; - - removeElementById(nonExistentId); - - expect(document.getElementById(nonExistentId)).toBeNull(); - }); -}); From f33a54a35fffb7d63c46e208dc1761bf410498e0 Mon Sep 17 00:00:00 2001 From: Happy Shandilya Date: Mon, 16 Oct 2023 16:51:45 -0700 Subject: [PATCH 02/12] removed unused utils method --- .../in-app-message-actions/utils.js | 22 ------------------- 1 file changed, 22 deletions(-) diff --git a/src/components/Personalization/in-app-message-actions/utils.js b/src/components/Personalization/in-app-message-actions/utils.js index 618f821a9..5660f3402 100644 --- a/src/components/Personalization/in-app-message-actions/utils.js +++ b/src/components/Personalization/in-app-message-actions/utils.js @@ -11,28 +11,6 @@ governing permissions and limitations under the License. */ import { startsWith } from "../../../utils"; -export const addStyle = (styleTagId, cssText) => { - const existingStyle = document.getElementById(styleTagId); - if (existingStyle) { - existingStyle.remove(); - } - - const styles = document.createElement("style"); - styles.id = styleTagId; - - styles.appendChild(document.createTextNode(cssText)); - document.head.appendChild(styles); -}; - -export const removeElements = cssClassName => { - [...document.getElementsByClassName(cssClassName)].forEach(element => { - if (!element) { - return; - } - element.remove(); - }); -}; - export const parseAnchor = anchor => { const nothing = {}; From 243f83ddc7b03c3ab21043c0e68bd82a0c6f1ee9 Mon Sep 17 00:00:00 2001 From: Happy Shandilya Date: Tue, 17 Oct 2023 11:22:28 -0700 Subject: [PATCH 03/12] renamed to "test:unit:debug" --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ad0ef9994..aff456a29 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "format": "prettier --write \"*.{html,js}\" \"{sandbox,src,test,scripts}/**/*.{html,js}\"", "test": "npm run test:unit && npm run test:scripts && npm run test:functional", "test:unit": "karma start --single-run", - "testdebug": "karma start --browsers=Chrome --single-run=false --debug", + "test:unit:debug": "karma start --browsers=Chrome --single-run=false --debug", "test:unit:watch": "karma start", "test:unit:saucelabs:local": "karma start karma.saucelabs.conf.js --single-run", "test:unit:coverage": "karma start --single-run --reporters spec,coverage", From 5471f2152d0f7cc3d1816f2335025d9dc09e1534 Mon Sep 17 00:00:00 2001 From: Happy Shandilya Date: Tue, 17 Oct 2023 11:33:28 -0700 Subject: [PATCH 04/12] added a try/catch to handle potential JSON parsing errors --- .../createEvaluableRulesetPayload.js | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/components/DecisioningEngine/createEvaluableRulesetPayload.js b/src/components/DecisioningEngine/createEvaluableRulesetPayload.js index 59a24ff65..dec1e8fb6 100644 --- a/src/components/DecisioningEngine/createEvaluableRulesetPayload.js +++ b/src/components/DecisioningEngine/createEvaluableRulesetPayload.js @@ -31,14 +31,20 @@ const isRulesetItem = item => { return false; } - const content = - typeof data.content === "string" ? JSON.parse(data.content) : data.content; - - return ( - content && - Object.prototype.hasOwnProperty.call(content, "version") && - Object.prototype.hasOwnProperty.call(content, "rules") - ); + try { + const content = + typeof data.content === "string" + ? JSON.parse(data.content) + : data.content; + + return ( + content && + Object.prototype.hasOwnProperty.call(content, "version") && + Object.prototype.hasOwnProperty.call(content, "rules") + ); + } catch (error) { + return false; + } }; export default (payload, eventRegistry, decisionHistory) => { From 6aaba46947a0594e48d49043447427371efecc75 Mon Sep 17 00:00:00 2001 From: Happy Shandilya Date: Tue, 17 Oct 2023 11:49:24 -0700 Subject: [PATCH 05/12] removed ensureSchemaBasedRulesetConsequences --- src/components/DecisioningEngine/index.js | 3 -- src/components/DecisioningEngine/utils.js | 11 ------- .../DecisioningEngine/index.spec.js | 30 ------------------- 3 files changed, 44 deletions(-) diff --git a/src/components/DecisioningEngine/index.js b/src/components/DecisioningEngine/index.js index 9b9bdbd56..93974750b 100644 --- a/src/components/DecisioningEngine/index.js +++ b/src/components/DecisioningEngine/index.js @@ -16,7 +16,6 @@ import createApplyResponse from "./createApplyResponse"; import createEventRegistry from "./createEventRegistry"; import createContextProvider from "./createContextProvider"; import createSubscribeRulesetItems from "./createSubscribeRulesetItems"; -import { ensureSchemaBasedRulesetConsequences } from "./utils"; import { CONTEXT_KEY, MOBILE_EVENT_SOURCE, @@ -56,8 +55,6 @@ const createDecisioningEngine = ({ config, createNamespacedStorage }) => { decisionContext = {}, onResponse = noop }) { - ensureSchemaBasedRulesetConsequences(event); - onResponse( createOnResponseHandler({ renderDecisions, diff --git a/src/components/DecisioningEngine/utils.js b/src/components/DecisioningEngine/utils.js index 9048661c8..bb13e312a 100644 --- a/src/components/DecisioningEngine/utils.js +++ b/src/components/DecisioningEngine/utils.js @@ -42,17 +42,6 @@ export const getExpirationDate = retentionPeriod => { expirationDate.setDate(expirationDate.getDate() - retentionPeriod); return expirationDate; }; - -export const ensureSchemaBasedRulesetConsequences = event => { - event.mergeData({ - __adobe: { - ajo: { - "in-app-response-format": 2 - } - } - }); -}; - export const getActivityId = proposition => { const { scopeDetails = {} } = proposition; const { activity = {} } = scopeDetails; diff --git a/test/unit/specs/components/DecisioningEngine/index.spec.js b/test/unit/specs/components/DecisioningEngine/index.spec.js index 17980fffb..c5fa73d17 100644 --- a/test/unit/specs/components/DecisioningEngine/index.spec.js +++ b/test/unit/specs/components/DecisioningEngine/index.spec.js @@ -138,34 +138,4 @@ describe("createDecisioningEngine:commands:evaluateRulesets", () => { propositions: [proposition] }); }); - - it("ensures schema-based ruleset consequences", () => { - onResponseHandler = onResponse => { - onResponse({ - response: mockRulesetResponseWithCondition({ - definition: { - key: "referringPage.path", - matcher: "eq", - values: ["/search"] - }, - type: "matcher" - }) - }); - }; - - decisioningEngine.lifecycle.onBeforeEvent({ - event: mockEvent, - renderDecisions: false, - decisionContext: {}, - onResponse: onResponseHandler - }); - - expect(mergeData).toHaveBeenCalledOnceWith({ - __adobe: { - ajo: { - "in-app-response-format": 2 - } - } - }); - }); }); From ca0229563840f94a58c4386fe0590a6ac5089c2c Mon Sep 17 00:00:00 2001 From: Happy Shandilya Date: Tue, 17 Oct 2023 13:10:19 -0700 Subject: [PATCH 06/12] using shorthand object property notation --- src/components/Personalization/createComponent.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/components/Personalization/createComponent.js b/src/components/Personalization/createComponent.js index 236c56002..e7464ee83 100644 --- a/src/components/Personalization/createComponent.js +++ b/src/components/Personalization/createComponent.js @@ -33,9 +33,7 @@ export default ({ }) => { return { lifecycle: { - onDecision({ viewName, renderDecisions, propositions }) { - return onDecisionHandler({ viewName, renderDecisions, propositions }); - }, + onDecision: onDecisionHandler, onBeforeRequest({ request }) { setTargetMigration(request); return Promise.resolve(); From 17c0de3b07db78d174cfcd89c6a3ab2545bcf030 Mon Sep 17 00:00:00 2001 From: Happy Shandilya Date: Tue, 17 Oct 2023 13:13:48 -0700 Subject: [PATCH 07/12] new line --- sandbox/src/components/InAppMessagesDemo/InAppMessagesStyle.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sandbox/src/components/InAppMessagesDemo/InAppMessagesStyle.css b/sandbox/src/components/InAppMessagesDemo/InAppMessagesStyle.css index 87e55e3cc..b97b1b608 100644 --- a/sandbox/src/components/InAppMessagesDemo/InAppMessagesStyle.css +++ b/sandbox/src/components/InAppMessagesDemo/InAppMessagesStyle.css @@ -4,4 +4,4 @@ } .element-spacing { margin-right: 10px; -} \ No newline at end of file +} From e89e6c77bfe01e52bfe94946b810a1fb091f394e Mon Sep 17 00:00:00 2001 From: Happy Shandilya Date: Tue, 17 Oct 2023 13:27:38 -0700 Subject: [PATCH 08/12] constants shared between components moved to the shared src/constants module --- .../consequenceAdapters/inAppMessageConsequenceAdapter.js | 4 ++-- .../DecisioningEngine/createEvaluableRulesetPayload.js | 5 +---- src/components/DecisioningEngine/createOnResponseHandler.js | 2 +- src/components/Personalization/createApplyPropositions.js | 2 +- .../Personalization/createPersonalizationDetails.js | 2 +- src/components/Personalization/createPreprocessors.js | 2 +- src/components/Personalization/createSubscribeMessageFeed.js | 2 +- src/components/Personalization/createViewCacheManager.js | 2 +- .../Personalization/handlers/createProcessInAppMessage.js | 2 +- .../in-app-message-actions/actions/displayIframeContent.js | 2 +- src/components/Personalization/index.js | 2 +- .../Personalization => }/constants/contentType.js | 0 src/{components/Personalization => }/constants/handle.js | 0 src/{components/Personalization => }/constants/schema.js | 0 .../inAppMessageConsequenceAdapter.spec.js | 2 +- .../consequenceAdapters/schemaTypeConsequenceAdapter.spec.js | 2 +- .../DecisioningEngine/createConsequenceAdapter.spec.js | 2 +- .../DecisioningEngine/createSubscribeRulesetItems.spec.js | 2 +- .../Personalization/createOnDecisionHandler.spec.js | 2 +- .../Personalization/createPersonalizationDetails.spec.js | 2 +- .../Personalization/createSubscribeMessageFeed.spec.js | 2 +- .../Personalization/createViewCacheManager.spec.js | 2 +- .../actions/displayIframeContent.spec.js | 2 +- .../specs/components/Personalization/topLevel/buildAlloy.js | 2 +- 24 files changed, 22 insertions(+), 25 deletions(-) rename src/{components/Personalization => }/constants/contentType.js (100%) rename src/{components/Personalization => }/constants/handle.js (100%) rename src/{components/Personalization => }/constants/schema.js (100%) diff --git a/src/components/DecisioningEngine/consequenceAdapters/inAppMessageConsequenceAdapter.js b/src/components/DecisioningEngine/consequenceAdapters/inAppMessageConsequenceAdapter.js index 852539857..13ca913ce 100644 --- a/src/components/DecisioningEngine/consequenceAdapters/inAppMessageConsequenceAdapter.js +++ b/src/components/DecisioningEngine/consequenceAdapters/inAppMessageConsequenceAdapter.js @@ -9,8 +9,8 @@ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTA OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -import { MESSAGE_IN_APP } from "../../Personalization/constants/schema"; -import { TEXT_HTML } from "../../Personalization/constants/contentType"; +import { MESSAGE_IN_APP } from "../../../constants/schema"; +import { TEXT_HTML } from "../../../constants/contentType"; export default (id, type, detail) => { const { html, mobileParameters } = detail; diff --git a/src/components/DecisioningEngine/createEvaluableRulesetPayload.js b/src/components/DecisioningEngine/createEvaluableRulesetPayload.js index dec1e8fb6..d1fa20995 100644 --- a/src/components/DecisioningEngine/createEvaluableRulesetPayload.js +++ b/src/components/DecisioningEngine/createEvaluableRulesetPayload.js @@ -10,10 +10,7 @@ OF ANY KIND, either express or implied. See the License for the specific languag governing permissions and limitations under the License. */ import RulesEngine from "@adobe/aep-rules-engine"; -import { - JSON_CONTENT_ITEM, - RULESET_ITEM -} from "../Personalization/constants/schema"; +import { JSON_CONTENT_ITEM, RULESET_ITEM } from "../../constants/schema"; import { DISPLAY } from "../../constants/eventType"; import { getActivityId } from "./utils"; diff --git a/src/components/DecisioningEngine/createOnResponseHandler.js b/src/components/DecisioningEngine/createOnResponseHandler.js index 89e6d43cd..60a11fb3c 100644 --- a/src/components/DecisioningEngine/createOnResponseHandler.js +++ b/src/components/DecisioningEngine/createOnResponseHandler.js @@ -9,7 +9,7 @@ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTA OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -import { PERSONALIZATION_DECISIONS_HANDLE } from "../Personalization/constants/handle"; +import { PERSONALIZATION_DECISIONS_HANDLE } from "../../constants/handle"; import flattenObject from "../../utils/flattenObject"; export default ({ diff --git a/src/components/Personalization/createApplyPropositions.js b/src/components/Personalization/createApplyPropositions.js index 023590291..0cd4181db 100644 --- a/src/components/Personalization/createApplyPropositions.js +++ b/src/components/Personalization/createApplyPropositions.js @@ -15,7 +15,7 @@ import { DOM_ACTION, HTML_CONTENT_ITEM, MESSAGE_IN_APP -} from "./constants/schema"; +} from "../../constants/schema"; import PAGE_WIDE_SCOPE from "../../constants/pageWideScope"; const SUPPORTED_SCHEMAS = [DOM_ACTION, HTML_CONTENT_ITEM, MESSAGE_IN_APP]; diff --git a/src/components/Personalization/createPersonalizationDetails.js b/src/components/Personalization/createPersonalizationDetails.js index a8fc25419..14bff1116 100644 --- a/src/components/Personalization/createPersonalizationDetails.js +++ b/src/components/Personalization/createPersonalizationDetails.js @@ -22,7 +22,7 @@ import { REDIRECT_ITEM, RULESET_ITEM, MESSAGE_FEED_ITEM -} from "./constants/schema"; +} from "../../constants/schema"; const addPageWideScope = scopes => { if (!includes(scopes, PAGE_WIDE_SCOPE)) { diff --git a/src/components/Personalization/createPreprocessors.js b/src/components/Personalization/createPreprocessors.js index 46dc3706c..71fc169e4 100644 --- a/src/components/Personalization/createPreprocessors.js +++ b/src/components/Personalization/createPreprocessors.js @@ -9,7 +9,7 @@ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTA OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -import { DOM_ACTION } from "./constants/schema"; +import { DOM_ACTION } from "../../constants/schema"; import remapHeadOffers from "./dom-actions/remapHeadOffers"; import remapCustomCodeOffers from "./dom-actions/remapCustomCodeOffers"; diff --git a/src/components/Personalization/createSubscribeMessageFeed.js b/src/components/Personalization/createSubscribeMessageFeed.js index bb67cb97d..ec39ca3f0 100644 --- a/src/components/Personalization/createSubscribeMessageFeed.js +++ b/src/components/Personalization/createSubscribeMessageFeed.js @@ -14,7 +14,7 @@ import { objectOf, string } from "../../utils/validation"; -import { MESSAGE_FEED_ITEM } from "./constants/schema"; +import { MESSAGE_FEED_ITEM } from "../../constants/schema"; import { DISPLAY, INTERACT } from "../../constants/eventType"; const validateOptions = ({ options }) => { diff --git a/src/components/Personalization/createViewCacheManager.js b/src/components/Personalization/createViewCacheManager.js index 896726907..a69913017 100644 --- a/src/components/Personalization/createViewCacheManager.js +++ b/src/components/Personalization/createViewCacheManager.js @@ -12,7 +12,7 @@ governing permissions and limitations under the License. import { assign, groupBy } from "../../utils"; import defer from "../../utils/defer"; -import { DEFAULT_CONTENT_ITEM } from "./constants/schema"; +import { DEFAULT_CONTENT_ITEM } from "../../constants/schema"; export default ({ createProposition }) => { const viewStorage = {}; diff --git a/src/components/Personalization/handlers/createProcessInAppMessage.js b/src/components/Personalization/handlers/createProcessInAppMessage.js index 41444a3d8..ba65ff808 100644 --- a/src/components/Personalization/handlers/createProcessInAppMessage.js +++ b/src/components/Personalization/handlers/createProcessInAppMessage.js @@ -9,7 +9,7 @@ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTA OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -import { APPLICATION_JSON } from "../constants/contentType"; +import { APPLICATION_JSON } from "../../../constants/contentType"; const DEFAULT_CONTENT = "defaultContent"; diff --git a/src/components/Personalization/in-app-message-actions/actions/displayIframeContent.js b/src/components/Personalization/in-app-message-actions/actions/displayIframeContent.js index 2478bbb63..def42ee42 100644 --- a/src/components/Personalization/in-app-message-actions/actions/displayIframeContent.js +++ b/src/components/Personalization/in-app-message-actions/actions/displayIframeContent.js @@ -12,7 +12,7 @@ governing permissions and limitations under the License. import { getNonce } from "../../dom-actions/dom"; import { parseAnchor } from "../utils"; -import { TEXT_HTML } from "../../constants/contentType"; +import { TEXT_HTML } from "../../../../constants/contentType"; import { assign } from "../../../../utils"; import { getEventType } from "../../../../constants/propositionEventType"; import { createNode, removeNode } from "../../../../utils/dom"; diff --git a/src/components/Personalization/index.js b/src/components/Personalization/index.js index 98ce046ea..39c67e7bd 100644 --- a/src/components/Personalization/index.js +++ b/src/components/Personalization/index.js @@ -32,7 +32,7 @@ import createPreprocess from "./dom-actions/createPreprocess"; import injectCreateProposition from "./handlers/injectCreateProposition"; import createAsyncArray from "./utils/createAsyncArray"; import createPendingNotificationsHandler from "./createPendingNotificationsHandler"; -import * as schema from "./constants/schema"; +import * as schema from "../../constants/schema"; import processDefaultContent from "./handlers/processDefaultContent"; import { isPageWideSurface } from "./utils/surfaceUtils"; import createProcessDomAction from "./handlers/createProcessDomAction"; diff --git a/src/components/Personalization/constants/contentType.js b/src/constants/contentType.js similarity index 100% rename from src/components/Personalization/constants/contentType.js rename to src/constants/contentType.js diff --git a/src/components/Personalization/constants/handle.js b/src/constants/handle.js similarity index 100% rename from src/components/Personalization/constants/handle.js rename to src/constants/handle.js diff --git a/src/components/Personalization/constants/schema.js b/src/constants/schema.js similarity index 100% rename from src/components/Personalization/constants/schema.js rename to src/constants/schema.js diff --git a/test/unit/specs/components/DecisioningEngine/consequenceAdapters/inAppMessageConsequenceAdapter.spec.js b/test/unit/specs/components/DecisioningEngine/consequenceAdapters/inAppMessageConsequenceAdapter.spec.js index 348a033fd..9c080e456 100644 --- a/test/unit/specs/components/DecisioningEngine/consequenceAdapters/inAppMessageConsequenceAdapter.spec.js +++ b/test/unit/specs/components/DecisioningEngine/consequenceAdapters/inAppMessageConsequenceAdapter.spec.js @@ -10,7 +10,7 @@ OF ANY KIND, either express or implied. See the License for the specific languag governing permissions and limitations under the License. */ import inAppMessageConsequenceAdapter from "../../../../../../src/components/DecisioningEngine/consequenceAdapters/inAppMessageConsequenceAdapter"; -import { TEXT_HTML } from "../../../../../../src/components/Personalization/constants/contentType"; +import { TEXT_HTML } from "../../../../../../src/constants/contentType"; describe("DecisioningEngine:inAppMessageConsequenceAdapter", () => { it("handles cjmiam", () => { diff --git a/test/unit/specs/components/DecisioningEngine/consequenceAdapters/schemaTypeConsequenceAdapter.spec.js b/test/unit/specs/components/DecisioningEngine/consequenceAdapters/schemaTypeConsequenceAdapter.spec.js index 3bd75efdf..26c7e4ff1 100644 --- a/test/unit/specs/components/DecisioningEngine/consequenceAdapters/schemaTypeConsequenceAdapter.spec.js +++ b/test/unit/specs/components/DecisioningEngine/consequenceAdapters/schemaTypeConsequenceAdapter.spec.js @@ -11,7 +11,7 @@ governing permissions and limitations under the License. */ import schemaTypeConsequenceAdapter from "../../../../../../src/components/DecisioningEngine/consequenceAdapters/schemaTypeConsequenceAdapter"; -import { TEXT_HTML } from "../../../../../../src/components/Personalization/constants/contentType"; +import { TEXT_HTML } from "../../../../../../src/constants/contentType"; describe("DecisioningEngine:schemaTypeConsequenceAdapter", () => { it("handles schema", () => { diff --git a/test/unit/specs/components/DecisioningEngine/createConsequenceAdapter.spec.js b/test/unit/specs/components/DecisioningEngine/createConsequenceAdapter.spec.js index 5a7e25c95..e48491720 100644 --- a/test/unit/specs/components/DecisioningEngine/createConsequenceAdapter.spec.js +++ b/test/unit/specs/components/DecisioningEngine/createConsequenceAdapter.spec.js @@ -10,7 +10,7 @@ OF ANY KIND, either express or implied. See the License for the specific languag governing permissions and limitations under the License. */ import createConsequenceAdapter from "../../../../../src/components/DecisioningEngine/createConsequenceAdapter"; -import { TEXT_HTML } from "../../../../../src/components/Personalization/constants/contentType"; +import { TEXT_HTML } from "../../../../../src/constants/contentType"; describe("DecisioningEngine:createConsequenceAdapter", () => { const ADAPTED_CONSEQUENCE = { diff --git a/test/unit/specs/components/DecisioningEngine/createSubscribeRulesetItems.spec.js b/test/unit/specs/components/DecisioningEngine/createSubscribeRulesetItems.spec.js index 24c3c0b71..183ac1394 100644 --- a/test/unit/specs/components/DecisioningEngine/createSubscribeRulesetItems.spec.js +++ b/test/unit/specs/components/DecisioningEngine/createSubscribeRulesetItems.spec.js @@ -11,7 +11,7 @@ governing permissions and limitations under the License. */ import { DOM_ACTION } from "@adobe/alloy/libEs5/components/Personalization/constants/schema"; import createSubscribeRulesetItems from "../../../../../src/components/DecisioningEngine/createSubscribeRulesetItems"; -import { MESSAGE_FEED_ITEM } from "../../../../../src/components/Personalization/constants/schema"; +import { MESSAGE_FEED_ITEM } from "../../../../../src/constants/schema"; describe("DecisioningEngine:subscribeRulesetItems", () => { let subscribeRulesetItems; diff --git a/test/unit/specs/components/Personalization/createOnDecisionHandler.spec.js b/test/unit/specs/components/Personalization/createOnDecisionHandler.spec.js index 2a7596047..e9e5d5e7f 100644 --- a/test/unit/specs/components/Personalization/createOnDecisionHandler.spec.js +++ b/test/unit/specs/components/Personalization/createOnDecisionHandler.spec.js @@ -10,7 +10,7 @@ OF ANY KIND, either express or implied. See the License for the specific languag governing permissions and limitations under the License. */ import createOnDecisionHandler from "../../../../../src/components/Personalization/createOnDecisionHandler"; -import { MESSAGE_FEED_ITEM } from "../../../../../src/components/Personalization/constants/schema"; +import { MESSAGE_FEED_ITEM } from "../../../../../src/constants/schema"; import injectCreateProposition from "../../../../../src/components/Personalization/handlers/injectCreateProposition"; describe("Personalization::createOnDecisionHandler", () => { diff --git a/test/unit/specs/components/Personalization/createPersonalizationDetails.spec.js b/test/unit/specs/components/Personalization/createPersonalizationDetails.spec.js index 0054ab452..1f0fa2b66 100644 --- a/test/unit/specs/components/Personalization/createPersonalizationDetails.spec.js +++ b/test/unit/specs/components/Personalization/createPersonalizationDetails.spec.js @@ -21,7 +21,7 @@ import { REDIRECT_ITEM, RULESET_ITEM, MESSAGE_FEED_ITEM -} from "../../../../../src/components/Personalization/constants/schema"; +} from "../../../../../src/constants/schema"; describe("Personalization::createPersonalizationDetails", () => { const TEST_SURFACE = "web://alloy.test.com/test/page/1"; diff --git a/test/unit/specs/components/Personalization/createSubscribeMessageFeed.spec.js b/test/unit/specs/components/Personalization/createSubscribeMessageFeed.spec.js index 32c0716d9..5613df127 100644 --- a/test/unit/specs/components/Personalization/createSubscribeMessageFeed.spec.js +++ b/test/unit/specs/components/Personalization/createSubscribeMessageFeed.spec.js @@ -10,7 +10,7 @@ OF ANY KIND, either express or implied. See the License for the specific languag governing permissions and limitations under the License. */ import createSubscribeMessageFeed from "../../../../../src/components/Personalization/createSubscribeMessageFeed"; -import { MESSAGE_FEED_ITEM } from "../../../../../src/components/Personalization/constants/schema"; +import { MESSAGE_FEED_ITEM } from "../../../../../src/constants/schema"; describe("Personalization:subscribeMessageFeed", () => { let collect; diff --git a/test/unit/specs/components/Personalization/createViewCacheManager.spec.js b/test/unit/specs/components/Personalization/createViewCacheManager.spec.js index fcbf24cd0..f68d60f89 100644 --- a/test/unit/specs/components/Personalization/createViewCacheManager.spec.js +++ b/test/unit/specs/components/Personalization/createViewCacheManager.spec.js @@ -10,7 +10,7 @@ OF ANY KIND, either express or implied. See the License for the specific languag governing permissions and limitations under the License. */ -import { DEFAULT_CONTENT_ITEM } from "../../../../../src/components/Personalization/constants/schema"; +import { DEFAULT_CONTENT_ITEM } from "../../../../../src/constants/schema"; import createViewCacheManager from "../../../../../src/components/Personalization/createViewCacheManager"; describe("Personalization::createViewCacheManager", () => { diff --git a/test/unit/specs/components/Personalization/in-app-message-actions/actions/displayIframeContent.spec.js b/test/unit/specs/components/Personalization/in-app-message-actions/actions/displayIframeContent.spec.js index e03314eaa..6fcad30e4 100644 --- a/test/unit/specs/components/Personalization/in-app-message-actions/actions/displayIframeContent.spec.js +++ b/test/unit/specs/components/Personalization/in-app-message-actions/actions/displayIframeContent.spec.js @@ -18,7 +18,7 @@ import { import cleanUpDomChanges from "../../../../../helpers/cleanUpDomChanges"; import { getNonce } from "../../../../../../../src/components/Personalization/dom-actions/dom"; import { testResetCachedNonce } from "../../../../../../../src/components/Personalization/dom-actions/dom/getNonce"; -import { TEXT_HTML } from "../../../../../../../src/components/Personalization/constants/contentType"; +import { TEXT_HTML } from "../../../../../../../src/constants/contentType"; describe("DOM Actions on Iframe", () => { beforeEach(() => { diff --git a/test/unit/specs/components/Personalization/topLevel/buildAlloy.js b/test/unit/specs/components/Personalization/topLevel/buildAlloy.js index f5f90d087..689bd034a 100644 --- a/test/unit/specs/components/Personalization/topLevel/buildAlloy.js +++ b/test/unit/specs/components/Personalization/topLevel/buildAlloy.js @@ -31,7 +31,7 @@ import injectCreateProposition from "../../../../../../src/components/Personaliz import createProcessPropositions from "../../../../../../src/components/Personalization/handlers/createProcessPropositions"; import createAsyncArray from "../../../../../../src/components/Personalization/utils/createAsyncArray"; import createPendingNotificationsHandler from "../../../../../../src/components/Personalization/createPendingNotificationsHandler"; -import * as schema from "../../../../../../src/components/Personalization/constants/schema"; +import * as schema from "../../../../../../src/constants/schema"; import createProcessDomAction from "../../../../../../src/components/Personalization/handlers/createProcessDomAction"; import createProcessHtmlContent from "../../../../../../src/components/Personalization/handlers/createProcessHtmlContent"; import createProcessRedirect from "../../../../../../src/components/Personalization/handlers/createProcessRedirect"; From 46fd3ff156919fa68da11a562b9eda21be19cb33 Mon Sep 17 00:00:00 2001 From: Happy Shandilya Date: Tue, 17 Oct 2023 15:07:40 -0700 Subject: [PATCH 09/12] use alloy utils method includes --- .../in-app-message-actions/actions/displayIframeContent.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/components/Personalization/in-app-message-actions/actions/displayIframeContent.js b/src/components/Personalization/in-app-message-actions/actions/displayIframeContent.js index def42ee42..3bc361deb 100644 --- a/src/components/Personalization/in-app-message-actions/actions/displayIframeContent.js +++ b/src/components/Personalization/in-app-message-actions/actions/displayIframeContent.js @@ -13,7 +13,7 @@ governing permissions and limitations under the License. import { getNonce } from "../../dom-actions/dom"; import { parseAnchor } from "../utils"; import { TEXT_HTML } from "../../../../constants/contentType"; -import { assign } from "../../../../utils"; +import { assign, includes } from "../../../../utils"; import { getEventType } from "../../../../constants/propositionEventType"; import { createNode, removeNode } from "../../../../utils/dom"; @@ -200,11 +200,11 @@ const isValidWebParameters = webParameters => { const ids = Object.keys(webParameters); - if (!ids.includes(ALLOY_MESSAGING_CONTAINER_ID)) { + if (!includes(ids, ALLOY_MESSAGING_CONTAINER_ID)) { return false; } - if (!ids.includes(ALLOY_OVERLAY_CONTAINER_ID)) { + if (!includes(ids, ALLOY_OVERLAY_CONTAINER_ID)) { return false; } From c9bed070c254051bbb5f81358ae4356add297420 Mon Sep 17 00:00:00 2001 From: Happy Shandilya Date: Tue, 17 Oct 2023 15:39:21 -0700 Subject: [PATCH 10/12] use alloy utils method values, objectOf --- .../actions/displayIframeContent.js | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/components/Personalization/in-app-message-actions/actions/displayIframeContent.js b/src/components/Personalization/in-app-message-actions/actions/displayIframeContent.js index 3bc361deb..4602a229d 100644 --- a/src/components/Personalization/in-app-message-actions/actions/displayIframeContent.js +++ b/src/components/Personalization/in-app-message-actions/actions/displayIframeContent.js @@ -13,9 +13,10 @@ governing permissions and limitations under the License. import { getNonce } from "../../dom-actions/dom"; import { parseAnchor } from "../utils"; import { TEXT_HTML } from "../../../../constants/contentType"; -import { assign, includes } from "../../../../utils"; +import { assign, includes, values } from "../../../../utils"; import { getEventType } from "../../../../constants/propositionEventType"; import { createNode, removeNode } from "../../../../utils/dom"; +import { objectOf } from "../../../../utils/validation"; const ALLOY_MESSAGING_CONTAINER_ID = "alloy-messaging-container"; const ALLOY_OVERLAY_CONTAINER_ID = "alloy-overlay-container"; @@ -208,24 +209,19 @@ const isValidWebParameters = webParameters => { return false; } - const values = Object.values(webParameters); + const valuesArray = values(webParameters); - for (let i = 0; i < values.length; i += 1) { - if (!Object.prototype.hasOwnProperty.call(values[i], "style")) { + for (let i = 0; i < valuesArray.length; i += 1) { + if (!objectOf(valuesArray[i], "style")) { return false; } - if (!Object.prototype.hasOwnProperty.call(values[i], "params")) { + if (!objectOf(valuesArray[i], "params")) { return false; } for (let j = 0; j < REQUIRED_PARAMS.length; j += 1) { - if ( - !Object.prototype.hasOwnProperty.call( - values[i].params, - REQUIRED_PARAMS[j] - ) - ) { + if (!objectOf(valuesArray[i].params, REQUIRED_PARAMS[j])) { return false; } } From ba6acc6ce2a59b8f8419ffdc3737251df004333c Mon Sep 17 00:00:00 2001 From: Happy Shandilya Date: Wed, 18 Oct 2023 09:02:52 -0700 Subject: [PATCH 11/12] lint --- .../in-app-message-actions/actions/displayIframeContent.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/components/Personalization/in-app-message-actions/actions/displayIframeContent.js b/src/components/Personalization/in-app-message-actions/actions/displayIframeContent.js index 5e35623db..e149b7430 100644 --- a/src/components/Personalization/in-app-message-actions/actions/displayIframeContent.js +++ b/src/components/Personalization/in-app-message-actions/actions/displayIframeContent.js @@ -19,7 +19,6 @@ import { objectOf } from "../../../../utils/validation"; import { PropositionEventType } from "../../../../constants/propositionEventType"; import { INTERACT } from "../../../../constants/eventType"; - const ALLOY_MESSAGING_CONTAINER_ID = "alloy-messaging-container"; const ALLOY_OVERLAY_CONTAINER_ID = "alloy-overlay-container"; const ALLOY_IFRAME_ID = "alloy-content-iframe"; From e4e08d389be5f79fea913bc1412c5855b90cdda4 Mon Sep 17 00:00:00 2001 From: Happy Shandilya Date: Wed, 18 Oct 2023 12:28:49 -0700 Subject: [PATCH 12/12] removeElementById for readability --- .../actions/displayIframeContent.js | 20 ++++--------- .../in-app-message-actions/utils.js | 7 +++++ .../in-app-message-actions/utils.spec.js | 29 +++++++++++++++++++ 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/components/Personalization/in-app-message-actions/actions/displayIframeContent.js b/src/components/Personalization/in-app-message-actions/actions/displayIframeContent.js index e149b7430..ebde2d92a 100644 --- a/src/components/Personalization/in-app-message-actions/actions/displayIframeContent.js +++ b/src/components/Personalization/in-app-message-actions/actions/displayIframeContent.js @@ -11,10 +11,10 @@ governing permissions and limitations under the License. */ import { getNonce } from "../../dom-actions/dom"; -import { parseAnchor } from "../utils"; +import { parseAnchor, removeElementById } from "../utils"; import { TEXT_HTML } from "../../../../constants/contentType"; import { assign, includes, values } from "../../../../utils"; -import { createNode, removeNode } from "../../../../utils/dom"; +import { createNode } from "../../../../utils/dom"; import { objectOf } from "../../../../utils/validation"; import { PropositionEventType } from "../../../../constants/propositionEventType"; import { INTERACT } from "../../../../constants/eventType"; @@ -23,19 +23,11 @@ const ALLOY_MESSAGING_CONTAINER_ID = "alloy-messaging-container"; const ALLOY_OVERLAY_CONTAINER_ID = "alloy-overlay-container"; const ALLOY_IFRAME_ID = "alloy-content-iframe"; -export const dismissMessage = () => { - const elementIdsToRemove = [ - ALLOY_MESSAGING_CONTAINER_ID, - ALLOY_OVERLAY_CONTAINER_ID - ]; +const dismissMessage = () => + [ALLOY_MESSAGING_CONTAINER_ID, ALLOY_OVERLAY_CONTAINER_ID].forEach( + removeElementById + ); - elementIdsToRemove.forEach(id => { - const element = document.getElementById(id); - if (element) { - removeNode(element); - } - }); -}; const setWindowLocationHref = link => { window.location.href = link; }; diff --git a/src/components/Personalization/in-app-message-actions/utils.js b/src/components/Personalization/in-app-message-actions/utils.js index 5660f3402..09c274fd9 100644 --- a/src/components/Personalization/in-app-message-actions/utils.js +++ b/src/components/Personalization/in-app-message-actions/utils.js @@ -10,7 +10,14 @@ OF ANY KIND, either express or implied. See the License for the specific languag governing permissions and limitations under the License. */ import { startsWith } from "../../../utils"; +import { removeNode } from "../../../utils/dom"; +export const removeElementById = id => { + const element = document.getElementById(id); + if (element) { + removeNode(element); + } +}; export const parseAnchor = anchor => { const nothing = {}; diff --git a/test/unit/specs/components/Personalization/in-app-message-actions/utils.spec.js b/test/unit/specs/components/Personalization/in-app-message-actions/utils.spec.js index 26ef92385..28e375686 100644 --- a/test/unit/specs/components/Personalization/in-app-message-actions/utils.spec.js +++ b/test/unit/specs/components/Personalization/in-app-message-actions/utils.spec.js @@ -9,3 +9,32 @@ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTA OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ + +import { removeElementById } from "../../../../../../src/components/Personalization/in-app-message-actions/utils"; + +describe("removeElementById", () => { + beforeEach(() => { + document.body.innerHTML = ` +
+`; + }); + + it("should remove an element when it exists", () => { + const elementId = "test-element"; + const element = document.getElementById(elementId); + + expect(element).toBeTruthy(); + + removeElementById(elementId); + + expect(document.getElementById(elementId)).toBeNull(); + }); + + it("should do nothing when the element does not exist", () => { + const nonExistentId = "non-existent-element"; + + removeElementById(nonExistentId); + + expect(document.getElementById(nonExistentId)).toBeNull(); + }); +});