Skip to content

Commit

Permalink
Code Review Fixes (#1052)
Browse files Browse the repository at this point in the history
* use existing Alloy DOM utils

* removed unused utils method

* renamed to "test:unit:debug"

* added a try/catch to  handle potential JSON parsing errors

* removed ensureSchemaBasedRulesetConsequences

* using shorthand object property notation

* new line

* constants shared between components moved to the shared src/constants module

* use alloy utils method includes

* use alloy utils method values, objectOf

* lint

* removeElementById for readability

---------

Co-authored-by: Jason Waters <[email protected]>
  • Loading branch information
shandilya3 and jasonwaters authored Oct 18, 2023
1 parent f738fcd commit da6857b
Show file tree
Hide file tree
Showing 30 changed files with 60 additions and 129 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
}
.element-spacing {
margin-right: 10px;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 14 additions & 11 deletions src/components/DecisioningEngine/createEvaluableRulesetPayload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -31,14 +28,20 @@ const isRulesetItem = item => {
return false;
}

const content =
typeof data.content === "string" ? JSON.parse(data.content) : data.content;
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")
);
return (
content &&
Object.prototype.hasOwnProperty.call(content, "version") &&
Object.prototype.hasOwnProperty.call(content, "rules")
);
} catch (error) {
return false;
}
};

export default (payload, eventRegistry, decisionHistory) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ({
Expand Down
3 changes: 0 additions & 3 deletions src/components/DecisioningEngine/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -56,8 +55,6 @@ const createDecisioningEngine = ({ config, createNamespacedStorage }) => {
decisionContext = {},
onResponse = noop
}) {
ensureSchemaBasedRulesetConsequences(event);

onResponse(
createOnResponseHandler({
renderDecisions,
Expand Down
11 changes: 0 additions & 11 deletions src/components/DecisioningEngine/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/components/Personalization/createApplyPropositions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion src/components/Personalization/createPreprocessors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/Personalization/createViewCacheManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ governing permissions and limitations under the License.
*/

import { getNonce } from "../../dom-actions/dom";
import { createElement, parseAnchor, removeElementById } from "../utils";
import { TEXT_HTML } from "../../constants/contentType";
import { assign } from "../../../../utils";
import { parseAnchor, removeElementById } from "../utils";
import { TEXT_HTML } from "../../../../constants/contentType";
import { assign, includes, values } from "../../../../utils";
import { createNode } from "../../../../utils/dom";
import { objectOf } from "../../../../utils/validation";
import { PropositionEventType } from "../../../../constants/propositionEventType";
import { INTERACT } from "../../../../constants/eventType";

Expand All @@ -27,7 +29,7 @@ const dismissMessage = () =>
);

const setWindowLocationHref = link => {
window.location.assign(link);
window.location.href = link;
};

export const createIframeClickHandler = (
Expand Down Expand Up @@ -74,11 +76,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;
Expand Down Expand Up @@ -190,32 +194,27 @@ 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;
}

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;
}
}
Expand Down Expand Up @@ -272,9 +271,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);
Expand Down
31 changes: 2 additions & 29 deletions src/components/Personalization/in-app-message-actions/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +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";

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();
});
};
import { removeNode } from "../../../utils/dom";

export const removeElementById = id => {
const element = document.getElementById(id);
if (element) {
element.remove();
removeNode(element);
}
};

export const parseAnchor = anchor => {
const nothing = {};

Expand Down Expand Up @@ -74,8 +52,3 @@ export const parseAnchor = anchor => {
uuid
};
};
export const createElement = elementTagId => {
const element = document.createElement("div");
element.id = elementTagId;
return element;
};
2 changes: 1 addition & 1 deletion src/components/Personalization/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import remapHeadOffers from "./dom-actions/remapHeadOffers";
import createPreprocess from "./dom-actions/createPreprocess";
import injectCreateProposition from "./handlers/injectCreateProposition";
import createAsyncArray from "./utils/createAsyncArray";
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";
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 0 additions & 30 deletions test/unit/specs/components/DecisioningEngine/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
Loading

0 comments on commit da6857b

Please sign in to comment.