Skip to content

Commit

Permalink
added unit testcases for useToastsInternal
Browse files Browse the repository at this point in the history
  • Loading branch information
anjumnnit committed Oct 28, 2024
1 parent 8058491 commit 7f54546
Showing 1 changed file with 152 additions and 0 deletions.
152 changes: 152 additions & 0 deletions __tests__/hooks/internal/useToastsInternal.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// Import required testing utilities and hooks
import { renderHook, act } from '@testing-library/react-hooks';
import { useToastInternal } from '../../../src/hooks/internal/useToastsInternal';
import { useSettingsContext } from '../../../src/context/SettingsContext';
import { useToastsContext } from '../../../src/context/ToastsContext';
import { useRcbEventInternal } from '../../../src/hooks/internal/useRcbEventInternal';
import { RcbEvent } from '../../../src/constants/RcbEvent';
import { generateSecureUUID } from '../../../src/utils/idGenerator';

// Mock dependencies used in the tests
jest.mock('../../../src/context/SettingsContext');
jest.mock('../../../src/context/ToastsContext');
jest.mock('../../../src/hooks/internal/useRcbEventInternal');
jest.mock('../../../src/utils/idGenerator', () => ({
generateSecureUUID: jest.fn(),
}));

describe('useToastInternal', () => {
let mockSettingsContext: any;

Check warning on line 19 in __tests__/hooks/internal/useToastsInternal.test.tsx

View workflow job for this annotation

GitHub Actions / CI / Run Lint

Unexpected any. Specify a different type
let mockToastsContext: any;

Check warning on line 20 in __tests__/hooks/internal/useToastsInternal.test.tsx

View workflow job for this annotation

GitHub Actions / CI / Run Lint

Unexpected any. Specify a different type
let mockRcbEventInternal: any;

Check warning on line 21 in __tests__/hooks/internal/useToastsInternal.test.tsx

View workflow job for this annotation

GitHub Actions / CI / Run Lint

Unexpected any. Specify a different type

beforeEach(() => {
// Set up mock settings and contexts before each test
mockSettingsContext = {
settings: {
toast: { maxCount: 3, forbidOnMax: true },
event: {
rcbShowToast: true,
rcbDismissToast: true,
}
}
};
(useSettingsContext as jest.Mock).mockReturnValue(mockSettingsContext);

mockToastsContext = {
toasts: [],
setToasts: jest.fn(),
};
(useToastsContext as jest.Mock).mockReturnValue(mockToastsContext);

mockRcbEventInternal = {
callRcbEvent: jest.fn(),
};
(useRcbEventInternal as jest.Mock).mockReturnValue(mockRcbEventInternal);

(generateSecureUUID as jest.Mock).mockReturnValue('mocked-uuid');
});

afterEach(() => {
// Clear mocks after each test
jest.clearAllMocks();
});

it('should add a new toast when not exceeding maxCount', () => {
// Test adding a toast when maxCount is not reached
mockRcbEventInternal.callRcbEvent.mockReturnValue({ defaultPrevented: false,
data: { toast: { id: 'mocked-uuid', content: 'New toast content', timeout: undefined }
} });
const { result } = renderHook(() => useToastInternal());
act(() => {
result.current.showToast('New toast content');
});
expect(generateSecureUUID).toHaveBeenCalled();
expect(mockToastsContext.setToasts).toHaveBeenCalledWith(expect.any(Function));

// Verify that the toast was added
const setToastsFn = mockToastsContext.setToasts.mock.calls[0][0];
const newToasts = setToastsFn([]);
expect(newToasts).toEqual([{ id: 'mocked-uuid', content: 'New toast content', timeout: undefined }]);
});

it('should not add a new toast if maxCount is reached and forbidOnMax is true', () => {
// Test forbidding new toast if maxCount is reached
mockToastsContext.toasts = [{ id: '1' }, { id: '2' }, { id: '3' }];
const { result } = renderHook(() => useToastInternal());
const toastId = result.current.showToast('Toast content');
expect(toastId).toBeNull();
expect(mockToastsContext.setToasts).not.toHaveBeenCalled();
});

it('should remove the oldest toast and add a new one if maxCount is reached but forbidOnMax is false', () => {
// Test replacing oldest toast if maxCount reached and forbidOnMax is false
mockSettingsContext.settings.toast.forbidOnMax = false;
mockToastsContext.toasts = [{ id: '1' }, { id: '2' }, { id: '3' }];
mockRcbEventInternal.callRcbEvent.mockReturnValue({
defaultPrevented: false, data: { toast: { id: 'mocked-uuid',
content: 'New toast content', timeout: undefined }
} });
const { result } = renderHook(() => useToastInternal());
act(() => {
result.current.showToast('New toast content');
});
expect(mockToastsContext.setToasts).toHaveBeenCalledWith(expect.any(Function));

// Verify the oldest toast was removed and new one added
const setToastsFn = mockToastsContext.setToasts.mock.calls[0][0];
const newToasts = setToastsFn([{ id: '1' }, { id: '2' }, { id: '3' }]);
expect(newToasts).toEqual([
{ id: '2' },
{ id: '3' },
{ id: 'mocked-uuid', content: 'New toast content', timeout: undefined },
]);
});

it('should dismiss a toast by id', () => {
// Test dismissing a toast by ID
const toast = { id: 'toast-1', content: 'Toast to dismiss' };
mockToastsContext.toasts = [toast];
mockRcbEventInternal.callRcbEvent.mockReturnValue({ defaultPrevented: false });
const { result } = renderHook(() => useToastInternal());
act(() => {
result.current.dismissToast('toast-1');
});
expect(mockToastsContext.setToasts).toHaveBeenCalledWith(expect.any(Function));

// Verify toast was dismissed
const setToastsFn = mockToastsContext.setToasts.mock.calls[0][0];
const updatedToasts = setToastsFn([toast]);
expect(updatedToasts).toEqual([]);
});

it('should not dismiss a toast if the id is not found', () => {
// Test no dismissal if ID not found
mockToastsContext.toasts = [{ id: 'toast-2', content: 'Another toast' }];
const { result } = renderHook(() => useToastInternal());
const resultId = result.current.dismissToast('invalid-id');
expect(resultId).toBeNull();
expect(mockToastsContext.setToasts).not.toHaveBeenCalled();
});

it('should not show toast if rcbShowToast event is prevented', () => {
// Test prevention of toast display by event
mockRcbEventInternal.callRcbEvent.mockReturnValue({ defaultPrevented: true });
const { result } = renderHook(() => useToastInternal());
const resultId = result.current.showToast('Prevented toast');
expect(resultId).toBeNull();
expect(mockToastsContext.setToasts).not.toHaveBeenCalled();
});

it('should call rcbDismissToast event when dismissing a toast', () => {
// Test triggering of dismiss event upon toast removal
const toast = { id: 'toast-1', content: 'Toast to dismiss' };
mockToastsContext.toasts = [toast];
mockRcbEventInternal.callRcbEvent.mockReturnValue({ defaultPrevented: false });
const { result } = renderHook(() => useToastInternal());
act(() => {
result.current.dismissToast('toast-1');
});
expect(mockRcbEventInternal.callRcbEvent).toHaveBeenCalledWith(RcbEvent.DISMISS_TOAST, { toast });
});
});

0 comments on commit 7f54546

Please sign in to comment.