Skip to content

Commit

Permalink
fix: fix bug block explorer field
Browse files Browse the repository at this point in the history
  • Loading branch information
salimtb committed Oct 24, 2024
1 parent 40f5bef commit e3ae00a
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ export class NetworkSettings extends PureComponent {
blockExplorerUrls: [],
selectedRpcEndpointIndex: 0,
blockExplorerUrl: undefined,
blockExplorerUrlForm: undefined,
nickname: undefined,
chainId: undefined,
ticker: undefined,
Expand Down Expand Up @@ -1288,10 +1289,21 @@ export class NetworkSettings extends PureComponent {
};

onBlockExplorerItemAdd = async (url) => {
// If URL is empty or undefined, return early
if (!url) {
return;
}

// Check if the URL already exists in blockExplorerUrls
const { blockExplorerUrls } = this.state;
const urlExists = blockExplorerUrls.includes(url);

if (urlExists) {
// If the URL already exists, return early
return;
}

// If the URL doesn't exist, proceed with adding it
await this.setState((prevState) => ({
blockExplorerUrls: [...prevState.blockExplorerUrls, url],
}));
Expand Down Expand Up @@ -1351,6 +1363,7 @@ export class NetworkSettings extends PureComponent {
onBlockExplorerUrlChange = async (url) => {
const { addMode } = this.state;
await this.setState({
blockExplorerUrlForm: url,
blockExplorerUrl: url,
});

Expand Down Expand Up @@ -1486,7 +1499,10 @@ export class NetworkSettings extends PureComponent {
};

closeAddBlockExplorerRpcForm = () => {
this.setState({ showAddBlockExplorerForm: { isVisible: false } });
this.setState({
showAddBlockExplorerForm: { isVisible: false },
blockExplorerUrlForm: undefined,
});
};

closeRpcModal = () => {
Expand Down Expand Up @@ -1603,6 +1619,7 @@ export class NetworkSettings extends PureComponent {
rpcUrlForm,
rpcNameForm,
rpcName,
blockExplorerUrlForm,
} = this.state;
const { route, networkConfigurations } = this.props;
const isCustomMainnet = route.params?.isCustomMainnet;
Expand Down Expand Up @@ -2204,6 +2221,7 @@ export class NetworkSettings extends PureComponent {
ref={this.inputBlockExplorerURL}
style={inputStyle}
autoCapitalize={'none'}
value={blockExplorerUrlForm}
autoCorrect={false}
onChangeText={this.onBlockExplorerUrlChange}
placeholder={strings(
Expand All @@ -2214,23 +2232,30 @@ export class NetworkSettings extends PureComponent {
onSubmitEditing={this.toggleNetworkDetailsModal}
keyboardAppearance={themeAppearance}
/>
{blockExplorerUrl && !isUrl(blockExplorerUrl) && (
<View>
<Text style={styles.warningText}>
{strings('app_settings.invalid_block_explorer_url')}
</Text>
</View>
)}
{blockExplorerUrl &&
(!isUrl(blockExplorerUrl) ||
blockExplorerUrls.includes(blockExplorerUrlForm)) && (
<View>
<Text style={styles.warningText}>
{strings('app_settings.invalid_block_explorer_url')}
</Text>
</View>
)}

<View style={styles.addRpcNameButton}>
<ButtonPrimary
label={strings('app_settings.add_block_explorer_url')}
size={ButtonSize.Lg}
onPress={() => {
this.onBlockExplorerItemAdd(blockExplorerUrl);
this.onBlockExplorerItemAdd(blockExplorerUrlForm);
}}
width={ButtonWidthTypes.Full}
labelTextVariant={TextVariant.DisplayMD}
isDisabled={!blockExplorerUrl || !isUrl(blockExplorerUrl)}
isDisabled={
!blockExplorerUrl ||
!blockExplorerUrlForm ||
!isUrl(blockExplorerUrl)
}
/>
</View>
</SafeAreaView>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,47 @@ describe('NetworkSettings', () => {
);
});

it('should not add an empty Block Explorer URL and should return early', async () => {
const instance = wrapper.instance();

// Initially, blockExplorerUrls should be empty
expect(wrapper.state('blockExplorerUrls').length).toBe(0);

// Open Block Explorer form modal and attempt to add an empty URL
instance.openAddBlockExplorerForm();
await instance.onBlockExplorerItemAdd('');

// Ensure the state is not updated with the empty URL
expect(wrapper.state('blockExplorerUrls').length).toBe(0);
expect(wrapper.state('blockExplorerUrl')).toBeUndefined();
});

it('should not add an existing Block Explorer URL and should return early', async () => {
const instance = wrapper.instance();

// Set initial state with an existing block explorer URL
await instance.setState({
blockExplorerUrls: ['https://existing-blockexplorer.com'],
});

// Ensure the initial state contains the existing URL
expect(wrapper.state('blockExplorerUrls').length).toBe(1);
expect(wrapper.state('blockExplorerUrls')[0]).toBe(
'https://existing-blockexplorer.com',
);

// Attempt to add the same URL again
await instance.onBlockExplorerItemAdd(
'https://existing-blockexplorer.com',
);

// Ensure the state remains unchanged and no duplicate is added
expect(wrapper.state('blockExplorerUrls').length).toBe(1);
expect(wrapper.state('blockExplorerUrls')[0]).toBe(
'https://existing-blockexplorer.com',
);
});

it('should call validateRpcAndChainId when chainId and rpcUrl are set', async () => {
const instance = wrapper.instance();
const validateRpcAndChainIdSpy = jest.spyOn(
Expand Down

0 comments on commit e3ae00a

Please sign in to comment.