Skip to content

Commit 8c46a7e

Browse files
committed
chore: update unit tests for removal of ramps selector
1 parent 8778dc2 commit 8c46a7e

3 files changed

Lines changed: 37 additions & 24 deletions

File tree

app/components/UI/AssetOverview/Balance/index.test.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ jest.mock('../../Earn/hooks/useMusdConversionTokens', () => ({
6161
}),
6262
}));
6363

64+
jest.mock('../../Earn/hooks/useMusdConversionEligibility', () => ({
65+
useMusdConversionEligibility: () => ({
66+
isEligible: true,
67+
isLoading: false,
68+
geolocation: 'US',
69+
blockedCountries: [],
70+
}),
71+
}));
72+
6473
const mockDAI = {
6574
address: '0x6b175474e89094c44da98b954eedeac495271d0f',
6675
aggregators: ['Metamask', 'Coinmarketcap'],

app/components/UI/Earn/hooks/useMusdConversionEligibility.test.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { renderHook } from '@testing-library/react-hooks';
22
import { useMusdConversionEligibility } from './useMusdConversionEligibility';
33

44
// Mock the selectors
5-
const mockSelectGeolocation = jest.fn();
5+
const mockGetDetectedGeolocation = jest.fn();
66
const mockSelectMusdConversionBlockedCountries = jest.fn();
77

8-
jest.mock('../../../../selectors/rampsController', () => ({
9-
selectGeolocation: (state: unknown) => mockSelectGeolocation(state),
8+
jest.mock('../../../../reducers/fiatOrders', () => ({
9+
getDetectedGeolocation: (state: unknown) => mockGetDetectedGeolocation(state),
1010
}));
1111

1212
jest.mock('../selectors/featureFlags', () => ({
@@ -21,13 +21,13 @@ jest.mock('react-redux', () => ({
2121
describe('useMusdConversionEligibility', () => {
2222
beforeEach(() => {
2323
jest.clearAllMocks();
24-
mockSelectGeolocation.mockReturnValue(null);
24+
mockGetDetectedGeolocation.mockReturnValue(null);
2525
mockSelectMusdConversionBlockedCountries.mockReturnValue([]);
2626
});
2727

2828
describe('isEligible', () => {
2929
it('returns false when geolocation is null (blocks by default for compliance)', () => {
30-
mockSelectGeolocation.mockReturnValue(null);
30+
mockGetDetectedGeolocation.mockReturnValue(null);
3131
mockSelectMusdConversionBlockedCountries.mockReturnValue(['GB']);
3232

3333
const { result } = renderHook(() => useMusdConversionEligibility());
@@ -36,7 +36,7 @@ describe('useMusdConversionEligibility', () => {
3636
});
3737

3838
it('returns false when geolocation is null even with empty blocked list', () => {
39-
mockSelectGeolocation.mockReturnValue(null);
39+
mockGetDetectedGeolocation.mockReturnValue(null);
4040
mockSelectMusdConversionBlockedCountries.mockReturnValue([]);
4141

4242
const { result } = renderHook(() => useMusdConversionEligibility());
@@ -45,7 +45,7 @@ describe('useMusdConversionEligibility', () => {
4545
});
4646

4747
it('returns true when blockedCountries is empty and geolocation is known', () => {
48-
mockSelectGeolocation.mockReturnValue('GB');
48+
mockGetDetectedGeolocation.mockReturnValue('GB');
4949
mockSelectMusdConversionBlockedCountries.mockReturnValue([]);
5050

5151
const { result } = renderHook(() => useMusdConversionEligibility());
@@ -54,7 +54,7 @@ describe('useMusdConversionEligibility', () => {
5454
});
5555

5656
it('returns false when user is in a blocked country', () => {
57-
mockSelectGeolocation.mockReturnValue('GB');
57+
mockGetDetectedGeolocation.mockReturnValue('GB');
5858
mockSelectMusdConversionBlockedCountries.mockReturnValue(['GB', 'US']);
5959

6060
const { result } = renderHook(() => useMusdConversionEligibility());
@@ -63,7 +63,7 @@ describe('useMusdConversionEligibility', () => {
6363
});
6464

6565
it('returns false when user country starts with blocked country code', () => {
66-
mockSelectGeolocation.mockReturnValue('GB-ENG');
66+
mockGetDetectedGeolocation.mockReturnValue('GB-ENG');
6767
mockSelectMusdConversionBlockedCountries.mockReturnValue(['GB']);
6868

6969
const { result } = renderHook(() => useMusdConversionEligibility());
@@ -72,7 +72,7 @@ describe('useMusdConversionEligibility', () => {
7272
});
7373

7474
it('returns true when user is not in any blocked country', () => {
75-
mockSelectGeolocation.mockReturnValue('FR');
75+
mockGetDetectedGeolocation.mockReturnValue('FR');
7676
mockSelectMusdConversionBlockedCountries.mockReturnValue(['GB', 'US']);
7777

7878
const { result } = renderHook(() => useMusdConversionEligibility());
@@ -81,7 +81,7 @@ describe('useMusdConversionEligibility', () => {
8181
});
8282

8383
it('performs case-insensitive comparison for country codes', () => {
84-
mockSelectGeolocation.mockReturnValue('gb');
84+
mockGetDetectedGeolocation.mockReturnValue('gb');
8585
mockSelectMusdConversionBlockedCountries.mockReturnValue(['GB']);
8686

8787
const { result } = renderHook(() => useMusdConversionEligibility());
@@ -90,7 +90,7 @@ describe('useMusdConversionEligibility', () => {
9090
});
9191

9292
it('handles US state codes correctly when US is blocked', () => {
93-
mockSelectGeolocation.mockReturnValue('US-CA');
93+
mockGetDetectedGeolocation.mockReturnValue('US-CA');
9494
mockSelectMusdConversionBlockedCountries.mockReturnValue(['US']);
9595

9696
const { result } = renderHook(() => useMusdConversionEligibility());
@@ -99,7 +99,7 @@ describe('useMusdConversionEligibility', () => {
9999
});
100100

101101
it('returns true for US user when only UK is blocked', () => {
102-
mockSelectGeolocation.mockReturnValue('US-CA');
102+
mockGetDetectedGeolocation.mockReturnValue('US-CA');
103103
mockSelectMusdConversionBlockedCountries.mockReturnValue(['GB']);
104104

105105
const { result } = renderHook(() => useMusdConversionEligibility());
@@ -110,7 +110,7 @@ describe('useMusdConversionEligibility', () => {
110110

111111
describe('return values', () => {
112112
it('returns geolocation from selector', () => {
113-
mockSelectGeolocation.mockReturnValue('FR');
113+
mockGetDetectedGeolocation.mockReturnValue('FR');
114114
mockSelectMusdConversionBlockedCountries.mockReturnValue([]);
115115

116116
const { result } = renderHook(() => useMusdConversionEligibility());
@@ -120,7 +120,7 @@ describe('useMusdConversionEligibility', () => {
120120

121121
it('returns blockedCountries from selector', () => {
122122
const blockedCountries = ['GB', 'US'];
123-
mockSelectGeolocation.mockReturnValue('FR');
123+
mockGetDetectedGeolocation.mockReturnValue('FR');
124124
mockSelectMusdConversionBlockedCountries.mockReturnValue(
125125
blockedCountries,
126126
);
@@ -133,7 +133,7 @@ describe('useMusdConversionEligibility', () => {
133133

134134
describe('isLoading', () => {
135135
it('returns true when geolocation is null', () => {
136-
mockSelectGeolocation.mockReturnValue(null);
136+
mockGetDetectedGeolocation.mockReturnValue(null);
137137
mockSelectMusdConversionBlockedCountries.mockReturnValue([]);
138138

139139
const { result } = renderHook(() => useMusdConversionEligibility());
@@ -142,7 +142,7 @@ describe('useMusdConversionEligibility', () => {
142142
});
143143

144144
it('returns false when geolocation is available', () => {
145-
mockSelectGeolocation.mockReturnValue('US');
145+
mockGetDetectedGeolocation.mockReturnValue('US');
146146
mockSelectMusdConversionBlockedCountries.mockReturnValue([]);
147147

148148
const { result } = renderHook(() => useMusdConversionEligibility());

app/components/UI/Earn/hooks/useMusdConversionEligibility.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useMemo } from 'react';
22
import { useSelector } from 'react-redux';
3-
import { selectGeolocation } from '../../../../selectors/rampsController';
3+
import { getDetectedGeolocation } from '../../../../reducers/fiatOrders';
44
import { selectMusdConversionBlockedCountries } from '../selectors/featureFlags';
55

66
/**
@@ -20,14 +20,18 @@ import { selectMusdConversionBlockedCountries } from '../selectors/featureFlags'
2020
* - blockedCountries: array of blocked country codes from LaunchDarkly
2121
*/
2222
export const useMusdConversionEligibility = () => {
23-
const geolocation = useSelector(selectGeolocation);
23+
const geolocation = useSelector(getDetectedGeolocation);
2424
const blockedCountries = useSelector(selectMusdConversionBlockedCountries);
25-
2625
const isLoading = geolocation === null;
2726

27+
const userCountry = useMemo(() => {
28+
if (geolocation) return geolocation?.toUpperCase().split('-')[0];
29+
return null;
30+
}, [geolocation]);
31+
2832
const isEligible = useMemo(() => {
2933
// Block by default when geolocation is unknown for regulatory compliance
30-
if (!geolocation) {
34+
if (!userCountry) {
3135
return false;
3236
}
3337

@@ -38,16 +42,16 @@ export const useMusdConversionEligibility = () => {
3842

3943
// Check if user's country starts with any blocked country code
4044
// Uses startsWith to handle both "GB" and "GB-ENG" formats
41-
const userCountry = geolocation.toUpperCase();
45+
4246
return blockedCountries.every(
4347
(blockedCountry) => !userCountry.startsWith(blockedCountry.toUpperCase()),
4448
);
45-
}, [geolocation, blockedCountries]);
49+
}, [userCountry, blockedCountries]);
4650

4751
return {
4852
isEligible,
4953
isLoading,
50-
geolocation,
54+
geolocation: userCountry,
5155
blockedCountries,
5256
};
5357
};

0 commit comments

Comments
 (0)