1
+ /*
2
+ * Copyright (c) 2021, salesforce.com, inc.
3
+ * All rights reserved.
4
+ * SPDX-License-Identifier: BSD-3-Clause
5
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
6
+ */
7
+ import React from 'react'
8
+ import { screen , waitFor } from '@testing-library/react'
9
+ import { rest } from 'msw'
10
+ import {
11
+ renderWithProviders ,
12
+ createPathWithDefaults ,
13
+ guestToken
14
+ } from '@salesforce/retail-react-app/app/utils/test-utils'
15
+ import Login from '.'
16
+ import { BrowserRouter as Router , Route } from 'react-router-dom'
17
+ 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'
20
+ import mockConfig from '@salesforce/retail-react-app/config/mocks/default'
21
+ 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'
24
+
25
+ const mockMergedBasket = {
26
+ basketId : 'a10ff320829cb0eef93ca5310a' ,
27
+ currency : 'USD' ,
28
+ customerInfo : {
29
+ customerId : 'registeredCustomerId' ,
30
+ email : 'customer@test.com'
31
+ }
32
+ }
33
+
34
+ const mockAuthHelperFunctions = {
35
+ [ AuthHelpers . LoginPasswordlessUser ] : { mutateAsync : jest . fn ( ) }
36
+ }
37
+
38
+ const MockedComponent = ( ) => {
39
+ const match = {
40
+ params : { pageName : 'profile' }
41
+ }
42
+ return (
43
+ < Router >
44
+ < Login />
45
+ < Route path = { createPathWithDefaults ( '/registration' ) } >
46
+ < Registration />
47
+ </ Route >
48
+ < Route path = { createPathWithDefaults ( '/reset-password' ) } >
49
+ < ResetPassword />
50
+ </ Route >
51
+ < Route path = { createPathWithDefaults ( '/account' ) } >
52
+ < Account match = { match } />
53
+ </ Route >
54
+ </ Router >
55
+ )
56
+ }
57
+
58
+ jest . mock ( 'react-router' , ( ) => {
59
+ return {
60
+ ...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 ( )
69
+ }
70
+ } )
71
+
72
+ jest . mock ( '@salesforce/commerce-sdk-react' , ( ) => {
73
+ const originalModule = jest . requireActual ( '@salesforce/commerce-sdk-react' )
74
+ return {
75
+ ...originalModule ,
76
+ useAuthHelper : jest
77
+ . fn ( )
78
+ . mockImplementation ( ( helperType ) => mockAuthHelperFunctions [ helperType ] ) ,
79
+ useCustomerBaskets : ( ) => { return { data : mockMergedBasket , isSuccess : true } } ,
80
+ useCustomerType : jest . fn ( ( ) => { return { isRegistered : false , customerType : 'guest' } } )
81
+ }
82
+ } )
83
+
84
+ // Set up and clean up
85
+ beforeEach ( ( ) => {
86
+ global . server . use (
87
+ rest . post ( '*/customers' , ( req , res , ctx ) => {
88
+ return res ( ctx . delay ( 0 ) , ctx . status ( 200 ) , ctx . json ( mockedRegisteredCustomer ) )
89
+ } ) ,
90
+ rest . get ( '*/customers/:customerId' , ( req , res , ctx ) => {
91
+ const { customerId} = req . params
92
+ if ( customerId === 'customerId' ) {
93
+ return res (
94
+ ctx . delay ( 0 ) ,
95
+ ctx . status ( 200 ) ,
96
+ ctx . json ( {
97
+ authType : 'guest' ,
98
+ customerId : 'customerid'
99
+ } )
100
+ )
101
+ }
102
+ return res ( ctx . delay ( 0 ) , ctx . status ( 200 ) , ctx . json ( mockedRegisteredCustomer ) )
103
+ } )
104
+ )
105
+ } )
106
+ afterEach ( ( ) => {
107
+ jest . resetAllMocks ( )
108
+ } )
109
+
110
+ describe ( 'Passwordless landing tests' , function ( ) {
111
+
112
+ test ( 'On passwordless landing, make sure token in magic link is passed as param' , async ( ) => {
113
+ const token = '12345678'
114
+ useLocation . mockReturnValue ( { search : `?token=${ token } &redirect_url=/womens-tops` } )
115
+ renderWithProviders ( < MockedComponent /> , {
116
+ wrapperProps : {
117
+ siteAlias : 'uk' ,
118
+ locale : { id : 'en-GB' } ,
119
+ appConfig : mockConfig . app ,
120
+ isGuest : true
121
+ }
122
+ } )
123
+ expect (
124
+ mockAuthHelperFunctions [ AuthHelpers . LoginPasswordlessUser ] . mutateAsync
125
+ ) . toHaveBeenCalledWith ( {
126
+ pwdlessLoginToken : token
127
+ } )
128
+ } )
129
+ } )
0 commit comments