Skip to content

Commit

Permalink
few test
Browse files Browse the repository at this point in the history
  • Loading branch information
shandilya3 committed Oct 26, 2023
1 parent b5a39e0 commit 31d3248
Showing 1 changed file with 64 additions and 18 deletions.
82 changes: 64 additions & 18 deletions test/unit/specs/components/DecisioningEngine/index.spec.js
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 createDecisioningEngine from "../../../../../src/components/DecisioningEngine/index";
import { defer, injectStorage } from "../../../../../src/utils";
import { defer } from "../../../../../src/utils";
import {
mockRulesetResponseWithCondition,
proposition
Expand All @@ -20,39 +20,54 @@ describe("createDecisioningEngine:commands:evaluateRulesets", () => {
let mergeData;
let mockEvent;
let onResponseHandler;
let decisioningEngine;
let awaitConsentDeferred;
let consent;
let persistentStorage;
let createNamespacedStorage;

beforeEach(async () => {
beforeEach(() => {
mergeData = jasmine.createSpy();
awaitConsentDeferred = defer();
consent = jasmine.createSpyObj("consent", {
awaitConsent: awaitConsentDeferred.promise
});
const config = {
orgId: "exampleOrgId",
personalizationStorageEnabled: true
};
window.referrer =
"https://www.google.com/search?q=adobe+journey+optimizer&oq=adobe+journey+optimizer";
const createNamespacedStorage = injectStorage(window);
decisioningEngine = createDecisioningEngine({
config,
createNamespacedStorage,
consent
persistentStorage = jasmine.createSpyObj("persistentStorage", [
"getItem",
"setItem",
"clear"
]);
createNamespacedStorage = jasmine.createSpy().and.returnValue({
persistent: persistentStorage
});
mockEvent = {
getContent: () => ({}),
hasQuery: () => true,
getViewName: () => undefined,
mergeData
};
decisioningEngine.lifecycle.onComponentsRegistered(() => {});
await awaitConsentDeferred.resolve();
});

it("should run the evaluateRulesets command and satisfy the rule based on global context", () => {
const setUpDecisionEngine = ({ personalizationStorageEnabled }) => {
const config = {
orgId: "exampleOrgId",
personalizationStorageEnabled
};
const decisioningEngine = createDecisioningEngine({
config,
createNamespacedStorage,
consent
});
decisioningEngine.lifecycle.onComponentsRegistered(() => {});
return decisioningEngine;
};

it("should run the evaluateRulesets command and satisfy the rule based on global context", async () => {
const decisioningEngine = setUpDecisionEngine({
personalizationStorageEnabled: true
});
await awaitConsentDeferred.resolve();
onResponseHandler = onResponse => {
onResponse({
response: mockRulesetResponseWithCondition({
Expand All @@ -77,7 +92,11 @@ describe("createDecisioningEngine:commands:evaluateRulesets", () => {
});
});

it("should run the evaluateRulesets command and does not satisfy rule due to unmatched global context", () => {
it("should run the evaluateRulesets command and does not satisfy rule due to unmatched global context", async () => {
const decisioningEngine = setUpDecisionEngine({
personalizationStorageEnabled: true
});
await awaitConsentDeferred.resolve();
onResponseHandler = onResponse => {
onResponse({
response: mockRulesetResponseWithCondition({
Expand All @@ -102,7 +121,11 @@ describe("createDecisioningEngine:commands:evaluateRulesets", () => {
});
});

it("should run the evaluateRulesets command and return propositions with renderDecisions true", () => {
it("should run the evaluateRulesets command and return propositions with renderDecisions true", async () => {
const decisioningEngine = setUpDecisionEngine({
personalizationStorageEnabled: true
});
await awaitConsentDeferred.resolve();
onResponseHandler = onResponse => {
onResponse({
response: mockRulesetResponseWithCondition({
Expand All @@ -127,7 +150,11 @@ describe("createDecisioningEngine:commands:evaluateRulesets", () => {
});
});

it("should run the evaluateRulesets command returns propositions with renderDecisions false", () => {
it("should run the evaluateRulesets command returns propositions with renderDecisions false", async () => {
const decisioningEngine = setUpDecisionEngine({
personalizationStorageEnabled: true
});
await awaitConsentDeferred.resolve();
onResponseHandler = onResponse => {
onResponse({
response: mockRulesetResponseWithCondition({
Expand All @@ -151,4 +178,23 @@ describe("createDecisioningEngine:commands:evaluateRulesets", () => {
propositions: [proposition]
});
});
it("should clear the local storage when personalizationStorageEnabled is false", async () => {
setUpDecisionEngine({ personalizationStorageEnabled: false });
await awaitConsentDeferred.resolve();
expect(persistentStorage.clear).toHaveBeenCalled();
});

it("should set eventRegistry storage when consent is obtained", async () => {
setUpDecisionEngine({ personalizationStorageEnabled: true });
await awaitConsentDeferred.resolve();
await expectAsync(awaitConsentDeferred.promise).toBeResolved();
expect(persistentStorage.getItem).toHaveBeenCalled();
});

it("should clear the local storage when consent is not obtained", async () => {
setUpDecisionEngine({ personalizationStorageEnabled: true });
await awaitConsentDeferred.reject();
await expectAsync(awaitConsentDeferred.promise).toBeRejected();
expect(persistentStorage.clear).toHaveBeenCalled();
});
});

0 comments on commit 31d3248

Please sign in to comment.