@oslokommune/punkt-elements 13.5.2 → 13.5.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.
@@ -1,26 +1,35 @@
1
1
  import '@testing-library/jest-dom'
2
2
  import { axe, toHaveNoViolations } from 'jest-axe'
3
3
  import { fireEvent } from '@testing-library/dom'
4
+ import { createElementTest, BaseTestConfig } from '../../tests/test-framework'
5
+ import { CustomElementFor } from '../../tests/component-registry'
6
+ import './consent'
4
7
 
5
8
  expect.extend(toHaveNoViolations)
6
9
 
7
- import './consent'
8
- import { PktConsent } from './consent'
9
-
10
- const waitForCustomElements = async () => {
11
- await customElements.whenDefined('pkt-consent')
12
- await customElements.whenDefined('pkt-button')
10
+ export interface ConsentTestConfig extends BaseTestConfig {
11
+ devMode?: boolean
12
+ googleAnalyticsId?: string
13
+ hotjarId?: string
14
+ cookieDomain?: string
15
+ cookieSecure?: string // Using string to match the component's string attributes
16
+ cookieExpiryDays?: string
17
+ triggerType?: string
18
+ triggerText?: string
19
+ i18nLanguage?: string
13
20
  }
14
21
 
15
- // Helper function to create consent markup
16
- const createConsent = async (consentProps = '') => {
17
- const container = document.createElement('div')
18
- container.innerHTML = `
19
- <pkt-consent ${consentProps}></pkt-consent>
20
- `
21
- document.body.appendChild(container)
22
- await waitForCustomElements()
23
- return container
22
+ // Use shared framework
23
+ export const createConsentTest = async (config: ConsentTestConfig = {}) => {
24
+ const { container, element } = await createElementTest<
25
+ CustomElementFor<'pkt-consent'>,
26
+ ConsentTestConfig
27
+ >('pkt-consent', config)
28
+
29
+ return {
30
+ container,
31
+ consent: element,
32
+ }
24
33
  }
25
34
 
26
35
  // Cleanup after each test
@@ -49,9 +58,8 @@ afterEach(() => {
49
58
  describe('PktConsent', () => {
50
59
  describe('Rendering and basic functionality', () => {
51
60
  test('renders without errors', async () => {
52
- const container = await createConsent()
61
+ const { consent } = await createConsentTest()
53
62
 
54
- const consent = container.querySelector('pkt-consent') as PktConsent
55
63
  expect(consent).toBeInTheDocument()
56
64
 
57
65
  await consent.updateComplete
@@ -59,9 +67,8 @@ describe('PktConsent', () => {
59
67
  })
60
68
 
61
69
  test('renders with default trigger type as button', async () => {
62
- const container = await createConsent()
70
+ const { consent } = await createConsentTest()
63
71
 
64
- const consent = container.querySelector('pkt-consent') as PktConsent
65
72
  await consent.updateComplete
66
73
 
67
74
  expect(consent.triggerType).toBe('button')
@@ -72,9 +79,10 @@ describe('PktConsent', () => {
72
79
 
73
80
  test('renders with custom trigger text', async () => {
74
81
  const customText = 'Custom consent settings'
75
- const container = await createConsent(`triggerText="${customText}"`)
82
+ const { consent } = await createConsentTest({
83
+ triggerText: customText,
84
+ })
76
85
 
77
- const consent = container.querySelector('pkt-consent') as PktConsent
78
86
  await consent.updateComplete
79
87
 
80
88
  expect(consent.triggerText).toBe(customText)
@@ -85,9 +93,8 @@ describe('PktConsent', () => {
85
93
 
86
94
  describe('Properties and attributes', () => {
87
95
  test('applies default properties correctly', async () => {
88
- const container = await createConsent()
96
+ const { consent } = await createConsentTest()
89
97
 
90
- const consent = container.querySelector('pkt-consent') as PktConsent
91
98
  await consent.updateComplete
92
99
 
93
100
  expect(consent.devMode).toBe(false)
@@ -101,9 +108,10 @@ describe('PktConsent', () => {
101
108
  })
102
109
 
103
110
  test('sets dev mode correctly', async () => {
104
- const container = await createConsent('devMode')
111
+ const { consent } = await createConsentTest({
112
+ devMode: true,
113
+ })
105
114
 
106
- const consent = container.querySelector('pkt-consent') as PktConsent
107
115
  await consent.updateComplete
108
116
 
109
117
  expect(consent.devMode).toBe(true)
@@ -112,11 +120,11 @@ describe('PktConsent', () => {
112
120
  test('sets analytics IDs correctly', async () => {
113
121
  const googleId = 'GA-123456789'
114
122
  const hotjarId = 'HJ-987654321'
115
- const container = await createConsent(
116
- `googleAnalyticsId="${googleId}" hotjarId="${hotjarId}"`,
117
- )
123
+ const { consent } = await createConsentTest({
124
+ googleAnalyticsId: googleId,
125
+ hotjarId: hotjarId,
126
+ })
118
127
 
119
- const consent = container.querySelector('pkt-consent') as PktConsent
120
128
  await consent.updateComplete
121
129
 
122
130
  expect(consent.googleAnalyticsId).toBe(googleId)
@@ -127,11 +135,12 @@ describe('PktConsent', () => {
127
135
  const domain = '.example.com'
128
136
  const secure = 'true'
129
137
  const expiryDays = '365'
130
- const container = await createConsent(
131
- `cookieDomain="${domain}" cookieSecure="${secure}" cookieExpiryDays="${expiryDays}"`,
132
- )
138
+ const { consent } = await createConsentTest({
139
+ cookieDomain: domain,
140
+ cookieSecure: secure,
141
+ cookieExpiryDays: expiryDays,
142
+ })
133
143
 
134
- const consent = container.querySelector('pkt-consent') as PktConsent
135
144
  await consent.updateComplete
136
145
 
137
146
  expect(consent.cookieDomain).toBe(domain)
@@ -140,9 +149,10 @@ describe('PktConsent', () => {
140
149
  })
141
150
 
142
151
  test('sets language correctly', async () => {
143
- const container = await createConsent('i18nLanguage="en"')
152
+ const { consent } = await createConsentTest({
153
+ i18nLanguage: 'en',
154
+ })
144
155
 
145
- const consent = container.querySelector('pkt-consent') as PktConsent
146
156
  await consent.updateComplete
147
157
 
148
158
  expect(consent.i18nLanguage).toBe('en')
@@ -151,9 +161,10 @@ describe('PktConsent', () => {
151
161
 
152
162
  describe('Trigger types', () => {
153
163
  test('renders as button trigger type', async () => {
154
- const container = await createConsent('triggerType="button"')
164
+ const { consent } = await createConsentTest({
165
+ triggerType: 'button',
166
+ })
155
167
 
156
- const consent = container.querySelector('pkt-consent') as PktConsent
157
168
  await consent.updateComplete
158
169
 
159
170
  expect(consent.triggerType).toBe('button')
@@ -163,10 +174,9 @@ describe('PktConsent', () => {
163
174
  })
164
175
 
165
176
  test('renders as link trigger type', async () => {
166
- const container = await createConsent('triggerType="link"')
167
-
168
- const consent = container.querySelector('pkt-consent') as PktConsent
169
- await consent.updateComplete
177
+ const { consent } = await createConsentTest({
178
+ triggerType: 'link',
179
+ })
170
180
 
171
181
  expect(consent.triggerType).toBe('link')
172
182
  const link = consent.querySelector('a.pkt-link')
@@ -175,10 +185,9 @@ describe('PktConsent', () => {
175
185
  })
176
186
 
177
187
  test('renders as footer link trigger type', async () => {
178
- const container = await createConsent('triggerType="footerlink"')
179
-
180
- const consent = container.querySelector('pkt-consent') as PktConsent
181
- await consent.updateComplete
188
+ const { consent } = await createConsentTest({
189
+ triggerType: 'footerlink',
190
+ })
182
191
 
183
192
  expect(consent.triggerType).toBe('footerlink')
184
193
  const footerLink = consent.querySelector('a.pkt-footer__link')
@@ -190,10 +199,9 @@ describe('PktConsent', () => {
190
199
  })
191
200
 
192
201
  test('renders as icon trigger type', async () => {
193
- const container = await createConsent('triggerType="icon"')
194
-
195
- const consent = container.querySelector('pkt-consent') as PktConsent
196
- await consent.updateComplete
202
+ const { consent } = await createConsentTest({
203
+ triggerType: 'icon',
204
+ })
197
205
 
198
206
  expect(consent.triggerType).toBe('icon')
199
207
  const button = consent.querySelector('pkt-button')
@@ -239,9 +247,7 @@ describe('PktConsent', () => {
239
247
  })
240
248
 
241
249
  test('handles click event on button trigger', async () => {
242
- const container = await createConsent()
243
- const consent = container.querySelector('pkt-consent') as PktConsent
244
- await consent.updateComplete
250
+ const { consent } = await createConsentTest()
245
251
 
246
252
  const button = consent.querySelector('pkt-button')
247
253
  expect(button).toBeInTheDocument()
@@ -255,9 +261,9 @@ describe('PktConsent', () => {
255
261
  })
256
262
 
257
263
  test('handles click event on link trigger', async () => {
258
- const container = await createConsent('triggerType="link"')
259
- const consent = container.querySelector('pkt-consent') as PktConsent
260
- await consent.updateComplete
264
+ const { consent } = await createConsentTest({
265
+ triggerType: 'link',
266
+ })
261
267
 
262
268
  const link = consent.querySelector('a.pkt-link')
263
269
  expect(link).toBeInTheDocument()
@@ -271,9 +277,7 @@ describe('PktConsent', () => {
271
277
  })
272
278
 
273
279
  test('dispatches toggle-consent event', async () => {
274
- const container = await createConsent()
275
- const consent = container.querySelector('pkt-consent') as PktConsent
276
- await consent.updateComplete
280
+ const { consent } = await createConsentTest()
277
281
 
278
282
  let eventFired = false
279
283
  let eventDetail: any = null
@@ -305,8 +309,7 @@ describe('PktConsent', () => {
305
309
 
306
310
  describe('Utility methods', () => {
307
311
  test('returnJsonOrObject handles JSON strings', async () => {
308
- const container = await createConsent()
309
- const consent = container.querySelector('pkt-consent') as PktConsent
312
+ const { consent } = await createConsentTest()
310
313
 
311
314
  const jsonString = '{"test": "value"}'
312
315
  const result = consent.returnJsonOrObject(jsonString)
@@ -315,8 +318,7 @@ describe('PktConsent', () => {
315
318
  })
316
319
 
317
320
  test('returnJsonOrObject handles non-JSON objects', async () => {
318
- const container = await createConsent()
319
- const consent = container.querySelector('pkt-consent') as PktConsent
321
+ const { consent } = await createConsentTest()
320
322
 
321
323
  const nonJsonObject = { test: 'value' }
322
324
  const result = consent.returnJsonOrObject(nonJsonObject)
@@ -325,8 +327,7 @@ describe('PktConsent', () => {
325
327
  })
326
328
 
327
329
  test('returnJsonOrObject handles invalid JSON gracefully', async () => {
328
- const container = await createConsent()
329
- const consent = container.querySelector('pkt-consent') as PktConsent
330
+ const { consent } = await createConsentTest()
330
331
 
331
332
  const invalidJson = 'invalid json string'
332
333
  const result = consent.returnJsonOrObject(invalidJson)
@@ -337,10 +338,9 @@ describe('PktConsent', () => {
337
338
 
338
339
  describe('Accessibility', () => {
339
340
  test('button trigger is accessible', async () => {
340
- const container = await createConsent('triggerText="Cookie settings"')
341
-
342
- const consent = container.querySelector('pkt-consent') as PktConsent
343
- await consent.updateComplete
341
+ const { container, consent } = await createConsentTest({
342
+ triggerText: 'Cookie settings',
343
+ })
344
344
 
345
345
  const results = await axe(container)
346
346
  expect(results).toHaveNoViolations()
@@ -351,10 +351,10 @@ describe('PktConsent', () => {
351
351
  })
352
352
 
353
353
  test('link trigger is accessible', async () => {
354
- const container = await createConsent('triggerType="link" triggerText="Cookie settings"')
355
-
356
- const consent = container.querySelector('pkt-consent') as PktConsent
357
- await consent.updateComplete
354
+ const { container, consent } = await createConsentTest({
355
+ triggerType: 'link',
356
+ triggerText: 'Cookie settings',
357
+ })
358
358
 
359
359
  const results = await axe(container)
360
360
  expect(results).toHaveNoViolations()
@@ -365,12 +365,10 @@ describe('PktConsent', () => {
365
365
  })
366
366
 
367
367
  test('footer link trigger is accessible', async () => {
368
- const container = await createConsent(
369
- 'triggerType="footerlink" triggerText="Cookie settings"',
370
- )
371
-
372
- const consent = container.querySelector('pkt-consent') as PktConsent
373
- await consent.updateComplete
368
+ const { container, consent } = await createConsentTest({
369
+ triggerType: 'footerlink',
370
+ triggerText: 'Cookie settings',
371
+ })
374
372
 
375
373
  const results = await axe(container)
376
374
  expect(results).toHaveNoViolations()
@@ -381,10 +379,10 @@ describe('PktConsent', () => {
381
379
  })
382
380
 
383
381
  test('icon trigger is accessible', async () => {
384
- const container = await createConsent('triggerType="icon" triggerText="Cookie settings"')
385
-
386
- const consent = container.querySelector('pkt-consent') as PktConsent
387
- await consent.updateComplete
382
+ const { container, consent } = await createConsentTest({
383
+ triggerType: 'icon',
384
+ triggerText: 'Cookie settings',
385
+ })
388
386
 
389
387
  const results = await axe(container)
390
388
  expect(results).toHaveNoViolations()
@@ -399,12 +397,12 @@ describe('PktConsent', () => {
399
397
  test('sets global variables on firstUpdated', async () => {
400
398
  const googleId = 'GA-123456789'
401
399
  const hotjarId = 'HJ-987654321'
402
- const container = await createConsent(
403
- `devMode googleAnalyticsId="${googleId}" hotjarId="${hotjarId}" cookieDomain=".test.com"`,
404
- )
405
-
406
- const consent = container.querySelector('pkt-consent') as PktConsent
407
- await consent.updateComplete
400
+ await createConsentTest({
401
+ devMode: true,
402
+ googleAnalyticsId: googleId,
403
+ hotjarId: hotjarId,
404
+ cookieDomain: '.test.com',
405
+ })
408
406
 
409
407
  expect(window.cookieBanner_googleAnalyticsId).toBe(googleId)
410
408
  expect(window.cookieBanner_hotjarId).toBe(hotjarId)
@@ -413,9 +411,7 @@ describe('PktConsent', () => {
413
411
  })
414
412
 
415
413
  test('cleans up event listeners on disconnect', async () => {
416
- const container = await createConsent()
417
- const consent = container.querySelector('pkt-consent') as PktConsent
418
- await consent.updateComplete
414
+ const { consent } = await createConsentTest()
419
415
 
420
416
  // Mock the event system
421
417
  window.__cookieEvents = {