@@ -13,15 +13,21 @@ const createJsonResponse = (payload: unknown) =>
1313const setupClientMocks = ( {
1414 isFirstTime = false ,
1515 markSuccess = mock ( ( ) => { } ) ,
16+ reloadUpdate = mock ( ( ) => Promise . resolve ( ) ) ,
17+ setNeedUpdate = mock ( ( ) => Promise . resolve ( ) ) ,
1618 downloadPatchFromPpk = mock ( ( ) => Promise . resolve ( ) ) ,
1719 downloadPatchFromPackage = mock ( ( ) => Promise . resolve ( ) ) ,
1820 downloadFullUpdate = mock ( ( ) => Promise . resolve ( ) ) ,
21+ restartApp = mock ( ( ) => Promise . resolve ( ) ) ,
1922} : {
2023 isFirstTime ?: boolean ;
2124 markSuccess ?: ReturnType < typeof mock > ;
25+ reloadUpdate ?: ReturnType < typeof mock > ;
26+ setNeedUpdate ?: ReturnType < typeof mock > ;
2227 downloadPatchFromPpk ?: ReturnType < typeof mock > ;
2328 downloadPatchFromPackage ?: ReturnType < typeof mock > ;
2429 downloadFullUpdate ?: ReturnType < typeof mock > ;
30+ restartApp ?: ReturnType < typeof mock > ;
2531} = { } ) => {
2632 ( globalThis as any ) . __DEV__ = false ;
2733
@@ -42,13 +48,13 @@ const setupClientMocks = ({
4248 mock . module ( '../core' , ( ) => ( {
4349 PushyModule : {
4450 markSuccess,
45- reloadUpdate : mock ( ( ) => Promise . resolve ( ) ) ,
46- setNeedUpdate : mock ( ( ) => Promise . resolve ( ) ) ,
51+ reloadUpdate,
52+ setNeedUpdate,
4753 downloadPatchFromPpk,
4854 downloadPatchFromPackage,
4955 downloadFullUpdate,
5056 downloadAndInstallApk : mock ( ( ) => Promise . resolve ( ) ) ,
51- restartApp : mock ( ( ) => Promise . resolve ( ) ) ,
57+ restartApp,
5258 } ,
5359 buildTime : '2023-01-01' ,
5460 cInfo : {
@@ -283,4 +289,78 @@ describe('Pushy server config', () => {
283289 } ) ,
284290 ) ;
285291 } ) ;
292+
293+ test ( 'waits for beforeReload before switching version' , async ( ) => {
294+ const calls : string [ ] = [ ] ;
295+ const reloadUpdate = mock ( ( ) => {
296+ calls . push ( 'reloadUpdate' ) ;
297+ return Promise . resolve ( ) ;
298+ } ) ;
299+ const beforeReload = mock ( async ( context : any ) => {
300+ calls . push ( 'beforeReload' ) ;
301+ expect ( context ) . toEqual ( {
302+ type : 'switchVersion' ,
303+ hash : 'next-hash' ,
304+ } ) ;
305+ } ) ;
306+ setupClientMocks ( { reloadUpdate } ) ;
307+
308+ const { Pushy, sharedState } = await importFreshClient ( 'before-reload-switch-version' ) ;
309+ sharedState . downloadedHash = 'next-hash' ;
310+ const client = new Pushy ( {
311+ appKey : 'demo-app' ,
312+ beforeReload,
313+ } ) ;
314+
315+ await client . switchVersion ( 'next-hash' ) ;
316+
317+ expect ( calls ) . toEqual ( [ 'beforeReload' , 'reloadUpdate' ] ) ;
318+ expect ( beforeReload ) . toHaveBeenCalledTimes ( 1 ) ;
319+ expect ( reloadUpdate ) . toHaveBeenCalledWith ( { hash : 'next-hash' } ) ;
320+ } ) ;
321+
322+ test ( 'skips switching version when beforeReload returns false' , async ( ) => {
323+ const reloadUpdate = mock ( ( ) => Promise . resolve ( ) ) ;
324+ const beforeReload = mock ( ( ) => false ) ;
325+ setupClientMocks ( { reloadUpdate } ) ;
326+
327+ const { Pushy, sharedState } = await importFreshClient ( 'before-reload-skip-switch' ) ;
328+ sharedState . downloadedHash = 'next-hash' ;
329+ const client = new Pushy ( {
330+ appKey : 'demo-app' ,
331+ beforeReload,
332+ } ) ;
333+
334+ await client . switchVersion ( 'next-hash' ) ;
335+
336+ expect ( beforeReload ) . toHaveBeenCalledTimes ( 1 ) ;
337+ expect ( reloadUpdate ) . not . toHaveBeenCalled ( ) ;
338+ expect ( sharedState . applyingUpdate ) . toBe ( false ) ;
339+ } ) ;
340+
341+ test ( 'calls beforeReload before restartApp' , async ( ) => {
342+ const calls : string [ ] = [ ] ;
343+ const restartApp = mock ( ( ) => {
344+ calls . push ( 'restartApp' ) ;
345+ return Promise . resolve ( ) ;
346+ } ) ;
347+ const beforeReload = mock ( async ( context : any ) => {
348+ calls . push ( 'beforeReload' ) ;
349+ expect ( context ) . toEqual ( {
350+ type : 'restartApp' ,
351+ } ) ;
352+ } ) ;
353+ setupClientMocks ( { restartApp } ) ;
354+
355+ const { Pushy } = await importFreshClient ( 'before-reload-restart-app' ) ;
356+ const client = new Pushy ( {
357+ appKey : 'demo-app' ,
358+ beforeReload,
359+ } ) ;
360+
361+ await client . restartApp ( ) ;
362+
363+ expect ( calls ) . toEqual ( [ 'beforeReload' , 'restartApp' ] ) ;
364+ expect ( restartApp ) . toHaveBeenCalled ( ) ;
365+ } ) ;
286366} ) ;
0 commit comments