Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
usavkov-epam committed Dec 18, 2024
1 parent 7c23fdb commit 57e7673
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* Developed collaboratively using AI (GitHub Copilot) */

import {
render,
screen,
} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { MemoryRouter } from 'react-router-dom';

import { dayjs } from '@folio/stripes/components';

import DelayClaimsModal from './DelayClaimsModal';

const FORMAT = 'MM/DD/YYYY';
const today = dayjs();

const defaultProps = {
claimsCount: 1,
onSubmit: jest.fn(() => console.log('')),
message: 'Test message',
onCancel: jest.fn(),
open: true,
};

const renderComponent = (props = {}) => render(
<DelayClaimsModal
{...defaultProps}
{...props}
/>,
{ wrapper: MemoryRouter },
);

describe('DelayClaimsModal', () => {
it('should call onCancel when cancel button is clicked', async () => {
renderComponent();

await userEvent.click(screen.getByText('stripes-acq-components.FormFooter.cancel'));

expect(defaultProps.onCancel).toHaveBeenCalled();
});

it('should validate "Delay to" field and call onSubmit when save button is clicked', async () => {
renderComponent();

const saveBtn = screen.getByRole('button', { name: 'stripes-acq-components.FormFooter.save' });

/* Empty input */
await userEvent.click(saveBtn);

expect(screen.getByText('stripes-acq-components.validation.required')).toBeInTheDocument();

/* Invalid date input */
await userEvent.type(screen.getByPlaceholderText(FORMAT), today.format(FORMAT));
await userEvent.click(saveBtn);

expect(screen.getByText('stripes-acq-components.validation.dateAfter')).toBeInTheDocument();

/* Valid date */
await userEvent.clear(screen.getByPlaceholderText(FORMAT));
await userEvent.type(screen.getByPlaceholderText(FORMAT), dayjs().add(5, 'days').format(FORMAT));
await userEvent.click(saveBtn);

expect(defaultProps.onSubmit).toHaveBeenCalled();
});
});
40 changes: 40 additions & 0 deletions lib/claiming/hooks/useClaimsDelay/useClaimsDelay.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { renderHook } from '@testing-library/react-hooks';

import { PIECE_STATUS } from '../../../constants';
import { usePiecesStatusBatchUpdate } from '../../../hooks';
import { useClaimsDelay } from './useClaimsDelay';

jest.mock('../../../hooks', () => ({
usePiecesStatusBatchUpdate: jest.fn(),
}));

const mockUpdatePiecesStatus = jest.fn(() => Promise.resolve());

describe('useClaimsDelay', () => {
beforeEach(() => {
usePiecesStatusBatchUpdate.mockReturnValue({
isLoading: false,
updatePiecesStatus: mockUpdatePiecesStatus,
});
});

afterEach(() => {
jest.clearAllMocks();
});

it('should call updatePiecesStatus with correct parameters when delayClaims is called', async () => {
const { result } = renderHook(() => useClaimsDelay());
const claimingInterval = 30;
const pieceIds = ['piece1', 'piece2'];

await result.current.delayClaims({ claimingInterval, pieceIds });

expect(mockUpdatePiecesStatus).toHaveBeenCalledWith({
data: {
claimingInterval,
pieceIds,
receivingStatus: PIECE_STATUS.claimDelayed,
},
});
});
});

0 comments on commit 57e7673

Please sign in to comment.