@hszrdm/create-commerce 1.20.0 → 1.22.0

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.
@@ -0,0 +1,120 @@
1
+ import assert from "node:assert/strict"
2
+ import { readFileSync } from "node:fs"
3
+ import { afterEach, test } from "node:test"
4
+ import {
5
+ COOKIE_CONSENT_KEY,
6
+ COOKIE_CONSENT_EVENT,
7
+ defaultConsentPreferences,
8
+ readConsentPreferences,
9
+ saveConsentPreferences,
10
+ toGoogleConsentState,
11
+ } from "../src/lib/core/consent.ts"
12
+
13
+ const originalWindow = globalThis.window
14
+ const originalCustomEvent = globalThis.CustomEvent
15
+
16
+ afterEach(() => {
17
+ if (originalWindow === undefined) delete globalThis.window
18
+ else globalThis.window = originalWindow
19
+ if (originalCustomEvent === undefined) delete globalThis.CustomEvent
20
+ else globalThis.CustomEvent = originalCustomEvent
21
+ })
22
+
23
+ test("first visit keeps necessary storage active and optional categories OFF", () => {
24
+ const preferences = defaultConsentPreferences()
25
+ assert.equal(preferences.necessary, true)
26
+ assert.equal(preferences.analytics, false)
27
+ assert.equal(preferences.marketing, false)
28
+ assert.deepEqual(toGoogleConsentState(preferences), {
29
+ analytics_storage: "denied",
30
+ ad_storage: "denied",
31
+ ad_user_data: "denied",
32
+ ad_personalization: "denied",
33
+ })
34
+ })
35
+
36
+ test("Core banner shows accept all, save settings and reject optional in that order", () => {
37
+ const component = readFileSync(new URL("../src/components/consent-banner.tsx", import.meta.url), "utf8")
38
+ const css = readFileSync(new URL("../src/app/globals.css", import.meta.url), "utf8")
39
+ const start = component.indexOf('<div className="consent-actions">')
40
+ assert.ok(start >= 0)
41
+ const end = component.indexOf("</div>", start)
42
+ assert.ok(end > start)
43
+ const actions = component.slice(start, end)
44
+ const accept = actions.indexOf("Mindet elfogadom")
45
+ const save = actions.indexOf("Beállítások mentése")
46
+ const reject = actions.indexOf("Opcionálisak elutasítása")
47
+ assert.ok(accept >= 0 && save > accept && reject > save)
48
+ assert.equal((actions.match(/<button /g) || []).length, 3)
49
+ assert.match(actions, /save\(\{ analytics: true, marketing: true \}\)/)
50
+ assert.match(actions, /save\(\{ analytics, marketing \}\)/)
51
+ assert.match(actions, /save\(\{ analytics: false, marketing: false \}\)/)
52
+ assert.match(actions, /className="button secondary consent-reject"/)
53
+ assert.match(css, /\.consent-actions > \.consent-reject \{ min-height: 44px;/)
54
+ assert.match(css, /font-size: \.88rem/)
55
+ })
56
+
57
+ test("necessary is checked, optional categories are not prechecked, and saved choices are restored", () => {
58
+ const component = readFileSync(new URL("../src/components/consent-banner.tsx", import.meta.url), "utf8")
59
+ assert.match(component, /<input type="checkbox" checked disabled \/>/)
60
+ assert.match(component, /useState\(false\)/)
61
+ assert.match(component, /setAnalytics\(Boolean\(stored\?\.analytics\)\)/)
62
+ assert.match(component, /setMarketing\(Boolean\(stored\?\.marketing\)\)/)
63
+ assert.match(component, /setOpen\(!stored\)/)
64
+ assert.match(component, /setAnalytics\(Boolean\(latest\?\.analytics\)\)/)
65
+ assert.match(component, /setMarketing\(Boolean\(latest\?\.marketing\)\)/)
66
+ })
67
+
68
+ test("save, accept all and reject optional persist exact choices and notify analytics", () => {
69
+ const storage = new Map()
70
+ const commands = []
71
+ const events = []
72
+ globalThis.CustomEvent = class {
73
+ constructor(type, init) {
74
+ this.type = type
75
+ this.detail = init?.detail
76
+ }
77
+ }
78
+ globalThis.window = {
79
+ localStorage: {
80
+ getItem: (key) => storage.get(key) ?? null,
81
+ setItem: (key, value) => storage.set(key, value),
82
+ },
83
+ gtag: (...args) => commands.push(args),
84
+ dispatchEvent: (event) => { events.push(event); return true },
85
+ }
86
+
87
+ assert.equal(readConsentPreferences(), null)
88
+
89
+ const accepted = saveConsentPreferences({ analytics: true, marketing: true })
90
+ assert.equal(accepted?.necessary, true)
91
+ assert.equal(readConsentPreferences()?.analytics, true)
92
+ assert.equal(readConsentPreferences()?.marketing, true)
93
+ assert.equal(commands.at(-1)[2].analytics_storage, "granted")
94
+ assert.equal(commands.at(-1)[2].ad_storage, "granted")
95
+
96
+ const custom = saveConsentPreferences({ analytics: true, marketing: false })
97
+ assert.equal(custom?.analytics, true)
98
+ assert.equal(custom?.marketing, false)
99
+ assert.equal(commands.at(-1)[2].analytics_storage, "granted")
100
+ assert.equal(commands.at(-1)[2].ad_storage, "denied")
101
+
102
+ const rejected = saveConsentPreferences({ analytics: false, marketing: false })
103
+ assert.equal(rejected?.necessary, true)
104
+ assert.equal(readConsentPreferences()?.analytics, false)
105
+ assert.equal(readConsentPreferences()?.marketing, false)
106
+ assert.equal(commands.at(-1)[2].analytics_storage, "denied")
107
+ assert.equal(commands.at(-1)[2].ad_storage, "denied")
108
+ assert.equal(events.length, 3)
109
+ assert.ok(events.every(event => event.type === COOKIE_CONSENT_EVENT))
110
+ assert.ok(storage.has(COOKIE_CONSENT_KEY))
111
+ })
112
+
113
+ test("tampered consent never silently opts into optional categories", () => {
114
+ globalThis.window = {
115
+ localStorage: {
116
+ getItem: () => JSON.stringify({ version: 1, necessary: true, analytics: "true", marketing: true }),
117
+ },
118
+ }
119
+ assert.equal(readConsentPreferences(), null)
120
+ })