-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathuseDNT.test.ts
More file actions
68 lines (61 loc) · 2.24 KB
/
useDNT.test.ts
File metadata and controls
68 lines (61 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* Copyright (c) 2024, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import useDNT from './useDNT'
import useAuthContext from './useAuthContext'
import useConfig from './useConfig'
jest.mock('./useAuthContext')
jest.mock('./useConfig')
const mockedUseAuthContext = useAuthContext as jest.MockedFunction<typeof Object>
const mockedUseConfig = useConfig as jest.MockedFunction<typeof Object>
const mockSetDnt = jest.fn()
const mockGetDnt = jest.fn()
describe('useDNT tests', () => {
beforeEach(() => {
mockedUseConfig.mockReset()
mockGetDnt.mockReset()
mockGetDnt.mockReturnValue(true)
mockedUseConfig.mockReturnValueOnce({
defaultDnt: true
})
mockedUseAuthContext.mockReturnValue({
refreshAccessToken: jest.fn(),
get: (param: string) => {
if (param === 'customer_type') return 'registered'
if (param === 'refresh_token_expires_in') return 7776000
},
getDnt: mockGetDnt,
setDnt: mockSetDnt,
parseSlasJWT: () => {
return {dnt: '1'}
}
})
})
it('updateDNT should create dw_dnt cookie', async () => {
const {updateDNT} = useDNT()
await updateDNT(true)
expect(mockSetDnt).toHaveBeenCalledWith(true)
})
it('selectedDnt should be false if dw_dnt cookie is "1"', () => {
const {selectedDnt} = useDNT()
expect(selectedDnt).toBe(true)
})
it('selectedDnt should be false if dw_dnt cookie is "0"', () => {
mockGetDnt.mockReturnValue(false)
const {selectedDnt} = useDNT()
expect(selectedDnt).toBe(false)
})
it('selectedDnt should be undefined if dw_dnt cookie is not defined', () => {
mockGetDnt.mockReturnValueOnce(undefined)
const {selectedDnt} = useDNT()
expect(selectedDnt).toBeUndefined()
})
it('selectedDnt should be undefined if dw_dnt cookie is invalid', () => {
mockGetDnt.mockReturnValueOnce(undefined)
const {selectedDnt} = useDNT()
expect(selectedDnt).toBeUndefined()
})
})