-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathuse-product-view-modal.test.js
More file actions
145 lines (127 loc) · 4.93 KB
/
use-product-view-modal.test.js
File metadata and controls
145 lines (127 loc) · 4.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
/*
* Copyright (c) 2021, salesforce.com, 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 {Router} from 'react-router-dom'
import PropTypes from 'prop-types'
import {screen, fireEvent, waitFor} from '@testing-library/react'
import {createMemoryHistory} from 'history'
import {IntlProvider} from 'react-intl'
import mockProductDetail from '../../mocks/variant-750518699578M'
import {useProductViewModal} from './use-product-view-modal'
import {DEFAULT_LOCALE, renderWithProviders} from '../utils/test-utils'
import messages from '../static/translations/compiled/en-GB.json'
import {rest} from 'msw'
jest.mock('@salesforce/commerce-sdk-react', () => {
const originalModule = jest.requireActual('@salesforce/commerce-sdk-react')
return {
...originalModule,
useProduct: jest.fn().mockReturnValue({isFetching: false})
}
})
const mockProduct = {
...mockProductDetail,
id: '750518699660M',
variationValues: {
color: 'BLACKFB',
size: '050',
width: 'V'
},
c_color: 'BLACKFB',
c_isNew: true,
c_refinementColor: 'black',
c_size: '050',
c_width: 'V'
}
const MockComponent = ({product}) => {
const productViewModalData = useProductViewModal(product)
const [isShown, setIsShown] = React.useState(false)
return (
<div>
<button onClick={() => setIsShown(!isShown)}>Toggle the content</button>
{isShown && (
<>
<div>{productViewModalData.product.id}</div>
<div data-testid="variant">{JSON.stringify(productViewModalData.variant)}</div>
<div>{`isFetching: ${productViewModalData.isFetching}`}</div>
</>
)}
</div>
)
}
MockComponent.propTypes = {
product: PropTypes.object
}
beforeEach(() => {
global.server.use(
rest.get('*/products/:productId', (req, res, ctx) => {
return res(ctx.delay(0), ctx.json(mockProduct))
})
)
})
describe('useProductViewModal hook', () => {
test('return proper data', async () => {
const history = createMemoryHistory()
history.push('/test/path')
renderWithProviders(<MockComponent product={mockProductDetail} />)
const toggleButton = screen.getByText(/Toggle the content/)
fireEvent.click(toggleButton)
await waitFor(() => {
expect(screen.getByText('750518699578M')).toBeInTheDocument()
expect(screen.getByText(/isFetching: false/i)).toBeInTheDocument()
expect(screen.getByTestId('variant')).toHaveTextContent(
'{"orderable":true,"price":299.99,"productId":"750518699578M","variationValues":{"color":"BLACKFB","size":"038","width":"V"}}'
)
})
})
test("clean up product's related url param when unmounting product content", () => {
const history = createMemoryHistory()
history.push('/test/path')
renderWithProviders(
<Router history={history}>
<IntlProvider
locale={DEFAULT_LOCALE}
defaultLocale={DEFAULT_LOCALE}
messages={messages}
>
<MockComponent product={mockProductDetail} />
</IntlProvider>
</Router>
)
const toggleButton = screen.getByText(/Toggle the content/)
// show the content
fireEvent.click(toggleButton)
expect(history.location.pathname).toBe('/test/path')
// hide the content
fireEvent.click(toggleButton)
const searchParams = new URLSearchParams(history.location.search.toString())
waitFor(() => {
expect(searchParams.get('color')).toBeUndefined()
expect(searchParams.get('width')).toBeUndefined()
expect(searchParams.get('pid')).toBeUndefined()
})
})
test('load new variant on variant selection', async () => {
const history = createMemoryHistory()
history.push('/test/path')
renderWithProviders(
<Router history={history}>
<IntlProvider locale={DEFAULT_LOCALE} defaultLocale={DEFAULT_LOCALE}>
<MockComponent product={mockProductDetail} />
</IntlProvider>
</Router>
)
const toggleButton = screen.getByText(/Toggle the content/)
fireEvent.click(toggleButton)
expect(screen.getByText('750518699578M')).toBeInTheDocument()
history.push('/test/path?color=BLACKFB&size=050&width=V&pid=750518699660M')
await waitFor(() => {
expect(screen.getByTestId('variant')).toHaveTextContent(
'{"orderable":true,"price":299.99,"productId":"750518699660M","variationValues":{"color":"BLACKFB","size":"050","width":"V"}}'
)
})
})
})