Skip to content

Commit 75e59c8

Browse files
committed
Refactor AuthModal logic and improve tests
- Simplified the condition for rendering the AuthModal by removing unnecessary checks for customer data. - Updated the welcome message to default to 'back' if the customer's first name is not available. - Adjusted the dependency array in the `useEffect` hook to only track `isRegistered`. - Enhanced tests to verify modal behavior during passkey login, ensuring proper assertions for modal visibility and user sign-in confirmation.
1 parent 5714c0b commit 75e59c8

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

packages/template-retail-react-app/app/hooks/use-auth-modal.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,7 @@ export const AuthModal = ({
275275
const isNowRegistered =
276276
(isOpen || isOtpAuthOpen) && isRegistered && (loggingIn || registering)
277277
// If the customer changed, but it's not because they logged in or registered. Do nothing.
278-
// Also ensure that the customer data is loaded.
279-
if (!isNowRegistered || !customer.data) {
278+
if (!isNowRegistered) {
280279
return
281280
}
282281

@@ -297,7 +296,7 @@ export const AuthModal = ({
297296
id: 'auth_modal.info.welcome_user'
298297
},
299298
{
300-
name: customer.data?.firstName || ''
299+
name: customer.data?.firstName || 'back'
301300
}
302301
)}`,
303302
description: `${formatMessage({
@@ -318,7 +317,7 @@ export const AuthModal = ({
318317
// Execute action to be performed on successful registration
319318
onRegistrationSuccess()
320319
}
321-
}, [isRegistered, customer.data])
320+
}, [isRegistered])
322321

323322
const onBackToSignInClick = () =>
324323
initialView === PASSWORD_VIEW ? onClose() : setCurrentView(LOGIN_VIEW)

packages/template-retail-react-app/app/hooks/use-auth-modal.test.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -922,11 +922,14 @@ describe('Passkey login', () => {
922922
await user.type(screen.getByLabelText(/^password$/i), 'Password!1')
923923
await user.click(screen.getByRole('button', {name: /sign in/i}))
924924

925-
// Wait for login to succeed and modal to close (cleanup runs and aborts passkey)
926925
await waitFor(
927926
() => {
928-
expect(screen.queryByText(/welcome back/i)).not.toBeInTheDocument()
927+
// Verify the passkey prompt was aborted
929928
expect(capturedSignal.aborted).toBe(true)
929+
// Verify the modal was closed
930+
expect(screen.queryByRole('button', {name: /sign in/i})).not.toBeInTheDocument()
931+
// Verify the user was signed in
932+
expect(screen.getByText(/You're now signed in./i)).toBeInTheDocument()
930933
},
931934
{timeout: 3000}
932935
)
@@ -958,6 +961,7 @@ describe('Passkey login', () => {
958961
const trigger = screen.getByText(/open modal/i)
959962
await user.click(trigger)
960963

964+
// Wait for modal to open
961965
await waitFor(() => {
962966
expect(screen.getByText(/welcome back/i)).toBeInTheDocument()
963967
})
@@ -1030,7 +1034,8 @@ describe('Passkey login', () => {
10301034

10311035
// login successfully and close the modal
10321036
await waitFor(() => {
1033-
expect(screen.queryByText(/Welcome back/i)).not.toBeInTheDocument()
1037+
expect(screen.queryByRole('button', {name: /Sign in/i})).not.toBeInTheDocument()
1038+
expect(screen.getByText(/You're now signed in./i)).toBeInTheDocument()
10341039
})
10351040
})
10361041
})
@@ -1062,7 +1067,11 @@ describe('Passkey Registration', () => {
10621067
})
10631068

10641069
test('shows passkey registration toast after login', async () => {
1065-
const {user} = renderWithProviders(<MockedComponent isPasswordlessEnabled={true} />)
1070+
const {user} = renderWithProviders(<MockedComponent isPasswordlessEnabled={true} />, {
1071+
wrapperProps: {
1072+
bypassAuth: false
1073+
}
1074+
})
10661075
const validEmail = 'test@salesforce.com'
10671076
const validPassword = 'Password123!'
10681077

0 commit comments

Comments
 (0)