1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-28 23:06:37 +01:00
metamask-extension/ui/app/ducks/swaps/swaps.test.js
Daniel e7d7d24d83
Reduce calls of the /featureFlag API (#10859)
* Remove periodic calls to the /featureFlag API

* Always show the Swap button on the main page

* Check if the Swaps feature is enabled, show loading animation while waiting

* Reuse an existing useEffect call

* Use ‘isFeatureFlagLoaded’ in React’s state, resolve lint issues

* Add a watch mode for Jest testing

* Add unit tests for Swaps: fetchSwapsLiveness, add /ducks/swaps into Jest testing

* Remove Swaps Jest tests from Mocha’s ESLint rules

* Ignore Swaps Jest tests while running Mocha, update paths

* Increase test coverage to the current max

* Fix ESLint issues for Swaps

* Enable the Swaps feature by default and after state reset, remove loading screen before seeing Swaps

* Update Jest config, fix tests

* Update Jest coverage threshold to the current maximum

* Update ESLint rule in jest.config.js

* Disable the “Review Swap” button if the feature flag hasn’t loaded yet

* Update jest threshold
2021-04-14 04:46:27 -02:30

119 lines
3.7 KiB
JavaScript

import nock from 'nock';
import { setSwapsLiveness } from '../../store/actions';
import { setStorageItem } from '../../../lib/storage-helpers';
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(
'cachedFetch:https://api.metaswap.codefi.network/featureFlag',
null,
);
};
afterEach(() => {
cleanFeatureFlagApiCache();
});
const mockFeatureFlagApiResponse = ({
active = false,
replyWithError = false,
} = {}) => {
const apiNock = nock('https://api.metaswap.codefi.network').get(
'/featureFlag',
);
if (replyWithError) {
return apiNock.replyWithError({
message: 'Server error. Try again later',
code: 'serverSideError',
});
}
return apiNock.reply(200, {
active,
});
};
const createGetState = () => {
return () => ({
metamask: { provider: { ...providerState } },
});
};
it('returns true if the Swaps feature is enabled', async () => {
const mockDispatch = jest.fn();
const featureFlagApiNock = mockFeatureFlagApiResponse({ active: true });
const isSwapsFeatureEnabled = await swaps.fetchSwapsLiveness()(
mockDispatch,
createGetState(),
);
expect(featureFlagApiNock.isDone()).toBe(true);
expect(mockDispatch).toHaveBeenCalledTimes(1);
expect(setSwapsLiveness).toHaveBeenCalledWith(true);
expect(isSwapsFeatureEnabled).toBe(true);
});
it('returns false if the Swaps feature is disabled', async () => {
const mockDispatch = jest.fn();
const featureFlagApiNock = mockFeatureFlagApiResponse({ active: false });
const isSwapsFeatureEnabled = await swaps.fetchSwapsLiveness()(
mockDispatch,
createGetState(),
);
expect(featureFlagApiNock.isDone()).toBe(true);
expect(mockDispatch).toHaveBeenCalledTimes(1);
expect(setSwapsLiveness).toHaveBeenCalledWith(false);
expect(isSwapsFeatureEnabled).toBe(false);
});
it('returns false if the /featureFlag API call throws an error', async () => {
const mockDispatch = jest.fn();
const featureFlagApiNock = mockFeatureFlagApiResponse({
replyWithError: true,
});
const isSwapsFeatureEnabled = await swaps.fetchSwapsLiveness()(
mockDispatch,
createGetState(),
);
expect(featureFlagApiNock.isDone()).toBe(true);
expect(mockDispatch).toHaveBeenCalledTimes(1);
expect(setSwapsLiveness).toHaveBeenCalledWith(false);
expect(isSwapsFeatureEnabled).toBe(false);
});
it('only calls the API once and returns true from cache for the second call', async () => {
const mockDispatch = jest.fn();
const featureFlagApiNock = mockFeatureFlagApiResponse({ active: true });
await swaps.fetchSwapsLiveness()(mockDispatch, createGetState());
expect(featureFlagApiNock.isDone()).toBe(true);
const featureFlagApiNock2 = mockFeatureFlagApiResponse({ active: true });
const isSwapsFeatureEnabled = await swaps.fetchSwapsLiveness()(
mockDispatch,
createGetState(),
);
expect(featureFlagApiNock2.isDone()).toBe(false); // Second API call wasn't made, cache was used instead.
expect(mockDispatch).toHaveBeenCalledTimes(2);
expect(setSwapsLiveness).toHaveBeenCalledWith(true);
expect(isSwapsFeatureEnabled).toBe(true);
});
});
});