Skip to content

Commit 8a40beb

Browse files
committed
test(packages/sui-segment-wrapper): add more tests
1 parent d35353c commit 8a40beb

2 files changed

Lines changed: 125 additions & 1 deletion

File tree

packages/sui-segment-wrapper/test/repositories/googleRepositorySpec.js

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {expect} from 'chai'
2+
import sinon from 'sinon'
23

3-
import {getCampaignDetails} from '../../src/repositories/googleRepository.js'
4+
import {getCampaignDetails, getGoogleConsentValue} from '../../src/repositories/googleRepository.js'
45
describe('GoogleRepository', () => {
56
let initialTrackingTagsType
67

@@ -160,4 +161,76 @@ describe('GoogleRepository', () => {
160161
expect(details.campaign).to.have.property('source', 'stc_source')
161162
expect(details.campaign).to.have.property('name', 'stc_campaign')
162163
})
164+
165+
describe('getGoogleConsentValue', () => {
166+
let getConsentStateStub
167+
168+
beforeEach(() => {
169+
// Ensure the nested structure exists before stubbing
170+
if (!window.google_tag_data) {
171+
window.google_tag_data = {ics: {}}
172+
} else if (!window.google_tag_data.ics) {
173+
window.google_tag_data.ics = {}
174+
}
175+
// Ensure the property exists so sinon.stub doesn't throw
176+
window.google_tag_data.ics.getConsentState = function () {}
177+
getConsentStateStub = sinon.stub(window.google_tag_data.ics, 'getConsentState')
178+
})
179+
180+
afterEach(() => {
181+
if (getConsentStateStub && typeof getConsentStateStub.restore === 'function') {
182+
getConsentStateStub.restore()
183+
}
184+
delete window.google_tag_data
185+
})
186+
187+
it("should return 'granted' when GTM consent state is 1", () => {
188+
// Given
189+
getConsentStateStub.returns(1)
190+
// When
191+
const consentValue = getGoogleConsentValue('analytics_storage')
192+
// Then
193+
expect(consentValue).to.equal('granted')
194+
expect(getConsentStateStub.calledOnceWith('analytics_storage')).to.be.true
195+
})
196+
197+
it("should return 'denied' when GTM consent state is 2", () => {
198+
// Given
199+
getConsentStateStub.returns(2)
200+
// When
201+
const consentValue = getGoogleConsentValue('ad_storage')
202+
// Then
203+
expect(consentValue).to.equal('denied')
204+
expect(getConsentStateStub.calledOnceWith('ad_storage')).to.be.true
205+
})
206+
207+
it('should return undefined if the GTM API returns a value other than 1 or 2', () => {
208+
// Given
209+
getConsentStateStub.returns(0) // An unexpected value
210+
// When
211+
const consentValue = getGoogleConsentValue('ad_user_data')
212+
// Then
213+
expect(consentValue).to.be.undefined
214+
})
215+
216+
it('should return undefined if the GTM API is not available', () => {
217+
// Given
218+
getConsentStateStub.restore() // Remove the stub to simulate the API not being there
219+
delete window.google_tag_data
220+
// When
221+
const consentValue = getGoogleConsentValue('ad_personalization')
222+
// Then
223+
expect(consentValue).to.be.undefined
224+
})
225+
226+
it('should return undefined if getConsentState is not a function', () => {
227+
// Given
228+
getConsentStateStub.restore() // Remove the stub
229+
window.google_tag_data.ics = {} // getConsentState is missing
230+
// When
231+
const consentValue = getGoogleConsentValue('analytics_storage')
232+
// Then
233+
expect(consentValue).to.be.undefined
234+
})
235+
})
163236
})

packages/sui-segment-wrapper/test/segmentWrapperSpec.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,4 +1050,55 @@ describe('Segment Wrapper', function () {
10501050
expect(externalIds).to.be.undefined
10511051
})
10521052
})
1053+
1054+
describe('google_consents', () => {
1055+
it('should be populated with GTM API values when available', async () => {
1056+
// Given
1057+
await simulateUserAcceptConsents()
1058+
const getConsentState = sinon.stub()
1059+
getConsentState.withArgs('analytics_storage').returns(1) // GRANTED
1060+
getConsentState.withArgs('ad_storage').returns(2) // DENIED
1061+
getConsentState.withArgs('ad_user_data').returns(1) // GRANTED
1062+
getConsentState.withArgs('ad_personalization').returns(2) // DENIED
1063+
1064+
window.google_tag_data = {
1065+
ics: {
1066+
getConsentState
1067+
}
1068+
}
1069+
1070+
// When
1071+
await suiAnalytics.track('fakeEvent')
1072+
const {context} = getDataFromLastTrack()
1073+
1074+
// Then
1075+
expect(context.google_consents).to.deep.equal({
1076+
analytics_storage: 'granted',
1077+
ad_storage: 'denied',
1078+
ad_user_data: 'granted',
1079+
ad_personalization: 'denied'
1080+
})
1081+
1082+
delete window.google_tag_data
1083+
})
1084+
1085+
it('should fallback to CMP values when GTM API is not available', async () => {
1086+
// Given
1087+
await simulateUserDeclinedAnalyticsConsentsAndAcceptedAdvertisingConsents()
1088+
// window.google_tag_data is undefined
1089+
1090+
// When
1091+
await suiAnalytics.track('fakeEvent')
1092+
const {context} = getDataFromLastTrack()
1093+
1094+
// Then
1095+
// Fallback logic derives from CMP consent, which is what we simulated
1096+
expect(context.google_consents).to.deep.equal({
1097+
analytics_storage: 'denied',
1098+
ad_storage: 'granted',
1099+
ad_user_data: 'granted',
1100+
ad_personalization: 'granted'
1101+
})
1102+
})
1103+
})
10531104
})

0 commit comments

Comments
 (0)