|
| 1 | +import { afterEach, describe, expect, test, vi } from "vitest"; |
| 2 | + |
| 3 | +// React Native defines a limited `window` polyfill but has no `document`, |
| 4 | +// `localStorage`, or `window.location`. The SDK must import, construct, and |
| 5 | +// make requests there without touching those globals. Analytics is disabled. |
| 6 | +describe("React Native environment", () => { |
| 7 | + afterEach(() => { |
| 8 | + vi.unstubAllGlobals(); |
| 9 | + vi.resetModules(); |
| 10 | + }); |
| 11 | + |
| 12 | + function stubReactNativeGlobals() { |
| 13 | + // `window` exists but is a bare object: no `location`, no `addEventListener`, |
| 14 | + // no `localStorage`. |
| 15 | + vi.stubGlobal("window", {}); |
| 16 | + // `document` is not defined on React Native. |
| 17 | + vi.stubGlobal("document", undefined); |
| 18 | + vi.stubGlobal("localStorage", undefined); |
| 19 | + } |
| 20 | + |
| 21 | + test("importing and constructing the client does not throw", async () => { |
| 22 | + stubReactNativeGlobals(); |
| 23 | + // Re-import so module-load code (e.g. the analytics shared-state factory) |
| 24 | + // is evaluated against the React Native globals. |
| 25 | + vi.resetModules(); |
| 26 | + const { createClient } = await import("../../src/index.ts"); |
| 27 | + |
| 28 | + const client = createClient({ |
| 29 | + serverUrl: "https://api.base44.com", |
| 30 | + appId: "test-app-id", |
| 31 | + }); |
| 32 | + |
| 33 | + expect(client.analytics).toBeDefined(); |
| 34 | + expect(() => client.cleanup()).not.toThrow(); |
| 35 | + }); |
| 36 | + |
| 37 | + test("analytics.track is a safe noop (no document access)", async () => { |
| 38 | + stubReactNativeGlobals(); |
| 39 | + vi.resetModules(); |
| 40 | + const { createClient } = await import("../../src/index.ts"); |
| 41 | + |
| 42 | + const client = createClient({ |
| 43 | + serverUrl: "https://api.base44.com", |
| 44 | + appId: "test-app-id", |
| 45 | + }); |
| 46 | + |
| 47 | + expect(() => |
| 48 | + client.analytics.track({ eventName: "test-event" }) |
| 49 | + ).not.toThrow(); |
| 50 | + |
| 51 | + client.cleanup(); |
| 52 | + }); |
| 53 | + |
| 54 | + test("isReactNative reflects the window-without-document environment", async () => { |
| 55 | + stubReactNativeGlobals(); |
| 56 | + vi.resetModules(); |
| 57 | + const { isReactNative, isNode } = await import("../../src/utils/common.ts"); |
| 58 | + |
| 59 | + expect(isNode).toBe(false); |
| 60 | + expect(isReactNative).toBe(true); |
| 61 | + }); |
| 62 | +}); |
0 commit comments