-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor and wrap all exported storage from libs
- Loading branch information
Showing
8 changed files
with
492 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { onConsentChange } from './onConsentChange'; | ||
import type { Callback, ConsentState } from './types'; | ||
import type { TCFv2ConsentState } from './types/tcfv2'; | ||
import {cmpGetCookie, cmpSetCookie } from './cmpCookies'; | ||
import { getCookie as getCookie_, setCookie as setCookie_} from '@guardian/libs'; | ||
|
||
jest.mock('./onConsentChange'); | ||
jest.mock('@guardian/libs', () => ({ | ||
getCookie: jest.fn(), | ||
setCookie: jest.fn(), | ||
storage: { | ||
local: { | ||
get: jest.fn(), | ||
set: jest.fn(), | ||
}, | ||
session: { | ||
get: jest.fn(), | ||
set: jest.fn(), | ||
}, | ||
} | ||
})); | ||
|
||
const tcfv2ConsentState: TCFv2ConsentState = { | ||
consents: { 1: true }, | ||
eventStatus: 'tcloaded', | ||
vendorConsents: { | ||
['5efefe25b8e05c06542b2a77']: true, | ||
}, | ||
addtlConsent: 'xyz', | ||
gdprApplies: true, | ||
tcString: 'YAAA', | ||
}; | ||
|
||
const tcfv2ConsentStateNoConsent: TCFv2ConsentState = { | ||
consents: { 1: false }, | ||
eventStatus: 'tcloaded', | ||
vendorConsents: {}, | ||
addtlConsent: 'xyz', | ||
gdprApplies: true, | ||
tcString: 'YAAA', | ||
}; | ||
|
||
const mockOnConsentChange = (consentState: ConsentState) => | ||
(onConsentChange as jest.Mock).mockImplementation((cb: Callback) => cb(consentState)); | ||
|
||
describe('cookies return the expected consent', () => { | ||
let mockContains:any; | ||
|
||
beforeEach(() => { | ||
mockContains = 'someTestData'; | ||
|
||
(getCookie_ as jest.Mock).mockImplementation(({name }: { | ||
name: string; | ||
}) => { | ||
if (name === 'gu.mock') {return mockContains} | ||
else {return(null)} | ||
}); | ||
|
||
(setCookie_ as jest.Mock).mockImplementation(({ name, value }: { | ||
name: string; | ||
value: string; | ||
}) => { | ||
if (name === 'gu.mock') {mockContains = value;} | ||
}); | ||
}); | ||
|
||
test('Targeted advertising get cookie returns null when canTarget is false', async () => { | ||
const consentState: ConsentState = { | ||
tcfv2: tcfv2ConsentState, | ||
canTarget: false, | ||
framework: 'tcfv2', | ||
}; | ||
mockOnConsentChange(consentState); | ||
const cookieValue = await cmpGetCookie({useCase: 'Targeted advertising', name: 'gu.mock'}); | ||
expect(cookieValue).toEqual(null); | ||
}); | ||
test('Targeted advertising can set and get cookies when canTarget is true', async () => { | ||
const consentState: ConsentState = { | ||
tcfv2: tcfv2ConsentState, | ||
canTarget: true, | ||
framework: 'tcfv2', | ||
}; | ||
mockOnConsentChange(consentState); | ||
const cookieValueDefault = await cmpGetCookie({useCase: 'Targeted advertising', name: 'gu.mock'}); | ||
expect(cookieValueDefault).toEqual('someTestData'); | ||
await cmpSetCookie({useCase: 'Essential', name: 'gu.mock', value: 'testdataAd'}); | ||
const cookieValue = await cmpGetCookie({useCase: 'Targeted advertising', name: 'gu.mock'}); | ||
expect(cookieValue).toEqual('testdataAd'); | ||
}); | ||
test('Essential can set and get cookies when no consents', async () => { | ||
const consentState: ConsentState = { | ||
tcfv2: tcfv2ConsentStateNoConsent, | ||
canTarget: false, | ||
framework: 'tcfv2', | ||
}; | ||
mockOnConsentChange(consentState); | ||
const cookieValueDefault = await cmpGetCookie({useCase: 'Essential', name:'gu.mock'}); | ||
expect(cookieValueDefault).toEqual('someTestData'); | ||
await cmpSetCookie({useCase: 'Essential', name: 'gu.mock', value: 'testdata'}); | ||
const cookieValue = await cmpGetCookie({useCase: 'Essential', name: 'gu.mock'}); | ||
expect(cookieValue).toEqual('testdata'); | ||
}); | ||
test('get null if cookie does not exist', async () => { | ||
const consentState: ConsentState = { | ||
tcfv2: tcfv2ConsentStateNoConsent, | ||
canTarget: false, | ||
framework: 'tcfv2', | ||
}; | ||
mockOnConsentChange(consentState); | ||
const cookieValue = await cmpGetCookie({useCase: 'Essential', name: 'gu.does_not_exist'}); | ||
expect(cookieValue).toEqual(null); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { getCookie, setCookie, setSessionCookie } from '@guardian/libs'; | ||
import type { ConsentUseCases } from './types/consentUseCases'; | ||
import { hasConsentForUseCase } from './hasConsentForUseCase'; | ||
|
||
//TODO: Write wrappers for the other cookie functions in @guardian/libs | ||
|
||
export const cmpGetCookie = async({ useCase, name, shouldMemoize, }: { | ||
useCase: ConsentUseCases, | ||
name: string; | ||
shouldMemoize?: boolean | undefined; | ||
}): Promise<string | null> => | ||
{ | ||
console.log('in cmpGetCookie'); | ||
|
||
if(await hasConsentForUseCase(useCase)) | ||
{ | ||
return getCookie({name: name, shouldMemoize: shouldMemoize}) | ||
} | ||
else | ||
{ | ||
console.error('cmp', `Cannot get cookie ${name} due to missing consent for use-case ${useCase}`) | ||
return(null) | ||
} | ||
}; | ||
|
||
export const cmpSetCookie = async ({ useCase, name, value, daysToLive, isCrossSubdomain, }: { | ||
useCase: ConsentUseCases, | ||
name: string; | ||
value: string; | ||
daysToLive?: number | undefined; | ||
isCrossSubdomain?: boolean | undefined; | ||
}): Promise<void> => | ||
{ | ||
console.log('in cmpSetCookie'); | ||
|
||
if(await hasConsentForUseCase(useCase)) | ||
{ | ||
setCookie({name:name, value:value, daysToLive:daysToLive, isCrossSubdomain:isCrossSubdomain}) | ||
} | ||
else | ||
{ | ||
console.error('cmp', `Cannot set cookie ${name} due to missing consent for use-case ${useCase}`) | ||
} | ||
}; | ||
|
||
export const cmpSetSessionCookie = async ({ useCase, name, value }: { | ||
useCase: ConsentUseCases, | ||
name: string; | ||
value: string; | ||
}): Promise<void> => | ||
{ | ||
console.log('in cmpSetSessionCookie'); | ||
|
||
if(await hasConsentForUseCase(useCase)) | ||
{ | ||
setSessionCookie({name:name, value:value}) | ||
} | ||
else | ||
{ | ||
console.error('cmp', `Cannot set cookie ${name} due to missing consent for use-case ${useCase}`) | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
3ba5a9a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage report
Test suite run success
339 tests passing in 18 suites.
Report generated by 🧪jest coverage report action from 3ba5a9a