|
| 1 | +import { renderHook } from '@testing-library/react' |
| 2 | +import type { ContextType, FC, ReactNode } from 'react' |
| 3 | +import React from 'react' |
| 4 | + |
| 5 | +import { UsercentricsContext } from '../../src/context.js' |
| 6 | +import { useAreAllConsentsAccepted } from '../../src/hooks/use-are-all-consents-accepted.js' |
| 7 | +import * as utils from '../../src/utils.js' |
| 8 | + |
| 9 | +const mockAreAllConsentsAccepted = jest.spyOn(utils, 'areAllConsentsAccepted') |
| 10 | + |
| 11 | +describe('Usercentrics', () => { |
| 12 | + describe('hooks', () => { |
| 13 | + describe('useAreAllConsentsAccepted', () => { |
| 14 | + const CONTEXT: ContextType<typeof UsercentricsContext> = { |
| 15 | + hasInteracted: false, |
| 16 | + isClientSide: true, |
| 17 | + isFailed: false, |
| 18 | + isInitialized: true, |
| 19 | + isOpen: false, |
| 20 | + localStorageState: [], |
| 21 | + ping: Symbol(), |
| 22 | + strictMode: false, |
| 23 | + } |
| 24 | + |
| 25 | + const getWrapper = |
| 26 | + (context?: Partial<ContextType<typeof UsercentricsContext>>): FC<{ children: ReactNode }> => |
| 27 | + // eslint-disable-next-line react/display-name |
| 28 | + ({ children }) => ( |
| 29 | + <UsercentricsContext.Provider value={{ ...CONTEXT, ...context }}> |
| 30 | + {children} |
| 31 | + </UsercentricsContext.Provider> |
| 32 | + ) |
| 33 | + |
| 34 | + it('should return null during SSR', () => { |
| 35 | + const { result } = renderHook(() => useAreAllConsentsAccepted(), { |
| 36 | + wrapper: getWrapper({ isClientSide: false }), |
| 37 | + }) |
| 38 | + |
| 39 | + expect(result.current).toEqual(null) |
| 40 | + }) |
| 41 | + |
| 42 | + it('should return null when not initialized and localStorage is empty', () => { |
| 43 | + const { result } = renderHook(() => useAreAllConsentsAccepted(), { |
| 44 | + wrapper: getWrapper({ isInitialized: false }), |
| 45 | + }) |
| 46 | + |
| 47 | + expect(result.current).toEqual(null) |
| 48 | + }) |
| 49 | + |
| 50 | + it('should return false when not initialized and some services in localStorage do not have consent', () => { |
| 51 | + const { result } = renderHook(() => useAreAllConsentsAccepted(), { |
| 52 | + wrapper: getWrapper({ |
| 53 | + isInitialized: false, |
| 54 | + localStorageState: [ |
| 55 | + { id: 'test-id', status: true }, |
| 56 | + { id: 'test-id2', status: false }, |
| 57 | + ], |
| 58 | + }), |
| 59 | + }) |
| 60 | + |
| 61 | + expect(result.current).toEqual(false) |
| 62 | + }) |
| 63 | + |
| 64 | + it('should return true when not initialized and all services in localStorage have consent', () => { |
| 65 | + const { result } = renderHook(() => useAreAllConsentsAccepted(), { |
| 66 | + wrapper: getWrapper({ |
| 67 | + isInitialized: false, |
| 68 | + localStorageState: [ |
| 69 | + { id: 'test-id', status: true }, |
| 70 | + { id: 'test-id2', status: true }, |
| 71 | + ], |
| 72 | + }), |
| 73 | + }) |
| 74 | + |
| 75 | + expect(result.current).toEqual(true) |
| 76 | + }) |
| 77 | + |
| 78 | + it('should return false when not all consents are given', () => { |
| 79 | + mockAreAllConsentsAccepted.mockReturnValue(false) |
| 80 | + |
| 81 | + const { result } = renderHook(() => useAreAllConsentsAccepted(), { |
| 82 | + wrapper: getWrapper(), |
| 83 | + }) |
| 84 | + |
| 85 | + expect(result.current).toEqual(false) |
| 86 | + }) |
| 87 | + |
| 88 | + it('should return true when all consents are given', () => { |
| 89 | + mockAreAllConsentsAccepted.mockReturnValue(true) |
| 90 | + |
| 91 | + const { result } = renderHook(() => useAreAllConsentsAccepted(), { |
| 92 | + wrapper: getWrapper(), |
| 93 | + }) |
| 94 | + |
| 95 | + expect(result.current).toEqual(true) |
| 96 | + }) |
| 97 | + }) |
| 98 | + }) |
| 99 | +}) |
0 commit comments