Files
medusa-store/packages/modules/analytics/integration-tests/__tests__/module.spec.ts
Stevche Radevski b9a51e217d feat: Add an analytics module and local and posthog providers (#12505)
* feat: Add an analytics module and local and posthog providers

* fix: Add tests and wire up in missing places

* fix: Address feedback and add missing module typing

* fix: Address feedback and add missing module typing

---------

Co-authored-by: Adrien de Peretti <adrien.deperetti@gmail.com>
Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
2025-05-19 19:57:13 +02:00

106 lines
2.6 KiB
TypeScript

import { moduleIntegrationTestRunner } from "@medusajs/test-utils"
import { Modules } from "@medusajs/framework/utils"
import { resolve } from "path"
import { IAnalyticsModuleService } from "@medusajs/types"
import { AnalyticsProviderServiceFixtures } from "../__fixtures__/providers/default-provider"
jest.setTimeout(100000)
const moduleOptions = {
providers: [
{
resolve: resolve(
process.cwd() +
"/integration-tests/__fixtures__/providers/default-provider"
),
id: "default-provider",
},
],
}
moduleIntegrationTestRunner<IAnalyticsModuleService>({
moduleName: Modules.ANALYTICS,
moduleOptions: moduleOptions,
testSuite: ({ service }) => {
describe("Analytics Module Service", () => {
let spies: {
track: jest.SpyInstance
identify: jest.SpyInstance
}
beforeAll(async () => {
spies = {
track: jest.spyOn(
AnalyticsProviderServiceFixtures.prototype,
"track"
),
identify: jest.spyOn(
AnalyticsProviderServiceFixtures.prototype,
"identify"
),
}
})
afterEach(async () => {
jest.clearAllMocks()
})
it("should call the provider's track method", async () => {
await service.track({
event: "test-event",
actor_id: "test-user",
properties: {
test: "test",
},
})
expect(spies.track).toHaveBeenCalledWith({
event: "test-event",
actor_id: "test-user",
properties: {
test: "test",
},
})
})
it("should call the provider's identify method to identify an actor", async () => {
await service.identify({
actor_id: "test-user",
properties: {
test: "test",
},
})
expect(spies.identify).toHaveBeenCalledWith({
actor_id: "test-user",
properties: {
test: "test",
},
})
})
it("should call the provider's identify method to identify a group", async () => {
await service.identify({
group: {
type: "organization",
id: "test-organization",
},
properties: {
test: "test",
},
})
expect(spies.identify).toHaveBeenCalledWith({
group: {
type: "organization",
id: "test-organization",
},
properties: {
test: "test",
},
})
})
})
},
})