|
1 | | -import { Walkerjs } from '..'; |
| 1 | +import type { SourceWalkerjs } from '..'; |
| 2 | +import { elb, Walkerjs } from '..'; |
2 | 3 | import { mockDataLayer } from '@elbwalker/jest/web.setup'; |
3 | 4 | import { sessionStart } from '@elbwalker/utils/web'; |
4 | | -import type { SourceWalkerjs } from '..'; |
5 | 5 |
|
6 | 6 | jest.mock('@elbwalker/utils/web', () => { |
7 | 7 | const utilsOrg = jest.requireActual('@elbwalker/utils/web'); |
@@ -78,4 +78,66 @@ describe('Session', () => { |
78 | 78 |
|
79 | 79 | expect(mockFn).toHaveBeenCalledTimes(2); |
80 | 80 | }); |
| 81 | + |
| 82 | + test('different consent keys', () => { |
| 83 | + walkerjs = Walkerjs({ |
| 84 | + default: true, |
| 85 | + session: { consent: ['marketing', 'analytics'], storage: true }, |
| 86 | + pageview: false, |
| 87 | + }); |
| 88 | + |
| 89 | + expect(mockDataLayer).toHaveBeenCalledTimes(0); |
| 90 | + elb('walker consent', { marketing: false, analytics: true }); |
| 91 | + |
| 92 | + expect(mockDataLayer).toHaveBeenCalledTimes(1); |
| 93 | + expect(mockDataLayer).toHaveBeenCalledWith( |
| 94 | + expect.objectContaining({ |
| 95 | + event: 'session start', |
| 96 | + data: expect.objectContaining({ |
| 97 | + storage: true, // Prefer granted consent |
| 98 | + }), |
| 99 | + }), |
| 100 | + ); |
| 101 | + }); |
| 102 | + |
| 103 | + test('multiple consent updates', () => { |
| 104 | + const originalLocation = window.location; |
| 105 | + Object.defineProperty(window, 'location', { |
| 106 | + value: new URL('https://www.elbwalker.com/?utm_campaign=foo'), |
| 107 | + }); |
| 108 | + |
| 109 | + walkerjs = Walkerjs({ |
| 110 | + default: true, |
| 111 | + session: { consent: 'marketing', storage: true }, |
| 112 | + pageview: false, |
| 113 | + }); |
| 114 | + |
| 115 | + expect(mockDataLayer).toHaveBeenCalledTimes(0); |
| 116 | + elb('walker consent', { marketing: true }); |
| 117 | + elb('walker consent', { marketing: true }); |
| 118 | + elb('walker consent', { marketing: true }); |
| 119 | + |
| 120 | + expect(mockDataLayer).toHaveBeenCalledTimes(1); |
| 121 | + expect(mockDataLayer).toHaveBeenCalledWith( |
| 122 | + expect.objectContaining({ |
| 123 | + event: 'session start', |
| 124 | + data: expect.any(Object), |
| 125 | + }), |
| 126 | + ); |
| 127 | + |
| 128 | + elb('walker run'); |
| 129 | + expect(mockDataLayer).toHaveBeenCalledTimes(2); |
| 130 | + expect(mockDataLayer).toHaveBeenCalledWith( |
| 131 | + expect.objectContaining({ |
| 132 | + event: 'session start', |
| 133 | + data: expect.objectContaining({ |
| 134 | + count: 2, |
| 135 | + }), |
| 136 | + }), |
| 137 | + ); |
| 138 | + |
| 139 | + Object.defineProperty(window, 'location', { |
| 140 | + value: originalLocation, |
| 141 | + }); |
| 142 | + }); |
81 | 143 | }); |
0 commit comments