From 69815e61d9e697fb56d3809b8b9cd41aaac2e5f5 Mon Sep 17 00:00:00 2001 From: vashjs Date: Tue, 30 Jan 2024 13:10:00 +0100 Subject: [PATCH] update tests --- src/hooks/useBulkOperationStats.test.js | 79 +++++++++++++++++++++++++ src/hooks/usePagination.test.js | 40 +++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 src/hooks/useBulkOperationStats.test.js create mode 100644 src/hooks/usePagination.test.js diff --git a/src/hooks/useBulkOperationStats.test.js b/src/hooks/useBulkOperationStats.test.js new file mode 100644 index 00000000..4190ab45 --- /dev/null +++ b/src/hooks/useBulkOperationStats.test.js @@ -0,0 +1,79 @@ +import { renderHook } from '@testing-library/react-hooks'; +import { EDITING_STEPS } from '../constants'; +import { useBulkOperationStats } from './useBulkOperationStats'; + +jest.mock('react', () => { + const ActualReact = jest.requireActual('react'); + return { + ...ActualReact, + useContext: () => ({ + countOfRecords: 0, + setCountOfRecords: jest.fn(), + visibleColumns: [], + }), + }; +}); + +describe('useBulkOperationStats', () => { + afterEach(() => { + jest.clearAllMocks(); + }); + + test('initial state is set correctly', () => { + const { result } = renderHook(() => useBulkOperationStats({ bulkDetails: { matchedNumOfRecords: 5, totalNumOfRecords: 10 }, step: EDITING_STEPS.UPLOAD })); + + expect(result.current.countOfRecords).toBe(0); + expect(result.current.countOfErrors).toBe(undefined); + expect(result.current.totalCount).toBe(10); + expect(result.current.visibleColumns).toEqual([]); + }); + + test('state is updated correctly for UPLOAD step', () => { + const { result, rerender } = renderHook( + ({ bulkDetails, step }) => useBulkOperationStats({ bulkDetails, step }), + { + initialProps: { + bulkDetails: { matchedNumOfRecords: 5, matchedNumOfErrors: 2, totalNumOfRecords: 10 }, + step: EDITING_STEPS.UPLOAD, + }, + } + ); + + expect(result.current.countOfRecords).toBe(0); + expect(result.current.countOfErrors).toBe(2); + expect(result.current.totalCount).toBe(10); + + rerender({ + bulkDetails: { matchedNumOfRecords: 8, matchedNumOfErrors: 3, totalNumOfRecords: 15 }, + step: EDITING_STEPS.UPLOAD, + }); + + expect(result.current.countOfRecords).toBe(0); + expect(result.current.countOfErrors).toBe(3); + expect(result.current.totalCount).toBe(15); + }); + + test('state is updated correctly for COMMIT step', () => { + const { result, rerender } = renderHook( + ({ bulkDetails, step }) => useBulkOperationStats({ bulkDetails, step }), + { + initialProps: { + bulkDetails: { matchedNumOfRecords: 5, matchedNumOfErrors: 2, totalNumOfRecords: 10 }, + step: EDITING_STEPS.COMMIT, + }, + } + ); + + expect(result.current.countOfRecords).toBe(0); + expect(result.current.countOfErrors).toBe(undefined); + expect(result.current.totalCount).toBe(5); + + rerender({ + bulkDetails: { committedNumOfRecords: 8, committedNumOfErrors: 3, totalNumOfRecords: 15 }, + step: EDITING_STEPS.COMMIT, + }); + + expect(result.current.countOfRecords).toBe(0); + expect(result.current.countOfErrors).toBe(3); + }); +}); diff --git a/src/hooks/usePagination.test.js b/src/hooks/usePagination.test.js new file mode 100644 index 00000000..1f01676e --- /dev/null +++ b/src/hooks/usePagination.test.js @@ -0,0 +1,40 @@ +import { renderHook, act } from '@testing-library/react-hooks'; +import { usePagination } from './usePagination'; + +describe('usePagination', () => { + test('initial state is set correctly', () => { + const { result } = renderHook(() => usePagination({ limit: 10, offset: 0 })); + + expect(result.current.pagination).toEqual({ limit: 10, offset: 0 }); + }); + + test('changePage updates pagination correctly', () => { + const { result } = renderHook(() => usePagination({ limit: 10, offset: 0 })); + + act(() => { + result.current.changePage({ offset: 10 }); + }); + + expect(result.current.pagination).toEqual({ limit: 10, offset: 10 }); + }); + + test('changePage does not modify other properties', () => { + const { result } = renderHook(() => usePagination({ limit: 10, offset: 0 })); + + act(() => { + result.current.changePage({ offset: 10 }); + }); + + expect(result.current.pagination.limit).toBe(10); + }); + + test('changePage works with multiple properties', () => { + const { result } = renderHook(() => usePagination({ limit: 10, offset: 0 })); + + act(() => { + result.current.changePage({ offset: 10, limit: 20 }); + }); + + expect(result.current.pagination).toEqual({ limit: 20, offset: 10 }); + }); +});