Skip to content

Commit

Permalink
feat(ramps): do not make requests to ramp api if basic functionality …
Browse files Browse the repository at this point in the history
…toggle is on
  • Loading branch information
georgeweiler committed Jun 27, 2024
1 parent 13bdc3b commit ba2eefa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
21 changes: 19 additions & 2 deletions ui/ducks/ramps/ramps.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { configureStore, Store } from '@reduxjs/toolkit';
import RampAPI from '../../helpers/ramps/rampApi/rampAPI';
import { getCurrentChainId } from '../../selectors';
import { getCurrentChainId, getUseExternalServices } from '../../selectors';
import { CHAIN_IDS } from '../../../shared/constants/network';
import rampsReducer, {
fetchBuyableChains,
Expand All @@ -14,6 +14,7 @@ const mockedRampAPI = RampAPI as jest.Mocked<typeof RampAPI>;

jest.mock('../../selectors', () => ({
getCurrentChainId: jest.fn(),
getUseExternalServices: jest.fn(),
getNames: jest.fn(),
}));

Expand Down Expand Up @@ -82,12 +83,28 @@ describe('rampsSlice', () => {
});

describe('fetchBuyableChains', () => {
it('should call RampAPI.getNetworks', async () => {
beforeEach(() => {
// simulate the Basic Functionality Toggle being on
const getUseExternalServicesMock = jest.mocked(getUseExternalServices);
getUseExternalServicesMock.mockReturnValue(true);
});

it('should call RampAPI.getNetworks when the Basic Functionality Toggle is on', async () => {
// @ts-expect-error this is a valid action
await store.dispatch(fetchBuyableChains());
expect(RampAPI.getNetworks).toHaveBeenCalledTimes(1);
});

it('should not call RampAPI.getNetworks when the Basic Functionality Toggle is off', async () => {
const getUseExternalServicesMock = jest.mocked(getUseExternalServices);
getUseExternalServicesMock.mockReturnValue(false);

// @ts-expect-error this is a valid action
await store.dispatch(fetchBuyableChains());

expect(RampAPI.getNetworks).not.toHaveBeenCalled();
});

it('should update the state with the data that is returned', async () => {
const mockBuyableChains = [
{
Expand Down
9 changes: 7 additions & 2 deletions ui/ducks/ramps/ramps.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { createSelector } from 'reselect';
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
import { getCurrentChainId } from '../../selectors';
import { getCurrentChainId, getUseExternalServices } from '../../selectors';
import RampAPI from '../../helpers/ramps/rampApi/rampAPI';
import { hexToDecimal } from '../../../shared/modules/conversion.utils';
import { defaultBuyableChains } from './constants';
import { AggregatorNetwork } from './types';

export const fetchBuyableChains = createAsyncThunk(
'ramps/fetchBuyableChains',
async () => {
async (_, { getState }) => {
const state = getState();
const allowExternalRequests = getUseExternalServices(state);
if (!allowExternalRequests) {
return defaultBuyableChains;
}
return await RampAPI.getNetworks();
},
);
Expand Down

0 comments on commit ba2eefa

Please sign in to comment.