Skip to content

Use expo-crypto for UUID on Expo, expose useGzipCompression option and fix JavaScript mode People flushing issue #288

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

Merged
merged 22 commits into from
May 30, 2025
Merged
Show file tree
Hide file tree
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
Prev Previous commit
Next Next commit
mock expo-crypto
  • Loading branch information
jaredmixpanel committed May 17, 2025
commit d84d59138dedb6b55e05e21ba821d1f27b079948
7 changes: 7 additions & 0 deletions __tests__/jest_setup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import * as ReactNative from "react-native";
import { jest } from "@jest/globals";

jest.mock("expo-crypto", () => ({
randomUUID: jest.fn(
() => "mocked-uuid-string-" + Math.random().toString(36).substring(2, 15)
), // Provides a somewhat unique mock UUID
}));

jest.mock("mixpanel-react-native/javascript/mixpanel-storage", () => {
return {
Expand Down
95 changes: 48 additions & 47 deletions __tests__/main.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {MixpanelType} from "mixpanel-react-native/javascript/mixpanel-constants";
import {exp} from "react-native/Libraries/Animated/src/Easing";
import {get} from "react-native/Libraries/Utilities/PixelRatio";
import { MixpanelType } from "mixpanel-react-native/javascript/mixpanel-constants";
import { exp } from "react-native/Libraries/Animated/src/Easing";
import { get } from "react-native/Libraries/Utilities/PixelRatio";

jest.mock("mixpanel-react-native/javascript/mixpanel-core", () => ({
MixpanelCore: jest.fn().mockImplementation(() => ({
initialize: jest.fn(),
startProcessingQueue: jest.fn(),
addToMixpanelQueue: jest.fn(),
flush: jest.fn(),
identifyUserQueue: jest.fn(),
})),
}));

Expand Down Expand Up @@ -117,8 +118,8 @@ describe("MixpanelMain", () => {
beforeEach(() => {
jest.clearAllMocks();
jest.isolateModules(async () => {
const MixpanelMain = require("mixpanel-react-native/javascript/mixpanel-main")
.default;
const MixpanelMain =
require("mixpanel-react-native/javascript/mixpanel-main").default;
mixpanelMain = new MixpanelMain(token);
MixpanelConfig.getInstance().getLoggingEnabled.mockReturnValue(true);
});
Expand All @@ -128,7 +129,7 @@ describe("MixpanelMain", () => {
it("should initialize properly", async () => {
const trackAutomaticEvents = false;
const optOutTrackingDefault = false;
const superProperties = {superProp1: "value1", superProp2: "value2"};
const superProperties = { superProp1: "value1", superProp2: "value2" };
const serverURL = "https://api.mixpanel.com";

await mixpanelMain.initialize(
Expand Down Expand Up @@ -156,10 +157,9 @@ describe("MixpanelMain", () => {
it("should not track if initialize with optOutTrackingDefault being true", async () => {
const trackAutomaticEvents = false;
const optOutTrackingDefault = true;
const superProperties = {superProp1: "value1", superProp2: "value2"};
const superProperties = { superProp1: "value1", superProp2: "value2" };
const serverURL = "https://api.mixpanel.com";


await mixpanelMain.initialize(
token,
trackAutomaticEvents,
Expand All @@ -169,11 +169,12 @@ describe("MixpanelMain", () => {
);

const eventName = "Test Event";
const eventProperties = {prop1: "value1", prop2: "value2"};
const eventProperties = { prop1: "value1", prop2: "value2" };

expect(
mixpanelMain.mixpanelPersistent.updateOptedOut
).toHaveBeenCalledWith(token, true);
expect(mixpanelMain.mixpanelPersistent.updateOptedOut).toHaveBeenCalledWith(
token,
true
);

mixpanelMain.mixpanelPersistent.getOptedOut.mockReturnValue(true);
await mixpanelMain.track(token, eventName, eventProperties);
Expand All @@ -183,7 +184,7 @@ describe("MixpanelMain", () => {
it("should track if initialize with optOutTrackingDefault being false", async () => {
const trackAutomaticEvents = false;
const optOutTrackingDefault = false;
const superProperties = {superProp1: "value1", superProp2: "value2"};
const superProperties = { superProp1: "value1", superProp2: "value2" };
const serverURL = "https://api.mixpanel.com";

await mixpanelMain.initialize(
Expand All @@ -195,14 +196,14 @@ describe("MixpanelMain", () => {
);
mixpanelMain.setLoggingEnabled(token, true);
const eventName = "Test Event";
const eventProperties = {prop1: "value1", prop2: "value2"};
const eventProperties = { prop1: "value1", prop2: "value2" };

await mixpanelMain.track(token, eventName, eventProperties);
expect(mixpanelMain.core.addToMixpanelQueue).toHaveBeenCalled();
});

it("register super properties should update properties", async () => {
mixpanelMain.registerSuperProperties(token, {superProp3: "value3"});
mixpanelMain.registerSuperProperties(token, { superProp3: "value3" });
expect(
mixpanelMain.mixpanelPersistent.updateSuperProperties
).toHaveBeenCalledWith(token, {
Expand All @@ -217,7 +218,7 @@ describe("MixpanelMain", () => {
});

it("register super properties once should update properties only once", async () => {
mixpanelMain.registerSuperPropertiesOnce(token, {superProp3: "value3"});
mixpanelMain.registerSuperPropertiesOnce(token, { superProp3: "value3" });
expect(
mixpanelMain.mixpanelPersistent.updateSuperProperties
).toHaveBeenCalledWith(token, {
Expand All @@ -229,7 +230,7 @@ describe("MixpanelMain", () => {
expect(
mixpanelMain.mixpanelPersistent.persistSuperProperties
).toHaveBeenCalledWith(token);
mixpanelMain.registerSuperPropertiesOnce(token, {superProp3: "value4"});
mixpanelMain.registerSuperPropertiesOnce(token, { superProp3: "value4" });
expect(
mixpanelMain.mixpanelPersistent.updateSuperProperties
).toHaveBeenCalledWith(token, {
Expand All @@ -244,7 +245,7 @@ describe("MixpanelMain", () => {
});

it("unregister super properties should update properties properly", async () => {
mixpanelMain.registerSuperPropertiesOnce(token, {superProp3: "value3"});
mixpanelMain.registerSuperPropertiesOnce(token, { superProp3: "value3" });
expect(
mixpanelMain.mixpanelPersistent.updateSuperProperties
).toHaveBeenCalledWith(token, {
Expand Down Expand Up @@ -276,7 +277,7 @@ describe("MixpanelMain", () => {

it("should send correct payload on track event", async () => {
const eventName = "Test Event";
const eventProperties = {prop1: "value1", prop2: "value2"};
const eventProperties = { prop1: "value1", prop2: "value2" };

await mixpanelMain.track(token, eventName, eventProperties);
expect(mixpanelMain.core.addToMixpanelQueue).toHaveBeenCalledWith(
Expand Down Expand Up @@ -319,7 +320,7 @@ describe("MixpanelMain", () => {
mixpanelMain.timeEvent(token, "test-event");
expect(
mixpanelMain.mixpanelPersistent.updateTimeEvents
).toHaveBeenCalledWith(token, {"test-event": expect.any(Number)});
).toHaveBeenCalledWith(token, { "test-event": expect.any(Number) });
expect(
mixpanelMain.mixpanelPersistent.persistTimeEvents
).toHaveBeenCalledWith(token);
Expand Down Expand Up @@ -359,7 +360,7 @@ describe("MixpanelMain", () => {
});

it("should send correct payload on set profile properties", async () => {
const properties = {prop1: "value1", prop2: "value2"};
const properties = { prop1: "value1", prop2: "value2" };

await mixpanelMain.set(token, properties);

Expand All @@ -369,7 +370,7 @@ describe("MixpanelMain", () => {
expect.objectContaining({
$token: token,
$time: expect.any(Number),
$set: {prop1: "value1", prop2: "value2"},
$set: { prop1: "value1", prop2: "value2" },
$distinct_id: "distinct-id-mock",
$device_id: "device-id-mock",
$user_id: "user-id-mock",
Expand All @@ -378,7 +379,7 @@ describe("MixpanelMain", () => {
});

it("should send correct payload on setOnce profile properties", async () => {
const properties = {prop1: "value1", prop2: "value2"};
const properties = { prop1: "value1", prop2: "value2" };

await mixpanelMain.setOnce(token, properties);

Expand All @@ -388,7 +389,7 @@ describe("MixpanelMain", () => {
expect.objectContaining({
$token: token,
$time: expect.any(Number),
$set_once: {prop1: "value1", prop2: "value2"},
$set_once: { prop1: "value1", prop2: "value2" },
$distinct_id: "distinct-id-mock",
$device_id: "device-id-mock",
$user_id: "user-id-mock",
Expand All @@ -397,7 +398,7 @@ describe("MixpanelMain", () => {
});

it("should send correct payload on increment profile properties", async () => {
const properties = {prop1: 3};
const properties = { prop1: 3 };

await mixpanelMain.increment(token, properties);

Expand All @@ -407,7 +408,7 @@ describe("MixpanelMain", () => {
expect.objectContaining({
$token: token,
$time: expect.any(Number),
$add: {prop1: 3},
$add: { prop1: 3 },
$distinct_id: "distinct-id-mock",
$device_id: "device-id-mock",
$user_id: "user-id-mock",
Expand All @@ -416,7 +417,7 @@ describe("MixpanelMain", () => {
});

it("should send correct payload on append profile properties", async () => {
const properties = {prop1: "value1"};
const properties = { prop1: "value1" };

await mixpanelMain.append(token, properties);

Expand All @@ -426,7 +427,7 @@ describe("MixpanelMain", () => {
expect.objectContaining({
$token: token,
$time: expect.any(Number),
$append: {prop1: "value1"},
$append: { prop1: "value1" },
$distinct_id: "distinct-id-mock",
$device_id: "device-id-mock",
$user_id: "user-id-mock",
Expand All @@ -441,7 +442,7 @@ describe("MixpanelMain", () => {
expect.objectContaining({
$token: token,
$time: expect.any(Number),
$append: {testProp: "testValue"},
$append: { testProp: "testValue" },
$distinct_id: "distinct-id-mock",
$device_id: "device-id-mock",
$user_id: "user-id-mock",
Expand All @@ -450,7 +451,7 @@ describe("MixpanelMain", () => {
});

it("should send correct payload on union profile properties", async () => {
const properties = {prop1: "value1"};
const properties = { prop1: "value1" };

await mixpanelMain.union(token, properties);

Expand All @@ -460,7 +461,7 @@ describe("MixpanelMain", () => {
expect.objectContaining({
$token: token,
$time: expect.any(Number),
$union: {prop1: "value1"},
$union: { prop1: "value1" },
$distinct_id: "distinct-id-mock",
$device_id: "device-id-mock",
$user_id: "user-id-mock",
Expand All @@ -475,7 +476,7 @@ describe("MixpanelMain", () => {
expect.objectContaining({
$token: token,
$time: expect.any(Number),
$union: {testProp: "testValue"},
$union: { testProp: "testValue" },
$distinct_id: "distinct-id-mock",
$device_id: "device-id-mock",
$user_id: "user-id-mock",
Expand All @@ -484,7 +485,7 @@ describe("MixpanelMain", () => {
});

it("should send correct payload on remove profile properties", async () => {
const properties = {prop1: "value1"};
const properties = { prop1: "value1" };

await mixpanelMain.remove(token, properties);

Expand All @@ -494,7 +495,7 @@ describe("MixpanelMain", () => {
expect.objectContaining({
$token: token,
$time: expect.any(Number),
$remove: {prop1: "value1"},
$remove: { prop1: "value1" },
$distinct_id: "distinct-id-mock",
$device_id: "device-id-mock",
$user_id: "user-id-mock",
Expand All @@ -509,7 +510,7 @@ describe("MixpanelMain", () => {
expect.objectContaining({
$token: token,
$time: expect.any(Number),
$remove: {testProp: "testValue"},
$remove: { testProp: "testValue" },
$distinct_id: "distinct-id-mock",
$device_id: "device-id-mock",
$user_id: "user-id-mock",
Expand All @@ -518,7 +519,7 @@ describe("MixpanelMain", () => {
});

it("should send correct payload on trackCharge", async () => {
const properties = {prop1: "value1"};
const properties = { prop1: "value1" };
const charge = 100;

await mixpanelMain.trackCharge(token, charge, properties);
Expand Down Expand Up @@ -598,9 +599,9 @@ describe("MixpanelMain", () => {
});

it("should send correct payload on trackWithGroups", async () => {
const properties = {prop1: "value1"};
const properties = { prop1: "value1" };
const eventName = "event1";
const groups = {company_id: 111};
const groups = { company_id: 111 };
await mixpanelMain.trackWithGroups(token, eventName, properties, groups);

expect(mixpanelMain.core.addToMixpanelQueue).toHaveBeenCalledWith(
Expand Down Expand Up @@ -632,7 +633,7 @@ describe("MixpanelMain", () => {
expect.objectContaining({
$token: token,
$time: expect.any(Number),
$union: {company_id: [111]},
$union: { company_id: [111] },
$distinct_id: "distinct-id-mock",
$device_id: "device-id-mock",
$user_id: "user-id-mock",
Expand Down Expand Up @@ -660,7 +661,7 @@ describe("MixpanelMain", () => {
expect.objectContaining({
$token: token,
$time: expect.any(Number),
$set: {company_id: [333]},
$set: { company_id: [333] },
$distinct_id: "distinct-id-mock",
$device_id: "device-id-mock",
$user_id: "user-id-mock",
Expand Down Expand Up @@ -700,7 +701,7 @@ describe("MixpanelMain", () => {
expect.objectContaining({
$token: token,
$time: expect.any(Number),
$remove: {company_id: 111},
$remove: { company_id: 111 },
$distinct_id: "distinct-id-mock",
$device_id: "device-id-mock",
$user_id: "user-id-mock",
Expand Down Expand Up @@ -734,7 +735,7 @@ describe("MixpanelMain", () => {
});

it("should send correct payload on group set", async () => {
const properties = {prop1: "value1", prop2: "value2"};
const properties = { prop1: "value1", prop2: "value2" };

await mixpanelMain.groupSetProperties(token, "company_id", 444, properties);

Expand All @@ -746,13 +747,13 @@ describe("MixpanelMain", () => {
$time: expect.any(Number),
$group_id: 444,
$group_key: "company_id",
$set: {prop1: "value1", prop2: "value2"},
$set: { prop1: "value1", prop2: "value2" },
})
);
});

it("should send correct payload on group set once", async () => {
const properties = {prop1: "value1", prop2: "value2"};
const properties = { prop1: "value1", prop2: "value2" };

await mixpanelMain.groupSetPropertyOnce(
token,
Expand All @@ -769,7 +770,7 @@ describe("MixpanelMain", () => {
$time: expect.any(Number),
$group_id: 444,
$group_key: "company_id",
$set_once: {prop1: "value1", prop2: "value2"},
$set_once: { prop1: "value1", prop2: "value2" },
})
);
});
Expand Down Expand Up @@ -807,7 +808,7 @@ describe("MixpanelMain", () => {
$time: expect.any(Number),
$group_id: 444,
$group_key: "company_id",
$remove: {prop1: "value1"},
$remove: { prop1: "value1" },
})
);
});
Expand All @@ -829,7 +830,7 @@ describe("MixpanelMain", () => {
$time: expect.any(Number),
$group_id: 444,
$group_key: "company_id",
$union: {prop1: "value1"},
$union: { prop1: "value1" },
})
);
});
Expand Down
Loading
Loading