-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathprovider.test.tsx
More file actions
152 lines (139 loc) · 5.93 KB
/
provider.test.tsx
File metadata and controls
152 lines (139 loc) · 5.93 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* Copyright (c) 2022, 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 React from 'react'
import {screen} from '@testing-library/react'
import useCommerceApi from './hooks/useCommerceApi'
import {renderWithProviders} from './test-utils'
import Auth from './auth'
jest.mock('./auth/index.ts')
describe('provider', () => {
beforeEach(() => {
jest.clearAllMocks()
})
test('api clients optional config are passed properly', () => {
const Component = () => {
const api = useCommerceApi()
return (
<ul>
<li>{api?.shopperSearch?.clientConfig?.headers?.['correlation-id']}</li>
<li>{api?.shopperSearch?.clientConfig?.fetchOptions?.timeout}</li>
</ul>
)
}
const config = {
headers: {'correlation-id': '373a3f80-6bbb-4157-a617-63d27fb15769'},
fetchOptions: {
timeout: 50
}
}
renderWithProviders(<Component />, config)
expect(screen.getByText(config.headers['correlation-id'])).toBeInTheDocument()
expect(screen.getByText(config.fetchOptions.timeout)).toBeInTheDocument()
})
test('Auth is initialized by invoking ready()', () => {
renderWithProviders(<h1>I can render with no problem!</h1>)
expect(screen.getByText('I can render with no problem!')).toBeInTheDocument()
expect(Auth).toHaveBeenCalledTimes(1)
const authInstance = (Auth as jest.Mock).mock.instances[0]
expect(authInstance.ready).toHaveBeenCalledTimes(1)
})
test('Auth, if initialized with `fetchedToken` short circuits auth.ready()', () => {
renderWithProviders(<h1>I can render with no problem!</h1>)
expect(screen.getByText('I can render with no problem!')).toBeInTheDocument()
expect(Auth).toHaveBeenCalledTimes(1)
const authInstance = (Auth as jest.Mock).mock.instances[0]
expect(authInstance.ready).toHaveBeenCalledTimes(1)
expect(authInstance.queueRequest).toHaveBeenCalledTimes(0)
})
test('shopper login api client uses private proxy when enabled', () => {
const Component = () => {
const api = useCommerceApi()
return (
<ul>
<li data-testid="proxy-value">{api?.shopperLogin?.clientConfig?.proxy}</li>
</ul>
)
}
const config = {
enablePWAKitPrivateClient: true,
privateClientProxyEndpoint: 'http://localhost:3000/mobify/slas/private'
}
renderWithProviders(<Component />, config)
const element = screen.getByTestId('proxy-value')
expect(element).toBeInTheDocument()
expect(element.textContent?.includes('/mobify/slas/private')).toBeTruthy()
})
test('does not call Auth.ready() when disableAuthInit is true', () => {
renderWithProviders(<h1>Auth not initialized!</h1>, {disableAuthInit: true})
expect(screen.getByText('Auth not initialized!')).toBeInTheDocument()
expect(Auth).toHaveBeenCalledTimes(1)
const authInstance = (Auth as jest.Mock).mock.instances[0]
expect(authInstance.ready).toHaveBeenCalledTimes(0)
})
test('calls Auth.ready() when disableAuthInit is false', () => {
renderWithProviders(<h1>Auth initialized!</h1>, {disableAuthInit: false})
expect(screen.getByText('Auth initialized!')).toBeInTheDocument()
expect(Auth).toHaveBeenCalledTimes(1)
const authInstance = (Auth as jest.Mock).mock.instances[0]
expect(authInstance.ready).toHaveBeenCalledTimes(1)
})
test('passes useHttpOnlySessionCookies to Auth constructor', () => {
renderWithProviders(<h1>HttpOnly cookies enabled!</h1>, {
useHttpOnlySessionCookies: true
})
expect(screen.getByText('HttpOnly cookies enabled!')).toBeInTheDocument()
expect(Auth).toHaveBeenCalledTimes(1)
expect(Auth).toHaveBeenCalledWith(
expect.objectContaining({
useHttpOnlySessionCookies: true
})
)
})
test('defaults fetchOptions.credentials to same-origin when useHttpOnlySessionCookies is true', () => {
renderWithProviders(<h1>test</h1>, {
useHttpOnlySessionCookies: true
})
expect(Auth).toHaveBeenCalledWith(
expect.objectContaining({
fetchOptions: expect.objectContaining({credentials: 'same-origin'})
})
)
})
test('overrides fetchOptions.credentials from omit to same-origin when useHttpOnlySessionCookies is true', () => {
renderWithProviders(<h1>test</h1>, {
useHttpOnlySessionCookies: true,
fetchOptions: {credentials: 'omit'}
})
expect(Auth).toHaveBeenCalledWith(
expect.objectContaining({
fetchOptions: expect.objectContaining({credentials: 'same-origin'})
})
)
})
test('keeps fetchOptions.credentials as include when useHttpOnlySessionCookies is true', () => {
renderWithProviders(<h1>test</h1>, {
useHttpOnlySessionCookies: true,
fetchOptions: {credentials: 'include'}
})
expect(Auth).toHaveBeenCalledWith(
expect.objectContaining({
fetchOptions: expect.objectContaining({credentials: 'include'})
})
)
})
test('does not modify fetchOptions.credentials when useHttpOnlySessionCookies is false', () => {
renderWithProviders(<h1>test</h1>, {
useHttpOnlySessionCookies: false,
fetchOptions: {credentials: 'omit'}
})
expect(Auth).toHaveBeenCalledWith(
expect.objectContaining({
fetchOptions: expect.objectContaining({credentials: 'omit'})
})
)
})
})