diff --git a/app/components/Views/confirmations/AlertSystem/ConfirmAlertModal/ConfirmAlertModal.test.tsx b/app/components/Views/confirmations/AlertSystem/ConfirmAlertModal/ConfirmAlertModal.test.tsx index 9687839c510..41eef41c76a 100644 --- a/app/components/Views/confirmations/AlertSystem/ConfirmAlertModal/ConfirmAlertModal.test.tsx +++ b/app/components/Views/confirmations/AlertSystem/ConfirmAlertModal/ConfirmAlertModal.test.tsx @@ -4,7 +4,6 @@ import { useAlerts } from '../context'; import ConfirmAlertModal, { ConfirmAlertModalProps } from './ConfirmAlertModal'; import { Severity } from '../../types/alerts'; - jest.mock('../context', () => ({ useAlerts: jest.fn(), })); @@ -17,7 +16,7 @@ const REJECT_BTN = 'Reject'; const REVIEW_ALERTS_LABEL = 'Review all alerts'; const ALERT_MESSAGE_MOCK = 'This is a test alert message.'; const ALERT_DETAILS_MOCK = ['Detail 1', 'Detail 2']; -const ALERT_MOCK = { +const ALERT_MOCK = { key: 'alert1', title: 'Test Alert', message: ALERT_MESSAGE_MOCK, @@ -39,6 +38,8 @@ describe('ConfirmAlertModal', () => { (useAlerts as jest.Mock).mockReturnValue({ showAlertModal: jest.fn(), fieldAlerts: [ALERT_MOCK], + hasUnconfirmedFieldDangerAlerts: false, + alertModalVisible: false, }); }); @@ -55,6 +56,8 @@ describe('ConfirmAlertModal', () => { it('does not render the "Review all alerts" link when there are no field alerts', () => { (useAlerts as jest.Mock).mockReturnValue({ fieldAlerts: [], + hasUnconfirmedFieldDangerAlerts: false, + alertModalVisible: false, }); const { queryByText } = render(); expect(queryByText(REVIEW_ALERTS_LABEL)).toBeNull(); @@ -84,6 +87,8 @@ describe('ConfirmAlertModal', () => { (useAlerts as jest.Mock).mockReturnValue({ showAlertModal: mockShowAlertModal, fieldAlerts: [ALERT_MOCK], + hasUnconfirmedFieldDangerAlerts: false, + alertModalVisible: false, }); const { getByText } = render(); await act(async () => { @@ -91,4 +96,17 @@ describe('ConfirmAlertModal', () => { }); expect(mockShowAlertModal).toHaveBeenCalled(); }); + + it('calls showAlertModal and returns null when alertModalVisible is false and hasUnconfirmedFieldDangerAlerts is true', () => { + const mockShowAlertModal = jest.fn(); + (useAlerts as jest.Mock).mockReturnValue({ + showAlertModal: mockShowAlertModal, + fieldAlerts: [ALERT_MOCK], + hasUnconfirmedFieldDangerAlerts: true, + alertModalVisible: false, + }); + const { queryByText } = render(); + expect(mockShowAlertModal).toHaveBeenCalled(); + expect(queryByText(CONFIRM_MODAL_TITLE_LABEL)).toBeNull(); + }); }); diff --git a/app/components/Views/confirmations/AlertSystem/context/Alerts.context.test.tsx b/app/components/Views/confirmations/AlertSystem/context/Alerts.context.test.tsx index a42333252e5..3b8987b21c7 100644 --- a/app/components/Views/confirmations/AlertSystem/context/Alerts.context.test.tsx +++ b/app/components/Views/confirmations/AlertSystem/context/Alerts.context.test.tsx @@ -2,12 +2,17 @@ import React from 'react'; import { act, renderHook } from '@testing-library/react-hooks'; import { Severity, Alert } from '../../types/alerts'; import { useAlerts, AlertsContextProvider, AlertsContextParams } from './Alerts.context'; +import { useAlertsConfirmed } from '../../../../hooks/useAlertsConfirmed'; jest.mock('../../hooks/useConfirmationAlerts', () => ({ __esModule: true, default: jest.fn(), })); +jest.mock('../../../../hooks/useAlertsConfirmed', () => ({ + useAlertsConfirmed: jest.fn(), +})); + describe('AlertsContext', () => { const dangerAlertMock: Alert = { key: 'alert1', @@ -33,8 +38,18 @@ describe('AlertsContext', () => { const alertsMock = [dangerAlertMock, warningAlertMock, infoAlertMock]; + const mockUseAlertsConfirmed = { + hasUnconfirmedDangerAlerts: false, + hasUnconfirmedFieldDangerAlerts: false, + isAlertConfirmed: jest.fn(), + setAlertConfirmed: jest.fn(), + unconfirmedDangerAlerts: [], + unconfirmedFieldDangerAlerts: [], + }; + beforeEach(() => { jest.clearAllMocks(); + (useAlertsConfirmed as jest.Mock).mockReturnValue(mockUseAlertsConfirmed); }); const renderHookWithProvider = (hook: () => AlertsContextParams) => renderHook(hook, { @@ -53,6 +68,12 @@ describe('AlertsContext', () => { expect(result.current.hasDangerAlerts).toBeDefined(); expect(result.current.hideAlertModal).toBeDefined(); expect(result.current.showAlertModal).toBeDefined(); + expect(result.current.hasUnconfirmedDangerAlerts).toBeDefined(); + expect(result.current.hasUnconfirmedFieldDangerAlerts).toBeDefined(); + expect(result.current.isAlertConfirmed).toBeDefined(); + expect(result.current.setAlertConfirmed).toBeDefined(); + expect(result.current.unconfirmedDangerAlerts).toBeDefined(); + expect(result.current.unconfirmedFieldDangerAlerts).toBeDefined(); }); it('provides showAlertModal and hideAlertModal functions', () => { @@ -83,46 +104,43 @@ describe('AlertsContext', () => { expect(result.current.hasAlerts).toBe(false); expect(result.current.hasDangerAlerts).toBe(false); }); - }); - - it('returns all alerts', () => { - const { result } = renderHookWithProvider(() => useAlerts()); - expect(result.current.alerts).toEqual(alertsMock); - expect(result.current.hasAlerts).toEqual(true); - }); + it('returns all alerts', () => { + const { result } = renderHookWithProvider(() => useAlerts()); + expect(result.current.alerts).toEqual(alertsMock); + expect(result.current.hasAlerts).toEqual(true); + }); - it('returns alerts ordered by severity', () => { - const { result } = renderHookWithProvider(() => useAlerts()); - const orderedAlerts = result.current.alerts; - expect(orderedAlerts[0].severity).toEqual(Severity.Danger); - }); + it('returns alerts ordered by severity', () => { + const { result } = renderHookWithProvider(() => useAlerts()); + const orderedAlerts = result.current.alerts; + expect(orderedAlerts[0].severity).toEqual(Severity.Danger); + }); - it('returns general alerts sorted by severity', () => { - const { result } = renderHookWithProvider(() => useAlerts()); - const generalAlerts = result.current.generalAlerts; - expect(generalAlerts[0]?.severity).toEqual(Severity.Warning); - }); + it('returns general alerts sorted by severity', () => { + const { result } = renderHookWithProvider(() => useAlerts()); + const generalAlerts = result.current.generalAlerts; + expect(generalAlerts[0]?.severity).toEqual(Severity.Warning); + }); - it('returns all alerts with field property', () => { - const { result } = renderHookWithProvider(() => useAlerts()); - expect(result.current.fieldAlerts).toEqual([dangerAlertMock]); - }); + it('returns all alerts with field property', () => { + const { result } = renderHookWithProvider(() => useAlerts()); + expect(result.current.fieldAlerts).toEqual([dangerAlertMock]); + }); - it('initializes with the correct alert key', () => { - const { result } = renderHookWithProvider(() => useAlerts()); - expect(result.current.alertKey).toBe(dangerAlertMock.key); - }); + it('initializes with the correct alert key', () => { + const { result } = renderHookWithProvider(() => useAlerts()); + expect(result.current.alertKey).toBe(dangerAlertMock.key); + }); - it('sets a new alert key', () => { - const { result } = renderHookWithProvider(() => useAlerts()); - act(() => { - result.current.setAlertKey(warningAlertMock.key); + it('sets a new alert key', () => { + const { result } = renderHookWithProvider(() => useAlerts()); + act(() => { + result.current.setAlertKey(warningAlertMock.key); + }); + expect(result.current.alertKey).toBe(warningAlertMock.key); }); - expect(result.current.alertKey).toBe(warningAlertMock.key); - }); - describe('AlertsContextProvider', () => { it('should throw error if not wrapped in AlertsContextProvider', () => { expect(() => { useAlerts();