Skip to content

Commit e95c66d

Browse files
committed
chore: add unit tests
1 parent 40d11e3 commit e95c66d

File tree

3 files changed

+123
-14
lines changed

3 files changed

+123
-14
lines changed

src/runtime/plugin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export default defineNuxtPlugin((nuxtApp) => {
4343
// Exit if the last entry is the same as the new entry
4444
if (isRepeatedEntry(data, sessionId)) return;
4545

46+
// Add the new item to the data array
4647
data.value.unshift(dataObject);
4748
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(data.value));
4849
});

src/runtime/utm.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const readLocalData = (localStorageKey: string) => {
1111

1212
try {
1313
if (localData) {
14-
return JSON.parse(localData);
14+
return JSON.parse(localData) as DataObject[];
1515
}
1616
} catch (error) {
1717
console.error("Error parsing local storage data", error);
@@ -29,29 +29,28 @@ export const getSessionID = (sessionIdKey: string) => {
2929
return sessionID;
3030
};
3131

32-
export const urlHasUtmParams = (query: LocationQuery) => {
33-
return (
32+
export const urlHasUtmParams = (query: LocationQuery): boolean => {
33+
return Boolean(
3434
query.utm_source ||
35-
query.utm_medium ||
36-
query.utm_campaign ||
37-
query.utm_term ||
38-
query.utm_content
35+
query.utm_medium ||
36+
query.utm_campaign ||
37+
query.utm_term ||
38+
query.utm_content
3939
);
4040
};
4141

42-
export const getUtmParams = (query: LocationQuery) => {
43-
const utmParams: UTMParams = {
42+
export const getUtmParams = (query: LocationQuery): UTMParams => {
43+
return {
4444
utm_source: query.utm_source?.toString(),
4545
utm_medium: query.utm_medium?.toString(),
4646
utm_campaign: query.utm_campaign?.toString(),
4747
utm_term: query.utm_term?.toString(),
4848
utm_content: query.utm_content?.toString(),
4949
};
50-
return utmParams;
5150
};
5251

53-
export const getAdditionalInfo = () => {
54-
const additionalInfo: AdditionalInfo = {
52+
export const getAdditionalInfo = (): AdditionalInfo => {
53+
return {
5554
referrer: document.referrer,
5655
userAgent: navigator.userAgent,
5756
language: navigator.language,
@@ -60,13 +59,12 @@ export const getAdditionalInfo = () => {
6059
height: screen.height,
6160
},
6261
};
63-
return additionalInfo;
6462
};
6563

6664
export const isRepeatedEntry = (
6765
data: Ref<DataObject[]>,
6866
currentSessionID: string
69-
) => {
67+
): boolean => {
7068
const lastEntry = data.value?.[0];
7169
return lastEntry && lastEntry.sessionId === currentSessionID;
7270
};

test/unit.test.ts

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import { ref, Ref } from "vue";
2+
import { DataObject } from "nuxt-utm";
3+
import {
4+
isRepeatedEntry,
5+
readLocalData,
6+
urlHasUtmParams,
7+
getUtmParams,
8+
} from "./../src/runtime/utm";
9+
import { describe, it, expect, beforeEach } from "vitest";
10+
11+
global.localStorage = (() => {
12+
let store: Record<string, string> = {};
13+
14+
return {
15+
getItem: (key: string): string => store[key] ?? null,
16+
setItem: (key: string, value: string): void => {
17+
store[key] = value.toString();
18+
},
19+
removeItem: (key: string): void => {
20+
delete store[key];
21+
},
22+
clear: (): void => {
23+
store = {};
24+
},
25+
key: (index: number): string | null => "",
26+
length: Object.keys(store).length,
27+
};
28+
})();
29+
30+
describe("readLocalData function", () => {
31+
beforeEach(() => {
32+
localStorage.clear();
33+
});
34+
it("Returns empty array if local storage is empty", () => {
35+
expect(readLocalData("nuxt-utm-test")).toEqual([]);
36+
});
37+
38+
it("Returns an array with objects if local storage has data", () => {
39+
localStorage.setItem("nuxt-utm-test", utmItem);
40+
expect(readLocalData("nuxt-utm-test")).toEqual(JSON.parse(utmItem));
41+
});
42+
});
43+
44+
describe("urlHasUtmParams function", () => {
45+
it("Returns false if there are no utm values", () => {
46+
const locationQueryMock = {};
47+
expect(urlHasUtmParams(locationQueryMock)).toBeFalsy();
48+
});
49+
50+
it("Returns true if at least one utm value exists", () => {
51+
const locationQueryMock = { utm_source: "test_source" };
52+
expect(urlHasUtmParams(locationQueryMock)).toBeTruthy();
53+
});
54+
});
55+
56+
describe("getUtmParams function", () => {
57+
it("Returns the correct utm values", () => {
58+
const locationQueryMock = {
59+
utm_source: "test_source",
60+
utm_medium: "test_medium",
61+
utm_term: "test_term",
62+
utm_content: "test_content",
63+
};
64+
expect(getUtmParams(locationQueryMock)).toEqual({
65+
utm_source: "test_source",
66+
utm_medium: "test_medium",
67+
utm_term: "test_term",
68+
utm_content: "test_content",
69+
});
70+
});
71+
});
72+
73+
describe("isRepeatedEntry function", () => {
74+
let data: Ref<DataObject[]>;
75+
beforeEach(() => {
76+
data = ref<DataObject[]>(JSON.parse(`[${utmItem}]`));
77+
});
78+
79+
it("Returns true if the utm params and the session already exists in local storage", () => {
80+
expect(isRepeatedEntry(data, "beai1gx7dg")).toBeTruthy();
81+
});
82+
it("Returns false if the utm params are the same but the session is different", () => {
83+
expect(isRepeatedEntry(data, "newSession")).toBeFalsy();
84+
});
85+
it("Returns false if the utm params are different but the session is the same", () => {
86+
const data = ref<DataObject[]>(JSON.parse(`[${utmItem}]`));
87+
expect(isRepeatedEntry(data, "newSession")).toBeFalsy();
88+
});
89+
});
90+
91+
const utmItem = `{
92+
"timestamp": "2023-11-02T10:11:17.219Z",
93+
"utmParams": {
94+
"utm_source": "test_source",
95+
"utm_medium": "test_medium",
96+
"utm_campaign": "test_campaign",
97+
"utm_term": "test_term",
98+
"utm_content": "test_content"
99+
},
100+
"additionalInfo": {
101+
"referrer": "http://localhost:3000/?utm_source=test_source&utm_medium=test_medium&utm_campaign=test_campaign&utm_term=test_term&utm_content=test_content",
102+
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36",
103+
"language": "en-GB",
104+
"screen": {
105+
"width": 1728,
106+
"height": 1117
107+
}
108+
},
109+
"sessionId": "beai1gx7dg"
110+
}`;

0 commit comments

Comments
 (0)