-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathorders.test.js
More file actions
208 lines (195 loc) · 7.93 KB
/
orders.test.js
File metadata and controls
208 lines (195 loc) · 7.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
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
/*
* Copyright (c) 2023, 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 {Route, Switch} from 'react-router-dom'
import {screen} from '@testing-library/react'
import {rest} from 'msw'
import {
renderWithProviders,
createPathWithDefaults
} from '@salesforce/retail-react-app/app/utils/test-utils'
import {
mockCustomerBaskets,
mockOrderHistory,
mockOrderProducts
} from '@salesforce/retail-react-app/app/mocks/mock-data'
import Orders from '@salesforce/retail-react-app/app/pages/account/orders'
import mockConfig from '@salesforce/retail-react-app/config/mocks/default'
const MockedComponent = () => {
return (
<Switch>
<Route path={createPathWithDefaults('/account/orders')}>
<Orders />
</Route>
</Switch>
)
}
// Set up and clean up
beforeEach(() => {
global.server.use(
rest.get('*/customers/:customerId/baskets', (req, res, ctx) =>
res(ctx.delay(0), ctx.json(mockCustomerBaskets))
)
)
window.history.pushState({}, 'Account', createPathWithDefaults('/account/orders'))
})
afterEach(() => {
jest.resetModules()
localStorage.clear()
})
test('Renders order history and details', async () => {
global.server.use(
rest.get('*/orders/:orderNo', (req, res, ctx) => {
return res(ctx.delay(0), ctx.json(mockOrderHistory.data[0]))
}),
rest.get('*/customers/:customerId/orders', (req, res, ctx) => {
return res(ctx.delay(0), ctx.json(mockOrderHistory))
}),
rest.get('*/products', (req, res, ctx) => {
return res(ctx.delay(0), ctx.json(mockOrderProducts))
})
)
const {user} = renderWithProviders(<MockedComponent history={history} />, {
wrapperProps: {siteAlias: 'uk', appConfig: mockConfig.app}
})
expect(await screen.findByTestId('account-order-history-page')).toBeInTheDocument()
expect(await screen.findAllByText(/Ordered: /i)).toHaveLength(3)
expect(
await screen.findAllByAltText(
'Pleated Bib Long Sleeve Shirt, Silver Grey, small',
{},
{timeout: 15000}
)
).toHaveLength(3)
await user.click((await screen.findAllByText(/view details/i))[0])
expect(await screen.findByTestId('account-order-details-page')).toBeInTheDocument()
expect(await screen.findByText(/order number: 00028011/i)).toBeInTheDocument()
expect(
await screen.findByAltText(/Pleated Bib Long Sleeve Shirt, Silver Grey, small/i)
).toBeInTheDocument()
expect(
await screen.findByAltText(/Long Sleeve Crew Neck, Fire Red, small/i)
).toBeInTheDocument()
})
test('Renders order history place holder when no orders', async () => {
global.server.use(
rest.get('*/customers/:customerId/orders', (req, res, ctx) => {
return res(ctx.delay(0), ctx.json({limit: 0, offset: 0, total: 0}))
})
)
await renderWithProviders(<MockedComponent history={history} />, {
wrapperProps: {siteAlias: 'uk', appConfig: mockConfig.app}
})
expect(await screen.findByTestId('account-order-history-place-holder')).toBeInTheDocument()
})
test('Handles order with empty product list gracefully', async () => {
// Create a mock order with no products
const emptyProductOrder = {
...mockOrderHistory.data[0],
productItems: []
}
const mockOrderHistoryWithEmptyProduct = {
...mockOrderHistory,
data: [emptyProductOrder]
}
global.server.use(
rest.get('*/orders/:orderNo', (req, res, ctx) => {
return res(ctx.delay(0), ctx.json(emptyProductOrder))
}),
rest.get('*/customers/:customerId/orders', (req, res, ctx) => {
return res(ctx.delay(0), ctx.json(mockOrderHistoryWithEmptyProduct))
})
)
const {user} = renderWithProviders(<MockedComponent history={history} />, {
wrapperProps: {siteAlias: 'uk', appConfig: mockConfig.app}
})
// Order history page should render
expect(await screen.findByTestId('account-order-history-page')).toBeInTheDocument()
// Click to view details
await user.click((await screen.findAllByText(/view details/i))[0])
// Order details page should render
expect(await screen.findByTestId('account-order-details-page')).toBeInTheDocument()
// Should show 0 items
expect(await screen.findByText(/0 items/i)).toBeInTheDocument()
// Should not render any product images
expect(screen.queryByAltText(/Pleated Bib Long Sleeve Shirt/i)).not.toBeInTheDocument()
})
test('Direct navigation to order details and back to order list', async () => {
global.server.use(
rest.get('*/orders/:orderNo', (req, res, ctx) => {
return res(ctx.delay(0), ctx.json(mockOrderHistory.data[0]))
}),
rest.get('*/customers/:customerId/orders', (req, res, ctx) => {
return res(ctx.delay(0), ctx.json(mockOrderHistory))
}),
rest.get('*/products', (req, res, ctx) => {
return res(ctx.delay(0), ctx.json(mockOrderProducts))
})
)
// Set initial URL to order details
const orderNo = mockOrderHistory.data[0].orderNo
window.history.pushState(
{},
'Order Details',
createPathWithDefaults(`/account/orders/${orderNo}`)
)
const {user} = renderWithProviders(<MockedComponent history={history} />, {
wrapperProps: {siteAlias: 'uk', appConfig: mockConfig.app}
})
// Assert we are on the order details page
expect(await screen.findByTestId('account-order-details-page')).toBeInTheDocument()
expect(window.location.pathname).toMatch(new RegExp(`/account/orders/${orderNo}$`))
// Click the back link
await user.click(await screen.findByRole('link', {name: /back to order history/i}))
// Assert we are back on the order history page
expect(await screen.findByTestId('account-order-history-page')).toBeInTheDocument()
expect(window.location.pathname).toMatch(/\/account\/orders$/)
expect(await screen.findAllByText(/Ordered: /i)).toHaveLength(3)
expect(
await screen.findAllByAltText(
'Pleated Bib Long Sleeve Shirt, Silver Grey, small',
{},
{timeout: 15000}
)
).toHaveLength(3)
})
test('Handles order with missing or partial data gracefully', async () => {
// Create a mock order with missing fields
const partialOrder = {
...mockOrderHistory.data[0],
billingAddress: undefined,
shipments: undefined,
paymentInstruments: undefined,
creationDate: undefined
}
global.server.use(
rest.get('*/orders/:orderNo', (req, res, ctx) => {
return res(ctx.delay(0), ctx.json(partialOrder))
}),
rest.get('*/customers/:customerId/orders', (req, res, ctx) => {
return res(ctx.delay(0), ctx.json({...mockOrderHistory, data: [partialOrder]}))
})
)
// Set initial URL to order details
const orderNo = partialOrder.orderNo
window.history.pushState(
{},
'Order Details',
createPathWithDefaults(`/account/orders/${orderNo}`)
)
renderWithProviders(<MockedComponent history={history} />, {
wrapperProps: {siteAlias: 'uk', appConfig: mockConfig.app}
})
// Assert the order details page renders
expect(await screen.findByTestId('account-order-details-page')).toBeInTheDocument()
// Should show the Order Details header
expect(screen.getByRole('heading', {name: /order details/i})).toBeInTheDocument()
// Should not throw or crash, and should not render billing address, payment, or shipment sections
expect(screen.queryByText(/billing address/i)).not.toBeInTheDocument()
expect(screen.queryByText(/payment method/i)).not.toBeInTheDocument()
expect(screen.queryByText(/shipping address/i)).not.toBeInTheDocument()
})