Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vashjs committed Jan 30, 2024
1 parent 2d22a61 commit 69815e6
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/hooks/useBulkOperationStats.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
40 changes: 40 additions & 0 deletions src/hooks/usePagination.test.js
Original file line number Diff line number Diff line change
@@ -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 });
});
});

0 comments on commit 69815e6

Please sign in to comment.