Skip to content

Commit e8f952d

Browse files
committed
tests
1 parent 3cfda69 commit e8f952d

File tree

3 files changed

+58
-12
lines changed

3 files changed

+58
-12
lines changed

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,12 @@ export const AuthModal = ({
144144
navigate('/account')
145145
return
146146
} catch (error) {
147-
form.setError('global', {type: 'manual', message: formatMessage(API_ERROR_MESSAGE)})
147+
// Show error and stop - don't fall back to passwordless
148+
form.setError('global', {
149+
type: 'manual',
150+
message: formatMessage(API_ERROR_MESSAGE)
151+
})
152+
return
148153
}
149154
}
150155
await handlePasswordlessLogin(email)
@@ -226,13 +231,14 @@ export const AuthModal = ({
226231
setCurrentView(initialView)
227232
form.reset()
228233
// Prompt user to login without username (discoverable credentials)
229-
try {
230-
loginWithPasskey()
231-
} catch (error) {
232-
setError(formatMessage(API_ERROR_MESSAGE))
234+
if (isWebAuthnEnabled) {
235+
loginWithPasskey().catch((error) => {
236+
// Silently fail passkey login, user can still use other methods
237+
console.log('Passkey login failed:', error)
238+
})
233239
}
234240
}
235-
}, [isOpen])
241+
}, [isOpen, isWebAuthnEnabled])
236242

237243
// Auto-focus the first field in each form view
238244
useEffect(() => {

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

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import userEvent from '@testing-library/user-event'
1111
import {
1212
renderWithProviders,
1313
createPathWithDefaults,
14-
guestToken
14+
guestToken,
15+
registerUserToken
1516
} from '@salesforce/retail-react-app/app/utils/test-utils'
1617
import {AuthModal, useAuthModal} from '@salesforce/retail-react-app/app/hooks/use-auth-modal'
1718
import {BrowserRouter as Router, Route} from 'react-router-dom'
@@ -20,6 +21,12 @@ import {rest} from 'msw'
2021
import {mockedRegisteredCustomer} from '@salesforce/retail-react-app/app/mocks/mock-data'
2122
import * as ReactHookForm from 'react-hook-form'
2223
import {AuthHelpers} from '@salesforce/commerce-sdk-react'
24+
import mockConfig from '@salesforce/retail-react-app/config/mocks/default'
25+
import {getConfig} from '@salesforce/pwa-kit-runtime/utils/ssr-config'
26+
27+
jest.mock('@salesforce/pwa-kit-runtime/utils/ssr-config', () => ({
28+
getConfig: jest.fn()
29+
}))
2330

2431
jest.setTimeout(60000)
2532

@@ -89,6 +96,8 @@ MockedComponent.propTypes = {
8996
// Set up and clean up
9097
beforeEach(() => {
9198
authModal = undefined
99+
// Set default config mock (passkey enabled by default in mockConfig)
100+
getConfig.mockReturnValue(mockConfig)
92101
global.server.use(
93102
rest.post('*/customers', (req, res, ctx) => {
94103
return res(ctx.delay(0), ctx.status(200), ctx.json(mockRegisteredCustomer))
@@ -225,6 +234,17 @@ describe('Passwordless enabled', () => {
225234
pathname: '/',
226235
origin: 'https://example.com'
227236
})
237+
// Disable passkey to test passwordless in isolation
238+
getConfig.mockReturnValue({
239+
...mockConfig,
240+
app: {
241+
...mockConfig.app,
242+
login: {
243+
...mockConfig.app.login,
244+
passkey: {enabled: false}
245+
}
246+
}
247+
})
228248
const {user} = renderWithProviders(<MockedComponent isPasswordlessEnabled={true} />)
229249
const validEmail = 'test@salesforce.com'
230250

@@ -274,6 +294,17 @@ describe('Passwordless enabled', () => {
274294
pathname: '/',
275295
origin: 'https://example.com'
276296
})
297+
// Disable passkey to test passwordless in isolation
298+
getConfig.mockReturnValue({
299+
...mockConfig,
300+
app: {
301+
...mockConfig.app,
302+
login: {
303+
...mockConfig.app.login,
304+
passkey: {enabled: false}
305+
}
306+
}
307+
})
277308
const {user} = renderWithProviders(<MockedComponent isPasswordlessEnabled={true} />)
278309
const validEmail = 'test@salesforce.com'
279310

@@ -673,11 +704,14 @@ describe('Passkey login', () => {
673704
await waitFor(
674705
() => {
675706
expect(mockStartWebauthnAuthentication).toHaveBeenCalled()
676-
expect(mockFinishWebauthnAuthentication).toHaveBeenCalledWith({
677-
credential: expect.objectContaining({
678-
id: 'mock-credential-id'
707+
expect(mockFinishWebauthnAuthentication).toHaveBeenCalledWith(
708+
expect.objectContaining({
709+
credential: expect.objectContaining({
710+
id: 'mock-credential-id'
711+
}),
712+
usid: expect.any(String)
679713
})
680-
})
714+
)
681715
},
682716
{timeout: 5000}
683717
)
@@ -797,6 +831,12 @@ describe('Passkey login', () => {
797831
}
798832
}
799833

834+
// Override getConfig to return config with passkey disabled
835+
getConfig.mockReturnValue({
836+
...mockConfig,
837+
app: mockAppConfig
838+
})
839+
800840
const {user} = renderWithProviders(<MockedComponent />, {
801841
wrapperProps: {
802842
appConfig: mockAppConfig,

packages/template-retail-react-app/app/hooks/use-passkey-login.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export const usePasskeyLogin = () => {
9797
credential: encodedCredential,
9898
usid
9999
})
100-
100+
101101
return
102102
}
103103

0 commit comments

Comments
 (0)