Skip to content

Commit

Permalink
address PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky committed Jul 5, 2023
1 parent bd181c9 commit 14630d3
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const createConsentStampingMiddleware: CreateConsentMw =
}
payload.obj.context.consent = {
...payload.obj.context.consent,
categoryPreferences: await getCategories(),
categoryPreferences: categories,
}
next(payload)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { validateCategories, validateOptions } from '../options-validators'
import { ValidationError } from '../validation-error'

describe(validateOptions, () => {
it('should throw if options is not a plain object', () => {
expect(() => validateOptions(null as any)).toThrow()
expect(() => validateOptions(undefined as any)).toThrow()
expect(() => validateOptions('hello' as any)).toThrow()
})

it('should throw an instance of ValidationError', () => {
expect(() => validateOptions(null as any)).toThrowError(Error)
expect(() => validateOptions(null as any)).toThrowError(ValidationError)
})

it('should throw with the expected error', () => {
expect(() =>
validateOptions(null as any)
).toThrowErrorMatchingInlineSnapshot(
`"[Validation] Options should be an object (Received: null)"`
)
})

it('should throw if required property(s) are not included', () => {
expect(() => validateOptions({} as any)).toThrow()
})

it('should throw if getCategories() is not a function', () => {
expect(() =>
validateOptions({
getCategories: {},
})
).toThrow()
})
})

describe(validateCategories, () => {
it('should throw if categories is not a plain object', () => {
expect(() => validateCategories(null)).toThrow()
expect(() => validateCategories(undefined)).toThrow()
expect(() => validateCategories('hello')).toThrow()
})

it('should throw an instance of ValidationError', () => {
expect(() => validateCategories(null)).toThrowError(Error)
expect(() => validateCategories(null)).toThrowError(ValidationError)
})

it('should throw with the expected error', () => {
expect(() => validateCategories(null)).toThrowErrorMatchingInlineSnapshot(
`"[Validation] Consent Categories should be {[categoryName: string]: boolean} (Received: null)"`
)
})

it('should throw if categories does not match {categoryName: boolean}', () => {
expect(() => validateCategories({})).not.toThrow() // if getCategories is empty object, it is the same as 'consent to all categories' if false
expect(() => validateCategories({ a: true })).not.toThrow()
expect(() => validateCategories({ a: 'foo' })).toThrow()
expect(() => validateCategories({ a: 'true' })).toThrow()
expect(() => validateCategories({ a: true, b: 'foo' })).toThrow()
expect(() => validateCategories({ a: true, b: 'foo', c: true })).toThrow()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@ import { ValidationError } from './validation-error'
export function validateCategories(
ctgs: unknown
): asserts ctgs is NonNullable<Categories> {
let hasError = true
if (ctgs && typeof ctgs === 'object' && !Array.isArray(ctgs)) {
hasError = false
for (const k in ctgs) {
if (typeof (ctgs as any)[k] === 'boolean') {
return
if (typeof (ctgs as any)[k] !== 'boolean') {
hasError = true
break
}
}
}
throw new ValidationError(
`Consent Categories should be {[categoryName: string]: boolean}`,
ctgs
)
if (hasError) {
throw new ValidationError(
`Consent Categories should be {[categoryName: string]: boolean}`,
ctgs
)
}
}

export function validateOptions(options: {
Expand Down

0 comments on commit 14630d3

Please sign in to comment.