-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathcontact-info.test.js
More file actions
255 lines (219 loc) · 10 KB
/
contact-info.test.js
File metadata and controls
255 lines (219 loc) · 10 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
/*
* 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, within} from '@testing-library/react'
import ContactInfo from '@salesforce/retail-react-app/app/pages/checkout/partials/contact-info'
import {renderWithProviders} from '@salesforce/retail-react-app/app/utils/test-utils'
import {rest} from 'msw'
import {scapiBasketWithItem} from '@salesforce/retail-react-app/app/mocks/mock-data'
import {AuthHelpers} from '@salesforce/commerce-sdk-react'
const invalidEmail = 'invalidEmail'
const validEmail = 'test@salesforce.com'
const password = 'abc123'
const mockAuthHelperFunctions = {
[AuthHelpers.LoginRegisteredUserB2C]: {mutateAsync: jest.fn()},
[AuthHelpers.AuthorizePasswordless]: {mutateAsync: jest.fn()}
}
jest.mock('@salesforce/commerce-sdk-react', () => {
const originalModule = jest.requireActual('@salesforce/commerce-sdk-react')
return {
...originalModule,
useAuthHelper: jest
.fn()
.mockImplementation((helperType) => mockAuthHelperFunctions[helperType])
}
})
jest.mock('../util/checkout-context', () => {
return {
useCheckout: jest.fn().mockReturnValue({
customer: null,
basket: {},
isGuestCheckout: true,
setIsGuestCheckout: jest.fn(),
step: 0,
login: null,
STEPS: {CONTACT_INFO: 0},
goToStep: null,
goToNextStep: jest.fn()
})
}
})
afterEach(() => {
jest.resetModules()
})
describe('passwordless and social disabled', () => {
test('renders component', async () => {
const {user} = renderWithProviders(
<ContactInfo isPasswordlessEnabled={false} isSocialEnabled={false} />
)
// switch to login
const trigger = screen.getByText(/Already have an account\? Log in/i)
await user.click(trigger)
// open forgot password modal
const withinCard = within(screen.getByTestId('sf-toggle-card-step-0'))
const openModal = withinCard.getByText(/Forgot password\?/i)
await user.click(openModal)
// check that forgot password modal is open
const withinForm = within(screen.getByTestId('sf-auth-modal-form'))
expect(withinForm.getByText(/Reset Password/i)).toBeInTheDocument()
})
test('does not allow login if email or password is missing', async () => {
const {user} = renderWithProviders(<ContactInfo />)
// switch to login
const trigger = screen.getByText(/Already have an account\? Log in/i)
await user.click(trigger)
// attempt to login
const loginButton = screen.getByText('Log In')
await user.click(loginButton)
expect(screen.getByText('Please enter your email address.')).toBeInTheDocument()
expect(screen.getByText('Please enter your password.')).toBeInTheDocument()
})
test('allows login', async () => {
const {user} = renderWithProviders(<ContactInfo />)
// switch to login
const trigger = screen.getByText(/Already have an account\? Log in/i)
await user.click(trigger)
// enter email address and password
await user.type(screen.getByLabelText('Email'), validEmail)
await user.type(screen.getByLabelText('Password'), password)
const loginButton = screen.getByText('Log In')
await user.click(loginButton)
expect(
mockAuthHelperFunctions[AuthHelpers.LoginRegisteredUserB2C].mutateAsync
).toHaveBeenCalledWith({username: validEmail, password: password})
})
})
describe('passwordless enabled', () => {
let currentBasket = JSON.parse(JSON.stringify(scapiBasketWithItem))
beforeEach(() => {
global.server.use(
rest.put('*/baskets/:basketId/customer', (req, res, ctx) => {
currentBasket.customerInfo.email = validEmail
return res(ctx.json(currentBasket))
})
)
})
test('renders component', async () => {
const {getByRole} = renderWithProviders(<ContactInfo isPasswordlessEnabled={true} />)
expect(getByRole('button', {name: 'Checkout as Guest'})).toBeInTheDocument()
expect(getByRole('button', {name: 'Secure Link'})).toBeInTheDocument()
expect(getByRole('button', {name: 'Password'})).toBeInTheDocument()
})
test('does not allow login if email is missing', async () => {
const {user} = renderWithProviders(<ContactInfo isPasswordlessEnabled={true} />)
// Click passwordless login button
const passwordlessLoginButton = screen.getByText('Secure Link')
await user.click(passwordlessLoginButton)
expect(screen.getByText('Please enter your email address.')).toBeInTheDocument()
// Click password login button
const passwordLoginButton = screen.getByText('Password')
await user.click(passwordLoginButton)
expect(screen.getByText('Please enter your email address.')).toBeInTheDocument()
})
test('does not allow passwordless login if email is invalid', async () => {
const {user} = renderWithProviders(<ContactInfo isPasswordlessEnabled={true} />)
// enter an invalid email address
await user.type(screen.getByLabelText('Email'), invalidEmail)
const passwordlessLoginButton = screen.getByText('Secure Link')
await user.click(passwordlessLoginButton)
expect(screen.queryByTestId('sf-form-resend-passwordless-email')).not.toBeInTheDocument()
})
test('allows passwordless login', async () => {
jest.spyOn(window, 'location', 'get').mockReturnValue({
pathname: '/checkout'
})
const {user} = renderWithProviders(<ContactInfo isPasswordlessEnabled={true} />)
// enter a valid email address
await user.type(screen.getByLabelText('Email'), validEmail)
// initiate passwordless login
const passwordlessLoginButton = screen.getByText('Secure Link')
// Click the button twice as the isPasswordlessLoginClicked state doesn't change after the first click
await user.click(passwordlessLoginButton)
await user.click(passwordlessLoginButton)
expect(
mockAuthHelperFunctions[AuthHelpers.AuthorizePasswordless].mutateAsync
).toHaveBeenCalledWith({
userid: validEmail,
callbackURI:
'https://webhook.site/27761b71-50c1-4097-a600-21a3b89a546c?redirectUrl=/checkout'
})
// check that check email modal is open
await waitFor(() => {
const withinForm = within(screen.getByTestId('sf-form-resend-passwordless-email'))
expect(withinForm.getByText(/Check Your Email/i)).toBeInTheDocument()
expect(withinForm.getByText(validEmail)).toBeInTheDocument()
})
// resend the email
user.click(screen.getByText(/Resend Link/i))
expect(
mockAuthHelperFunctions[AuthHelpers.AuthorizePasswordless].mutateAsync
).toHaveBeenCalledWith({
userid: validEmail,
callbackURI:
'https://webhook.site/27761b71-50c1-4097-a600-21a3b89a546c?redirectUrl=/checkout'
})
})
test('allows login using password', async () => {
const {user} = renderWithProviders(<ContactInfo isPasswordlessEnabled={true} />)
// enter a valid email address
await user.type(screen.getByLabelText('Email'), validEmail)
// initiate login using password
const passwordButton = screen.getByText('Password')
await user.click(passwordButton)
// enter a password
await user.type(screen.getByLabelText('Password'), password)
const loginButton = screen.getByText('Log In')
await user.click(loginButton)
expect(
mockAuthHelperFunctions[AuthHelpers.LoginRegisteredUserB2C].mutateAsync
).toHaveBeenCalledWith({username: validEmail, password: password})
})
test.each([
[
'User not found',
'This feature is not currently available. You must create an account to access this feature.'
],
[
"callback_uri doesn't match the registered callbacks",
'This feature is not currently available.'
],
[
'PasswordLess Permissions Error for clientId:aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',
'This feature is not currently available.'
],
['client secret is not provided', 'This feature is not currently available.'],
['unexpected error message', 'Something went wrong. Try again!']
])(
'maps API error "%s" to the displayed error message"%s"',
async (apiErrorMessage, expectedMessage) => {
mockAuthHelperFunctions[
AuthHelpers.AuthorizePasswordless
].mutateAsync.mockImplementation(() => {
throw new Error(apiErrorMessage)
})
const {user} = renderWithProviders(<ContactInfo isPasswordlessEnabled={true} />)
await user.type(screen.getByLabelText('Email'), validEmail)
const passwordlessLoginButton = screen.getByText('Secure Link')
// Click the button twice as the isPasswordlessLoginClicked state doesn't change after the first click
await user.click(passwordlessLoginButton)
await user.click(passwordlessLoginButton)
await waitFor(() => {
expect(screen.getByText(expectedMessage)).toBeInTheDocument()
})
}
)
})
describe('social login enabled', () => {
test('renders component', async () => {
const {getByRole} = renderWithProviders(
<ContactInfo isSocialEnabled={true} idps={['google']} />
)
expect(getByRole('button', {name: 'Checkout as Guest'})).toBeInTheDocument()
expect(getByRole('button', {name: 'Password'})).toBeInTheDocument()
expect(getByRole('button', {name: /Google/i})).toBeInTheDocument()
})
})