Skip to content

Commit 4c6c2dd

Browse files
authored
W-13546967 - Fix change password form (#1634)
* Fix password form functionality * Add test for failed password update. * Reset for after success * Update CHANGELOG.md
1 parent 1ade451 commit 4c6c2dd

File tree

4 files changed

+56
-26
lines changed

4 files changed

+56
-26
lines changed

packages/template-retail-react-app/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545
### Bug Fixes
4646

47+
- Fix password change functionality [#1634](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/1634)
4748
- Remove internal linter rule that is missing in generated projects [#1554](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/1554)
4849
- Fix bug where you can add duplicates of the same item to the wishlist. Also fixes bug where skeleton appears when removing last item from the wishlist. [#1560](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/1560)
4950
- Replace max-age with s-maxage to only cache shared caches [#1564](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/1564)

packages/template-retail-react-app/app/mocks/mock-data.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5638,3 +5638,10 @@ export const mockCartVariant = {
56385638
c_size: '9MD',
56395639
c_width: 'Z'
56405640
}
5641+
5642+
export const mockPasswordUpdateFalure = {
5643+
title: 'Update Password',
5644+
type: 'https://api.commercecloud.salesforce.com/documentation/error/v1/errors/update-password',
5645+
detail: 'The update password request is invalid. Customer\u0027s current password is not valid',
5646+
errorMessage: 'Customer\u0027s current password is not valid'
5647+
}

packages/template-retail-react-app/app/pages/account/index.test.js

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import {
1616
mockOrderHistory,
1717
mockedGuestCustomer,
1818
mockedRegisteredCustomer,
19-
mockOrderProducts
19+
mockOrderProducts,
20+
mockPasswordUpdateFalure
2021
} from '@salesforce/retail-react-app/app/mocks/mock-data'
2122
import Account from '@salesforce/retail-react-app/app/pages/account/index'
2223
import Login from '@salesforce/retail-react-app/app/pages/login'
@@ -162,27 +163,52 @@ describe('updating profile', function () {
162163
})
163164

164165
describe('updating password', function () {
165-
beforeEach(() => {
166-
global.server.use(
167-
rest.put('*/password', (req, res, ctx) => {
168-
return res(ctx.json())
169-
})
170-
)
171-
})
172-
test('Allows customer to update password', async () => {
166+
test('Password update form is rendered correctly', async () => {
173167
const {user} = renderWithProviders(<MockedComponent />)
174168
expect(await screen.findByTestId('account-page')).toBeInTheDocument()
175169
expect(await screen.findByTestId('account-detail-page')).toBeInTheDocument()
176170

171+
const el = within(screen.getByTestId('sf-toggle-card-password'))
172+
await user.click(el.getByText(/edit/i))
173+
174+
expect(el.getByLabelText(/current password/i)).toBeInTheDocument()
175+
expect(el.getByLabelText(/new password/i)).toBeInTheDocument()
176+
expect(el.getByText(/forgot password/i)).toBeInTheDocument()
177+
})
178+
179+
test('Allows customer to update password', async () => {
180+
global.server.use(
181+
rest.put('*/password', (req, res, ctx) => res(ctx.status(204), ctx.json()))
182+
)
183+
184+
const {user} = renderWithProviders(<MockedComponent />)
185+
177186
const el = within(screen.getByTestId('sf-toggle-card-password'))
178187
await user.click(el.getByText(/edit/i))
179188
await user.type(el.getByLabelText(/current password/i), 'Password!12345')
180189
await user.type(el.getByLabelText(/new password/i), 'Password!98765')
181190
await user.click(el.getByText(/Forgot password/i))
191+
await user.click(el.getByText(/save/i))
182192

183-
expect(await screen.findByTestId('account-detail-page')).toBeInTheDocument()
193+
expect(await screen.findByText('••••••••')).toBeInTheDocument()
194+
})
195+
196+
test('Warns customer when updating password with invalid current password', async () => {
197+
global.server.use(
198+
rest.put('*/password', (req, res, ctx) =>
199+
res(ctx.status(401), ctx.json(mockPasswordUpdateFalure))
200+
)
201+
)
184202

203+
const {user} = renderWithProviders(<MockedComponent />)
204+
205+
const el = within(screen.getByTestId('sf-toggle-card-password'))
206+
await user.click(el.getByText(/edit/i))
207+
await user.type(el.getByLabelText(/current password/i), 'Password!123456')
208+
await user.type(el.getByLabelText(/new password/i), 'Password!98765')
209+
await user.click(el.getByText(/Forgot password/i))
185210
await user.click(el.getByText(/save/i))
186-
expect(await screen.findByText('••••••••')).toBeInTheDocument()
211+
212+
expect(await screen.findByTestId('password-update-error')).toBeInTheDocument()
187213
})
188214
})

packages/template-retail-react-app/app/pages/account/profile.jsx

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ const PasswordCard = () => {
234234
const {formatMessage} = useIntl()
235235

236236
const {data: customer} = useCurrentCustomer()
237-
const {isRegistered, customerId} = customer
237+
const {isRegistered, customerId, email} = customer
238238

239239
const login = useAuthHelper(AuthHelpers.LoginRegisteredUserB2C)
240240

@@ -275,24 +275,20 @@ const PasswordCard = () => {
275275
isClosable: true
276276
})
277277
login.mutate({
278-
email: values.email,
278+
username: email,
279279
password: values.password
280280
})
281281
passwordHeading?.focus()
282+
form.reset()
283+
},
284+
onError: async (err) => {
285+
const resObj = await err.response.json()
286+
form.setError('root.global', {type: 'manual', message: resObj.detail})
282287
}
283288
}
284289
)
285-
setIsEditing(false)
286-
toast({
287-
title: formatMessage({
288-
defaultMessage: 'Password updated',
289-
id: 'password_card.info.password_updated'
290-
}),
291-
status: 'success',
292-
isClosable: true
293-
})
294290
} catch (error) {
295-
form.setError('global', {type: 'manual', message: error.message})
291+
form.setError('root.global', {type: 'manual', message: error.message})
296292
}
297293
}
298294

@@ -313,11 +309,11 @@ const PasswordCard = () => {
313309
<Container variant="form">
314310
<form onSubmit={form.handleSubmit(submit)}>
315311
<Stack spacing={6}>
316-
{form.formState.errors?.global && (
317-
<Alert status="error">
312+
{form.formState.errors?.root?.global && (
313+
<Alert data-testid="password-update-error" status="error">
318314
<AlertIcon color="red.500" boxSize={4} />
319315
<Text fontSize="sm" ml={3}>
320-
{form.formState.errors.global.message}
316+
{form.formState.errors.root.global.message}
321317
</Text>
322318
</Alert>
323319
)}

0 commit comments

Comments
 (0)