Skip to content

Commit

Permalink
chore: fix redirection logs user out (#17954)
Browse files Browse the repository at this point in the history
  • Loading branch information
suisin-deriv authored Jan 8, 2025
1 parent 7fcb0c5 commit 68d1aec
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
47 changes: 30 additions & 17 deletions packages/hooks/src/__tests__/useIsHubRedirectionEnabled.spec.tsx
Original file line number Diff line number Diff line change
@@ -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(() => [
Expand All @@ -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 }) => (
<StoreProvider store={mock_store}>{children}</StoreProvider>
);

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);
});
});
15 changes: 7 additions & 8 deletions packages/hooks/src/useIsHubRedirectionEnabled.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useClientCountry, useSettings } from '@deriv/api';
import { useStore } from '@deriv/stores';

import useGrowthbookGetFeatureValue from './useGrowthbookGetFeatureValue';

Expand All @@ -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 };
};
Expand Down

0 comments on commit 68d1aec

Please sign in to comment.