Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions apps/mobile/src/app/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { useNetworkStatus } from "@/hooks/useNetworkStatus";
import {
POSTHOG_API_KEY,
POSTHOG_OPTIONS,
useRegisterAppVersion,
useScreenTracking,
} from "@/lib/posthog";
import { queryClient } from "@/lib/queryClient";
Expand All @@ -36,6 +37,7 @@ function RootLayoutNav({ isConnected }: RootLayoutNavProps) {
const pathname = usePathname();

useScreenTracking();
useRegisterAppVersion();

useEffect(() => {
initializeAuth();
Expand Down
104 changes: 104 additions & 0 deletions apps/mobile/src/lib/posthog.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { beforeEach, describe, expect, it, vi } from "vitest";

const expoApplication = {
nativeApplicationVersion: null as string | null,
};

const expoConstants = {
expoConfig: null as { version?: string } | null,
};

vi.mock("posthog-react-native", () => ({
usePostHog: () => null,
}));

vi.mock("expo-router", () => ({
usePathname: () => "/",
useSegments: () => [] as string[],
}));

vi.mock("expo-application", () => ({
get nativeApplicationVersion() {
return expoApplication.nativeApplicationVersion;
},
}));

vi.mock("expo-constants", () => ({
default: {
get expoConfig() {
return expoConstants.expoConfig;
},
},
}));

beforeEach(() => {
vi.clearAllMocks();
expoApplication.nativeApplicationVersion = null;
expoConstants.expoConfig = { version: "0.0.0-test" };
});

describe("getAppVersion", () => {
it("prefers the native application version when present", async () => {
expoApplication.nativeApplicationVersion = "9.8.7";
expoConstants.expoConfig = { version: "0.0.0-test" };

const { getAppVersion } = await import("./posthog");

expect(getAppVersion()).toBe("9.8.7");
});

it("falls back to the Expo config version when no native version is available", async () => {
expoApplication.nativeApplicationVersion = null;
expoConstants.expoConfig = { version: "1.2.3" };

const { getAppVersion } = await import("./posthog");

expect(getAppVersion()).toBe("1.2.3");
});

it("returns null when neither source has a version", async () => {
expoApplication.nativeApplicationVersion = null;
expoConstants.expoConfig = null;

const { getAppVersion } = await import("./posthog");

expect(getAppVersion()).toBeNull();
});
});
Comment on lines +40 to +67
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Prefer parameterised tests for getAppVersion

The three getAppVersion cases share an identical structure — set nativeApplicationVersion, set expoConfig, call getAppVersion, assert the result. The project convention is to consolidate these into a single it.each table rather than repeating the scaffolding three times. The same pattern applies to the two "does nothing" cases in registerAppVersion.

Context Used: Do not attempt to comment on incorrect alphabetica... (source)

Prompt To Fix With AI
This is a comment left during a code review.
Path: apps/mobile/src/lib/posthog.test.ts
Line: 40-67

Comment:
**Prefer parameterised tests for `getAppVersion`**

The three `getAppVersion` cases share an identical structure — set `nativeApplicationVersion`, set `expoConfig`, call `getAppVersion`, assert the result. The project convention is to consolidate these into a single `it.each` table rather than repeating the scaffolding three times. The same pattern applies to the two "does nothing" cases in `registerAppVersion`.

**Context Used:** Do not attempt to comment on incorrect alphabetica... ([source](https://app.greptile.com/review/custom-context?memory=instruction-0))

How can I resolve this? If you propose a fix, please make it concise.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!


describe("registerAppVersion", () => {
it("registers app_version as a super property on the PostHog client", async () => {
const register = vi.fn();
const { registerAppVersion } = await import("./posthog");

registerAppVersion({ register }, "1.2.3");

expect(register).toHaveBeenCalledTimes(1);
expect(register).toHaveBeenCalledWith({ app_version: "1.2.3" });
});

it("does nothing when the PostHog client is not yet available", async () => {
const { registerAppVersion } = await import("./posthog");

expect(() => registerAppVersion(null, "1.2.3")).not.toThrow();
});

it("does nothing when no app version can be resolved", async () => {
const register = vi.fn();
const { registerAppVersion } = await import("./posthog");

registerAppVersion({ register }, null);

expect(register).not.toHaveBeenCalled();
});

it("resolves the version from getAppVersion when none is provided", async () => {
expoApplication.nativeApplicationVersion = "4.5.6";
const register = vi.fn();
const { registerAppVersion } = await import("./posthog");

registerAppVersion({ register });

expect(register).toHaveBeenCalledWith({ app_version: "4.5.6" });
});
});
45 changes: 45 additions & 0 deletions apps/mobile/src/lib/posthog.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as Application from "expo-application";
import Constants from "expo-constants";
import { usePathname, useSegments } from "expo-router";
import { usePostHog } from "posthog-react-native";
import { useEffect, useRef } from "react";
Expand All @@ -18,6 +20,49 @@ export const POSTHOG_OPTIONS = {
},
};

/**
* Resolve the app version that should ride along on every custom event. Prefer
* the native runtime value so OTA-updated binaries still report their actual
* shipped version; fall back to the Expo config (app.json) when running where
* expo-application has no native value (e.g. Expo Go, web preview).
*/
export function getAppVersion(): string | null {
return (
Application.nativeApplicationVersion ??
Constants.expoConfig?.version ??
null
);
}

type PostHogRegisterClient = {
register: (properties: { app_version: string }) => unknown;
};

/**
* Register the app version as a PostHog super property so it is attached to
* every event the client emits. No-op if the client is not yet ready or no
* version is available.
*/
export function registerAppVersion(
client: PostHogRegisterClient | null | undefined,
version: string | null = getAppVersion(),
) {
if (!client || version === null) return;
client.register({ app_version: version });
}

/**
* Hook variant of `registerAppVersion`. Runs once per client instance so the
* super property is re-applied if the PostHog client is recreated.
*/
export function useRegisterAppVersion() {
const posthog = usePostHog();

useEffect(() => {
registerAppVersion(posthog);
}, [posthog]);
}

/**
* Screen tracking hook for expo-router.
* Must be used inside PostHogProvider.
Expand Down
Loading