From cca60206fd952975b6dd344aee07f4b23a984467 Mon Sep 17 00:00:00 2001 From: Seth Silesky <5115498+silesky@users.noreply.github.com> Date: Wed, 1 Nov 2023 23:04:55 -0500 Subject: [PATCH] add tests --- .../src/domain/__tests__/wrapper.test.ts | 95 ++++++++++--------- .../src/test-helpers/mocks.ts | 16 +++- 2 files changed, 65 insertions(+), 46 deletions(-) diff --git a/packages/consent/consent-wrapper-onetrust/src/domain/__tests__/wrapper.test.ts b/packages/consent/consent-wrapper-onetrust/src/domain/__tests__/wrapper.test.ts index 049acfb79..a24180b79 100644 --- a/packages/consent/consent-wrapper-onetrust/src/domain/__tests__/wrapper.test.ts +++ b/packages/consent/consent-wrapper-onetrust/src/domain/__tests__/wrapper.test.ts @@ -6,20 +6,9 @@ import { OneTrustMockGlobal, analyticsMock, domainDataMock, + domainGroupMock, } from '../../test-helpers/mocks' -const grpFixture = { - StrictlyNeccessary: { - CustomGroupId: 'C0001', - }, - Targeting: { - CustomGroupId: 'C0004', - }, - Performance: { - CustomGroupId: 'C0005', - }, -} - const getConsentedGroupIdsSpy = jest .spyOn(OneTrustAPI, 'getConsentedGroupIds') .mockImplementationOnce(() => { @@ -47,7 +36,7 @@ const createWrapperSpyHelper = { * We should prefer unit tests for most functionality (see lib/__tests__) */ describe('High level "integration" tests', () => { - let resolveResolveWhen = () => {} + let checkResolveWhen = () => {} beforeEach(() => { jest .spyOn(OneTrustAPI, 'getOneTrustGlobal') @@ -58,39 +47,57 @@ describe('High level "integration" tests', () => { * Typically, resolveWhen triggers when a predicate is true. We can manually 'check' so we don't have to use timeouts. */ jest.spyOn(ConsentTools, 'resolveWhen').mockImplementation(async (fn) => { - return new Promise((_resolve) => { - resolveResolveWhen = () => { - if (fn()) { - _resolve() - } else { - throw new Error('Refuse to resolve, resolveWhen condition is false') - } + return new Promise((_resolve, _reject) => { + checkResolveWhen = () => { + fn() ? _resolve() : _reject('predicate failed.') } }) }) }) describe('shouldLoadSegment', () => { - it('should be resolved successfully', async () => { + it('should load if alert box is closed and groups are defined', async () => { + withOneTrust(analyticsMock) + + const shouldLoadP = Promise.resolve( + createWrapperSpyHelper.shouldLoadSegment({} as any) + ) + OneTrustMockGlobal.GetDomainData.mockReturnValueOnce(domainDataMock) + OneTrustMockGlobal.IsAlertBoxClosed.mockReturnValueOnce(true) + getConsentedGroupIdsSpy.mockImplementation(() => [ + domainGroupMock.StrictlyNeccessary.CustomGroupId, + ]) + checkResolveWhen() + await expect(shouldLoadP).resolves.toBeUndefined() + }) + + it('should not load at all if no groups are defined', async () => { + withOneTrust(analyticsMock) + getConsentedGroupIdsSpy.mockImplementation(() => []) + const shouldLoadP = Promise.resolve( + createWrapperSpyHelper.shouldLoadSegment({} as any) + ) + void shouldLoadP.catch(() => {}) + OneTrustMockGlobal.IsAlertBoxClosed.mockReturnValueOnce(true) + checkResolveWhen() + await expect(shouldLoadP).rejects.toEqual(expect.anything()) + }) + + it("should load regardless of AlertBox status if showAlertNotice is true (e.g. 'show banner is unchecked')", async () => { withOneTrust(analyticsMock) OneTrustMockGlobal.GetDomainData.mockReturnValueOnce({ ...domainDataMock, - Groups: [grpFixture.StrictlyNeccessary, grpFixture.Performance], + ShowAlertNotice: false, // meaning, it's open }) getConsentedGroupIdsSpy.mockImplementation(() => [ - grpFixture.StrictlyNeccessary.CustomGroupId, + domainGroupMock.StrictlyNeccessary.CustomGroupId, ]) const shouldLoadP = Promise.resolve( createWrapperSpyHelper.shouldLoadSegment({} as any) ) - let shouldLoadResolved = false - void shouldLoadP.then(() => (shouldLoadResolved = true)) - await sleep(0) - expect(shouldLoadResolved).toBe(false) - OneTrustMockGlobal.IsAlertBoxClosed.mockReturnValueOnce(true) - resolveResolveWhen() - const result = await shouldLoadP - expect(result).toBe(undefined) + OneTrustMockGlobal.IsAlertBoxClosed.mockReturnValueOnce(false) // alert box is _never open + checkResolveWhen() + await expect(shouldLoadP).resolves.toBeUndefined() }) }) @@ -100,13 +107,13 @@ describe('High level "integration" tests', () => { OneTrustMockGlobal.GetDomainData.mockReturnValue({ ...domainDataMock, Groups: [ - grpFixture.StrictlyNeccessary, - grpFixture.Performance, - grpFixture.Targeting, + domainGroupMock.StrictlyNeccessary, + domainGroupMock.Performance, + domainGroupMock.Targeting, ], }) getConsentedGroupIdsSpy.mockImplementation(() => [ - grpFixture.StrictlyNeccessary.CustomGroupId, + domainGroupMock.StrictlyNeccessary.CustomGroupId, ]) const categories = createWrapperSpyHelper.getCategories() // contain both consented and denied category @@ -124,9 +131,9 @@ describe('High level "integration" tests', () => { OneTrustMockGlobal.GetDomainData.mockReturnValue({ ...domainDataMock, Groups: [ - grpFixture.StrictlyNeccessary, - grpFixture.Performance, - grpFixture.Targeting, + domainGroupMock.StrictlyNeccessary, + domainGroupMock.Performance, + domainGroupMock.Targeting, ], }) const onCategoriesChangedCb = jest.fn() @@ -135,7 +142,7 @@ describe('High level "integration" tests', () => { createWrapperSpyHelper.registerOnConsentChanged(onCategoriesChangedCb) onCategoriesChangedCb() - resolveResolveWhen() // wait for OneTrust global to be available + checkResolveWhen() // wait for OneTrust global to be available await sleep(0) analyticsMock.track.mockImplementationOnce(() => {}) // ignore track event sent by consent changed @@ -145,17 +152,17 @@ describe('High level "integration" tests', () => { onConsentChangedArg( new CustomEvent('', { detail: [ - grpFixture.StrictlyNeccessary.CustomGroupId, - grpFixture.Performance.CustomGroupId, + domainGroupMock.StrictlyNeccessary.CustomGroupId, + domainGroupMock.Performance.CustomGroupId, ], }) ) // expect to be normalized! expect(onCategoriesChangedCb.mock.lastCall[0]).toEqual({ - [grpFixture.StrictlyNeccessary.CustomGroupId]: true, - [grpFixture.Performance.CustomGroupId]: true, - [grpFixture.Targeting.CustomGroupId]: false, + [domainGroupMock.StrictlyNeccessary.CustomGroupId]: true, + [domainGroupMock.Performance.CustomGroupId]: true, + [domainGroupMock.Targeting.CustomGroupId]: false, }) }) }) diff --git a/packages/consent/consent-wrapper-onetrust/src/test-helpers/mocks.ts b/packages/consent/consent-wrapper-onetrust/src/test-helpers/mocks.ts index 5198e326c..c070ea187 100644 --- a/packages/consent/consent-wrapper-onetrust/src/test-helpers/mocks.ts +++ b/packages/consent/consent-wrapper-onetrust/src/test-helpers/mocks.ts @@ -22,9 +22,21 @@ export const analyticsMock: jest.Mocked = { track: jest.fn(), } +export const domainGroupMock = { + StrictlyNeccessary: { + CustomGroupId: 'C0001', + }, + Targeting: { + CustomGroupId: 'C0004', + }, + Performance: { + CustomGroupId: 'C0005', + }, +} + export const domainDataMock: jest.Mocked = { - Groups: [], - ShowAlertNotice: false, + Groups: [domainGroupMock.StrictlyNeccessary], + ShowAlertNotice: true, } addDebugMockImplementation(OneTrustMockGlobal)