@vnejs/plugins.platform.metric 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vnejs/plugins.platform.metric",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -18,7 +18,7 @@
18
18
  "tsconfig.json"
19
19
  ],
20
20
  "scripts": {
21
- "test": "echo \"Error: no test specified\" && exit 1",
21
+ "test": "npx @vnejs/monorepo test",
22
22
  "build": "npx @vnejs/monorepo package",
23
23
  "publish:major:plugin": "npm run publish:major",
24
24
  "publish:minor:plugin": "npm run publish:minor",
@@ -38,6 +38,9 @@
38
38
  "@vnejs/shared": "~0.1.0"
39
39
  },
40
40
  "devDependencies": {
41
- "@vnejs/configs.ts-common": "~0.1.0"
41
+ "@vnejs/configs.ts-common": "~0.1.0",
42
+ "@vnejs/configs.vitest": "~0.1.0",
43
+ "@vnejs/contracts.core.stack": "~0.1.0",
44
+ "@vnejs/test-utils": "~0.1.0"
42
45
  }
43
46
  }
@@ -0,0 +1,22 @@
1
+ import { PARAMS, PLUGIN_NAME, SUBSCRIBE_EVENTS } from "@vnejs/contracts.platform.metric";
2
+ import { createTestModule as baseCreateTestModule, registerCoreVne, registerVnePlugin, stubSilentEvent } from "@vnejs/test-utils";
3
+
4
+ export const METRIC_COUNTER_ID = 123456;
5
+
6
+ export const registerMetricPluginVne = (counterId = METRIC_COUNTER_ID) => {
7
+ registerCoreVne();
8
+ registerVnePlugin(PLUGIN_NAME, { events: SUBSCRIBE_EVENTS, params: { ...PARAMS, YA_COUNTER_ID: counterId } });
9
+ };
10
+
11
+ export const createMetricTestModule = <T extends Parameters<typeof baseCreateTestModule>[0]>(
12
+ ModuleClass: T,
13
+ options: Parameters<typeof baseCreateTestModule>[1] = {},
14
+ ) => {
15
+ const result = baseCreateTestModule(ModuleClass, options);
16
+
17
+ stubSilentEvent(result.observer, SUBSCRIBE_EVENTS.GOAL);
18
+
19
+ return result;
20
+ };
21
+
22
+ export { SUBSCRIBE_EVENTS, PLUGIN_NAME, PARAMS };
@@ -0,0 +1,64 @@
1
+ import { beforeEach, describe, expect, it, vi } from "vitest";
2
+
3
+ import { SUBSCRIBE_EVENTS as STACK_EVENTS } from "@vnejs/contracts.core.stack";
4
+
5
+ import { YaMetrika } from "../modules/yandex.js";
6
+ import { createMetricTestModule, METRIC_COUNTER_ID, registerMetricPluginVne, SUBSCRIBE_EVENTS } from "./setup.js";
7
+
8
+ vi.mock("../utils/metric.js", () => ({
9
+ initYandexMetrika: vi.fn(),
10
+ }));
11
+
12
+ describe("YaMetrika", () => {
13
+ const ym = vi.fn();
14
+
15
+ beforeEach(() => {
16
+ registerMetricPluginVne();
17
+ window.ym = ym;
18
+ ym.mockClear();
19
+ });
20
+
21
+ it("init initializes yandex metrika when counter id is configured", async () => {
22
+ const { module } = createMetricTestModule(YaMetrika);
23
+ const { initYandexMetrika } = await import("../utils/metric.js");
24
+
25
+ await (module as YaMetrika).init();
26
+
27
+ expect(initYandexMetrika).toHaveBeenCalledExactlyOnceWith(METRIC_COUNTER_ID);
28
+ });
29
+
30
+ it("GOAL sends reachGoal to ym", async () => {
31
+ const { observer } = createMetricTestModule(YaMetrika);
32
+
33
+ await observer.emit(SUBSCRIBE_EVENTS.GOAL, "purchase");
34
+
35
+ expect(ym).toHaveBeenCalledExactlyOnceWith(METRIC_COUNTER_ID, "reachGoal", "purchase");
36
+ });
37
+
38
+ it("PARAMS sends params to ym", async () => {
39
+ const { observer } = createMetricTestModule(YaMetrika);
40
+
41
+ await observer.emit(SUBSCRIBE_EVENTS.PARAMS, { chapter: 1 });
42
+
43
+ expect(ym).toHaveBeenCalledExactlyOnceWith(METRIC_COUNTER_ID, "params", { chapter: 1 });
44
+ });
45
+
46
+ it("STACK.EMIT forwards stack label and args as metric params", async () => {
47
+ const { observer } = createMetricTestModule(YaMetrika);
48
+
49
+ await observer.emit(STACK_EVENTS.EMIT, { label: "main", args: { step: 2 } });
50
+
51
+ expect(ym).toHaveBeenCalledExactlyOnceWith(METRIC_COUNTER_ID, "params", { VNE_STACK: { label: "main", args: { step: 2 } } });
52
+ });
53
+
54
+ it("does not subscribe when counter id is missing", async () => {
55
+ registerMetricPluginVne(0);
56
+
57
+ const { observer } = createMetricTestModule(YaMetrika);
58
+
59
+ await observer.emit(SUBSCRIBE_EVENTS.GOAL, "purchase");
60
+ await observer.emit(STACK_EVENTS.EMIT, { label: "main" });
61
+
62
+ expect(ym).not.toHaveBeenCalled();
63
+ });
64
+ });
package/tsconfig.json CHANGED
@@ -5,5 +5,5 @@
5
5
  "outDir": "dist"
6
6
  },
7
7
  "include": ["src/**/*.ts", "src/**/*.d.ts"],
8
- "exclude": ["dist", "node_modules"]
8
+ "exclude": ["dist", "node_modules", "src/tests"]
9
9
  }