Skip to content

Commit 6f45d2b

Browse files
committed
Refactor authentication modal tests and cleanup unused constants
- Removed unused error message constants from use-auth-modal.js. - Updated test-utils.js to include a function for clearing cookies after tests. - Enhanced passwordless login tests to ensure proper modal behavior and state management.
1 parent 57d8961 commit 6f45d2b

File tree

4 files changed

+92
-66
lines changed

4 files changed

+92
-66
lines changed

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,13 @@ import OtpAuth from '@salesforce/retail-react-app/app/components/otp-auth'
3333
import {noop} from '@salesforce/retail-react-app/app/utils/utils'
3434
import {
3535
API_ERROR_MESSAGE,
36-
FEATURE_UNAVAILABLE_ERROR_MESSAGE,
37-
PASSWORDLESS_ERROR_MESSAGES,
3836
INVALID_TOKEN_ERROR,
3937
INVALID_TOKEN_ERROR_MESSAGE
4038
} from '@salesforce/retail-react-app/app/constants'
4139
import {
4240
getPasswordlessErrorMessage,
4341
getPasswordResetErrorMessage
4442
} from '@salesforce/retail-react-app/app/utils/auth-utils'
45-
import {API_ERROR_MESSAGE} from '@salesforce/retail-react-app/app/constants'
4643
import useNavigation from '@salesforce/retail-react-app/app/hooks/use-navigation'
4744
import {usePrevious} from '@salesforce/retail-react-app/app/hooks/use-previous'
4845
import {usePasswordReset} from '@salesforce/retail-react-app/app/hooks/use-password-reset'

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

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import userEvent from '@testing-library/user-event'
1111
import {
1212
renderWithProviders,
1313
createPathWithDefaults,
14-
guestToken
14+
guestToken,
15+
registerUserToken,
16+
clearAllCookies
1517
} from '@salesforce/retail-react-app/app/utils/test-utils'
1618
import {AuthModal, useAuthModal} from '@salesforce/retail-react-app/app/hooks/use-auth-modal'
1719
import {BrowserRouter as Router, Route} from 'react-router-dom'
@@ -109,6 +111,7 @@ beforeEach(() => {
109111
})
110112
afterEach(() => {
111113
localStorage.clear()
114+
clearAllCookies()
112115
jest.resetModules()
113116
jest.restoreAllMocks()
114117
})
@@ -199,6 +202,20 @@ describe('Passwordless enabled', () => {
199202
global.server.use(
200203
rest.post('*/oauth2/passwordless/login', (req, res, ctx) => {
201204
return res(ctx.delay(0), ctx.status(200), ctx.json({}))
205+
}),
206+
rest.post('*/oauth2/passwordless/token', (req, res, ctx) => {
207+
return res(
208+
ctx.delay(0),
209+
ctx.status(200),
210+
ctx.json({
211+
customer_id: 'registeredCustomerId',
212+
access_token: registerUserToken,
213+
refresh_token: 'testrefeshtoken',
214+
usid: 'testusid',
215+
enc_user_id: 'testEncUserId',
216+
id_token: 'testIdToken'
217+
})
218+
)
202219
})
203220
)
204221
})
@@ -216,11 +233,11 @@ describe('Passwordless enabled', () => {
216233
})
217234

218235
test('Allows passwordless login', async () => {
219-
jest.spyOn(window, 'location', 'get').mockReturnValue({
220-
pathname: '/',
221-
origin: 'https://example.com'
236+
const {user} = renderWithProviders(<MockedComponent isPasswordlessEnabled={true} />, {
237+
wrapperProps: {
238+
bypassAuth: false
239+
}
222240
})
223-
const {user} = renderWithProviders(<MockedComponent isPasswordlessEnabled={true} />)
224241
const validEmail = 'test@salesforce.com'
225242

226243
// open the modal
@@ -238,24 +255,19 @@ describe('Passwordless enabled', () => {
238255
const passwordlessLoginButton = screen.getByText(/Continue/i)
239256
await user.click(passwordlessLoginButton)
240257

241-
// check that OTP auth modal is open
242-
await waitFor(
243-
() => {
244-
expect(
245-
screen.getByText(/To log in to your account, enter the code/i)
246-
).toBeInTheDocument()
247-
},
248-
{timeout: 5000}
249-
)
250-
251-
// resend the email
252-
await user.click(screen.getByText(/Resend Link/i))
258+
// check that the auth modal is closed
259+
expect(authModal.isOpen).toBe(false)
253260

254-
// check that check email modal is still open
261+
// check that OTP auth modal is open
255262
await waitFor(() => {
256-
expect(screen.getByText(/Check Your Email/i)).toBeInTheDocument()
263+
expect(
264+
screen.getByText(/To log in to your account, enter the code/i)
265+
).toBeInTheDocument()
257266
})
258267

268+
// resend the email
269+
await user.click(screen.getByText(/Resend Code/i))
270+
259271
// enter the code manually
260272
const code = '12345678'
261273
const otpInputs = screen.getAllByRole('textbox')
@@ -265,18 +277,16 @@ describe('Passwordless enabled', () => {
265277

266278
await waitFor(() => {
267279
expect(
268-
mockAuthHelperFunctions[AuthHelpers.LoginPasswordlessUser].mutateAsync
269-
).toHaveBeenCalledWith({
270-
pwdlessLoginToken: code
271-
})
280+
screen.queryByText(/To log in to your account, enter the code/i)
281+
).not.toBeInTheDocument()
282+
})
283+
284+
await waitFor(() => {
285+
expect(screen.getByText(/You're now signed in./i)).toBeInTheDocument()
272286
})
273287
})
274288

275289
test('allows passwordless login via Enter key', async () => {
276-
jest.spyOn(window, 'location', 'get').mockReturnValue({
277-
pathname: '/',
278-
origin: 'https://example.com'
279-
})
280290
const {user} = renderWithProviders(<MockedComponent isPasswordlessEnabled={true} />)
281291
const validEmail = 'test@salesforce.com'
282292

@@ -294,15 +304,15 @@ describe('Passwordless enabled', () => {
294304
// simulate Enter key press in email field
295305
await user.keyboard('{Enter}')
296306

297-
// check that check OtpAuthModal is open
298-
await waitFor(
299-
() => {
300-
expect(
301-
screen.getByText(/To log in to your account, enter the code/i)
302-
).toBeInTheDocument()
303-
},
304-
{timeout: 5000}
305-
)
307+
// check that the auth modal is closed
308+
expect(authModal.isOpen).toBe(false)
309+
310+
// check that the OtpAuthModal is open
311+
await waitFor(() => {
312+
expect(
313+
screen.getByText(/To log in to your account, enter the code/i)
314+
).toBeInTheDocument()
315+
})
306316
})
307317

308318
test('sends callbackURI when passwordless callback is configured', async () => {
@@ -319,11 +329,6 @@ describe('Passwordless enabled', () => {
319329
}
320330
})
321331

322-
jest.spyOn(window, 'location', 'get').mockReturnValue({
323-
pathname: '/',
324-
origin: 'https://example.com'
325-
})
326-
327332
const {user} = renderWithProviders(<MockedComponent isPasswordlessEnabled={true} />)
328333
const validEmail = 'test@salesforce.com'
329334

@@ -337,8 +342,14 @@ describe('Passwordless enabled', () => {
337342
await user.type(screen.getByLabelText('Email'), validEmail)
338343
await user.click(screen.getByText(/Continue/i))
339344

345+
// check that the auth modal is closed
346+
expect(authModal.isOpen).toBe(false)
347+
348+
// check that the OtpAuthModal is open
340349
await waitFor(() => {
341-
expect(screen.getByText(/Check Your Email/i)).toBeInTheDocument()
350+
expect(
351+
screen.getByText(/To log in to your account, enter the code/i)
352+
).toBeInTheDocument()
342353
})
343354
})
344355

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

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import ResetPassword from '@salesforce/retail-react-app/app/pages/reset-password
2020
import mockConfig from '@salesforce/retail-react-app/config/mocks/default'
2121
import {mockedRegisteredCustomer} from '@salesforce/retail-react-app/app/mocks/mock-data'
2222
import {getConfig} from '@salesforce/pwa-kit-runtime/utils/ssr-config'
23-
import {AuthHelpers} from '@salesforce/commerce-sdk-react'
2423

2524
const mockMergedBasket = {
2625
basketId: 'a10ff320829cb0eef93ca5310a',
@@ -328,6 +327,21 @@ describe('Passwordless login tests', () => {
328327
global.server.use(
329328
rest.post('*/oauth2/passwordless/login', (req, res, ctx) => {
330329
return res(ctx.delay(0), ctx.status(200), ctx.json({}))
330+
}),
331+
rest.post('*/oauth2/passwordless/token', (req, res, ctx) => {
332+
return res(
333+
ctx.delay(0),
334+
ctx.status(200),
335+
ctx.json({
336+
customer_id: 'customerid_1',
337+
access_token:
338+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdXQiOiJHVUlEIiwic2NwIjoic2ZjYy5zaG9wcGVyLW15YWNjb3VudC5iYXNrZXRzIHNmY2Muc2hvcHBlci1teWFjY291bnQuYWRkcmVzc2VzIHNmY2Muc2hvcHBlci1wcm9kdWN0cyBzZmNjLnNob3BwZXItZGlzY292ZXJ5LXNlYXJjaCBzZmNjLnNob3BwZXItbXlhY2NvdW50LnJ3IHNmY2Muc2hvcHBlci1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sSEZKd0dZWWt1eEo6OnJjaWQ6YWJVTXNhdnBEOVk2alcwMGRpMlNqeEdDTVU6OmNoaWQ6UmVmQXJjaEdsb2JhbCIsImV4cCI6MjY3ODgzNjEwMSwiaWF0IjoxNjc4ODM0MzAxLCJqdGkiOiJDMkM0ODU2MjAxODYwLTE4OTA2Nzg5MDM0ODA1ODMyNTcwNjY2NTQyIn0._tUrxeXdFYPj6ZoY-GILFRd3-aD1RGPkZX6TqHeS494',
339+
refresh_token: 'testrefeshtoken_1',
340+
usid: 'testusid_1',
341+
enc_user_id: 'testEncUserId_1',
342+
id_token: 'testIdToken_1'
343+
})
344+
)
331345
})
332346
)
333347
})
@@ -338,7 +352,7 @@ describe('Passwordless login tests', () => {
338352
siteAlias: 'uk',
339353
locale: {id: 'en-GB'},
340354
appConfig: mockConfig.app,
341-
bypassAuth: true
355+
bypassAuth: false
342356
}
343357
})
344358

@@ -349,20 +363,13 @@ describe('Passwordless login tests', () => {
349363
// Click the submit button
350364
await user.click(screen.getByRole('button', {name: /Continue/i}))
351365

352-
// check that check email page is open
366+
// check that OTP auth modal is open
353367
await waitFor(() => {
354-
expect(screen.getByText(/Check Your Email/i)).toBeInTheDocument()
368+
expect(
369+
screen.getByText(/To log in to your account, enter the code/i)
370+
).toBeInTheDocument()
355371
})
356372

357-
// check that OTP auth modal is open
358-
await waitFor(
359-
() => {
360-
expect(
361-
screen.getByText(/To log in to your account, enter the code/i)
362-
).toBeInTheDocument()
363-
}
364-
)
365-
366373
// resend the email
367374
await user.click(screen.getByText(/Resend Code/i))
368375

@@ -372,14 +379,11 @@ describe('Passwordless login tests', () => {
372379
for (let i = 0; i < 8; i++) {
373380
await user.type(otpInputs[i], code[i])
374381
}
375-
376-
await waitFor(
377-
() => {
378-
expect(
379-
query.getByText(/To log in to your account, enter the code/i)
380-
).not.toBeInTheDocument()
381-
}
382-
)
382+
383+
await waitFor(() => {
384+
expect(window.location.pathname).toBe('/uk/en-GB/account')
385+
expect(screen.getByText(/My Profile/i)).toBeInTheDocument()
386+
})
383387
})
384388

385389
test.each([

packages/template-retail-react-app/app/utils/test-utils.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
} from '@salesforce/retail-react-app/app/constants'
4141
import jwt from 'jsonwebtoken'
4242
import userEvent from '@testing-library/user-event'
43+
import Cookies from 'js-cookie'
4344
// This JWT's payload is special
4445
// it includes 3 fields that commerce-sdk-react cares:
4546
// exp, isb and sub
@@ -287,3 +288,16 @@ export const withPageProvider = (Component, options) => {
287288

288289
return WrappedComponent
289290
}
291+
292+
/**
293+
* Clears all cookies. Useful for test cleanup to ensure clean auth state.
294+
* @returns {void}
295+
*/
296+
export const clearAllCookies = () => {
297+
document.cookie.split(';').forEach((cookie) => {
298+
const name = cookie.split('=')[0].trim()
299+
if (name) {
300+
Cookies.remove(name, {path: '/'})
301+
}
302+
})
303+
}

0 commit comments

Comments
 (0)