Skip to content

Commit

Permalink
fix: check redirection based on authorize country (#18004)
Browse files Browse the repository at this point in the history
  • Loading branch information
lubega-deriv authored Jan 20, 2025
1 parent d7e88cd commit 967475c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
7 changes: 5 additions & 2 deletions packages/api-v2/src/hooks/useIsHubRedirectionEnabled.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import useAuthorize from './useAuthorize';
import useClientCountry from './useClientCountry';
import useGrowthbookGetFeatureValue from './useGrowthbookGetFeatureValue';

Expand All @@ -10,13 +11,15 @@ const useIsHubRedirectionEnabled = () => {
featureFlag: 'hub_enabled_country_list',
});
const { data: clientCountry } = useClientCountry();
const { data: authorize } = useAuthorize();
const country = authorize?.country ? authorize.country : clientCountry;

const isHubRedirectionEnabled =
typeof hubEnabledCountryList === 'object' &&
hubEnabledCountryList !== null &&
Array.isArray((hubEnabledCountryList as THubEnabledCountryList).hub_enabled_country_list) &&
clientCountry &&
(hubEnabledCountryList as THubEnabledCountryList).hub_enabled_country_list.includes(clientCountry);
country &&
(hubEnabledCountryList as THubEnabledCountryList).hub_enabled_country_list.includes(country);

return { isHubRedirectionEnabled };
};
Expand Down
44 changes: 38 additions & 6 deletions packages/hooks/src/__tests__/useIsHubRedirectionEnabled.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import React from 'react';

import { mockStore, StoreProvider } from '@deriv/stores';
import { renderHook } from '@testing-library/react-hooks';

import useAuthorize from '../useAuthorize';
import useGrowthbookGetFeatureValue from '../useGrowthbookGetFeatureValue';
import useIsHubRedirectionEnabled from '../useIsHubRedirectionEnabled';
import { mockStore, StoreProvider } from '@deriv/stores';

jest.mock('../useGrowthbookGetFeatureValue', () =>
jest.fn(() => [
Expand All @@ -13,6 +15,14 @@ jest.mock('../useGrowthbookGetFeatureValue', () =>
])
);

jest.mock('../useAuthorize', () =>
jest.fn(() => ({
data: {
country: 'UK',
},
}))
);

describe('useIsHubRedirectionEnabled', () => {
const mock_store = mockStore({
client: {
Expand All @@ -24,29 +34,51 @@ describe('useIsHubRedirectionEnabled', () => {
mock_store.client.clients_country = 'US';
(useGrowthbookGetFeatureValue as jest.Mock).mockReturnValue([
{
hub_enabled_country_list: ['AU'],
hub_enabled_country_list: ['US', 'AU', 'UK'],
},
]);
(useAuthorize as jest.Mock).mockReturnValue({
data: {
country: 'UK',
},
});
});

const wrapper = ({ children }: { children: JSX.Element }) => (
<StoreProvider store={mock_store}>{children}</StoreProvider>
);

it('should return initial state correctly', () => {
(useGrowthbookGetFeatureValue as jest.Mock).mockReturnValue([
{
hub_enabled_country_list: ['AU'],
},
]);
const { result } = renderHook(() => useIsHubRedirectionEnabled(), { wrapper });

expect(result.current.isHubRedirectionEnabled).toBe(false);
});

it('should return true if client country is in the hub enabled list', () => {
mock_store.client.clients_country = 'UK';
it('should return true if authorize country is in the hub enabled list', () => {
const { result } = renderHook(() => useIsHubRedirectionEnabled(), { wrapper });
expect(result.current.isHubRedirectionEnabled).toBe(true);
});

it('should return true if client country is in the hub enabled list and authorize is undefined', () => {
(useAuthorize as jest.Mock).mockReturnValue({
data: undefined,
});
const { result } = renderHook(() => useIsHubRedirectionEnabled(), { wrapper });
expect(result.current.isHubRedirectionEnabled).toBe(true);
});

it('should return false if authorize and client country is not in the hub enabled list', () => {
(useGrowthbookGetFeatureValue as jest.Mock).mockReturnValue([
{
hub_enabled_country_list: ['US', 'AU', 'UK'],
hub_enabled_country_list: ['AU'],
},
]);
const { result } = renderHook(() => useIsHubRedirectionEnabled(), { wrapper });
expect(result.current.isHubRedirectionEnabled).toBe(true);
expect(result.current.isHubRedirectionEnabled).toBe(false);
});
});
7 changes: 5 additions & 2 deletions packages/hooks/src/useIsHubRedirectionEnabled.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useStore } from '@deriv/stores';

import useAuthorize from './useAuthorize';
import useGrowthbookGetFeatureValue from './useGrowthbookGetFeatureValue';

type THubEnabledCountryList = {
Expand All @@ -10,15 +11,17 @@ const useIsHubRedirectionEnabled = () => {
const [hubEnabledCountryList] = useGrowthbookGetFeatureValue({
featureFlag: 'hub_enabled_country_list',
});
const { data: authorize } = useAuthorize();
const { client } = useStore();
const { clients_country } = client;
const country = authorize?.country ? authorize.country : clients_country;

const isHubRedirectionEnabled =
typeof hubEnabledCountryList === 'object' &&
hubEnabledCountryList !== null &&
Array.isArray((hubEnabledCountryList as THubEnabledCountryList).hub_enabled_country_list) &&
clients_country &&
(hubEnabledCountryList as THubEnabledCountryList).hub_enabled_country_list.includes(clients_country);
country &&
(hubEnabledCountryList as THubEnabledCountryList).hub_enabled_country_list.includes(country);

return { isHubRedirectionEnabled };
};
Expand Down

0 comments on commit 967475c

Please sign in to comment.