2021-04-14 09:16:27 +02:00
|
|
|
import nock from 'nock';
|
|
|
|
|
2021-07-30 13:35:30 +02:00
|
|
|
import { MOCKS, createSwapsMockStore } from '../../../test/jest';
|
2021-04-14 09:16:27 +02:00
|
|
|
import { setSwapsLiveness } from '../../store/actions';
|
2021-04-28 21:53:59 +02:00
|
|
|
import { setStorageItem } from '../../helpers/utils/storage-helpers';
|
2021-04-14 09:16:27 +02:00
|
|
|
import * as swaps from './swaps';
|
|
|
|
|
|
|
|
jest.mock('../../store/actions.js', () => ({
|
|
|
|
setSwapsLiveness: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
const providerState = {
|
|
|
|
chainId: '0x1',
|
|
|
|
nickname: '',
|
|
|
|
rpcPrefs: {},
|
|
|
|
rpcUrl: '',
|
|
|
|
ticker: 'ETH',
|
|
|
|
type: 'mainnet',
|
|
|
|
};
|
|
|
|
|
|
|
|
describe('Ducks - Swaps', () => {
|
|
|
|
afterEach(() => {
|
|
|
|
nock.cleanAll();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('fetchSwapsLiveness', () => {
|
|
|
|
const cleanFeatureFlagApiCache = () => {
|
|
|
|
setStorageItem(
|
2021-07-09 17:24:00 +02:00
|
|
|
'cachedFetch:https://api2.metaswap.codefi.network/featureFlags',
|
2021-04-14 09:16:27 +02:00
|
|
|
null,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
cleanFeatureFlagApiCache();
|
|
|
|
});
|
|
|
|
|
2021-07-09 17:24:00 +02:00
|
|
|
const mockFeatureFlagsApiResponse = ({
|
|
|
|
featureFlagsResponse,
|
2021-04-14 09:16:27 +02:00
|
|
|
replyWithError = false,
|
|
|
|
} = {}) => {
|
2021-07-09 17:24:00 +02:00
|
|
|
const apiNock = nock('https://api2.metaswap.codefi.network').get(
|
|
|
|
'/featureFlags',
|
2021-04-14 09:16:27 +02:00
|
|
|
);
|
|
|
|
if (replyWithError) {
|
|
|
|
return apiNock.replyWithError({
|
|
|
|
message: 'Server error. Try again later',
|
|
|
|
code: 'serverSideError',
|
|
|
|
});
|
|
|
|
}
|
2021-07-09 17:24:00 +02:00
|
|
|
return apiNock.reply(200, featureFlagsResponse);
|
2021-04-14 09:16:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const createGetState = () => {
|
|
|
|
return () => ({
|
|
|
|
metamask: { provider: { ...providerState } },
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-07-09 17:24:00 +02:00
|
|
|
it('checks that Swaps for ETH are enabled and can use new API', async () => {
|
|
|
|
const mockDispatch = jest.fn();
|
|
|
|
const expectedSwapsLiveness = {
|
|
|
|
swapsFeatureIsLive: true,
|
|
|
|
};
|
|
|
|
const featureFlagsResponse = MOCKS.createFeatureFlagsResponse();
|
|
|
|
const featureFlagApiNock = mockFeatureFlagsApiResponse({
|
|
|
|
featureFlagsResponse,
|
|
|
|
});
|
|
|
|
const swapsLiveness = await swaps.fetchSwapsLiveness()(
|
|
|
|
mockDispatch,
|
|
|
|
createGetState(),
|
|
|
|
);
|
|
|
|
expect(featureFlagApiNock.isDone()).toBe(true);
|
2021-12-01 17:25:09 +01:00
|
|
|
expect(mockDispatch).toHaveBeenCalledTimes(2);
|
2021-07-09 17:24:00 +02:00
|
|
|
expect(setSwapsLiveness).toHaveBeenCalledWith(expectedSwapsLiveness);
|
|
|
|
expect(swapsLiveness).toMatchObject(expectedSwapsLiveness);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('checks that Swaps for ETH are disabled for API v2 and enabled for API v1', async () => {
|
2021-04-14 09:16:27 +02:00
|
|
|
const mockDispatch = jest.fn();
|
2021-07-09 17:24:00 +02:00
|
|
|
const expectedSwapsLiveness = {
|
|
|
|
swapsFeatureIsLive: true,
|
|
|
|
};
|
|
|
|
const featureFlagsResponse = MOCKS.createFeatureFlagsResponse();
|
|
|
|
featureFlagsResponse.ethereum.extension_active = false;
|
|
|
|
const featureFlagApiNock = mockFeatureFlagsApiResponse({
|
|
|
|
featureFlagsResponse,
|
|
|
|
});
|
|
|
|
const swapsLiveness = await swaps.fetchSwapsLiveness()(
|
2021-04-14 09:16:27 +02:00
|
|
|
mockDispatch,
|
|
|
|
createGetState(),
|
|
|
|
);
|
|
|
|
expect(featureFlagApiNock.isDone()).toBe(true);
|
2021-12-01 17:25:09 +01:00
|
|
|
expect(mockDispatch).toHaveBeenCalledTimes(2);
|
2021-07-09 17:24:00 +02:00
|
|
|
expect(setSwapsLiveness).toHaveBeenCalledWith(expectedSwapsLiveness);
|
|
|
|
expect(swapsLiveness).toMatchObject(expectedSwapsLiveness);
|
2021-04-14 09:16:27 +02:00
|
|
|
});
|
|
|
|
|
2021-07-09 17:24:00 +02:00
|
|
|
it('checks that Swaps for ETH are disabled for API v1 and v2', async () => {
|
2021-04-14 09:16:27 +02:00
|
|
|
const mockDispatch = jest.fn();
|
2021-07-09 17:24:00 +02:00
|
|
|
const expectedSwapsLiveness = {
|
|
|
|
swapsFeatureIsLive: false,
|
|
|
|
};
|
|
|
|
const featureFlagsResponse = MOCKS.createFeatureFlagsResponse();
|
|
|
|
featureFlagsResponse.ethereum.extension_active = false;
|
|
|
|
featureFlagsResponse.ethereum.fallback_to_v1 = false;
|
|
|
|
const featureFlagApiNock = mockFeatureFlagsApiResponse({
|
|
|
|
featureFlagsResponse,
|
|
|
|
});
|
|
|
|
const swapsLiveness = await swaps.fetchSwapsLiveness()(
|
2021-04-14 09:16:27 +02:00
|
|
|
mockDispatch,
|
|
|
|
createGetState(),
|
|
|
|
);
|
|
|
|
expect(featureFlagApiNock.isDone()).toBe(true);
|
2021-12-01 17:25:09 +01:00
|
|
|
expect(mockDispatch).toHaveBeenCalledTimes(2);
|
2021-07-09 17:24:00 +02:00
|
|
|
expect(setSwapsLiveness).toHaveBeenCalledWith(expectedSwapsLiveness);
|
|
|
|
expect(swapsLiveness).toMatchObject(expectedSwapsLiveness);
|
2021-04-14 09:16:27 +02:00
|
|
|
});
|
|
|
|
|
2021-07-09 17:24:00 +02:00
|
|
|
it('checks that Swaps for ETH are disabled if the /featureFlags API call throws an error', async () => {
|
2021-04-14 09:16:27 +02:00
|
|
|
const mockDispatch = jest.fn();
|
2021-07-09 17:24:00 +02:00
|
|
|
const expectedSwapsLiveness = {
|
|
|
|
swapsFeatureIsLive: false,
|
|
|
|
};
|
|
|
|
const featureFlagApiNock = mockFeatureFlagsApiResponse({
|
2021-04-14 09:16:27 +02:00
|
|
|
replyWithError: true,
|
|
|
|
});
|
2021-07-09 17:24:00 +02:00
|
|
|
const swapsLiveness = await swaps.fetchSwapsLiveness()(
|
2021-04-14 09:16:27 +02:00
|
|
|
mockDispatch,
|
|
|
|
createGetState(),
|
|
|
|
);
|
|
|
|
expect(featureFlagApiNock.isDone()).toBe(true);
|
2021-12-01 17:25:09 +01:00
|
|
|
expect(mockDispatch).toHaveBeenCalledTimes(2);
|
2021-07-09 17:24:00 +02:00
|
|
|
expect(setSwapsLiveness).toHaveBeenCalledWith(expectedSwapsLiveness);
|
|
|
|
expect(swapsLiveness).toMatchObject(expectedSwapsLiveness);
|
2021-04-14 09:16:27 +02:00
|
|
|
});
|
|
|
|
|
2021-07-09 17:24:00 +02:00
|
|
|
it('only calls the API once and returns response from cache for the second call', async () => {
|
2021-04-14 09:16:27 +02:00
|
|
|
const mockDispatch = jest.fn();
|
2021-07-09 17:24:00 +02:00
|
|
|
const expectedSwapsLiveness = {
|
|
|
|
swapsFeatureIsLive: true,
|
|
|
|
};
|
|
|
|
const featureFlagsResponse = MOCKS.createFeatureFlagsResponse();
|
|
|
|
const featureFlagApiNock = mockFeatureFlagsApiResponse({
|
|
|
|
featureFlagsResponse,
|
|
|
|
});
|
2021-04-14 09:16:27 +02:00
|
|
|
await swaps.fetchSwapsLiveness()(mockDispatch, createGetState());
|
|
|
|
expect(featureFlagApiNock.isDone()).toBe(true);
|
2021-07-09 17:24:00 +02:00
|
|
|
const featureFlagApiNock2 = mockFeatureFlagsApiResponse({
|
|
|
|
featureFlagsResponse,
|
|
|
|
});
|
|
|
|
const swapsLiveness = await swaps.fetchSwapsLiveness()(
|
2021-04-14 09:16:27 +02:00
|
|
|
mockDispatch,
|
|
|
|
createGetState(),
|
|
|
|
);
|
|
|
|
expect(featureFlagApiNock2.isDone()).toBe(false); // Second API call wasn't made, cache was used instead.
|
2021-12-01 17:25:09 +01:00
|
|
|
expect(mockDispatch).toHaveBeenCalledTimes(4);
|
2021-07-09 17:24:00 +02:00
|
|
|
expect(setSwapsLiveness).toHaveBeenCalledWith(expectedSwapsLiveness);
|
|
|
|
expect(swapsLiveness).toMatchObject(expectedSwapsLiveness);
|
2021-04-14 09:16:27 +02:00
|
|
|
});
|
|
|
|
});
|
2021-07-30 13:35:30 +02:00
|
|
|
|
|
|
|
describe('getCustomSwapsGas', () => {
|
|
|
|
it('returns "customMaxGas"', () => {
|
|
|
|
const state = createSwapsMockStore();
|
|
|
|
const customMaxGas = '29000';
|
|
|
|
state.metamask.swapsState.customMaxGas = customMaxGas;
|
|
|
|
expect(swaps.getCustomSwapsGas(state)).toBe(customMaxGas);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getCustomMaxFeePerGas', () => {
|
|
|
|
it('returns "customMaxFeePerGas"', () => {
|
|
|
|
const state = createSwapsMockStore();
|
|
|
|
const customMaxFeePerGas = '20';
|
|
|
|
state.metamask.swapsState.customMaxFeePerGas = customMaxFeePerGas;
|
|
|
|
expect(swaps.getCustomMaxFeePerGas(state)).toBe(customMaxFeePerGas);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getCustomMaxPriorityFeePerGas', () => {
|
|
|
|
it('returns "customMaxPriorityFeePerGas"', () => {
|
|
|
|
const state = createSwapsMockStore();
|
|
|
|
const customMaxPriorityFeePerGas = '3';
|
|
|
|
state.metamask.swapsState.customMaxPriorityFeePerGas = customMaxPriorityFeePerGas;
|
|
|
|
expect(swaps.getCustomMaxPriorityFeePerGas(state)).toBe(
|
|
|
|
customMaxPriorityFeePerGas,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getSwapsFeatureIsLive', () => {
|
|
|
|
it('returns true for "swapsFeatureIsLive"', () => {
|
|
|
|
const state = createSwapsMockStore();
|
|
|
|
const swapsFeatureIsLive = true;
|
|
|
|
state.metamask.swapsState.swapsFeatureIsLive = swapsFeatureIsLive;
|
|
|
|
expect(swaps.getSwapsFeatureIsLive(state)).toBe(swapsFeatureIsLive);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns false for "swapsFeatureIsLive"', () => {
|
|
|
|
const state = createSwapsMockStore();
|
|
|
|
const swapsFeatureIsLive = false;
|
|
|
|
state.metamask.swapsState.swapsFeatureIsLive = swapsFeatureIsLive;
|
|
|
|
expect(swaps.getSwapsFeatureIsLive(state)).toBe(swapsFeatureIsLive);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getUsedQuote', () => {
|
|
|
|
it('returns selected quote', () => {
|
|
|
|
const state = createSwapsMockStore();
|
|
|
|
expect(swaps.getUsedQuote(state)).toMatchObject(
|
|
|
|
state.metamask.swapsState.quotes.TEST_AGG_2,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns best quote', () => {
|
|
|
|
const state = createSwapsMockStore();
|
|
|
|
state.metamask.swapsState.selectedAggId = null;
|
|
|
|
expect(swaps.getUsedQuote(state)).toMatchObject(
|
|
|
|
state.metamask.swapsState.quotes.TEST_AGG_BEST,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2021-04-14 09:16:27 +02:00
|
|
|
});
|