Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: close network/accounts toasts when opening the Edit page #29239

Merged
merged 8 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ export const ReviewPermissions = () => {
setShowAccountToast(true);
};

const hideAllToasts = () => {
setShowAccountToast(false);
setShowNetworkToast(false);
};

return (
<Page
data-testid="connections-page"
Expand All @@ -195,6 +200,7 @@ export const ReviewPermissions = () => {
onSelectChainIds={handleSelectChainIds}
selectedAccountAddresses={connectedAccountAddresses}
selectedChainIds={connectedChainIds}
hideAllToasts={hideAllToasts}
/>
) : (
<NoConnectionContent />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import React from 'react';
import { Provider } from 'react-redux';
import { render, fireEvent } from '@testing-library/react';
import configureStore from '../../../../../store/store';
import { SiteCell } from './site-cell';

describe('SiteCell', () => {
const store = configureStore({
metamask: {
useBlockie: false,
},
});

describe('toast handling', () => {
it('should call hideAllToasts when edit accounts is clicked', () => {
const hideAllToasts = jest.fn();
const { getAllByTestId } = render(
<Provider store={store}>
<SiteCell
nonTestNetworks={[]}
testNetworks={[]}
accounts={[]}
onSelectAccountAddresses={() => undefined}
onSelectChainIds={() => undefined}
selectedAccountAddresses={[]}
selectedChainIds={[]}
hideAllToasts={hideAllToasts}
/>
</Provider>,
);

const editButtons = getAllByTestId('edit');
fireEvent.click(editButtons[0]);
expect(hideAllToasts).toHaveBeenCalled();
});

it('should call hideAllToasts when edit networks is clicked', () => {
const hideAllToasts = jest.fn();
const { getAllByTestId } = render(
<Provider store={store}>
<SiteCell
nonTestNetworks={[]}
testNetworks={[]}
accounts={[]}
onSelectAccountAddresses={() => undefined}
onSelectChainIds={() => undefined}
selectedAccountAddresses={[]}
selectedChainIds={[]}
hideAllToasts={hideAllToasts}
/>
</Provider>,
);

const editButtons = getAllByTestId('edit');
fireEvent.click(editButtons[1]);
expect(hideAllToasts).toHaveBeenCalled();
});

it('should not throw if hideAllToasts is not provided', () => {
const { getAllByTestId } = render(
<Provider store={store}>
<SiteCell
nonTestNetworks={[]}
testNetworks={[]}
accounts={[]}
onSelectAccountAddresses={() => undefined}
onSelectChainIds={() => undefined}
selectedAccountAddresses={[]}
selectedChainIds={[]}
/>
</Provider>,
);

expect(() => {
const editButtons = getAllByTestId('edit');
fireEvent.click(editButtons[0]);
fireEvent.click(editButtons[1]);
}).not.toThrow();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type SiteCellProps = {
selectedAccountAddresses: string[];
selectedChainIds: string[];
isConnectFlow?: boolean;
hideAllToasts?: () => void;
};

export const SiteCell: React.FC<SiteCellProps> = ({
Expand All @@ -49,6 +50,7 @@ export const SiteCell: React.FC<SiteCellProps> = ({
selectedAccountAddresses,
selectedChainIds,
isConnectFlow,
hideAllToasts = () => undefined,
}) => {
const t = useI18nContext();
const trackEvent = useContext(MetaMetricsContext);
Expand Down Expand Up @@ -91,6 +93,30 @@ export const SiteCell: React.FC<SiteCellProps> = ({
? t('requestingForNetwork', [selectedNetworks[0].name])
: t('requestingFor');

const handleOpenAccountsModal = () => {
hideAllToasts?.();
setShowEditAccountsModal(true);
trackEvent({
category: MetaMetricsEventCategory.Navigation,
event: MetaMetricsEventName.ViewPermissionedAccounts,
properties: {
location: 'Connect view, Permissions toast, Permissions (dapp)',
},
});
};

const handleOpenNetworksModal = () => {
hideAllToasts?.();
setShowEditNetworksModal(true);
trackEvent({
category: MetaMetricsEventCategory.Navigation,
event: MetaMetricsEventName.ViewPermissionedNetworks,
properties: {
location: 'Connect view, Permissions toast, Permissions (dapp)',
},
});
};

return (
<>
<Box
Expand All @@ -105,16 +131,7 @@ export const SiteCell: React.FC<SiteCellProps> = ({
connectedMessage={accountMessageConnectedState}
unconnectedMessage={accountMessageNotConnectedState}
isConnectFlow={isConnectFlow}
onClick={() => {
setShowEditAccountsModal(true);
trackEvent({
category: MetaMetricsEventCategory.Navigation,
event: MetaMetricsEventName.ViewPermissionedAccounts,
properties: {
location: 'Connect view, Permissions toast, Permissions (dapp)',
},
});
}}
onClick={handleOpenAccountsModal}
paddingBottomValue={2}
paddingTopValue={0}
content={
Expand All @@ -136,16 +153,7 @@ export const SiteCell: React.FC<SiteCellProps> = ({
connectedMessage={networkMessageConnectedState}
unconnectedMessage={networkMessageNotConnectedState}
isConnectFlow={isConnectFlow}
onClick={() => {
setShowEditNetworksModal(true);
trackEvent({
category: MetaMetricsEventCategory.Navigation,
event: MetaMetricsEventName.ViewPermissionedNetworks,
properties: {
location: 'Connect view, Permissions toast, Permissions (dapp)',
},
});
}}
onClick={handleOpenNetworksModal}
paddingTopValue={2}
paddingBottomValue={0}
content={<SiteCellTooltip networks={selectedNetworks} />}
Expand Down
Loading