generated from PostHog/posthog-plugin-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.test.ts
130 lines (102 loc) · 3.83 KB
/
index.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { PluginEvent, PluginInput, PluginMeta } from "@posthog/plugin-scaffold";
import { processEvent } from "./index";
/**
* Given a url, construct a page view event.
*
* @param $current_url The current url of the page view
* @returns A new PostHog page view event
*/
function buildPageViewEvent($current_url: string): PluginEvent {
const event: PluginEvent = {
properties: { $current_url },
distinct_id: "distinct_id",
ip: "1.2.3.4",
site_url: "test.com",
team_id: 0,
now: "2022-06-17T20:21:31.778000+00:00",
event: "$pageview",
uuid: "01817354-06bb-0000-d31c-2c4eed374100",
};
return event;
}
function buildEventWithoutCurrentUrl(): PluginEvent {
const event: PluginEvent = {
properties: {},
distinct_id: "distinct_id",
ip: "1.2.3.4",
site_url: "test.com",
team_id: 0,
now: "2022-06-17T20:21:31.778000+00:00",
event: "$identify",
uuid: "01817354-06bb-0000-d31c-2c4eed374100",
};
return event;
}
function getMeta(): PluginMeta<PluginInput> {
return {} as unknown as PluginMeta<PluginInput>;
}
describe("processEvent", () => {
it("shouldn't change a url that's already lowercase", () => {
const sourceEvent = buildPageViewEvent("http://www.google.com/test");
const processedEvent = processEvent(sourceEvent, getMeta());
expect(processedEvent?.properties?.$current_url).toEqual(
"http://www.google.com/test"
);
});
it("should convert the current_url to lowercase", () => {
const sourceEvent = buildPageViewEvent(
"http://www.GoOGle.com/WhatAreYouThinking"
);
const processedEvent = processEvent(sourceEvent, getMeta());
expect(processedEvent?.properties?.$current_url).toEqual(
"http://www.google.com/whatareyouthinking"
);
});
it("should remove the trailing slash from the current_url", () => {
const sourceEvent = buildPageViewEvent(
"http://www.google.com/this_is_a_test/"
);
const processedEvent = processEvent(sourceEvent, getMeta());
expect(processedEvent?.properties?.$current_url).toEqual(
"http://www.google.com/this_is_a_test"
);
});
it("should preserve the trailing slash if it's the only character in the path", () => {
const sourceEvent = buildPageViewEvent("http://www.google.com/");
const processedEvent = processEvent(sourceEvent, getMeta());
expect(processedEvent?.properties?.$current_url).toEqual(
"http://www.google.com/"
);
});
it("should preserve trailing id anchors", () => {
const sourceEvent = buildPageViewEvent(
"http://www.google.com/this_is_a_test#id_anchor"
);
const processedEvent = processEvent(sourceEvent, getMeta());
expect(processedEvent?.properties?.$current_url).toEqual(
"http://www.google.com/this_is_a_test#id_anchor"
);
});
it("should preserve trailing anchors but drop trailing slashes", () => {
const sourceEvent = buildPageViewEvent(
"http://www.google.com/this_is_a_test_with_trailing_slash/#id_anchor"
);
const processedEvent = processEvent(sourceEvent, getMeta());
expect(processedEvent?.properties?.$current_url).toEqual(
"http://www.google.com/this_is_a_test_with_trailing_slash#id_anchor"
);
});
it("shouldn't modify events that don't have a $current_url set", () => {
const sourceEvent = buildEventWithoutCurrentUrl();
const processedEvent = processEvent(sourceEvent, getMeta());
expect(processedEvent).toEqual(sourceEvent);
expect(processedEvent?.properties).toEqual(sourceEvent.properties);
expect(processedEvent?.properties?.$current_url).toBeUndefined();
});
it("should raise an error if the $current_url is an invalid url", () => {
const sourceEvent = buildPageViewEvent("invalid url");
expect(() => processEvent(sourceEvent, getMeta())).toThrowError(
`Unable to normalize invalid URL: "invalid url"`
);
});
});