forked from SalesforceCommerceCloud/pwa-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
confirmation.test.js
252 lines (221 loc) · 7.79 KB
/
confirmation.test.js
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
/*
* 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
*/
/* eslint-disable no-unused-vars */
import React from 'react'
import {screen, waitFor} from '@testing-library/react'
import user from '@testing-library/user-event'
import {rest} from 'msw'
import {renderWithProviders, createPathWithDefaults} from '../../utils/test-utils'
import Confirmation from './confirmation'
import {keysToCamel} from '../../commerce-api/utils'
import useBasket from '../../commerce-api/hooks/useBasket'
import useShopper from '../../commerce-api/hooks/useShopper'
import {ocapiOrderResponse} from '../../commerce-api/mock-data'
import {mockedGuestCustomer, exampleTokenReponse} from '../../commerce-api/mock-data'
jest.mock('../../commerce-api/auth', () => {
return class AuthMock {
login() {
return mockedGuestCustomer
}
}
})
const mockOrder = keysToCamel({
basket_id: 'testorderbasket',
...ocapiOrderResponse
})
const mockBasketOrder = {
baskets: [mockOrder]
}
const mockProducts = {
data: [
{
id: 'SimpleProduct',
currency: 'USD',
imageGroups: [
{
images: [
{
alt: 'alttext',
disBaseLink: '/image',
link: '/image',
title: 'simpleproduct'
}
],
viewType: 'small'
}
],
name: 'Simple Product',
price: 46.99,
variationAttributes: [
{
id: 'color',
name: 'Color',
values: [
{
name: 'Grey Heather Multi',
orderable: true,
value: 'JJ1MCE6'
},
{
name: 'Begonia Multi',
orderable: true,
value: 'JJHL3XX'
}
]
},
{
id: 'size',
name: 'Size',
values: [
{
name: 'S',
orderable: true,
value: '9SM'
},
{
name: 'M',
orderable: true,
value: '9MD'
},
{
name: 'L',
orderable: true,
value: '9LG'
},
{
name: 'XL',
orderable: true,
value: '9XL'
}
]
}
],
variationValues: {
color: 'JJ1MCE6',
size: '9MD'
}
}
]
}
const WrappedConfirmation = () => {
useShopper()
const basket = useBasket()
if (basket?._type !== 'order') {
return null
}
return <Confirmation />
}
// Set up and clean up
beforeAll(() => {
jest.resetModules()
// Since we're testing some navigation logic, we are using a simple Router
// around our component. We need to initialize the default route/path here.
window.history.pushState({}, 'Account', createPathWithDefaults('/account'))
})
beforeEach(() => {
global.server.use(
rest.get('*/baskets*', (_, res, ctx) => {
return res(ctx.json(keysToCamel(mockBasketOrder)))
}),
rest.post('*/customers/actions/login', (_, res, ctx) => {
return res(
ctx.json(mockedGuestCustomer),
ctx.set('Authorization', exampleTokenReponse.access_token)
)
}),
rest.post('*/customers', (_, res, ctx) => {
const successfulAccountCreation = {
authType: 'registered',
creationDate: '2021-05-03T07:04:56.566Z',
customerId: 'abQfkJHegtUQfaCBRL5AjuTKY7',
customerNo: '00154003',
email: '[email protected]',
enabled: true,
firstName: 'John',
lastModified: '2021-05-03T07:04:56.572Z',
lastName: 'Smith',
login: '[email protected]'
}
return res(ctx.json(successfulAccountCreation))
}),
rest.get('*/customers/:customerId', (req, res, ctx) => {
return res(
ctx.delay(0),
ctx.status(200),
ctx.json({
authType: 'guest',
customerId: 'customerid'
})
)
}),
rest.get('*/products', (req, res, ctx) => {
return res(ctx.delay(0), ctx.status(200), ctx.json(mockProducts))
})
)
})
afterEach(() => {
localStorage.clear()
sessionStorage.clear()
window.history.pushState({}, 'Account', createPathWithDefaults('/account'))
})
test('Navigates to homepage when no order present', async () => {
renderWithProviders(<Confirmation />, {
wrapperProps: {siteAlias: 'uk', locale: {id: 'en-GB'}}
})
expect(screen.queryByTestId('sf-checkout-confirmation-container')).not.toBeInTheDocument()
await waitFor(() => {
expect(window.location.pathname).toEqual('/')
})
})
test('Renders the order detail when present', async () => {
renderWithProviders(<WrappedConfirmation />)
const rootEl = await screen.findByTestId(
'sf-checkout-confirmation-container',
{},
{timeout: 15000}
)
expect(rootEl).toBeInTheDocument()
})
test('Renders the Create Account form for guest customer', async () => {
renderWithProviders(<WrappedConfirmation />)
const button = await screen.findByRole('button', {name: /create account/i})
expect(button).toBeInTheDocument()
// Email should already have been auto-filled
const email = screen.getByDisplayValue('[email protected]')
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.json(failedAccountCreation))
})
)
renderWithProviders(<WrappedConfirmation />)
const createAccountButton = await screen.findByRole('button', {name: /create account/i})
const password = screen.getByLabelText('Password')
user.type(password, 'P4ssword!')
user.click(createAccountButton)
const alert = await screen.findByRole('alert', {}, {timeout: 2000})
expect(alert).toBeInTheDocument()
})
test('Create Account form - successful submission results in redirect to the Account page', async () => {
renderWithProviders(<WrappedConfirmation />)
const createAccountButton = await screen.findByRole('button', {name: /create account/i})
const password = screen.getByLabelText('Password')
user.type(password, 'P4ssword!')
user.click(createAccountButton)
await waitFor(() => {
expect(window.location.pathname).toEqual('/uk/en-GB/account')
})
})