Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

few test #1067

Merged
merged 5 commits into from
Oct 26, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});
Loading