-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathwith-optional-commerce-sdk-react-provider.test.jsx
More file actions
105 lines (88 loc) · 3.93 KB
/
with-optional-commerce-sdk-react-provider.test.jsx
File metadata and controls
105 lines (88 loc) · 3.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
/*
* Copyright (c) 2025, 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 {render, screen} from '@testing-library/react'
import {withOptionalCommerceSdkReactProvider} from './with-optional-commerce-sdk-react-provider'
import PropTypes from 'prop-types'
jest.mock('@salesforce/commerce-sdk-react', () => ({
useCommerceApi: jest.fn(),
// eslint-disable-next-line react/prop-types
CommerceApiProvider: ({children}) => {
return <div data-testid="commerce-provider">{children}</div>
}
}))
jest.mock('@salesforce/pwa-kit-react-sdk/utils/url', () => ({
getAppOrigin: jest.fn(() => 'https://example.com')
}))
describe('withOptionalCommerceSdkReactProvider', () => {
const TestComponent = () => <div>Test Component</div>
const mockConfig = {
commerceApi: {
parameters: {
shortCode: 'test',
clientId: 'test-client',
organizationId: 'test-org',
siteId: 'test-site',
locale: 'en-US',
currency: 'USD'
},
proxyPath: '/api'
}
}
beforeEach(() => {
jest.clearAllMocks()
})
it('wraps component with CommerceApiProvider when no provider exists', () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const {useCommerceApi} = require('@salesforce/commerce-sdk-react')
useCommerceApi.mockImplementation(() => {
throw new Error('No provider')
})
const WrappedComponent = withOptionalCommerceSdkReactProvider(TestComponent, mockConfig)
const {container} = render(<WrappedComponent />)
expect(screen.getByTestId('commerce-provider')).toBeTruthy()
expect(screen.getByText('Test Component')).toBeTruthy()
})
it('does not wrap component when provider already exists', () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const {useCommerceApi} = require('@salesforce/commerce-sdk-react')
useCommerceApi.mockReturnValue({ShopperProducts: {}, ShopperBaskets: {}})
const WrappedComponent = withOptionalCommerceSdkReactProvider(TestComponent, mockConfig)
const {container} = render(<WrappedComponent />)
expect(container.querySelector('[data-testid="commerce-provider"]')).toBeNull()
expect(screen.getByText('Test Component')).toBeTruthy()
})
it('passes props to wrapped component', () => {
const TestComponentWithProps = ({testProp}) => <div>{testProp}</div>
TestComponentWithProps.propTypes = {
testProp: PropTypes.string
}
// eslint-disable-next-line @typescript-eslint/no-var-requires
const {useCommerceApi} = require('@salesforce/commerce-sdk-react')
useCommerceApi.mockImplementation(() => {
throw new Error('No provider')
})
const WrappedComponent = withOptionalCommerceSdkReactProvider(
TestComponentWithProps,
mockConfig
)
render(<WrappedComponent testProp="test value" />)
expect(screen.getByText('test value')).toBeTruthy()
})
it('renders wrapped component without provider when config is missing', () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const {useCommerceApi} = require('@salesforce/commerce-sdk-react')
useCommerceApi.mockImplementation(() => {
throw new Error('No provider')
})
const invalidConfig = {}
const WrappedComponent = withOptionalCommerceSdkReactProvider(TestComponent, invalidConfig)
const {container} = render(<WrappedComponent />)
expect(container.querySelector('[data-testid="commerce-provider"]')).toBeNull()
expect(screen.getByText('Test Component')).toBeTruthy()
})
})