-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathdrawer-menu.test.js
More file actions
225 lines (186 loc) · 8.49 KB
/
drawer-menu.test.js
File metadata and controls
225 lines (186 loc) · 8.49 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* 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 {screen, waitFor} from '@testing-library/react'
import {DrawerMenu} from '@salesforce/retail-react-app/app/components/drawer-menu'
import {renderWithProviders} from '@salesforce/retail-react-app/app/utils/test-utils'
import {mockCategories} from '@salesforce/retail-react-app/app/mocks/mock-data'
// Mock the hooks and modules
jest.mock('@salesforce/retail-react-app/app/hooks/use-navigation')
jest.mock('@salesforce/retail-react-app/app/hooks/use-multi-site')
jest.mock('@salesforce/commerce-sdk-react', () => ({
...jest.requireActual('@salesforce/commerce-sdk-react'),
useCustomerType: jest.fn(),
useAuthHelper: jest.fn(),
AuthHelpers: {
Logout: 'logout'
}
}))
import useNavigation from '@salesforce/retail-react-app/app/hooks/use-navigation'
import useMultiSite from '@salesforce/retail-react-app/app/hooks/use-multi-site'
import {useCustomerType, useAuthHelper} from '@salesforce/commerce-sdk-react'
describe('DrawerMenu', () => {
const mockNavigate = jest.fn()
const mockLogout = {
mutateAsync: jest.fn().mockResolvedValue({})
}
const mockOnClose = jest.fn()
const mockOnLogoClick = jest.fn()
beforeEach(() => {
jest.clearAllMocks()
// Setup default mocks
useNavigation.mockReturnValue(mockNavigate)
useMultiSite.mockReturnValue({
site: {
l10n: {
supportedLocales: [{id: 'en-US'}, {id: 'fr-FR'}, {id: 'de-DE'}]
}
},
buildUrl: jest.fn()
})
useCustomerType.mockReturnValue({isRegistered: false})
useAuthHelper.mockReturnValue(mockLogout)
})
test('Renders DrawerMenu without errors', async () => {
renderWithProviders(<DrawerMenu isOpen={true} root={mockCategories.root} />)
const drawer = document.querySelector('.chakra-portal')
const accordion = document.querySelector('.chakra-accordion')
const socialIcons = document.querySelector('.sf-social-icons')
expect(drawer).toBeInTheDocument()
expect(accordion).toBeInTheDocument()
expect(socialIcons).toBeInTheDocument()
})
test('Renders DrawerMenu Spinner without root', async () => {
renderWithProviders(<DrawerMenu isOpen={true} />, {
wrapperProps: {initialCategories: {}}
})
const spinner = document.querySelector('.chakra-spinner')
expect(spinner).toBeInTheDocument()
})
/*
* DO NOT REMOVE THIS COMMENT! This test was generated by Cursor
* This test was generated with the following model: Claude Sonnet 4
*/
test('renders drawer in closed state', () => {
renderWithProviders(<DrawerMenu isOpen={false} root={mockCategories.root} />)
// When drawer is closed, the drawer content should not be visible
const drawerContent = document.querySelector('.chakra-modal__content')
expect(drawerContent).not.toBeInTheDocument()
})
/*
* DO NOT REMOVE THIS COMMENT! This test was generated by Cursor
* This test was generated with the following model: Claude Sonnet 4
*/
test('calls onClose when close button is clicked', async () => {
const {user} = renderWithProviders(
<DrawerMenu isOpen={true} root={mockCategories.root} onClose={mockOnClose} />
)
const closeButton = screen.getByRole('button', {name: /close/i})
await user.click(closeButton)
expect(mockOnClose).toHaveBeenCalledTimes(1)
})
/*
* DO NOT REMOVE THIS COMMENT! This test was generated by Cursor
* This test was generated with the following model: Claude Sonnet 4
*/
test('calls onLogoClick when logo is clicked', async () => {
const {user} = renderWithProviders(
<DrawerMenu isOpen={true} root={mockCategories.root} onLogoClick={mockOnLogoClick} />
)
// The logo button is the first button in the header that contains the brand-logo icon
const logoButton = screen.getAllByRole('button')[0]
await user.click(logoButton)
expect(mockOnLogoClick).toHaveBeenCalledTimes(1)
})
/*
* DO NOT REMOVE THIS COMMENT! This test was generated by Cursor
* This test was generated with the following model: Claude Sonnet 4
*/
test('displays sign in link for guest users', () => {
useCustomerType.mockReturnValue({isRegistered: false})
renderWithProviders(<DrawerMenu isOpen={true} root={mockCategories.root} />)
const signInLink = screen.getByRole('link', {name: /sign in/i})
expect(signInLink).toBeInTheDocument()
// The Link component from retail-react-app may not set href directly since it uses react-router
// Check that the link exists rather than checking specific href attribute
expect(signInLink).toHaveClass('chakra-link')
})
/*
* DO NOT REMOVE THIS COMMENT! This test was generated by Cursor
* This test was generated with the following model: Claude Sonnet 4
*/
test('displays user account menu for registered users', () => {
useCustomerType.mockReturnValue({isRegistered: true})
renderWithProviders(<DrawerMenu isOpen={true} root={mockCategories.root} />)
const myAccountButton = screen.getByRole('button', {name: /my account/i})
expect(myAccountButton).toBeInTheDocument()
})
/*
* DO NOT REMOVE THIS COMMENT! This test was generated by Cursor
* This test was generated with the following model: Claude Sonnet 4
*/
test('expands user account menu and shows account options', async () => {
useCustomerType.mockReturnValue({isRegistered: true})
const {user} = renderWithProviders(<DrawerMenu isOpen={true} root={mockCategories.root} />)
const myAccountButton = screen.getByRole('button', {name: /my account/i})
await user.click(myAccountButton)
await waitFor(() => {
expect(screen.getByText('Account Details')).toBeInTheDocument()
expect(screen.getByText('Order History')).toBeInTheDocument()
expect(screen.getByText('Addresses')).toBeInTheDocument()
})
})
/*
* DO NOT REMOVE THIS COMMENT! This test was generated by Cursor
* This test was generated with the following model: Claude Sonnet 4
*/
test('handles logout action for registered users', async () => {
useCustomerType.mockReturnValue({isRegistered: true})
const {user} = renderWithProviders(<DrawerMenu isOpen={true} root={mockCategories.root} />)
// First expand the account menu
const myAccountButton = screen.getByRole('button', {name: /my account/i})
await user.click(myAccountButton)
// Then click the logout button
const logoutButton = await screen.findByRole('button', {name: /log out/i})
await user.click(logoutButton)
expect(mockLogout.mutateAsync).toHaveBeenCalledTimes(1)
await waitFor(() => {
expect(mockNavigate).toHaveBeenCalledWith('/login')
})
})
/*
* DO NOT REMOVE THIS COMMENT! This test was generated by Cursor
* This test was generated with the following model: Claude Sonnet 4
*/
test('renders category navigation when root categories are provided', () => {
renderWithProviders(<DrawerMenu isOpen={true} root={mockCategories.root} />)
// Check that category navigation is rendered
const categoryNav = document.querySelector('#category-nav')
expect(categoryNav).toBeInTheDocument()
expect(categoryNav).toHaveAttribute('aria-live', 'polite')
expect(categoryNav).toHaveAttribute('aria-atomic', 'true')
})
/*
* DO NOT REMOVE THIS COMMENT! This test was generated by Cursor
* This test was generated with the following model: Claude Sonnet 4
*/
test('renders with custom itemsKey and itemsCountKey props', () => {
const customRoot = {
customItems: [{id: 'test-item', name: 'Test Item', customItems: []}]
}
renderWithProviders(
<DrawerMenu
isOpen={true}
root={customRoot}
itemsKey="customItems"
itemsCountKey="customItemsCount"
/>
)
const categoryNav = document.querySelector('#category-nav')
expect(categoryNav).toBeInTheDocument()
})
})