Skip to content

Commit 73f6482

Browse files
authored
Passwordless redirect unit test (#2235)
* add unit tests for passwordless landing
1 parent e23bdce commit 73f6482

File tree

2 files changed

+55
-30
lines changed

2 files changed

+55
-30
lines changed

packages/template-retail-react-app/app/pages/login/index.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ const Login = ({initialView = LOGIN_VIEW}) => {
179179
const redirectTo = redirectPath ? redirectPath : '/account'
180180
navigate(redirectTo)
181181
}
182-
}, [isRegistered])
182+
}, [isRegistered, redirectPath])
183183

184184
/**************** Einstein ****************/
185185
useEffect(() => {

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

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,18 @@
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
77
import React from 'react'
8-
import {screen, waitFor} from '@testing-library/react'
8+
import {waitFor} from '@testing-library/react'
99
import {rest} from 'msw'
1010
import {
1111
renderWithProviders,
12-
createPathWithDefaults,
13-
guestToken
12+
createPathWithDefaults
1413
} from '@salesforce/retail-react-app/app/utils/test-utils'
1514
import Login from '.'
1615
import {BrowserRouter as Router, Route} from 'react-router-dom'
1716
import Account from '@salesforce/retail-react-app/app/pages/account'
18-
import Registration from '@salesforce/retail-react-app/app/pages/registration'
19-
import ResetPassword from '@salesforce/retail-react-app/app/pages/reset-password'
2017
import mockConfig from '@salesforce/retail-react-app/config/mocks/default'
2118
import {mockedRegisteredCustomer} from '@salesforce/retail-react-app/app/mocks/mock-data'
22-
import {AuthHelpers, useCustomerType} from '@salesforce/commerce-sdk-react'
23-
import {useLocation} from 'react-router-dom'
19+
import {AuthHelpers} from '@salesforce/commerce-sdk-react'
2420

2521
const mockMergedBasket = {
2622
basketId: 'a10ff320829cb0eef93ca5310a',
@@ -42,12 +38,6 @@ const MockedComponent = () => {
4238
return (
4339
<Router>
4440
<Login />
45-
<Route path={createPathWithDefaults('/registration')}>
46-
<Registration />
47-
</Route>
48-
<Route path={createPathWithDefaults('/reset-password')}>
49-
<ResetPassword />
50-
</Route>
5141
<Route path={createPathWithDefaults('/account')}>
5242
<Account match={match} />
5343
</Route>
@@ -58,14 +48,9 @@ const MockedComponent = () => {
5848
jest.mock('react-router', () => {
5949
return {
6050
...jest.requireActual('react-router'),
61-
useRouteMatch: () => {return {path: '/passwordless-login-landing'}}
62-
}
63-
})
64-
65-
jest.mock('react-router-dom', () => {
66-
return {
67-
...jest.requireActual('react-router-dom'),
68-
useLocation: jest.fn()
51+
useRouteMatch: () => {
52+
return {path: '/passwordless-login-landing'}
53+
}
6954
}
7055
})
7156

@@ -76,8 +61,12 @@ jest.mock('@salesforce/commerce-sdk-react', () => {
7661
useAuthHelper: jest
7762
.fn()
7863
.mockImplementation((helperType) => mockAuthHelperFunctions[helperType]),
79-
useCustomerBaskets: () => {return {data: mockMergedBasket, isSuccess: true}},
80-
useCustomerType: jest.fn(() => {return {isRegistered: false, customerType: 'guest'}})
64+
useCustomerBaskets: () => {
65+
return {data: mockMergedBasket, isSuccess: true}
66+
},
67+
useCustomerType: jest.fn(() => {
68+
return {isRegistered: true, customerType: 'guest'}
69+
})
8170
}
8271
})
8372

@@ -104,26 +93,62 @@ beforeEach(() => {
10493
)
10594
})
10695
afterEach(() => {
107-
jest.resetAllMocks()
96+
jest.resetModules()
10897
})
10998

11099
describe('Passwordless landing tests', function () {
100+
test('redirects to account page when redirect url is not passed', async () => {
101+
const token = '12345678'
102+
window.history.pushState(
103+
{},
104+
'Passwordless Login Landing',
105+
createPathWithDefaults(`/passwordless-login-landing?token=${token}`)
106+
)
107+
renderWithProviders(<MockedComponent />, {
108+
wrapperProps: {
109+
siteAlias: 'uk',
110+
locale: {id: 'en-GB'},
111+
appConfig: mockConfig.app
112+
}
113+
})
111114

112-
test('On passwordless landing, make sure token in magic link is passed as param', async () => {
115+
expect(
116+
mockAuthHelperFunctions[AuthHelpers.LoginPasswordlessUser].mutateAsync
117+
).toHaveBeenCalledWith({
118+
pwdlessLoginToken: token
119+
})
120+
121+
await waitFor(() => {
122+
expect(window.location.pathname).toBe('/uk/en-GB/account')
123+
})
124+
})
125+
126+
test('redirects to redirectUrl when passed as param', async () => {
113127
const token = '12345678'
114-
useLocation.mockReturnValue({search: `?token=${token}&redirect_url=/womens-tops`})
128+
const redirectUrl = '/womens-tops'
129+
window.history.pushState(
130+
{},
131+
'Passwordless Login Landing',
132+
createPathWithDefaults(
133+
`/passwordless-login-landing?token=${token}&redirect_url=${redirectUrl}`
134+
)
135+
)
115136
renderWithProviders(<MockedComponent />, {
116137
wrapperProps: {
117138
siteAlias: 'uk',
118139
locale: {id: 'en-GB'},
119-
appConfig: mockConfig.app,
120-
isGuest: true
140+
appConfig: mockConfig.app
121141
}
122142
})
143+
123144
expect(
124145
mockAuthHelperFunctions[AuthHelpers.LoginPasswordlessUser].mutateAsync
125146
).toHaveBeenCalledWith({
126147
pwdlessLoginToken: token
127148
})
149+
150+
await waitFor(() => {
151+
expect(window.location.pathname).toBe('/uk/en-GB/womens-tops')
152+
})
128153
})
129-
})
154+
})

0 commit comments

Comments
 (0)