Skip to content

Commit

Permalink
release: SDK 4.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaud-roland committed Jan 8, 2024
1 parent ec5d472 commit 6b9d442
Show file tree
Hide file tree
Showing 71 changed files with 4,692 additions and 2,853 deletions.
101 changes: 101 additions & 0 deletions Sources/__tests__/data-collection.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/* eslint-env jest */
// @ts-nocheck

import { expect, jest } from "@jest/globals";

import BaseSdk from "../lib/dom/sdk-impl/sdk-base";
import { fillDefaultDataCollectionConfiguration, serializeDataCollectionConfig } from "../lib/shared/data-collection";
import { InternalSDKEvent } from "../lib/shared/event/event-names";
import EventTracker from "../lib/shared/event/event-tracker";
import { ProfileKeys } from "../lib/shared/parameters/keys.profile";
import { ProfilePersistence } from "../lib/shared/persistence/profile";
jest.mock("com.batch.shared/persistence/profile");
jest.mock("../lib/shared/event/event-tracker");

describe("Data Collection configuration Tests", () => {
it("Test default data collection cases", () => {
const defaultDataCollection = {
geoIP: false,
};
// When given conf is null, default conf is returned
expect(fillDefaultDataCollectionConfiguration(null)).toEqual(defaultDataCollection);

// When given conf is undefined, default conf is returned
expect(fillDefaultDataCollectionConfiguration(undefined)).toEqual(defaultDataCollection);

// When given conf is empty, default conf is returned
expect(fillDefaultDataCollectionConfiguration({})).toEqual(defaultDataCollection);

// When a conf has been set up, default conf is NOT returned
expect(fillDefaultDataCollectionConfiguration({ geoIP: true })).toEqual({ geoIP: true });
});
it("Test data collection serialization", () => {
expect(serializeDataCollectionConfig({ geoIP: true })).toEqual({ geoip: true });
});
});

describe("Event DataCollectionChanged Tests", () => {
beforeEach(() => {
EventTracker.mockClear();
});
it("Should not be triggered when lastConfig is null", async () => {
const sdk = new BaseSdk();
await sdk.setup({
apiKey: "DEV12345",
authKey: "1.test",
defaultDataCollection: {
geoIP: false,
},
});
const mockedEventTracker: EventTracker = EventTracker.mock.instances[0];
const expectedTrackedEvent = expect.objectContaining({
name: InternalSDKEvent.DataCollectionChanged,
});
expect(mockedEventTracker.track).not.toHaveBeenCalledWith(expectedTrackedEvent);
});

it("Should be triggered with new default data collection settings", async () => {
const sdk = new BaseSdk();
const profilePersistence = await ProfilePersistence.getInstance();
await profilePersistence.setData(ProfileKeys.LastConfiguration, {
defaultDataCollection: {
geoIP: false,
},
});
await sdk.setup({
apiKey: "DEV12345",
authKey: "1.test",
defaultDataCollection: {
geoIP: true,
},
});
const mockedEventTracker: EventTracker = EventTracker.mock.instances[0];
const expectedTrackedEvent = expect.objectContaining({
name: InternalSDKEvent.DataCollectionChanged,
params: { geoip: true },
});
expect(mockedEventTracker.track).toHaveBeenCalledWith(expectedTrackedEvent);
});

it("Should not be triggered when data collection settings has not changed", async () => {
const sdk = new BaseSdk();
const profilePersistence = await ProfilePersistence.getInstance();
await profilePersistence.setData(ProfileKeys.LastConfiguration, {
defaultDataCollection: {
geoIP: true,
},
});
await sdk.setup({
apiKey: "DEV12345",
authKey: "1.test",
defaultDataCollection: {
geoIP: true,
},
});
const mockedEventTracker: EventTracker = EventTracker.mock.instances[0];
const expectedTrackedEvent = expect.objectContaining({
name: InternalSDKEvent.DataCollectionChanged,
});
expect(mockedEventTracker.track).not.toHaveBeenCalledWith(expectedTrackedEvent);
});
});
50 changes: 30 additions & 20 deletions Sources/__tests__/event-data.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EventData, TypedEventAttributeType } from "com.batch.shared/user/event-data";
import { EventData } from "../lib/shared/event/event-data";
import { TypedEventAttributeType } from "../lib/shared/event/event-types";

describe("Event Data to internal representation", () => {
it("when params are empty, empty tags and attributes should be returned, not tags ", () => {
Expand All @@ -13,7 +14,7 @@ describe("Event Data to internal representation", () => {

describe("Event Data: Label", () => {
it("should return the label", () => {
const eventData = new EventData({ label: "label" });
const eventData = new EventData({ attributes: { $label: "label" } });

expect(eventData).toEqual({
attributes: {},
Expand All @@ -23,7 +24,7 @@ describe("Event Data: Label", () => {
});

it("should not return the label when is equal to null", () => {
const eventData = new EventData({ label: undefined });
const eventData = new EventData({ attributes: { $label: undefined } });

expect(eventData).toEqual({
attributes: {},
Expand All @@ -33,10 +34,12 @@ describe("Event Data: Label", () => {

it("should not return the label when longer than 200 characters", () => {
const eventData = new EventData({
label:
"pneumonoultramicroscopicsilicovolcanoconiosispneumonoultramicroscopicsilicovo" +
"lcanoconiosispneumonoultramicroscopicsilicovolcanoconiosispneumonoultramicrosc" +
"opicsilicovolcanoconiosispneumonoultramicroscopicsilicovolcanoconiosis",
attributes: {
$label:
"pneumonoultramicroscopicsilicovolcanoconiosispneumonoultramicroscopicsilicovo" +
"lcanoconiosispneumonoultramicroscopicsilicovolcanoconiosispneumonoultramicrosc" +
"opicsilicovolcanoconiosispneumonoultramicroscopicsilicovolcanoconiosis",
},
});

expect(eventData).toEqual({
Expand All @@ -46,10 +49,7 @@ describe("Event Data: Label", () => {
});

it("should not return the label when it is not a STRING", () => {
const eventData = new EventData({
label: 3,
});

const eventData = new EventData({ attributes: { $label: 3 } });
expect(eventData).toEqual({
attributes: {},
tags: [],
Expand All @@ -59,38 +59,40 @@ describe("Event Data: Label", () => {

describe("Event Data: Tags", () => {
it("should return all tags excepts duplicates", () => {
const { tags } = new EventData({ tags: ["sports", "fruits", "foot"] });
const { tags } = new EventData({ attributes: { $tags: ["sports", "fruits", "foot"] } });

expect(tags).toEqual(["sports", "fruits", "foot"]);
});

it("should return 10 tags maximum", () => {
const { tags } = new EventData({ tags: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"] });
const { tags } = new EventData({ attributes: { $tags: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"] } });

expect(tags).toHaveLength(10);
});

it("should return the lowercase tag", () => {
const { tags } = new EventData({ tags: ["TAG"] });
const { tags } = new EventData({ attributes: { $tags: ["TAG"] } });

expect(tags).toEqual(["tag"]);
});

it("should not return the tag when it undefined", () => {
const { tags } = new EventData({ tags: undefined });
const { tags } = new EventData({ attributes: { $tags: undefined } });

expect(tags).toEqual([]);
});

it("should not return the tag when it's not a string", () => {
const { tags } = new EventData({ tags: [1, "foot"] });
const { tags } = new EventData({ attributes: { $tags: [1, "foot"] } });

expect(tags).toEqual(["foot"]);
});

it("should not return the tag when it's longer than 64 characters", () => {
const { tags } = new EventData({
tags: ["pneumonoultramicroscopicsilicovolcanoconiosispneumonoultramicroscopicsilicovolcanoconiosis"],
attributes: {
$tags: ["pneumonoultramicroscopicsilicovolcanoconiosispneumonoultramicroscopicsilicovolcanoconiosis"],
},
});

expect(tags).toEqual([]);
Expand Down Expand Up @@ -122,6 +124,8 @@ describe("Event Data: Attributes", () => {
key19: "value",
key20: "value",
key21: "value",
$label: "label",
$tags: ["michel", "c'est le bresil"],
},
});

Expand All @@ -138,10 +142,13 @@ describe("Event Data: Attributes", () => {
expect(attributes).toEqual({});
});

it("should not return the attribute when it's longer than 64 characters", () => {
it("should not return the attribute when it's longer than 200 characters", () => {
const { attributes } = new EventData({
attributes: {
key: "pneumonoultramicroscopicsilicovolcanoconiosispneumonoultramicroscopicsilicovolcanoconiosis",
key:
"pneumonoultramicroscopicsilicovolcanoconiosispneumonoultramicroscopicsilicovolcanoconiosi" +
"pneumonoultramicroscopicsilicovolcanoconiosispneumonoultramicroscopicsilicovolcanoconiosiss" +
"silicovolcanoconiosis",
},
});

Expand Down Expand Up @@ -185,7 +192,10 @@ describe("Event Data: Attributes", () => {
type: TypedEventAttributeType.STRING,
value: "value",
},
keyInError: "pneumonoultramicroscopicsilicovolcanoconiosispneumonoultramicroscopicsilicovolcanoconiosis",
keyInError:
"pneumonoultramicroscopicsilicovolcanoconiosispneumonoultramicroscopicsilicovolcanoconiosis" +
"pneumonoultramicroscopicsilicovolcanoconiosispneumonoultramicroscopicsilicovolcanoconiosis" +
"covolcanoconiosisazert",
absurdity: {
type: TypedEventAttributeType.STRING,
value: new Date(),
Expand Down
6 changes: 3 additions & 3 deletions Sources/__tests__/parameter-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ beforeAll(() => {
});

test("can get a single param", () => {
return store.getParameterValue(keysByProvider.system.SDKAPILevel).then(v => expect(v).toEqual("1"));
return store.getParameterValue(keysByProvider.system.DeviceDate).then(v => expect(typeof v).toBe("string"));
});

test("can get multiple params", done => {
store
.getParametersValues([keysByProvider.system.SDKAPILevel, keysByProvider.system.DeviceDate])
.getParametersValues([keysByProvider.system.DeviceDate, keysByProvider.system.DeviceTimezone])
.then(response => {
expect(response[keysByProvider.system.SDKAPILevel]).toBe("1");
expect(typeof response.da).toBe("string");
expect(typeof response.dtz).toBe("string");
return done();
})
.catch(error => done(error));
Expand Down
24 changes: 14 additions & 10 deletions Sources/__tests__/sdk-base.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
/* eslint-env jest */
// @ts-nocheck

import { expect, jest } from "@jest/globals";
jest.mock("com.batch.shared/persistence/profile");
jest.mock("com.batch.shared/persistence/session");
jest.mock("com.batch.shared/persistence/user-data");

import BaseSdk from "com.batch.dom/sdk-impl/sdk-base";
import { keysByProvider } from "com.batch.shared/parameters/keys";
import ParameterStore from "com.batch.shared/parameters/parameter-store";
import { ProfilePersistence } from "com.batch.shared/persistence/profile";
import Session from "com.batch.shared/persistence/session";

import { LocalEventBus } from "../lib/shared/local-event-bus";
import LocalSDKEvent from "../lib/shared/local-sdk-events";

const sdk = new BaseSdk();

// TODO: Find a better to mock this
Expand All @@ -18,7 +23,6 @@ window.Notification = {
return "granted";
},
};

beforeAll(async () => {
await sdk.setup({
apiKey: "DEV12345",
Expand All @@ -29,7 +33,7 @@ beforeAll(async () => {
await sdk.start();
});

test("is correctly isntancied", () => {
test("is correctly instanced", () => {
expect(sdk instanceof BaseSdk).toBe(true);
expect(sdk.parameterStore instanceof ParameterStore).toBe(true);
expect(sdk.parameterStore.providers.session.storage instanceof Session).toBe(true);
Expand All @@ -49,12 +53,12 @@ test("it has a session id", () =>
expect(val.length).toBe(36);
}));

test("it can write and read a custom identifier", done => {
sdk.setCustomUserID("toto").then(resp => {
expect(resp).toBe("toto");
sdk.getCustomUserID().then(nextVal => {
expect(nextVal).toBe("toto");
done();
});
});
test("it can write and custom identifier", async () => {
const profile = await sdk.profile();
const profilePersistence = await ProfilePersistence.getInstance();
await profile.identify({ customId: "test_custom_identifier" });
const cus = await profilePersistence.getData("cus");
expect(cus).toBe("test_custom_identifier");
// cleaning
await profilePersistence.removeData("cus");
});
Loading

0 comments on commit 6b9d442

Please sign in to comment.