@@ -11,6 +11,10 @@ import { RootState } from '../../../reducers';
1111import StorageWrapper from '../../../store/storage-wrapper' ;
1212import { Authentication } from '../../../core' ;
1313import Routes from '../../../constants/navigation/Routes' ;
14+ import {
15+ OPTIN_META_METRICS_UI_SEEN ,
16+ EXISTING_USER ,
17+ } from '../../../constants/storage' ;
1418import { strings } from '../../../../locales/i18n' ;
1519import { NavigationContainer } from '@react-navigation/native' ;
1620import configureMockStore from 'redux-mock-store' ;
@@ -20,7 +24,6 @@ import { mockTheme, ThemeContext } from '../../../util/theme';
2024const initialState : DeepPartial < RootState > = {
2125 user : {
2226 userLoggedIn : true ,
23- isMetaMetricsUISeen : true ,
2427 } ,
2528 engine : {
2629 backgroundState,
@@ -83,6 +86,14 @@ const mockMetrics = {
8386 addTraitsToUser : jest . fn ( ) ,
8487} ;
8588
89+ // Mock Authentication module
90+ jest . mock ( '../../../core' , ( ) => ( {
91+ Authentication : {
92+ appTriggeredAuth : jest . fn ( ) . mockResolvedValue ( undefined ) ,
93+ lockApp : jest . fn ( ) ,
94+ } ,
95+ } ) ) ;
96+
8697// Need to mock this module since it uses store.getState, which interferes with the mocks from this test file.
8798jest . mock (
8899 '../../../util/metrics/UserSettingsAnalyticsMetaData/generateUserProfileAnalyticsMetaData' ,
@@ -94,6 +105,16 @@ jest.mock(
94105 ( ) => jest . fn ( ) . mockReturnValue ( { deviceProp : 'Device value' } ) ,
95106) ;
96107
108+ // Mock essential dependencies
109+ jest . mock ( 'react-native-branch' , ( ) => ( {
110+ subscribe : jest . fn ( ) ,
111+ getLatestReferringParams : jest . fn ( ) . mockResolvedValue ( { } ) ,
112+ } ) ) ;
113+
114+ jest . mock ( 'react-native-device-info' , ( ) => ( {
115+ getVersion : jest . fn ( ) . mockReturnValue ( '1.0.0' ) ,
116+ } ) ) ;
117+
97118( MetaMetrics . getInstance as jest . Mock ) . mockReturnValue ( mockMetrics ) ;
98119
99120describe ( 'App' , ( ) => {
@@ -122,7 +143,12 @@ describe('App', () => {
122143
123144 describe ( 'Authentication flow logic' , ( ) => {
124145 it ( 'navigates to onboarding when user does not exist' , async ( ) => {
125- jest . spyOn ( StorageWrapper , 'getItem' ) . mockResolvedValue ( null ) ;
146+ jest . spyOn ( StorageWrapper , 'getItem' ) . mockImplementation ( async ( key ) => {
147+ if ( key === EXISTING_USER ) {
148+ return null ; // User does not exist
149+ }
150+ return null ; // Default for other keys
151+ } ) ;
126152 renderScreen ( App , { name : 'App' } , { state : initialState } ) ;
127153 await waitFor ( ( ) => {
128154 expect ( mockReset ) . toHaveBeenCalledWith ( {
@@ -131,7 +157,15 @@ describe('App', () => {
131157 } ) ;
132158 } ) ;
133159 it ( 'navigates to login when user exists and logs in' , async ( ) => {
134- jest . spyOn ( StorageWrapper , 'getItem' ) . mockResolvedValue ( true ) ;
160+ jest . spyOn ( StorageWrapper , 'getItem' ) . mockImplementation ( async ( key ) => {
161+ if ( key === EXISTING_USER ) {
162+ return true ; // User exists
163+ }
164+ if ( key === OPTIN_META_METRICS_UI_SEEN ) {
165+ return true ; // OptinMetrics UI has been seen
166+ }
167+ return null ; // Default for other keys
168+ } ) ;
135169 jest . spyOn ( Authentication , 'appTriggeredAuth' ) . mockResolvedValue ( ) ;
136170 renderScreen ( App , { name : 'App' } , { state : initialState } ) ;
137171 await waitFor ( ( ) => {
@@ -141,30 +175,47 @@ describe('App', () => {
141175 } ) ;
142176 } ) ;
143177
144- it ( 'navigates to OptinMetrics when user exists and isMetaMetricsUISeen is false' , async ( ) => {
145- jest . spyOn ( StorageWrapper , 'getItem' ) . mockResolvedValue ( true ) ;
146- jest . spyOn ( Authentication , 'appTriggeredAuth' ) . mockResolvedValue ( ) ;
178+ it ( 'navigates to OptinMetrics when user exists and OptinMetaMetricsUISeen is false' , async ( ) => {
179+ // Mock StorageWrapper.getItem to return different values based on the key
180+ jest . spyOn ( StorageWrapper , 'getItem' ) . mockImplementation ( async ( key ) => {
181+ if ( key === EXISTING_USER ) {
182+ return true ; // User exists
183+ }
184+ if ( key === OPTIN_META_METRICS_UI_SEEN ) {
185+ return false ; // OptinMetrics UI has not been seen
186+ }
187+ return null ; // Default for other keys
188+ } ) ;
189+
147190 renderScreen (
148191 App ,
149192 { name : 'App' } ,
150193 {
151194 state : {
152195 ...initialState ,
153- user : { ...initialState . user , isMetaMetricsUISeen : false } ,
154196 } ,
155197 } ,
156198 ) ;
157- await waitFor ( ( ) => {
158- expect ( mockNavigate ) . toHaveBeenCalledWith ( Routes . ONBOARDING . ROOT_NAV , {
159- screen : Routes . ONBOARDING . NAV ,
160- params : {
161- screen : Routes . ONBOARDING . OPTIN_METRICS ,
162- params : {
163- onContinue : expect . any ( Function ) ,
164- } ,
165- } ,
166- } ) ;
167- } ) ;
199+
200+ // Wait a bit longer and add debugging
201+ await waitFor (
202+ ( ) => {
203+ expect ( mockReset ) . toHaveBeenCalledWith ( {
204+ routes : [
205+ {
206+ name : 'OnboardingRootNav' ,
207+ params : {
208+ screen : 'OnboardingNav' ,
209+ params : {
210+ screen : 'OptinMetrics' ,
211+ } ,
212+ } ,
213+ } ,
214+ ] ,
215+ } ) ;
216+ } ,
217+ { timeout : 5000 } ,
218+ ) ;
168219 } ) ;
169220 } ) ;
170221
0 commit comments