diff --git a/lib/claiming/components/modals/DelayClaimsModal/DelayClaimsModal.test.js b/lib/claiming/components/modals/DelayClaimsModal/DelayClaimsModal.test.js new file mode 100644 index 00000000..cd9505b2 --- /dev/null +++ b/lib/claiming/components/modals/DelayClaimsModal/DelayClaimsModal.test.js @@ -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( + , + { 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(); + }); +}); diff --git a/lib/claiming/hooks/useClaimsDelay/useClaimsDelay.test.js b/lib/claiming/hooks/useClaimsDelay/useClaimsDelay.test.js new file mode 100644 index 00000000..450d8d4c --- /dev/null +++ b/lib/claiming/hooks/useClaimsDelay/useClaimsDelay.test.js @@ -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, + }, + }); + }); +});