From e1f5b675bb770089e7b41728f1f2bd090f204560 Mon Sep 17 00:00:00 2001 From: LucasBeneston Date: Fri, 29 Nov 2024 18:41:40 +0100 Subject: [PATCH] (PC-32821) feat(NC): update conditions in useGetCurrencyToDisplay --- .../useGetCurrencyToDisplay.native.test.ts | 18 ++++++-------- .../currency/useGetCurrencyToDisplay.ts | 24 ++++++++++--------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/src/shared/currency/useGetCurrencyToDisplay.native.test.ts b/src/shared/currency/useGetCurrencyToDisplay.native.test.ts index 86df01f22a6..376f51de719 100644 --- a/src/shared/currency/useGetCurrencyToDisplay.native.test.ts +++ b/src/shared/currency/useGetCurrencyToDisplay.native.test.ts @@ -23,6 +23,7 @@ const mockUseAuthContext = jest.mocked(useAuthContext) describe('useGetCurrencyToDisplay', () => { beforeEach(() => { activateFeatureFlags() + mockUseGeolocation.mockReturnValue({ userLocation: null } as ILocationContext) mockUseAuthContext.mockReturnValue({ isLoggedIn: true, user: undefined, @@ -33,13 +34,12 @@ describe('useGetCurrencyToDisplay', () => { }) it('should return Euro by default when location and user are not provided', () => { - mockUseGeolocation.mockReturnValueOnce({ userLocation: null } as ILocationContext) const { result } = renderHook(() => useGetCurrencyToDisplay()) expect(result.current).toBe('€') }) - describe('when location is outside New Caledonia', () => { + describe('when user is not connected and location is outside New Caledonia', () => { beforeEach(() => { mockUseGeolocation.mockReturnValue({ userLocation: PARIS_DEFAULT_POSITION, @@ -66,7 +66,7 @@ describe('useGetCurrencyToDisplay', () => { }) }) - describe('when location is in New Caledonia', () => { + describe('when user is not connected and location is in New Caledonia', () => { beforeEach(() => { mockUseGeolocation.mockReturnValue({ userLocation: NOUMEA_DEFAULT_POSITION, @@ -117,7 +117,7 @@ describe('useGetCurrencyToDisplay', () => { }) }) - describe('when user is in Euro region', () => { + describe('when user is registered in Euro region', () => { beforeEach(() => { mockUseAuthContext.mockReturnValue({ isLoggedIn: true, @@ -147,9 +147,7 @@ describe('useGetCurrencyToDisplay', () => { }) describe('and the feature flag is disabled', () => { - beforeEach(() => { - activateFeatureFlags() - }) + beforeEach(() => activateFeatureFlags()) it('should return Euro when displayFormat is "short"', () => { const { result } = renderHook(() => useGetCurrencyToDisplay('short')) @@ -165,7 +163,7 @@ describe('useGetCurrencyToDisplay', () => { }) }) - describe('when user is in Pacific Franc region', () => { + describe('when user is registered in Pacific Franc region', () => { beforeEach(() => { mockUseAuthContext.mockReturnValue({ isLoggedIn: true, @@ -195,9 +193,7 @@ describe('useGetCurrencyToDisplay', () => { }) describe('and the feature flag is disabled', () => { - beforeEach(() => { - activateFeatureFlags() - }) + beforeEach(() => activateFeatureFlags()) it('should return Euro when displayFormat is "short"', () => { const { result } = renderHook(() => useGetCurrencyToDisplay('short')) diff --git a/src/shared/currency/useGetCurrencyToDisplay.ts b/src/shared/currency/useGetCurrencyToDisplay.ts index 4fa1d9d1790..fd3b8234ce7 100644 --- a/src/shared/currency/useGetCurrencyToDisplay.ts +++ b/src/shared/currency/useGetCurrencyToDisplay.ts @@ -25,20 +25,22 @@ export const useGetCurrencyToDisplay = ( const { selectedPlace } = useLocation() const isNewCaledonianLocationSelected = selectedPlace?.info === 'Nouvelle-Calédonie' - const isNotNewCaledonianLocationSelected = !isNewCaledonianLocationSelected + const isNotNewCaledonianLocationSelected = selectedPlace?.info !== 'Nouvelle-Calédonie' const pacificFrancCurrency = displayFormat === 'full' ? Currency.PACIFIC_FRANC_FULL : Currency.PACIFIC_FRANC_SHORT - switch (true) { - case disablePacificFrancCurrency: - case isUserRegisteredInEuroRegion: - case isNotNewCaledonianLocationSelected: - return Currency.EURO - case enablePacificFrancCurrency && - (isUserRegisteredInPacificFrancRegion || isNewCaledonianLocationSelected): - return pacificFrancCurrency - default: - return Currency.EURO + if (disablePacificFrancCurrency) return Currency.EURO + + if (selectedPlace) { + if (isNotNewCaledonianLocationSelected) return Currency.EURO + if (isNewCaledonianLocationSelected) return pacificFrancCurrency + } + + if (user) { + if (isUserRegisteredInEuroRegion) return Currency.EURO + if (isUserRegisteredInPacificFrancRegion) return pacificFrancCurrency } + + return Currency.EURO }