-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(BSR) refactor(useMustUpdateApp): use isLoading to not return a value…
… before getting data (#7644)
- Loading branch information
1 parent
e8e403a
commit f971857
Showing
10 changed files
with
117 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/features/forceUpdate/helpers/useMustUpdateApp.native.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import * as useMinimalBuildNumber from 'features/forceUpdate/helpers/useMinimalBuildNumber' | ||
import { MustUpdateAppState, useMustUpdateApp } from 'features/forceUpdate/helpers/useMustUpdateApp' | ||
import * as PackageJson from 'libs/packageJson' | ||
import { reactQueryProviderHOC } from 'tests/reactQueryProviderHOC' | ||
import { act, renderHook } from 'tests/utils' | ||
|
||
jest.mock('@react-native-firebase/firestore') | ||
|
||
const useGetMinimalBuildNumberSpy = jest.spyOn(useMinimalBuildNumber, 'useMinimalBuildNumber') | ||
|
||
const buildVersion = 10_304_000 | ||
jest.spyOn(PackageJson, 'getAppBuildVersion').mockReturnValue(buildVersion) | ||
|
||
describe('useMustUpdateApp', () => { | ||
it('should not update when the minimal build number is lower than local version', async () => { | ||
useGetMinimalBuildNumberSpy.mockReturnValueOnce({ | ||
minimalBuildNumber: 10_303_999, | ||
isLoading: false, | ||
}) | ||
|
||
const { result } = renderUseMustUpdateApp() | ||
|
||
await act(() => {}) | ||
|
||
expect(result.current).toEqual(MustUpdateAppState.SHOULD_NOT_UPDATE) | ||
}) | ||
|
||
it('should update when the minimal build number is higher than local version', async () => { | ||
useGetMinimalBuildNumberSpy.mockReturnValueOnce({ | ||
minimalBuildNumber: 10_304_001, | ||
isLoading: false, | ||
}) | ||
|
||
const { result } = renderUseMustUpdateApp() | ||
|
||
await act(() => {}) | ||
|
||
expect(result.current).toEqual(MustUpdateAppState.SHOULD_UPDATE) | ||
}) | ||
|
||
it('should not update when the minimal build number is equal to the local version', async () => { | ||
useGetMinimalBuildNumberSpy.mockReturnValueOnce({ | ||
minimalBuildNumber: buildVersion, | ||
isLoading: false, | ||
}) | ||
|
||
const { result } = renderUseMustUpdateApp() | ||
|
||
await act(() => {}) | ||
|
||
expect(result.current).toEqual(MustUpdateAppState.SHOULD_NOT_UPDATE) | ||
}) | ||
}) | ||
|
||
const renderUseMustUpdateApp = () => | ||
renderHook(useMustUpdateApp, { | ||
wrapper: ({ children }) => reactQueryProviderHOC(children), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,45 @@ | ||
import { useEffect, useRef } from 'react' | ||
import { useEffect } from 'react' | ||
|
||
import { useMinimalBuildNumber } from 'features/forceUpdate/helpers/useMinimalBuildNumber' | ||
import { eventMonitoring } from 'libs/monitoring/services' | ||
import { getAppBuildVersion } from 'libs/packageJson' | ||
|
||
const DELAY_BEFORE_VALUE_SHOULD_BE_SET_IN_MS = 15000 | ||
|
||
export const useMustUpdateApp = () => { | ||
const minimalBuildNumber = useRef<number | undefined>(undefined) | ||
minimalBuildNumber.current = useMinimalBuildNumber() | ||
const mustUpdateApp = | ||
!!minimalBuildNumber.current && getAppBuildVersion() < minimalBuildNumber.current | ||
export enum MustUpdateAppState { | ||
PENDING = 'pending', | ||
SHOULD_UPDATE = 'shouldUpdate', | ||
SHOULD_NOT_UPDATE = 'shouldNotUpdate', | ||
} | ||
|
||
export const useMustUpdateApp: () => MustUpdateAppState = () => { | ||
const { minimalBuildNumber, isLoading } = useMinimalBuildNumber() | ||
const appBuildVersion = getAppBuildVersion() | ||
|
||
useEffect(() => { | ||
const timer = globalThis.setTimeout(() => { | ||
if (!minimalBuildNumber) { | ||
eventMonitoring.captureException(new Error('MustUpdateNoMinimalBuildNumberError'), { | ||
extra: { | ||
mustUpdateApp, | ||
minimalBuildNumber, | ||
build: getAppBuildVersion(), | ||
build: appBuildVersion, | ||
}, | ||
}) | ||
} | ||
}, DELAY_BEFORE_VALUE_SHOULD_BE_SET_IN_MS) | ||
return () => { | ||
clearInterval(timer) | ||
} | ||
}, [mustUpdateApp]) | ||
}, [appBuildVersion, minimalBuildNumber]) | ||
|
||
if (isLoading) return MustUpdateAppState.PENDING | ||
|
||
const isLocalBuildSmallerThanMinimalBuild = | ||
!!minimalBuildNumber && appBuildVersion < minimalBuildNumber | ||
|
||
const mustUpdateApp = isLocalBuildSmallerThanMinimalBuild | ||
? MustUpdateAppState.SHOULD_UPDATE | ||
: MustUpdateAppState.SHOULD_NOT_UPDATE | ||
|
||
return mustUpdateApp | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters