@@ -20,11 +20,12 @@ import {
2020 mockOrderProducts ,
2121 mockPasswordUpdateFalure
2222} from '@salesforce/retail-react-app/app/mocks/mock-data'
23+ import { useCustomerType } from '@salesforce/commerce-sdk-react'
2324import Account from '@salesforce/retail-react-app/app/pages/account/index'
2425import Login from '@salesforce/retail-react-app/app/pages/login'
2526import mockConfig from '@salesforce/retail-react-app/config/mocks/default'
26- import * as sdk from '@salesforce/commerce-sdk-react'
2727
28+ jest . setTimeout ( 60000 )
2829jest . mock ( '@salesforce/commerce-sdk-react' , ( ) => ( {
2930 ...jest . requireActual ( '@salesforce/commerce-sdk-react' ) ,
3031 useCustomerType : jest . fn ( )
@@ -73,7 +74,7 @@ beforeEach(() => {
7374} )
7475afterEach ( ( ) => {
7576 jest . resetModules ( )
76- localStorage . clear ( )
77+ jest . restoreAllMocks ( )
7778} )
7879
7980const expectedBasePath = '/uk/en-GB'
@@ -86,50 +87,45 @@ describe('Test redirects', function () {
8687 )
8788 } )
8889 test ( 'Redirects to login page if the customer is not logged in' , async ( ) => {
89- sdk . useCustomerType . mockReturnValue ( { isRegistered : false , isGuest : true } )
90- const Component = ( ) => {
91- return (
92- < Switch >
93- < Route
94- path = { createPathWithDefaults ( '/account' ) }
95- render = { ( props ) => < Account { ...props } /> }
96- />
97- </ Switch >
98- )
99- }
100- renderWithProviders ( < Component /> , {
90+ useCustomerType . mockReturnValue ( { isRegistered : false , isGuest : true } )
91+ renderWithProviders ( < MockedComponent /> , {
10192 wrapperProps : { siteAlias : 'uk' , appConfig : mockConfig . app , isGuest : true }
10293 } )
10394 await waitFor ( ( ) => expect ( window . location . pathname ) . toBe ( `${ expectedBasePath } /login` ) )
10495 } )
10596} )
106-
107- test ( 'Provides navigation for subpages' , async ( ) => {
108- sdk . useCustomerType . mockReturnValue ( { isRegistered : true , isGuest : false } )
109- global . server . use (
110- rest . get ( '*/products' , ( req , res , ctx ) => {
111- return res ( ctx . delay ( 0 ) , ctx . json ( mockOrderProducts ) )
112- } ) ,
113- rest . get ( '*/customers/:customerId/orders' , ( req , res , ctx ) => {
114- return res ( ctx . delay ( 0 ) , ctx . json ( mockOrderHistory ) )
97+ describe ( 'Page Navigation' , ( ) => {
98+ test ( 'works for subpages' , async ( ) => {
99+ useCustomerType . mockReturnValue ( { isRegistered : true , isGuest : false } )
100+ global . server . use (
101+ rest . get ( '*/products' , ( req , res , ctx ) => {
102+ return res ( ctx . delay ( 0 ) , ctx . json ( mockOrderProducts ) )
103+ } ) ,
104+ rest . get ( '*/customers/:customerId/orders' , ( req , res , ctx ) => {
105+ return res ( ctx . delay ( 0 ) , ctx . json ( mockOrderHistory ) )
106+ } )
107+ )
108+ const { user} = renderWithProviders ( < MockedComponent /> , {
109+ wrapperProps : { siteAlias : 'uk' , appConfig : mockConfig . app }
115110 } )
116- )
117- const { user} = renderWithProviders ( < MockedComponent /> , {
118- wrapperProps : { siteAlias : 'uk' , appConfig : mockConfig . app }
119- } )
120- expect ( await screen . findByTestId ( 'account-page' ) ) . toBeInTheDocument ( )
111+ expect ( await screen . findByTestId ( 'account-page' ) ) . toBeInTheDocument ( )
121112
122- const nav = within ( screen . getByTestId ( 'account-detail-nav' ) )
123- await user . click ( nav . getByText ( 'Addresses' ) )
124- await waitFor ( ( ) =>
125- expect ( window . location . pathname ) . toBe ( `${ expectedBasePath } /account/addresses` )
126- )
127- await user . click ( nav . getByText ( 'Order History' ) )
128- await waitFor ( ( ) => expect ( window . location . pathname ) . toBe ( `${ expectedBasePath } /account/orders` ) )
113+ const nav = within ( screen . getByTestId ( 'account-detail-nav' ) )
114+ await user . click ( nav . getByText ( 'Addresses' ) )
115+ await waitFor ( ( ) =>
116+ expect ( window . location . pathname ) . toBe ( `${ expectedBasePath } /account/addresses` )
117+ )
118+ await user . click ( nav . getByText ( 'Order History' ) )
119+ await waitFor ( ( ) =>
120+ expect ( window . location . pathname ) . toBe ( `${ expectedBasePath } /account/orders` )
121+ )
122+ } )
129123} )
130124
131125describe ( 'Render and logs out' , function ( ) {
132126 test ( 'Renders account detail page by default for logged-in customer, and can log out' , async ( ) => {
127+ useCustomerType . mockReturnValue ( { isRegistered : true , isGuest : false } )
128+
133129 const { user} = renderWithProviders ( < MockedComponent /> )
134130
135131 // Render user profile page
@@ -145,7 +141,10 @@ describe('Render and logs out', function () {
145141 } )
146142
147143 await user . click ( screen . getAllByText ( / L o g O u t / ) [ 0 ] )
144+ useCustomerType . mockReturnValue ( { isRegistered : false , isGuest : true } )
145+
148146 await waitFor ( ( ) => {
147+ expect ( window . location . pathname ) . toBe ( `${ expectedBasePath } /login` )
149148 expect ( screen . getByTestId ( 'login-page' ) ) . toBeInTheDocument ( )
150149 } )
151150 } )
@@ -166,7 +165,7 @@ describe('updating profile', function () {
166165 )
167166 } )
168167 test ( 'Allows customer to edit profile details' , async ( ) => {
169- sdk . useCustomerType . mockReturnValue ( { isRegistered : true , isExternal : false } )
168+ useCustomerType . mockReturnValue ( { isRegistered : true , isExternal : false } )
170169 const { user} = renderWithProviders ( < MockedComponent /> )
171170 expect ( await screen . findByTestId ( 'account-page' ) ) . toBeInTheDocument ( )
172171 expect ( await screen . findByTestId ( 'account-detail-page' ) ) . toBeInTheDocument ( )
@@ -190,6 +189,7 @@ describe('updating profile', function () {
190189
191190describe ( 'updating password' , function ( ) {
192191 beforeEach ( ( ) => {
192+ useCustomerType . mockReturnValue ( { isRegistered : true , isExternal : false } )
193193 global . server . use (
194194 rest . post ( '*/oauth2/token' , ( req , res , ctx ) =>
195195 res (
0 commit comments