@@ -10,7 +10,13 @@ const createJsonResponse = (payload: unknown) =>
1010 json : async ( ) => payload ,
1111 } ) as Response ;
1212
13- const setupClientMocks = ( ) => {
13+ const setupClientMocks = ( {
14+ isFirstTime = false ,
15+ markSuccess = mock ( ( ) => { } ) ,
16+ } : {
17+ isFirstTime ?: boolean ;
18+ markSuccess ?: ReturnType < typeof mock > ;
19+ } = { } ) => {
1420 ( globalThis as any ) . __DEV__ = false ;
1521
1622 mock . module ( 'react-native' , ( ) => ( {
@@ -29,7 +35,7 @@ const setupClientMocks = () => {
2935
3036 mock . module ( '../core' , ( ) => ( {
3137 PushyModule : {
32- markSuccess : mock ( ( ) => { } ) ,
38+ markSuccess,
3339 reloadUpdate : mock ( ( ) => Promise . resolve ( ) ) ,
3440 setNeedUpdate : mock ( ( ) => Promise . resolve ( ) ) ,
3541 downloadPatchFromPpk : mock ( ( ) => Promise . resolve ( ) ) ,
@@ -47,7 +53,7 @@ const setupClientMocks = () => {
4753 } ,
4854 currentVersion : 'hash' ,
4955 currentVersionInfo : { } ,
50- isFirstTime : false ,
56+ isFirstTime,
5157 isRolledBack : false ,
5258 packageVersion : '1.0.0' ,
5359 pushyNativeEventEmitter : {
@@ -199,4 +205,40 @@ describe('Pushy server config', () => {
199205 error : fetchError ,
200206 } ) ;
201207 } ) ;
208+
209+ test ( 'waits for native markSuccess before logging success' , async ( ) => {
210+ let resolveNativeMarkSuccess = ( ) => { } ;
211+ const nativeMarkSuccess = mock (
212+ ( ) =>
213+ new Promise < void > ( resolve => {
214+ resolveNativeMarkSuccess = resolve ;
215+ } ) ,
216+ ) ;
217+ const logger = mock ( ( ) => { } ) ;
218+ setupClientMocks ( {
219+ isFirstTime : true ,
220+ markSuccess : nativeMarkSuccess ,
221+ } ) ;
222+
223+ const { Pushy, sharedState } = await importFreshClient ( 'mark-success-awaits-native' ) ;
224+ const client = new Pushy ( {
225+ appKey : 'demo-app' ,
226+ logger,
227+ } ) ;
228+
229+ const markPromise = client . markSuccess ( ) ;
230+ expect ( nativeMarkSuccess ) . toHaveBeenCalledTimes ( 1 ) ;
231+ expect ( sharedState . marked ) . toBe ( false ) ;
232+ expect ( logger ) . not . toHaveBeenCalled ( ) ;
233+
234+ resolveNativeMarkSuccess ( ) ;
235+ await markPromise ;
236+
237+ expect ( sharedState . marked ) . toBe ( true ) ;
238+ expect ( logger ) . toHaveBeenCalledWith (
239+ expect . objectContaining ( {
240+ type : 'markSuccess' ,
241+ } ) ,
242+ ) ;
243+ } ) ;
202244} ) ;
0 commit comments