|
1 | 1 | import {expect} from 'chai' |
| 2 | +import sinon from 'sinon' |
2 | 3 |
|
3 | | -import {getCampaignDetails} from '../../src/repositories/googleRepository.js' |
| 4 | +import {getCampaignDetails, getGoogleConsentValue} from '../../src/repositories/googleRepository.js' |
4 | 5 | describe('GoogleRepository', () => { |
5 | 6 | let initialTrackingTagsType |
6 | 7 |
|
@@ -160,4 +161,76 @@ describe('GoogleRepository', () => { |
160 | 161 | expect(details.campaign).to.have.property('source', 'stc_source') |
161 | 162 | expect(details.campaign).to.have.property('name', 'stc_campaign') |
162 | 163 | }) |
| 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 | + }) |
163 | 236 | }) |
0 commit comments