-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathindex.test.jsx
More file actions
197 lines (151 loc) · 6.99 KB
/
index.test.jsx
File metadata and controls
197 lines (151 loc) · 6.99 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* 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, fireEvent} from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import {IntlProvider} from 'react-intl'
import PickupOrDelivery, {
DELIVERY_OPTIONS
} from '@salesforce/retail-react-app/app/components/pickup-or-delivery/index'
// Mock the shared UI components
jest.mock('@salesforce/retail-react-app/app/components/shared/ui', () => ({
// eslint-disable-next-line react/prop-types
Box: ({children, ...props}) => <div {...props}>{children}</div>,
// eslint-disable-next-line react/prop-types
Text: ({children, ...props}) => <span {...props}>{children}</span>,
// eslint-disable-next-line react/prop-types
Select: ({children, onChange, size, ...props}) => (
<select onChange={onChange} data-size={size} {...props}>
{children}
</select>
)
}))
// Helper function to render component with IntlProvider
const renderWithIntl = (component) => {
return render(
<IntlProvider locale="en" messages={{}}>
{component}
</IntlProvider>
)
}
describe('PickupOrDelivery', () => {
beforeEach(() => {
jest.clearAllMocks()
})
test('renders with default props', () => {
renderWithIntl(<PickupOrDelivery />)
const select = screen.getByTestId('delivery-option-select')
expect(select).toBeInTheDocument()
expect(select).toHaveValue(DELIVERY_OPTIONS.SHIP)
// Should not show labels by default
expect(screen.queryByText('Delivery:')).not.toBeInTheDocument()
// Should have both options
expect(screen.getByText('Ship to Address')).toBeInTheDocument()
expect(screen.getByText('Pick Up in Store')).toBeInTheDocument()
})
test('renders with showLabels=true', () => {
renderWithIntl(<PickupOrDelivery showLabels={true} />)
expect(screen.getByText('Delivery:')).toBeInTheDocument()
})
test('renders with showLabels=false', () => {
renderWithIntl(<PickupOrDelivery showLabels={false} />)
expect(screen.queryByText('Delivery:')).not.toBeInTheDocument()
})
test('renders with pickup selected', () => {
renderWithIntl(<PickupOrDelivery value={DELIVERY_OPTIONS.PICKUP} />)
const select = screen.getByTestId('delivery-option-select')
expect(select).toHaveValue(DELIVERY_OPTIONS.PICKUP)
})
test('passes size prop to Select component', () => {
renderWithIntl(<PickupOrDelivery size="md" />)
const select = screen.getByTestId('delivery-option-select')
expect(select).toBeInTheDocument()
// Check that size is passed as a data attribute
expect(select).toHaveAttribute('data-size', 'md')
})
test('disables ship option when isShipDisabled=true', () => {
renderWithIntl(<PickupOrDelivery isShipDisabled={true} />)
const shipOption = screen.getByRole('option', {name: 'Ship to Address'})
expect(shipOption).toBeDisabled()
const pickupOption = screen.getByRole('option', {name: 'Pick Up in Store'})
expect(pickupOption).not.toBeDisabled()
})
test('disables pickup option when isPickupDisabled=true', () => {
renderWithIntl(<PickupOrDelivery isPickupDisabled={true} />)
const shipOption = screen.getByRole('option', {name: 'Ship to Address'})
expect(shipOption).not.toBeDisabled()
const pickupOption = screen.getByRole('option', {name: 'Pick Up in Store'})
expect(pickupOption).toBeDisabled()
})
test('disables both options when both disabled props are true', () => {
renderWithIntl(<PickupOrDelivery isShipDisabled={true} isPickupDisabled={true} />)
const shipOption = screen.getByRole('option', {name: 'Ship to Address'})
expect(shipOption).toBeDisabled()
const pickupOption = screen.getByRole('option', {name: 'Pick Up in Store'})
expect(pickupOption).toBeDisabled()
})
test('calls onChange when selection changes', async () => {
const user = userEvent.setup()
const mockOnChange = jest.fn()
renderWithIntl(<PickupOrDelivery onChange={mockOnChange} />)
const select = screen.getByTestId('delivery-option-select')
await user.selectOptions(select, DELIVERY_OPTIONS.PICKUP)
expect(mockOnChange).toHaveBeenCalledTimes(1)
expect(mockOnChange).toHaveBeenCalledWith(DELIVERY_OPTIONS.PICKUP)
})
test('calls onChange when changing back to ship', async () => {
const user = userEvent.setup()
const mockOnChange = jest.fn()
renderWithIntl(<PickupOrDelivery value={DELIVERY_OPTIONS.PICKUP} onChange={mockOnChange} />)
const select = screen.getByTestId('delivery-option-select')
await user.selectOptions(select, DELIVERY_OPTIONS.SHIP)
expect(mockOnChange).toHaveBeenCalledTimes(1)
expect(mockOnChange).toHaveBeenCalledWith(DELIVERY_OPTIONS.SHIP)
})
test('does not call onChange when no onChange handler provided', () => {
renderWithIntl(<PickupOrDelivery />)
const select = screen.getByTestId('delivery-option-select')
// Should not throw error when no onChange handler is provided
expect(() => {
fireEvent.change(select, {target: {value: DELIVERY_OPTIONS.PICKUP}})
}).not.toThrow()
})
test('handles onChange with undefined handler gracefully', () => {
renderWithIntl(<PickupOrDelivery onChange={undefined} />)
const select = screen.getByTestId('delivery-option-select')
// Should not throw error when onChange is undefined
expect(() => {
fireEvent.change(select, {target: {value: DELIVERY_OPTIONS.PICKUP}})
}).not.toThrow()
})
test('renders with all props combined', () => {
const mockOnChange = jest.fn()
renderWithIntl(
<PickupOrDelivery
value={DELIVERY_OPTIONS.PICKUP}
onChange={mockOnChange}
isPickupDisabled={true}
isShipDisabled={false}
showLabels={true}
size="lg"
/>
)
const select = screen.getByTestId('delivery-option-select')
expect(select).toHaveValue(DELIVERY_OPTIONS.PICKUP)
expect(select).toHaveAttribute('data-size', 'lg')
expect(screen.getByText('Delivery:')).toBeInTheDocument()
const shipOption = screen.getByRole('option', {name: 'Ship to Address'})
expect(shipOption).not.toBeDisabled()
const pickupOption = screen.getByRole('option', {name: 'Pick Up in Store'})
expect(pickupOption).toBeDisabled()
})
test('exports DELIVERY_OPTIONS constants', () => {
expect(DELIVERY_OPTIONS).toBeDefined()
expect(DELIVERY_OPTIONS.SHIP).toBe('ship')
expect(DELIVERY_OPTIONS.PICKUP).toBe('pickup')
})
})