diff --git a/packages/hooks/src/__tests__/useIsHubRedirectionEnabled.spec.tsx b/packages/hooks/src/__tests__/useIsHubRedirectionEnabled.spec.tsx index 5f2859afcb41..8068c675e8ea 100644 --- a/packages/hooks/src/__tests__/useIsHubRedirectionEnabled.spec.tsx +++ b/packages/hooks/src/__tests__/useIsHubRedirectionEnabled.spec.tsx @@ -1,14 +1,9 @@ -import { useClientCountry, useSettings } from '@deriv/api'; +import React from 'react'; import { renderHook } from '@testing-library/react-hooks'; import useGrowthbookGetFeatureValue from '../useGrowthbookGetFeatureValue'; import useIsHubRedirectionEnabled from '../useIsHubRedirectionEnabled'; - -jest.mock('@deriv/api', () => ({ - ...jest.requireActual('@deriv/api'), - useClientCountry: jest.fn(() => ({ data: 'US' })), - useSettings: jest.fn(() => ({ data: { citizen: 'US' } })), -})); +import { mockStore, StoreProvider } from '@deriv/stores'; jest.mock('../useGrowthbookGetFeatureValue', () => jest.fn(() => [ @@ -19,46 +14,64 @@ jest.mock('../useGrowthbookGetFeatureValue', () => ); describe('useIsHubRedirectionEnabled', () => { + const mock_store = mockStore({ + client: { + account_settings: { citizen: 'US' }, + clients_country: 'US', + }, + }); + + beforeEach(() => { + mock_store.client.clients_country = 'US'; + mock_store.client.account_settings.citizen = 'US'; + (useGrowthbookGetFeatureValue as jest.Mock).mockReturnValue([ + { + hub_enabled_country_list: ['AU'], + }, + ]); + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + it('should return initial state correctly', () => { - const { result } = renderHook(() => useIsHubRedirectionEnabled()); + const { result } = renderHook(() => useIsHubRedirectionEnabled(), { wrapper }); expect(result.current.isHubRedirectionEnabled).toBe(false); expect(result.current.isChangingToHubAppId).toBe(false); }); it('should return false if client country is not in the hub enabled list', () => { - (useClientCountry as jest.Mock).mockReturnValue({ data: 'UK' }); - (useSettings as jest.Mock).mockReturnValue({ data: { citizen: 'UK' } }); + mock_store.client.account_settings.citizen = 'UK'; (useGrowthbookGetFeatureValue as jest.Mock).mockReturnValue([ { hub_enabled_country_list: ['US', 'AU'], }, ]); - const { result } = renderHook(() => useIsHubRedirectionEnabled()); + const { result } = renderHook(() => useIsHubRedirectionEnabled(), { wrapper }); expect(result.current.isHubRedirectionEnabled).toBe(false); }); it('should return true if client country is in the hub enabled list', () => { - (useClientCountry as jest.Mock).mockReturnValue({ data: 'UK' }); - (useSettings as jest.Mock).mockReturnValue({ data: { citizen: 'UK' } }); + mock_store.client.clients_country = 'UK'; (useGrowthbookGetFeatureValue as jest.Mock).mockReturnValue([ { hub_enabled_country_list: ['US', 'AU', 'UK'], }, ]); - const { result } = renderHook(() => useIsHubRedirectionEnabled()); + const { result } = renderHook(() => useIsHubRedirectionEnabled(), { wrapper }); expect(result.current.isHubRedirectionEnabled).toBe(true); }); it('should return isChangingToHubAppId true if client country is in the hub enabled list but not in the citizen list', () => { - (useClientCountry as jest.Mock).mockReturnValue({ data: 'UK' }); - (useSettings as jest.Mock).mockReturnValue({ data: { citizen: 'MY' } }); + mock_store.client.clients_country = 'UK'; (useGrowthbookGetFeatureValue as jest.Mock).mockReturnValue([ { hub_enabled_country_list: ['US', 'AU', 'UK'], }, ]); - const { result } = renderHook(() => useIsHubRedirectionEnabled()); + const { result } = renderHook(() => useIsHubRedirectionEnabled(), { wrapper }); expect(result.current.isChangingToHubAppId).toBe(true); }); }); diff --git a/packages/hooks/src/useIsHubRedirectionEnabled.ts b/packages/hooks/src/useIsHubRedirectionEnabled.ts index d9bd376825ea..1f540a817ece 100644 --- a/packages/hooks/src/useIsHubRedirectionEnabled.ts +++ b/packages/hooks/src/useIsHubRedirectionEnabled.ts @@ -1,4 +1,4 @@ -import { useClientCountry, useSettings } from '@deriv/api'; +import { useStore } from '@deriv/stores'; import useGrowthbookGetFeatureValue from './useGrowthbookGetFeatureValue'; @@ -10,23 +10,22 @@ const useIsHubRedirectionEnabled = () => { const [hubEnabledCountryList] = useGrowthbookGetFeatureValue({ featureFlag: 'hub_enabled_country_list', }); - const { data: clientCountry } = useClientCountry(); - const { data: accountSettings } = useSettings(); - const { citizen } = accountSettings; + const { client } = useStore(); + const { account_settings, clients_country } = client; const isHubRedirectionEnabled = typeof hubEnabledCountryList === 'object' && hubEnabledCountryList !== null && Array.isArray((hubEnabledCountryList as THubEnabledCountryList).hub_enabled_country_list) && - citizen && - (hubEnabledCountryList as THubEnabledCountryList).hub_enabled_country_list.includes(citizen); + account_settings.citizen && + (hubEnabledCountryList as THubEnabledCountryList).hub_enabled_country_list.includes(account_settings.citizen); const isChangingToHubAppId = typeof hubEnabledCountryList === 'object' && hubEnabledCountryList !== null && Array.isArray((hubEnabledCountryList as THubEnabledCountryList).hub_enabled_country_list) && - clientCountry && - (hubEnabledCountryList as THubEnabledCountryList).hub_enabled_country_list.includes(clientCountry); + clients_country && + (hubEnabledCountryList as THubEnabledCountryList).hub_enabled_country_list.includes(clients_country); return { isHubRedirectionEnabled, isChangingToHubAppId }; };