Skip to content

Commit a45ed60

Browse files
committed
test passwordless login in useauthmodal hook file
1 parent 1d20200 commit a45ed60

File tree

1 file changed

+78
-8
lines changed

1 file changed

+78
-8
lines changed

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

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import Account from '@salesforce/retail-react-app/app/pages/account'
1919
import {rest} from 'msw'
2020
import {mockedRegisteredCustomer} from '@salesforce/retail-react-app/app/mocks/mock-data'
2121
import * as ReactHookForm from 'react-hook-form'
22+
import {AuthHelpers} from '@salesforce/commerce-sdk-react'
2223

2324
jest.setTimeout(60000)
2425

@@ -47,6 +48,21 @@ const mockRegisteredCustomer = {
4748
login: 'customer@test.com'
4849
}
4950

51+
const mockAuthHelperFunctions = {
52+
[AuthHelpers.AuthorizePasswordless]: {mutateAsync: jest.fn()},
53+
[AuthHelpers.Register]: {mutateAsync: jest.fn()}
54+
}
55+
56+
jest.mock('@salesforce/commerce-sdk-react', () => {
57+
const originalModule = jest.requireActual('@salesforce/commerce-sdk-react')
58+
return {
59+
...originalModule,
60+
useAuthHelper: jest
61+
.fn()
62+
.mockImplementation((helperType) => mockAuthHelperFunctions[helperType])
63+
}
64+
})
65+
5066
let authModal = undefined
5167
const MockedComponent = (props) => {
5268
const {initialView, isPasswordlessEnabled = false} = props
@@ -155,17 +171,71 @@ test('Renders check email modal on email mode', async () => {
155171
mockUseForm.mockRestore()
156172
})
157173

158-
test('Renders passwordless login when enabled', async () => {
159-
const user = userEvent.setup()
174+
describe('Passwordless enabled', () => {
175+
beforeAll(() => {
176+
jest.spyOn(window.localStorage, 'setItem')
177+
})
160178

161-
renderWithProviders(<MockedComponent isPasswordlessEnabled={true} />)
179+
beforeEach(() => {
180+
jest.resetModules()
181+
window.localStorage.setItem.mockClear()
182+
})
162183

163-
// open the modal
164-
const trigger = screen.getByText(/open modal/i)
165-
await user.click(trigger)
184+
afterAll(() => {
185+
window.localStorage.setItem.mockRestore()
186+
})
166187

167-
await waitFor(() => {
168-
expect(screen.getByText(/continue securely/i)).toBeInTheDocument()
188+
test('Renders passwordless login when enabled', async () => {
189+
const user = userEvent.setup()
190+
191+
renderWithProviders(<MockedComponent isPasswordlessEnabled={true} />)
192+
193+
// open the modal
194+
const trigger = screen.getByText(/open modal/i)
195+
await user.click(trigger)
196+
197+
await waitFor(() => {
198+
expect(screen.getByText(/continue securely/i)).toBeInTheDocument()
199+
})
200+
})
201+
202+
test('Allows passwordless login', async () => {
203+
const {user} = renderWithProviders(<MockedComponent isPasswordlessEnabled={true} />)
204+
const validEmail = 'test@salesforce.com'
205+
206+
// open the modal
207+
const trigger = screen.getByText(/open modal/i)
208+
await user.click(trigger)
209+
210+
await waitFor(() => {
211+
expect(screen.getByText(/continue securely/i)).toBeInTheDocument()
212+
})
213+
214+
// enter a valid email address
215+
await user.type(screen.getByLabelText('Email'), validEmail)
216+
217+
// initiate passwordless login
218+
const passwordlessLoginButton = screen.getByText(/continue securely/i)
219+
// Click the button twice as the isPasswordlessLoginClicked state doesn't change after the first click
220+
await user.click(passwordlessLoginButton)
221+
await user.click(passwordlessLoginButton)
222+
expect(
223+
mockAuthHelperFunctions[AuthHelpers.AuthorizePasswordless].mutateAsync
224+
).toHaveBeenCalledWith({userid: validEmail})
225+
226+
// check that check email modal is open
227+
await waitFor(() => {
228+
const withinForm = within(screen.getByTestId('sf-form-resend-passwordless-email'))
229+
expect(withinForm.getByText(/Check Your Email/i)).toBeInTheDocument()
230+
expect(withinForm.getByText(validEmail)).toBeInTheDocument()
231+
expect(window.localStorage.setItem).toHaveBeenCalled()
232+
})
233+
234+
// resend the email
235+
user.click(screen.getByText(/Resend Link/i))
236+
expect(
237+
mockAuthHelperFunctions[AuthHelpers.AuthorizePasswordless].mutateAsync
238+
).toHaveBeenCalledWith({userid: validEmail})
169239
})
170240
})
171241

0 commit comments

Comments
 (0)