Skip to content

Commit

Permalink
Merge branch 'NOTIFY-709-hotfix-basic-functionality-switch-breaks-e2e…
Browse files Browse the repository at this point in the history
…-tests' of github.com:MetaMask/metamask-extension into NOTIFY-709-hotfix-basic-functionality-switch-breaks-e2e-tests
  • Loading branch information
Prithpal-Sooriya committed May 30, 2024
2 parents 3b3b313 + 279852c commit a31ffac
Show file tree
Hide file tree
Showing 23 changed files with 208 additions and 221 deletions.
6 changes: 6 additions & 0 deletions app/_locales/en/messages.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ describe('AlertModal', () => {
},
});

useAlertsSpy.mockReturnValue({
(useAlertsSpy as jest.Mock).mockReturnValue({
setAlertConfirmed: setAlertConfirmedMock,
alerts: [alertsMock[1]],
generalAlerts: [],
Expand Down
25 changes: 16 additions & 9 deletions ui/components/app/alert-system/alert-modal/alert-modal.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React, { useCallback } from 'react';
import { ButtonVariant } from '@metamask/snaps-sdk';

import { SecurityProvider } from '../../../../../shared/constants/security-provider';
import {
Box,
Button,
Expand Down Expand Up @@ -133,6 +135,11 @@ function AlertHeader({
);
}

function BlockaidAlertDetails() {
const t = useI18nContext();
return <Text textAlign={TextAlign.Center}>{t('blockaidAlertInfo')}</Text>;
}

function AlertDetails({
selectedAlert,
customDetails,
Expand Down Expand Up @@ -277,11 +284,7 @@ export function AlertModal({
customAcknowledgeButton,
enableProvider = true,
}: AlertModalProps) {
const {
isAlertConfirmed,
setAlertConfirmed,
fieldAlerts: alerts,
} = useAlerts(ownerId);
const { isAlertConfirmed, setAlertConfirmed, alerts } = useAlerts(ownerId);

const handleClose = useCallback(() => {
onClose();
Expand Down Expand Up @@ -311,10 +314,14 @@ export function AlertModal({
/>
<AlertHeader selectedAlert={selectedAlert} customTitle={customTitle} />
<ModalBody>
<AlertDetails
selectedAlert={selectedAlert}
customDetails={customDetails}
/>
{selectedAlert?.provider === SecurityProvider.Blockaid ? (
<BlockaidAlertDetails />
) : (
<AlertDetails
selectedAlert={selectedAlert}
customDetails={customDetails}
/>
)}
{customAcknowledgeCheckbox ?? (
<AcknowledgeCheckboxBase
selectedAlert={selectedAlert}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,37 +93,41 @@ describe('ConfirmAlertModal', () => {
expect(onSubmitMock).toHaveBeenCalledTimes(1);
});

it('calls open multiple alert modal when review alerts link is clicked', () => {
const { getByTestId } = renderWithProvider(
<ConfirmAlertModal {...defaultProps} />,
mockStore,
);
// todo: following 2 tests have been temporarily commented out
// we can un-comment as we add more alert providers

fireEvent.click(getByTestId('confirm-alert-modal-review-all-alerts'));
expect(getByTestId('alert-modal-button')).toBeInTheDocument();
});
// it('calls open multiple alert modal when review alerts link is clicked', () => {
// const { getByTestId } = renderWithProvider(
// <ConfirmAlertModal {...defaultProps} />,
// mockStore,
// );

describe('when there are multiple alerts', () => {
it('renders the next alert when the "Got it" button is clicked', () => {
const mockStoreAcknowledgeAlerts = configureMockStore([])({
...STATE_MOCK,
confirmAlerts: {
alerts: { [OWNER_ID_MOCK]: alertsMock },
confirmed: {
[OWNER_ID_MOCK]: {
[FROM_ALERT_KEY_MOCK]: true,
[DATA_ALERT_KEY_MOCK]: false,
},
},
},
});
const { getByTestId, getByText } = renderWithProvider(
<ConfirmAlertModal {...defaultProps} />,
mockStoreAcknowledgeAlerts,
);
fireEvent.click(getByTestId('alert-modal-button'));

expect(getByText(DATA_ALERT_MESSAGE_MOCK)).toBeInTheDocument();
});
});
// fireEvent.click(getByTestId('confirm-alert-modal-review-all-alerts'));
// expect(getByTestId('alert-modal-button')).toBeInTheDocument();
// });

// describe('when there are multiple alerts', () => {
// it('renders the next alert when the "Got it" button is clicked', () => {
// const mockStoreAcknowledgeAlerts = configureMockStore([])({
// ...STATE_MOCK,
// confirmAlerts: {
// alerts: { [OWNER_ID_MOCK]: alertsMock },
// confirmed: {
// [OWNER_ID_MOCK]: {
// [FROM_ALERT_KEY_MOCK]: true,
// [DATA_ALERT_KEY_MOCK]: false,
// },
// },
// },
// });
// const { getByTestId, getByText } = renderWithProvider(
// <ConfirmAlertModal {...defaultProps} />,
// mockStoreAcknowledgeAlerts,
// );
// fireEvent.click(getByTestId('confirm-alert-modal-review-all-alerts'));
// fireEvent.click(getByTestId('alert-modal-button'));

// expect(getByText(DATA_ALERT_MESSAGE_MOCK)).toBeInTheDocument();
// });
// });
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import React, { useCallback, useState } from 'react';

import { SecurityProvider } from '../../../../../shared/constants/security-provider';
import {
Box,
Button,
Expand All @@ -13,7 +15,6 @@ import {
} from '../../../component-library';
import {
AlignItems,
Severity,
TextAlign,
TextVariant,
} from '../../../../helpers/constants/design-system';
Expand All @@ -22,7 +23,6 @@ import useAlerts from '../../../../hooks/useAlerts';
import { AlertModal } from '../alert-modal';
import { AcknowledgeCheckboxBase } from '../alert-modal/alert-modal';
import { MultipleAlertModal } from '../multiple-alert-modal';
import { Alert } from '../../../../ducks/confirm-alerts/confirm-alerts';

export type ConfirmAlertModalProps = {
/** The unique key representing the specific alert field. */
Expand Down Expand Up @@ -56,7 +56,7 @@ function ConfirmButtons({
variant={ButtonVariant.Secondary}
data-testid="confirm-alert-modal-cancel-button"
>
{t('cancel')}
{t('reject')}
</Button>
<Button
variant={ButtonVariant.Primary}
Expand Down Expand Up @@ -119,21 +119,14 @@ export function ConfirmAlertModal({
ownerId,
}: ConfirmAlertModalProps) {
const t = useI18nContext();
const { isAlertConfirmed, fieldAlerts } = useAlerts(ownerId);
const { alerts, unconfirmedDangerAlerts } = useAlerts(ownerId);

const unconfirmedDangerAlerts = fieldAlerts.filter(
(alert: Alert) =>
!isAlertConfirmed(alert.key) && alert.severity === Severity.Danger,
);
const selectedAlert = fieldAlerts.find(
(alert: Alert) => alert.key === alertKey,
);
const hasUnconfirmedDangerAlerts = unconfirmedDangerAlerts.length > 0;
const selectedAlert = alerts.find((alert) => alert.key === alertKey);

const [confirmCheckbox, setConfirmCheckbox] = useState<boolean>(false);
// if there are multiple alerts, show the multiple alert modal
const [multipleAlertModalVisible, setMultipleAlertModalVisible] =
useState<boolean>(hasUnconfirmedDangerAlerts);
useState<boolean>(unconfirmedDangerAlerts.length > 1);

const handleCloseMultipleAlertModal = useCallback(() => {
setMultipleAlertModalVisible(false);
Expand Down Expand Up @@ -170,14 +163,22 @@ export function ConfirmAlertModal({
onClose={onClose}
customTitle={t('confirmAlertModalTitle')}
customDetails={
<ConfirmDetails onAlertLinkClick={handleOpenMultipleAlertModal} />
selectedAlert?.provider === SecurityProvider.Blockaid ? (
SecurityProvider.Blockaid
) : (
<ConfirmDetails onAlertLinkClick={handleOpenMultipleAlertModal} />
)
}
customAcknowledgeCheckbox={
<AcknowledgeCheckboxBase
selectedAlert={selectedAlert}
isConfirmed={confirmCheckbox}
onCheckboxClick={handleConfirmCheckbox}
label={t('confirmAlertModalAcknowledge')}
label={
selectedAlert?.provider === SecurityProvider.Blockaid
? t('confirmAlertModalAcknowledgeBlockaid')
: t('confirmAlertModalAcknowledge')
}
/>
}
customAcknowledgeButton={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export function MultipleAlertModal({
onFinalAcknowledgeClick,
ownerId,
}: MultipleAlertModalProps) {
const { isAlertConfirmed, fieldAlerts: alerts } = useAlerts(ownerId);
const { isAlertConfirmed, alerts } = useAlerts(ownerId);

const [selectedIndex, setSelectedIndex] = useState(
alerts.findIndex((alert: Alert) => alert.key === alertKey),
Expand Down
38 changes: 34 additions & 4 deletions ui/hooks/useAlerts.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Severity } from '../helpers/constants/design-system';
import { renderHookWithProvider } from '../../test/lib/render-helpers';
import { ConfirmAlertsState } from '../ducks/confirm-alerts/confirm-alerts';
import {
ConfirmAlertsState,
AlertSeverity,
} from '../ducks/confirm-alerts/confirm-alerts';
import useAlerts from './useAlerts';

describe('useAlerts', () => {
Expand All @@ -12,10 +15,14 @@ describe('useAlerts', () => {
{
key: fromAlertKeyMock,
field: fromAlertKeyMock,
severity: Severity.Danger,
severity: Severity.Danger as AlertSeverity,
message: 'Alert 1',
},
{ key: dataAlertKeyMock, severity: Severity.Warning, message: 'Alert 2' },
{
key: dataAlertKeyMock,
severity: Severity.Warning as AlertSeverity,
message: 'Alert 2',
},
];

const mockState = {
Expand All @@ -28,7 +35,10 @@ describe('useAlerts', () => {
},
};

const renderHookUseAlert = (ownerId?: string, state?: ConfirmAlertsState) => {
const renderHookUseAlert = (
ownerId?: string,
state?: { confirmAlerts: ConfirmAlertsState },
) => {
return renderHookWithProvider(
() => useAlerts(ownerId ?? ownerIdMock),
state ?? mockState,
Expand All @@ -40,6 +50,26 @@ describe('useAlerts', () => {
describe('alerts', () => {
it('returns all alerts', () => {
expect(result.current.alerts).toEqual(alertsMock);
expect(result.current.hasAlerts).toEqual(true);
expect(result.current.hasDangerAlerts).toEqual(true);
expect(result.current.hasUnconfirmedDangerAlerts).toEqual(false);
});
});

describe('unconfirmedDangerAlerts', () => {
it('returns all unconfirmed danger alerts', () => {
const { result: result1 } = renderHookUseAlert(undefined, {
confirmAlerts: {
alerts: {
[ownerIdMock]: alertsMock,
[ownerId2Mock]: [alertsMock[0]],
},
confirmed: {},
},
});
expect(result1.current.hasAlerts).toEqual(true);
expect(result1.current.hasUnconfirmedDangerAlerts).toEqual(true);
expect(result1.current.unconfirmedDangerAlerts).toHaveLength(1);
});
});

Expand Down
19 changes: 17 additions & 2 deletions ui/hooks/useAlerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
Alert,
setAlertConfirmed as setAlertConfirmedAction,
} from '../ducks/confirm-alerts/confirm-alerts';
import { Severity } from '../helpers/constants/design-system';

const useAlerts = (ownerId: string) => {
const dispatch = useDispatch();
Expand Down Expand Up @@ -56,13 +57,27 @@ const useAlerts = (ownerId: string) => {
[confirmedAlertKeys],
);

const unconfirmedDangerAlerts = alerts.filter(
(alert) =>
!isAlertConfirmed(alert.key) && alert.severity === Severity.Danger,
);
const hasAlerts = alerts.length > 0;
const hasDangerAlerts = alerts.some(
(alert) => alert.severity === Severity.Danger,
);
const hasUnconfirmedDangerAlerts = unconfirmedDangerAlerts.length > 0;

return {
alerts,
generalAlerts,
fieldAlerts,
generalAlerts,
getFieldAlerts,
setAlertConfirmed,
hasAlerts,
hasDangerAlerts,
hasUnconfirmedDangerAlerts,
isAlertConfirmed,
setAlertConfirmed,
unconfirmedDangerAlerts,
};
};

Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit a31ffac

Please sign in to comment.