Skip to content

Commit

Permalink
add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vlkerag committed Dec 18, 2023
1 parent 55bf627 commit f9c03f6
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
74 changes: 74 additions & 0 deletions src/cmpStorage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { onConsentChange } from './onConsentChange';
import type { Callback, ConsentState } from './types';
import type { TCFv2ConsentState } from './types/tcfv2';
import {hasConsentForUseCase} from './cmpStorage';

jest.mock('./onConsentChange');

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('cmpStorage.hasConsentForUseCase returns the expected consent', () => {
test('Targeted advertising when canTarget is true', async () => {
const consentState: ConsentState = {
tcfv2: tcfv2ConsentState,
canTarget: true,
framework: 'tcfv2',
};
mockOnConsentChange(consentState);
const hasConsent = await hasConsentForUseCase('Targeted advertising');
expect(hasConsent).toEqual(true);
});
test('Targeted advertising when canTarget is false', async () => {
const consentState: ConsentState = {
tcfv2: tcfv2ConsentState,
canTarget: false,
framework: 'tcfv2',
};
mockOnConsentChange(consentState);
const hasConsent = await hasConsentForUseCase('Targeted advertising');
expect(hasConsent).toEqual(false);
});
test('Targeted advertising when canTarget is true', async () => {
const consentState: ConsentState = {
tcfv2: tcfv2ConsentState,
canTarget: true,
framework: 'tcfv2',
};
mockOnConsentChange(consentState);
const hasConsent = await hasConsentForUseCase('Targeted advertising');
expect(hasConsent).toEqual(true);
});
test('Essential when no consents', async () => {
const consentState: ConsentState = {
tcfv2: tcfv2ConsentStateNoConsent,
canTarget: false,
framework: 'tcfv2',
};
mockOnConsentChange(consentState);
const hasConsent = await hasConsentForUseCase('Essential');
expect(hasConsent).toEqual(true);
});
});
6 changes: 3 additions & 3 deletions src/cmpStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ export const UseCaseOptions = [
] as const;
export type UseCases = typeof UseCaseOptions[number];

const hasConsentForUseCase = async (useCase: UseCases): Promise<boolean> =>
export const hasConsentForUseCase = async (useCase: UseCases): Promise<boolean> =>
{
const consentState = await onConsent();

/*console.log(`consentState.tcfv2?.consents['1']: ${consentState.tcfv2?.consents['1']}`);
console.log(`consentState.tcfv2?.consents['2']: ${consentState.tcfv2?.consents['2']}`);
console.log(`consentState.tcfv2?.consents['3']: ${consentState.tcfv2?.consents['3']}`);
Expand All @@ -23,8 +25,6 @@ const hasConsentForUseCase = async (useCase: UseCases): Promise<boolean> =>
console.log(`consentState.canTarget: ${consentState.canTarget}`);
*/

const consentState = await onConsent();

switch(useCase) {
case "Targeted advertising": return(consentState.canTarget)
//could be more granular than this, for example by using explicit tcf purposes:
Expand Down

1 comment on commit f9c03f6

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report

St.
Category Percentage Covered / Total
🟢 Statements 84.26% 257/305
🟡 Branches 75% 96/128
🟢 Functions 83.12% 64/77
🟢 Lines 83.78% 248/296

Test suite run success

332 tests passing in 17 suites.

Report generated by 🧪jest coverage report action from f9c03f6

Please sign in to comment.