Skip to content

Commit

Permalink
fix: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistevam committed Mar 3, 2025
1 parent 8eedcda commit 4b9b592
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { useAlerts } from '../context';
import ConfirmAlertModal, { ConfirmAlertModalProps } from './ConfirmAlertModal';
import { Severity } from '../../types/alerts';


jest.mock('../context', () => ({
useAlerts: jest.fn(),
}));
Expand All @@ -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,
Expand All @@ -39,6 +38,8 @@ describe('ConfirmAlertModal', () => {
(useAlerts as jest.Mock).mockReturnValue({
showAlertModal: jest.fn(),
fieldAlerts: [ALERT_MOCK],
hasUnconfirmedFieldDangerAlerts: false,
alertModalVisible: false,
});
});

Expand All @@ -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(<ConfirmAlertModal {...baseProps} />);
expect(queryByText(REVIEW_ALERTS_LABEL)).toBeNull();
Expand Down Expand Up @@ -84,11 +87,26 @@ describe('ConfirmAlertModal', () => {
(useAlerts as jest.Mock).mockReturnValue({
showAlertModal: mockShowAlertModal,
fieldAlerts: [ALERT_MOCK],
hasUnconfirmedFieldDangerAlerts: false,
alertModalVisible: false,
});
const { getByText } = render(<ConfirmAlertModal {...baseProps} />);
await act(async () => {
fireEvent.press(getByText(REVIEW_ALERTS_LABEL));
});
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(<ConfirmAlertModal {...baseProps} />);
expect(mockShowAlertModal).toHaveBeenCalled();
expect(queryByText(CONFIRM_MODAL_TITLE_LABEL)).toBeNull();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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, {
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 4b9b592

Please sign in to comment.