|
| 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