@vnejs/plugins.platform.metric 0.1.1 → 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/dist/index.d.ts CHANGED
@@ -1 +1 @@
1
- import "@vnejs/plugins.platform.metric.contract";
1
+ import "@vnejs/contracts.platform.metric";
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import "@vnejs/plugins.platform.metric.contract";
1
+ import "@vnejs/contracts.platform.metric";
2
2
  import { regPlugin } from "@vnejs/shared";
3
- import { PARAMS, PLUGIN_NAME, SUBSCRIBE_EVENTS } from "@vnejs/plugins.platform.metric.contract";
3
+ import { PARAMS, PLUGIN_NAME, SUBSCRIBE_EVENTS } from "@vnejs/contracts.platform.metric";
4
4
  import { YaMetrika } from "./modules/yandex.js";
5
5
  regPlugin(PLUGIN_NAME, { events: SUBSCRIBE_EVENTS, params: PARAMS }, [YaMetrika]);
@@ -1,5 +1,5 @@
1
1
  import { ModuleCore } from "@vnejs/module.core";
2
- import type { StackEmitPayload } from "@vnejs/plugins.core.stack.contract";
2
+ import type { StackEmitPayload } from "@vnejs/contracts.core.stack";
3
3
  import type { MetricPluginConstants, MetricPluginEvents, MetricPluginParams, MetricPluginSettings } from "../types.js";
4
4
  export declare class YaMetrika extends ModuleCore<MetricPluginEvents, MetricPluginConstants, MetricPluginSettings, MetricPluginParams> {
5
5
  name: string;
package/dist/types.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { ModuleCoreConstants, ModuleCoreEvents, ModuleCoreParams, ModuleCoreSettings } from "@vnejs/module.core";
2
- import type { PluginName, Params, SubscribeEvents } from "@vnejs/plugins.platform.metric.contract";
2
+ import type { PluginName, Params, SubscribeEvents } from "@vnejs/contracts.platform.metric";
3
3
  export type MetricPluginEvents = ModuleCoreEvents & Record<PluginName, SubscribeEvents>;
4
4
  export type MetricPluginConstants = ModuleCoreConstants;
5
5
  export type MetricPluginSettings = ModuleCoreSettings;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vnejs/plugins.platform.metric",
3
- "version": "0.1.1",
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",
@@ -30,14 +30,17 @@
30
30
  "author": "",
31
31
  "license": "ISC",
32
32
  "dependencies": {
33
- "@vnejs/plugins.platform.metric.contract": "~0.1.0"
33
+ "@vnejs/contracts.platform.metric": "~0.1.0"
34
34
  },
35
35
  "peerDependencies": {
36
36
  "@vnejs/module.core": "~0.1.0",
37
- "@vnejs/plugins.core.stack.contract": "~0.1.0",
37
+ "@vnejs/contracts.core.stack": "~0.1.0",
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
  }
package/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
- import "@vnejs/plugins.platform.metric.contract";
1
+ import "@vnejs/contracts.platform.metric";
2
2
 
3
3
  import { regPlugin } from "@vnejs/shared";
4
- import { PARAMS, PLUGIN_NAME, SUBSCRIBE_EVENTS } from "@vnejs/plugins.platform.metric.contract";
4
+ import { PARAMS, PLUGIN_NAME, SUBSCRIBE_EVENTS } from "@vnejs/contracts.platform.metric";
5
5
 
6
6
  import { YaMetrika } from "./modules/yandex.js";
7
7
 
@@ -1,5 +1,5 @@
1
1
  import { ModuleCore } from "@vnejs/module.core";
2
- import type { StackEmitPayload } from "@vnejs/plugins.core.stack.contract";
2
+ import type { StackEmitPayload } from "@vnejs/contracts.core.stack";
3
3
 
4
4
  import type { MetricPluginConstants, MetricPluginEvents, MetricPluginParams, MetricPluginSettings } from "../types.js";
5
5
  import { initYandexMetrika } from "../utils/metric.js";
@@ -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/src/types.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { ModuleCoreConstants, ModuleCoreEvents, ModuleCoreParams, ModuleCoreSettings } from "@vnejs/module.core";
2
- import type { PluginName, Params, SubscribeEvents } from "@vnejs/plugins.platform.metric.contract";
2
+ import type { PluginName, Params, SubscribeEvents } from "@vnejs/contracts.platform.metric";
3
3
 
4
4
  export type MetricPluginEvents = ModuleCoreEvents & Record<PluginName, SubscribeEvents>;
5
5
 
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
  }