Skip to content

Commit

Permalink
fix: fix test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
salimtb committed Aug 8, 2024
1 parent 53527e2 commit 4b32b82
Show file tree
Hide file tree
Showing 2 changed files with 225 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -545,14 +545,18 @@ export class NetworkSettings extends PureComponent {
};

checkIfChainIdExists = async (chainId) => {
const checkCustomNetworks = Object.values(
this.props.networkConfigurations,
).filter((item) => item.chainId === toHex(chainId));
const { networkConfigurations } = this.props;

if (isNetworkUiRedesignEnabled && checkCustomNetworks.length > 0) {
return true;
}
return false;
// Convert the chainId to hex format
const hexChainId = toHex(chainId);

// Check if any network configuration matches the given chainId
const chainIdExists = Object.values(networkConfigurations).some(
(item) => item.chainId === hexChainId,
);

// Return true if the chainId exists and the UI redesign is enabled, otherwise false
return isNetworkUiRedesignEnabled && chainIdExists;
};

checkIfNetworkExists = async (rpcUrl) => {
Expand Down Expand Up @@ -1277,6 +1281,51 @@ export class NetworkSettings extends PureComponent {
return null;
};

const renderButtons = () => {
if (addMode || editable) {
return (
<View style={styles.buttonsWrapper}>
{editable ? (
<View style={styles.editableButtonsContainer}>
<Button
size={ButtonSize.Lg}
variant={ButtonVariants.Secondary}
isDanger
onPress={this.removeRpcUrl}
testID={NetworksViewSelectorsIDs.REMOVE_NETWORK_BUTTON}
style={{ ...styles.button, ...styles.cancel }}
label={strings('app_settings.delete')}
/>
<Button
size={ButtonSize.Lg}
variant={ButtonVariants.Primary}
onPress={this.addRpcUrl}
testID={NetworksViewSelectorsIDs.ADD_NETWORKS_BUTTON}
style={styles.button}
label={strings('app_settings.network_save')}
isDisabled={isActionDisabled}
/>
</View>
) : (
<View style={styles.buttonsContainer}>
<Button
size={ButtonSize.Lg}
variant={ButtonVariants.Primary}
onPress={this.toggleNetworkDetailsModal}
testID={NetworksViewSelectorsIDs.ADD_CUSTOM_NETWORK_BUTTON}
style={styles.button}
label={strings('app_settings.network_add')}
isDisabled={isActionDisabled}
width={ButtonWidthTypes.Full}
/>
</View>
)}
</View>
);
}
return null;
};

return this.state.showNetworkDetailsModal ? (
<CustomNetwork
isNetworkModalVisible={this.state.showNetworkDetailsModal}
Expand Down Expand Up @@ -1452,47 +1501,7 @@ export class NetworkSettings extends PureComponent {
testID={CustomDefaultNetworkIDs.USE_THIS_NETWORK_BUTTON_ID}
/>
) : (
(addMode || editable) && (
<View style={styles.buttonsWrapper}>
{editable ? (
<View style={styles.editableButtonsContainer}>
<Button
size={ButtonSize.Lg}
variant={ButtonVariants.Secondary}
isDanger
onPress={this.removeRpcUrl}
testID={NetworksViewSelectorsIDs.REMOVE_NETWORK_BUTTON}
style={{ ...styles.button, ...styles.cancel }}
label={strings('app_settings.delete')}
/>
<Button
size={ButtonSize.Lg}
variant={ButtonVariants.Primary}
onPress={this.addRpcUrl}
testID={NetworksViewSelectorsIDs.ADD_NETWORKS_BUTTON}
style={styles.button}
label={strings('app_settings.network_save')}
isDisabled={isActionDisabled}
/>
</View>
) : (
<View style={styles.buttonsContainer}>
<Button
size={ButtonSize.Lg}
variant={ButtonVariants.Primary}
onPress={this.toggleNetworkDetailsModal}
testID={
NetworksViewSelectorsIDs.ADD_CUSTOM_NETWORK_BUTTON
}
style={styles.button}
label={strings('app_settings.network_add')}
isDisabled={isActionDisabled}
width={ButtonWidthTypes.Full}
/>
</View>
)}
</View>
)
renderButtons()
)}
</View>
</KeyboardAwareScrollView>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import configureMockStore from 'redux-mock-store';
import { Provider } from 'react-redux';
import { ThemeContext, mockTheme } from '../../../../../../app/util/theme';
import { backgroundState } from '../../../../../util/test/initial-root-state';
import { NETWORKS_CHAIN_ID } from '../../../../../constants/network';
import { getEtherscanBaseUrl } from '../../../../../util/etherscan';
import Networks, { getAllNetworks } from '../../../../../util/networks';
import { strings } from '../../../../../../locales/i18n';

jest.useFakeTimers();
const mockStore = configureMockStore();
Expand All @@ -28,6 +24,15 @@ const store = mockStore(initialState);

const SAMPLE_NETWORKSETTINGS_PROPS = {
route: { params: {} },
networkConfigurations: {
chainId: '0x1',
rpcUrl: 'https://mainnet.infura.io/v3/YOUR-PROJECT-ID',
nickname: 'Ethereum mainnet',
rpcPrefs: {
blockExplorerUrl: 'https://etherscan.io',
},
ticker: 'ETH',
},
navigation: { setOptions: jest.fn(), navigate: jest.fn(), goBack: jest.fn() },
};

Expand Down Expand Up @@ -98,12 +103,25 @@ describe('NetworkSettings', () => {

it('should initialize state correctly when networkTypeOrRpcUrl is provided', () => {
const SAMPLE_NETWORKSETTINGS_PROPS_2 = {
route: { params: { networkTypeOrRpcUrl: true } },
route: {
params: { network: 'mainnet' },
},
navigation: {
setOptions: jest.fn(),
navigate: jest.fn(),
goBack: jest.fn(),
},
networkConfigurations: {
'0x1': {
chainId: '0x1',
rpcUrl: 'https://mainnet.infura.io/v3/YOUR-PROJECT-ID',
nickname: 'Ethereum mainnet',
rpcPrefs: {
blockExplorerUrl: 'https://etherscan.io',
},
ticker: 'ETH',
},
},
};

const wrapper2 = shallow(
Expand All @@ -117,10 +135,96 @@ describe('NetworkSettings', () => {
const instance = wrapper2.instance() as NetworkSettings;
instance.componentDidMount();

expect(wrapper2.state('blockExplorerUrl')).toBe(undefined);
expect(wrapper2.state('nickname')).toBe(undefined);
expect(wrapper2.state('chainId')).toBe(undefined);
expect(wrapper2.state('rpcUrl')).toBe(undefined);
expect(wrapper2.state('blockExplorerUrl')).toBe('https://etherscan.io');
expect(wrapper2.state('nickname')).toBe('Ethereum Main Network');
expect(wrapper2.state('chainId')).toBe('0x1');
expect(wrapper2.state('rpcUrl')).toBe('https://mainnet.infura.io/v3/');
});

it('should initialize state correctly when networkTypeOrRpcUrl is provided and isCustomMainnet is true', () => {
const SAMPLE_NETWORKSETTINGS_PROPS_2 = {
route: {
params: { network: 'mainnet', isCustomMainnet: true },
},
navigation: {
setOptions: jest.fn(),
navigate: jest.fn(),
goBack: jest.fn(),
},
networkConfigurations: {
'0x1': {
chainId: '0x1',
rpcUrl: 'https://mainnet.infura.io/v3/YOUR-PROJECT-ID',
nickname: 'Ethereum mainnet',
rpcPrefs: {
blockExplorerUrl: 'https://etherscan.io',
},
ticker: 'ETH',
},
},
};

const wrapperComponent = shallow(
<Provider store={store}>
<NetworkSettings {...SAMPLE_NETWORKSETTINGS_PROPS_2} />
</Provider>,
)
.find(NetworkSettings)
.dive();

const instance = wrapperComponent.instance() as NetworkSettings;
instance.componentDidMount();

expect(wrapperComponent.state('blockExplorerUrl')).toBe(
'https://etherscan.io',
);
expect(wrapperComponent.state('nickname')).toBe('Ethereum Main Custom');
expect(wrapperComponent.state('chainId')).toBe('0x1');
expect(wrapperComponent.state('rpcUrl')).toBe(
'https://mainnet.infura.io/v3/YOUR-PROJECT-ID',
);
});

it('should initialize state correctly when networkTypeOrRpcUrl is provided and allNetworks is not found', () => {
const SAMPLE_NETWORKSETTINGS_PROPS_2 = {
route: {
params: { network: 'https://mainnet.infura.io/v3/YOUR-PROJECT-ID' },
},
navigation: {
setOptions: jest.fn(),
navigate: jest.fn(),
goBack: jest.fn(),
},
networkConfigurations: {
'0x1': {
chainId: '0x1',
rpcUrl: 'https://mainnet.infura.io/v3/YOUR-PROJECT-ID',
nickname: 'Ethereum mainnet',
rpcPrefs: {
blockExplorerUrl: 'https://etherscan.io',
},
ticker: 'ETH',
},
},
};

const wrapper2 = shallow(
<Provider store={store}>
<NetworkSettings {...SAMPLE_NETWORKSETTINGS_PROPS_2} />
</Provider>,
)
.find(NetworkSettings)
.dive();

const instance = wrapper2.instance() as NetworkSettings;
instance.componentDidMount();

expect(wrapper2.state('blockExplorerUrl')).toBe('https://etherscan.io');
expect(wrapper2.state('nickname')).toBe('Ethereum mainnet');
expect(wrapper2.state('chainId')).toBe('0x1');
expect(wrapper2.state('rpcUrl')).toBe(
'https://mainnet.infura.io/v3/YOUR-PROJECT-ID',
);
});

it('should update state and call getCurrentState on nickname change', async () => {
Expand Down Expand Up @@ -221,4 +325,59 @@ describe('NetworkSettings', () => {
expect(wrapper.state('chainId')).toBe('0x1');
expect(getCurrentStateSpy).toHaveBeenCalled();
});

describe('getDecimalChainId', () => {
let wrapperTest;
// Do not need to mock entire Engine. Only need subset of data for testing purposes.
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let instanceTest: any;

beforeEach(() => {
wrapperTest = shallow(
<Provider store={store}>
<ThemeContext.Provider value={mockTheme}>
<NetworkSettings {...SAMPLE_NETWORKSETTINGS_PROPS} />
</ThemeContext.Provider>
</Provider>,
)
.find(NetworkSettings)
.dive();

instanceTest = wrapperTest.instance();
});

afterEach(() => {
jest.clearAllMocks();
});

it('should return the chainId as is if it is falsy', () => {
expect(instanceTest.getDecimalChainId(null)).toBe(null);
expect(instanceTest.getDecimalChainId(undefined)).toBe(undefined);
});

it('should return the chainId as is if it is not a string', () => {
expect(instanceTest.getDecimalChainId(123)).toBe(123);
});

it('should return the chainId as is if it does not start with 0x', () => {
expect(instanceTest.getDecimalChainId('123')).toBe('123');
expect(instanceTest.getDecimalChainId('abc')).toBe('abc');
});

it('should convert hex chainId to decimal string', () => {
expect(instanceTest.getDecimalChainId('0x1')).toBe('1');
expect(instanceTest.getDecimalChainId('0xa')).toBe('10');
expect(instanceTest.getDecimalChainId('0x64')).toBe('100');
expect(instanceTest.getDecimalChainId('0x12c')).toBe('300');
});

it('should handle edge cases for hex chainId conversion', () => {
expect(instanceTest.getDecimalChainId('0x0')).toBe('0');
expect(instanceTest.getDecimalChainId('0xff')).toBe('255');
expect(instanceTest.getDecimalChainId('0x7fffffffffffffff')).toBe(
'9223372036854776000',
);
});
});
});

0 comments on commit 4b32b82

Please sign in to comment.