Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky committed Nov 2, 2023
1 parent 8b8571b commit cca6020
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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')
Expand All @@ -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()
})
})

Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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,
})
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,21 @@ export const analyticsMock: jest.Mocked<AnyAnalytics> = {
track: jest.fn(),
}

export const domainGroupMock = {
StrictlyNeccessary: {
CustomGroupId: 'C0001',
},
Targeting: {
CustomGroupId: 'C0004',
},
Performance: {
CustomGroupId: 'C0005',
},
}

export const domainDataMock: jest.Mocked<OneTrustDomainData> = {
Groups: [],
ShowAlertNotice: false,
Groups: [domainGroupMock.StrictlyNeccessary],
ShowAlertNotice: true,
}

addDebugMockImplementation(OneTrustMockGlobal)
Expand Down

0 comments on commit cca6020

Please sign in to comment.