Skip to content

Commit 22ad02c

Browse files
committed
Merge branch 'W-21056536-error-handling-passkey-registration-and-login' of https://github.com/SalesforceCommerceCloud/pwa-kit into W-21056536-error-handling-passkey-registration-and-login
2 parents e000ff0 + b2b9709 commit 22ad02c

File tree

2 files changed

+44
-65
lines changed

2 files changed

+44
-65
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ export const AuthModal = ({
270270
const isNowRegistered =
271271
(isOpen || isOtpAuthOpen) && isRegistered && (loggingIn || registering)
272272
// If the customer changed, but it's not because they logged in or registered. Do nothing.
273-
if (!isNowRegistered) {
273+
// Also ensure that the customer data is loaded.
274+
if (!isNowRegistered || !customer.data) {
274275
return
275276
}
276277

@@ -282,8 +283,7 @@ export const AuthModal = ({
282283
showRegisterPasskeyToast()
283284

284285
// Show a toast only for those registed users returning to the site.
285-
// Only show toast when customer data is available (user is logged in and data is loaded)
286-
if (loggingIn && customer.data) {
286+
if (loggingIn) {
287287
toast({
288288
variant: 'subtle',
289289
title: `${formatMessage(

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

Lines changed: 41 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,9 @@ describe('Passwordless enabled', () => {
298298
).not.toBeInTheDocument()
299299
})
300300

301-
await waitFor(
302-
() => {
303-
expect(screen.getByText(/You're now signed in./i)).toBeInTheDocument()
304-
},
305-
{timeout: 5000}
306-
)
301+
await waitFor(() => {
302+
expect(screen.getByText(/You're now signed in./i)).toBeInTheDocument()
303+
})
307304
})
308305

309306
test('allows passwordless login via Enter key', async () => {
@@ -923,72 +920,54 @@ describe('Passkey login', () => {
923920
expect(screen.queryByText(/Welcome back/i)).not.toBeInTheDocument()
924921
})
925922
})
923+
})
926924

927-
describe('Passkey Registration', () => {
928-
beforeEach(() => {
929-
getConfig.mockReturnValue({
930-
...mockConfig,
931-
app: {
932-
...mockConfig.app,
933-
login: {
934-
...mockConfig.app.login,
935-
passkey: {enabled: true}
936-
}
925+
describe('Passkey Registration', () => {
926+
beforeEach(() => {
927+
getConfig.mockReturnValue({
928+
...mockConfig,
929+
app: {
930+
...mockConfig.app,
931+
login: {
932+
...mockConfig.app.login,
933+
passkey: {enabled: true}
937934
}
938-
})
939-
940-
// Mock WebAuthn API
941-
global.PublicKeyCredential = {
942-
isUserVerifyingPlatformAuthenticatorAvailable: jest.fn().mockResolvedValue(true),
943-
isConditionalMediationAvailable: jest.fn().mockResolvedValue(true)
944935
}
945-
global.window.PublicKeyCredential = global.PublicKeyCredential
946-
947-
global.server.use(
948-
rest.post('*/oauth2/token', (req, res, ctx) =>
949-
res(
950-
ctx.delay(0),
951-
ctx.json({
952-
customer_id: 'registeredCustomerId',
953-
access_token: registerUserToken,
954-
refresh_token: 'testrefeshtoken',
955-
usid: 'testusid',
956-
enc_user_id: 'testEncUserId',
957-
id_token: 'testIdToken'
958-
})
959-
)
960-
)
961-
)
962936
})
963937

964-
afterEach(() => {
965-
delete global.PublicKeyCredential
966-
delete global.window.PublicKeyCredential
967-
})
938+
// Mock WebAuthn API
939+
global.PublicKeyCredential = {
940+
isUserVerifyingPlatformAuthenticatorAvailable: jest.fn().mockResolvedValue(true),
941+
isConditionalMediationAvailable: jest.fn().mockResolvedValue(true)
942+
}
943+
global.window.PublicKeyCredential = global.PublicKeyCredential
944+
})
968945

969-
test('shows passkey registration toast after login', async () => {
970-
const {user} = renderWithProviders(<MockedComponent isPasswordlessEnabled={true} />)
971-
const validEmail = 'test@salesforce.com'
972-
const validPassword = 'Password123!'
946+
afterEach(() => {
947+
delete global.PublicKeyCredential
948+
delete global.window.PublicKeyCredential
949+
})
973950

974-
const trigger = screen.getByText(/open modal/i)
975-
await user.click(trigger)
951+
test('shows passkey registration toast after login', async () => {
952+
const {user} = renderWithProviders(<MockedComponent isPasswordlessEnabled={true} />)
953+
const validEmail = 'test@salesforce.com'
954+
const validPassword = 'Password123!'
976955

977-
await waitFor(() => {
978-
expect(screen.getByText(/Continue/i)).toBeInTheDocument()
979-
})
956+
const trigger = screen.getByText(/open modal/i)
957+
await user.click(trigger)
980958

981-
await user.type(screen.getByLabelText('Email'), validEmail)
982-
await user.click(screen.getByText(/password/i))
983-
await user.type(screen.getByLabelText('Password'), validPassword)
984-
await user.keyboard('{Enter}')
959+
await waitFor(() => {
960+
expect(screen.getByText(/Welcome Back/i)).toBeInTheDocument()
961+
})
985962

986-
// Create passkey toast is shown after login
987-
await waitFor(() => {
988-
// 2 matches are found for the toast
989-
const toasts = screen.getAllByText(/Create Passkey/i)
990-
expect(toasts.length).toBeGreaterThanOrEqual(1)
991-
})
963+
await user.type(screen.getByLabelText('Email'), validEmail)
964+
await user.click(screen.getByText(/password/i))
965+
await user.type(screen.getByLabelText('Password'), validPassword)
966+
await user.keyboard('{Enter}')
967+
968+
// Create passkey toast is shown after login
969+
await waitFor(() => {
970+
expect(screen.getByText(/Create Passkey/i)).toBeInTheDocument()
992971
})
993972
})
994973
})

0 commit comments

Comments
 (0)