|
| 1 | +import { describe, test, expect, beforeEach, afterEach, vi } from "vitest"; |
| 2 | +import { |
| 3 | + AnalyticsModuleOptions, |
| 4 | + createClient, |
| 5 | + SessionContext, |
| 6 | + TrackEventData, |
| 7 | +} from "../../src/index.ts"; |
| 8 | +import { getSharedInstance } from "../../src/utils/sharedInstance.ts"; |
| 9 | + |
| 10 | +describe("Analytics Module", () => { |
| 11 | + let base44: ReturnType<typeof createClient>; |
| 12 | + let sharedState: null | { |
| 13 | + requestsQueue: TrackEventData[]; |
| 14 | + isProcessing: boolean; |
| 15 | + sessionContext: SessionContext; |
| 16 | + config: AnalyticsModuleOptions; |
| 17 | + }; |
| 18 | + const appId = "test-app-id"; |
| 19 | + const serverUrl = "https://api.base44.com"; |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + base44 = createClient({ |
| 23 | + serverUrl, |
| 24 | + appId, |
| 25 | + }); |
| 26 | + sharedState = getSharedInstance("analytics", () => ({ |
| 27 | + requestsQueue: [], |
| 28 | + isProcessing: false, |
| 29 | + sessionContext: { |
| 30 | + user_id: "test-user-id", |
| 31 | + }, |
| 32 | + config: { |
| 33 | + enabled: true, |
| 34 | + maxQueueSize: 1000, |
| 35 | + throttleTime: 1000, |
| 36 | + batchSize: 30, |
| 37 | + }, |
| 38 | + })); |
| 39 | + }); |
| 40 | + |
| 41 | + afterEach(() => { |
| 42 | + base44.cleanup(); |
| 43 | + sharedState = null; |
| 44 | + }); |
| 45 | + |
| 46 | + test("should create analytics module with shared state", () => { |
| 47 | + expect(base44.analytics).toBeDefined(); |
| 48 | + expect(sharedState).toBeDefined(); |
| 49 | + expect(sharedState?.requestsQueue).toBeDefined(); |
| 50 | + expect(sharedState?.isProcessing).toBe(true); |
| 51 | + }); |
| 52 | + |
| 53 | + test("should track an event", () => { |
| 54 | + vi.spyOn(base44.analytics, "track").mockImplementation(() => { |
| 55 | + console.log("track called"); |
| 56 | + }); |
| 57 | + |
| 58 | + base44.analytics.track({ eventName: "test-event" }); |
| 59 | + expect(base44.analytics.track).toHaveBeenCalledWith({ |
| 60 | + eventName: "test-event", |
| 61 | + }); |
| 62 | + }); |
| 63 | +}); |
0 commit comments