-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathconfirmation.test.js
More file actions
293 lines (255 loc) · 11.5 KB
/
confirmation.test.js
File metadata and controls
293 lines (255 loc) · 11.5 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* 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 {Route, Switch} from 'react-router-dom'
import {rest} from 'msw'
import {
renderWithProviders,
createPathWithDefaults
} from '@salesforce/retail-react-app/app/utils/test-utils'
import Confirmation from '@salesforce/retail-react-app/app/pages/checkout/confirmation'
import {
mockOrder,
mockProducts
} from '@salesforce/retail-react-app/app/pages/checkout/confirmation.mock'
const MockedComponent = () => {
return (
<Switch>
<Route path={createPathWithDefaults('/checkout/confirmation/:orderNo')}>
<Confirmation />
</Route>
</Switch>
)
}
const mockCustomer = {
authType: 'registered',
customerId: 'registeredCustomerId',
customerNo: '00151503',
email: 'jkeane@64labs.com',
firstName: 'John',
lastName: 'Keane',
login: 'jkeane@64labs.com'
}
beforeEach(() => {
global.server.use(
rest.get('*/orders/:orderId', (req, res, ctx) => {
return res(ctx.delay(0), ctx.json(mockOrder))
}),
rest.get('*/products', (req, res, ctx) => {
return res(ctx.delay(0), ctx.json(mockProducts))
})
)
window.history.pushState(
{},
'Checkout',
createPathWithDefaults('/checkout/confirmation/000123')
)
})
test('Renders the order detail', async () => {
renderWithProviders(<MockedComponent />)
const el = await screen.findByText(mockOrder.orderNo)
expect(el).toBeInTheDocument()
})
test('Renders the Create Account form for guest customer', async () => {
renderWithProviders(<MockedComponent />, {
wrapperProps: {isGuest: true}
})
const button = await screen.findByRole('button', {name: /create account/i})
expect(button).toBeInTheDocument()
// Email should already have been auto-filled
const email = await screen.findByText(mockOrder.customerInfo.email)
expect(email).toBeInTheDocument()
const password = screen.getByLabelText('Password')
expect(password).toBeInTheDocument()
})
test('Create Account form - renders error message', async () => {
global.server.use(
rest.post('*/customers', (_, res, ctx) => {
const failedAccountCreation = {
title: 'Login Already In Use',
type: 'https://api.commercecloud.salesforce.com/documentation/error/v1/errors/login-already-in-use',
detail: 'The login is already in use.'
}
return res(ctx.status(400), ctx.json(failedAccountCreation))
})
)
const {user} = renderWithProviders(<MockedComponent />, {
wrapperProps: {isGuest: true}
})
const createAccountButton = await screen.findByRole('button', {name: /create account/i})
const passwordEl = await screen.findByLabelText('Password')
await user.type(passwordEl, 'P4ssword!')
await user.click(createAccountButton)
const alert = await screen.findByRole('alert')
expect(alert).toBeInTheDocument()
})
test('Create Account form - successful submission results in redirect to the Account page', async () => {
global.server.use(
rest.post('*/customers', (_, res, ctx) => {
return res(ctx.status(200), ctx.json(mockCustomer))
}),
rest.post('*/customers/:customerId/addresses', (_, res, ctx) => {
return res(ctx.status(200))
})
)
const {user} = renderWithProviders(<MockedComponent />, {
wrapperProps: {isGuest: true}
})
const createAccountButton = await screen.findByRole('button', {name: /create account/i})
const password = screen.getByLabelText('Password')
await user.type(password, 'P4ssword!')
await user.click(createAccountButton)
await waitFor(() => {
expect(window.location.pathname).toBe('/uk/en-GB/account')
})
})
describe('Account form', () => {
test('saves phone number from billing address to customer', async () => {
let registrationRequestBody = null
global.server.use(
rest.post('*/customers', (req, res, ctx) => {
registrationRequestBody = req.body
return res(
ctx.status(200),
ctx.json({
customerId: 'new-customer-id',
email: mockOrder.customerInfo.email,
firstName: mockOrder.billingAddress.firstName,
lastName: mockOrder.billingAddress.lastName,
phoneHome: mockOrder.shipments[0].shippingAddress.phone
})
)
}),
rest.post('*/customers/*/addresses', (_, res, ctx) => {
return res(ctx.status(200))
})
)
const {user} = renderWithProviders(<MockedComponent />, {
wrapperProps: {isGuest: true}
})
const createAccountButton = await screen.findByRole('button', {name: /create account/i})
const password = screen.getByLabelText('Password')
// Fill out the form (firstName and lastName are hidden fields pre-filled from order data)
await user.type(password, 'P4ssword!')
await user.click(createAccountButton)
// Wait for the registration request to complete
await waitFor(() => {
expect(registrationRequestBody).not.toBeNull()
})
// Verify that the phone number from the order's billing address is included in the registration
expect(registrationRequestBody.customer.phoneHome).toBe(mockOrder.billingAddress.phone)
expect(registrationRequestBody.customer.phoneHome).toBe('(778) 888-8888')
// Verify other expected customer data (firstName/lastName come from order's billingAddress)
expect(registrationRequestBody.customer.firstName).toBe(mockOrder.billingAddress.firstName)
expect(registrationRequestBody.customer.lastName).toBe(mockOrder.billingAddress.lastName)
expect(registrationRequestBody.customer.email).toBe(mockOrder.customerInfo.email)
expect(registrationRequestBody.customer.login).toBe(mockOrder.customerInfo.email)
expect(registrationRequestBody.password).toBe('P4ssword!')
})
test('Integration test - phone number from order is visible in customer account after registration', async () => {
let savedCustomerData = null
global.server.use(
// Mock customer registration
rest.post('*/customers', (req, res, ctx) => {
savedCustomerData = {
customerId: 'new-customer-id-123',
email: mockOrder.customerInfo.email,
firstName: mockOrder.billingAddress.firstName,
lastName: mockOrder.billingAddress.lastName,
phoneHome: mockOrder.billingAddress.phone,
login: mockOrder.customerInfo.email
}
return res(ctx.status(200), ctx.json(savedCustomerData))
}),
// Mock address creation
rest.post('*/customers/*/addresses', (_, res, ctx) => {
return res(ctx.status(200))
}),
// Mock customer profile fetch for account page
rest.get('*/customers/new-customer-id-123', (_, res, ctx) => {
return res(
ctx.status(200),
ctx.json({
...savedCustomerData,
addresses: [
{
addressId: 'address-1',
firstName: mockOrder.billingAddress.firstName,
lastName: mockOrder.billingAddress.lastName,
address1: mockOrder.shipments[0].shippingAddress.address1,
city: mockOrder.shipments[0].shippingAddress.city,
phone: mockOrder.billingAddress.phone,
postalCode: mockOrder.shipments[0].shippingAddress.postalCode,
stateCode: mockOrder.shipments[0].shippingAddress.stateCode,
countryCode: mockOrder.shipments[0].shippingAddress.countryCode
}
]
})
)
}),
// Mock any other account page dependencies
rest.get('*/customers/*/orders', (_, res, ctx) => {
return res(ctx.status(200), ctx.json({data: [], total: 0}))
}),
rest.get('*/customers/*/product-lists', (_, res, ctx) => {
return res(ctx.status(200), ctx.json({data: []}))
})
)
const {user} = renderWithProviders(<MockedComponent />, {
wrapperProps: {isGuest: true}
})
// Step 1: Fill out and submit the registration form
const createAccountButton = await screen.findByRole('button', {name: /create account/i})
const password = screen.getByLabelText('Password')
// Fill out the form (firstName and lastName are hidden fields pre-filled from order data)
await user.type(password, 'P4ssword!')
await user.click(createAccountButton)
// Step 2: Wait for redirect to account page
await waitFor(
() => {
expect(window.location.pathname).toBe('/uk/en-GB/account')
},
{timeout: 5000}
)
// Step 3: Verify that the customer data was saved correctly
expect(savedCustomerData).not.toBeNull()
expect(savedCustomerData.phoneHome).toBe('(778) 888-8888')
// Note: This test verifies the API calls and data flow.
// A full end-to-end test would require rendering the Account page component
// and verifying the phone number is displayed in the UI, but that would require
// additional setup of the Account page component and its dependencies.
// The key assertion is that the phone from the order's billing address
// is correctly saved to the customer's phoneHome field during registration
expect(savedCustomerData.phoneHome).toBe(mockOrder.billingAddress.phone)
})
test('successful submission redirects to the Account page even if shipping address is not saved', async () => {
global.server.use(
rest.post('*/customers', (_, res, ctx) => {
return res(ctx.status(200), ctx.json(mockCustomer))
}),
rest.post('*/customers/:customerId/addresses', (_, res, ctx) => {
const failedAddressCreation = {
title: 'Invalid Customer',
type: 'https://api.commercecloud.salesforce.com/documentation/error/v1/errors/invalid-customer',
detail: 'The customer is invalid.'
}
return res(ctx.status(400), ctx.json(failedAddressCreation))
})
)
const {user} = renderWithProviders(<MockedComponent />, {
wrapperProps: {isGuest: true}
})
const createAccountButton = await screen.findByRole('button', {name: /create account/i})
const password = screen.getByLabelText('Password')
await user.type(password, 'P4ssword!')
await user.click(createAccountButton)
await waitFor(() => {
expect(window.location.pathname).toBe('/uk/en-GB/account')
})
})
})