diff --git a/src/Piece/PieceForm/PieceForm.test.js b/src/Piece/PieceForm/PieceForm.test.js index f766231c..7485cd5a 100644 --- a/src/Piece/PieceForm/PieceForm.test.js +++ b/src/Piece/PieceForm/PieceForm.test.js @@ -1,11 +1,10 @@ -import moment from 'moment'; - import user from '@folio/jest-config-stripes/testing-library/user-event'; import { act, screen, waitFor, } from '@folio/jest-config-stripes/testing-library/react'; +import { dayjs } from '@folio/stripes/components'; import { FieldInventory, INVENTORY_RECORDS_TYPE, @@ -90,7 +89,7 @@ const logs = [ ]; const DATE_FORMAT = 'MM/DD/YYYY'; -const today = moment(); +const today = dayjs(); const renderPieceForm = (props = {}) => renderWithRouter( { await user.click(dropdownButton); - const unReceiveButton = screen.getByTestId('unReceivable-piece-button'); + const unreceivableButton = screen.getByTestId('unreceivable-button'); - await user.click(unReceiveButton); + await user.click(unreceivableButton); expect(defaultProps.onSubmit).toHaveBeenCalled(); }); @@ -392,7 +391,7 @@ describe('PieceForm', () => { it('should handle "Delay claim" action', async () => { await user.click(screen.getByTestId('delay-claim-button')); - await user.type(screen.getByRole('textbox', { name: 'ui-receiving.modal.delayClaim.field.delayTo' }), date.format(DATE_FORMAT)); + await user.type(screen.getByRole('textbox', { name: /field.delayTo/ }), date.format(DATE_FORMAT)); await user.click(await findButton('stripes-acq-components.FormFooter.save')); expect(defaultProps.onSubmit).toHaveBeenCalledWith( @@ -404,20 +403,5 @@ describe('PieceForm', () => { expect.anything(), ); }); - - it('should handle "Send claim" action', async () => { - await user.click(screen.getByTestId('send-claim-button')); - await user.type(screen.getByRole('textbox', { name: 'ui-receiving.modal.sendClaim.field.claimExpiryDate' }), date.format(DATE_FORMAT)); - await user.click(await findButton('stripes-acq-components.FormFooter.save')); - - expect(defaultProps.onSubmit).toHaveBeenCalledWith( - expect.objectContaining({ - claimingInterval: 3, - receivingStatus: PIECE_STATUS.claimSent, - }), - expect.anything(), - expect.anything(), - ); - }); }); }); diff --git a/src/Piece/PieceForm/PieceFormActionButtons/constants.js b/src/Piece/PieceForm/PieceFormActionButtons/constants.js index 8320d32c..24e56532 100644 --- a/src/Piece/PieceForm/PieceFormActionButtons/constants.js +++ b/src/Piece/PieceForm/PieceFormActionButtons/constants.js @@ -148,6 +148,7 @@ export const PIECE_ACTIONS = ({ ), [PIECE_ACTION_NAMES.unReceivable]: ( { onToggle(e); diff --git a/src/Piece/PieceForm/PieceFormActionButtons/utils.test.js b/src/Piece/PieceForm/PieceFormActionButtons/utils.test.js index ad6b780b..ba09d010 100644 --- a/src/Piece/PieceForm/PieceFormActionButtons/utils.test.js +++ b/src/Piece/PieceForm/PieceFormActionButtons/utils.test.js @@ -81,9 +81,9 @@ describe('getPieceActionMenus', () => { onToggle, onUnreceivePiece, }); - const receiveButton = result.find(i => i.props['data-testid'] === 'unReceive-piece-button'); + const unreceiveButton = result.find(i => i.props['data-testid'] === 'unReceive-piece-button'); - receiveButton.props.onClick(); + unreceiveButton.props.onClick(); expect(onUnreceivePiece).toHaveBeenCalledWith(); }); @@ -97,9 +97,9 @@ describe('getPieceActionMenus', () => { onStatusChange, onToggle, }); - const receiveButton = result.find(i => i.props['data-testid'] === 'unReceivable-piece-button'); + const unreceivableButton = result.find(i => i.props['data-testid'] === 'unreceivable-button'); - receiveButton.props.onClick(); + unreceivableButton.props.onClick(); expect(onStatusChange).toHaveBeenCalledWith(PIECE_STATUS.unreceivable); }); diff --git a/src/Piece/PieceForm/PieceFormContainer.test.js b/src/Piece/PieceForm/PieceFormContainer.test.js index 03ee1934..6ce05c68 100644 --- a/src/Piece/PieceForm/PieceFormContainer.test.js +++ b/src/Piece/PieceForm/PieceFormContainer.test.js @@ -1,14 +1,18 @@ import user from '@folio/jest-config-stripes/testing-library/user-event'; import { screen } from '@folio/jest-config-stripes/testing-library/react'; +import { dayjs } from '@folio/stripes/components'; import { ORDER_FORMATS, ORDER_STATUSES, PIECE_STATUS, useAcqRestrictions, + useClaimsSend, + useCurrentUserTenants, useLocationsQuery, useOrderLine, - useCurrentUserTenants, + usePiecesStatusBatchUpdate, } from '@folio/stripes-acq-components'; +import { useOkapiKy } from '@folio/stripes/core'; import { renderWithRouter } from '../../../test/jest/helpers'; import { @@ -26,11 +30,13 @@ import { PieceFormContainer } from './PieceFormContainer'; jest.mock('@folio/stripes-acq-components', () => ({ ...jest.requireActual('@folio/stripes-acq-components'), FieldInventory: jest.fn().mockReturnValue('FieldInventory'), - useCentralOrderingContext: jest.fn(), useAcqRestrictions: jest.fn(), + useCentralOrderingContext: jest.fn(), + useClaimsSend: jest.fn(), + useCurrentUserTenants: jest.fn(), useLocationsQuery: jest.fn(), useOrderLine: jest.fn(), - useCurrentUserTenants: jest.fn(), + usePiecesStatusBatchUpdate: jest.fn(), })); jest.mock('../../common/components/LineLocationsView/LineLocationsView', () => jest.fn().mockReturnValue('LineLocationsView')); jest.mock('../../common/hooks', () => ({ @@ -58,7 +64,10 @@ jest.mock('../hooks', () => ({ usePieceStatusChangeLog: jest.fn(() => ({ data: [] })), })); -const mutatePieceMock = jest.fn(() => Promise.resolve()); +const DATE_FORMAT = 'MM/DD/YYYY'; +const today = dayjs(); + +const mutatePieceMock = jest.fn(() => Promise.resolve({ id: 'piece-id' })); const unreceiveMock = jest.fn(() => Promise.resolve()); const onQuickReceiveMock = jest.fn(() => Promise.resolve()); const receiveMock = jest.fn(() => Promise.resolve()); @@ -117,48 +126,37 @@ const renderPieceFormContainer = (props = {}) => renderWithRouter( ); describe('PieceFormContainer', () => { + const sendClaims = jest.fn(() => Promise.resolve()); + const updatePiecesStatus = jest.fn(() => Promise.resolve()); + + const kyMock = { + get: jest.fn(() => ({ json: () => Promise.resolve({ configs: [] }) })), + }; + beforeEach(() => { - mutatePieceMock.mockClear(); - useAcqRestrictions - .mockClear() - .mockReturnValue({ restrictions }); - useHoldingItems - .mockClear() - .mockReturnValue({ itemsCount: 2 }); - useOrder - .mockClear() - .mockReturnValue({ order }); - useOrderLine - .mockClear() - .mockReturnValue({ orderLine }); - useLocationsQuery - .mockClear() - .mockReturnValue({ locations }); - usePieceMutator - .mockClear() - .mockReturnValue({ mutatePiece: mutatePieceMock }); - usePieces - .mockClear() - .mockReturnValue({ piecesCount: 2 }); - usePieceQuickReceiving - .mockClear() - .mockReturnValue({ - onCancelReceive: jest.fn(), - onConfirmReceive: jest.fn(), - onQuickReceive: onQuickReceiveMock, - }); - useTitle - .mockClear() - .mockReturnValue({ title }); - useReceive - .mockClear() - .mockReturnValue({ receive: receiveMock }); - useUnreceive - .mockClear() - .mockReturnValue({ unreceive: unreceiveMock }); - useCurrentUserTenants - .mockClear() - .mockReturnValue(tenants); + useAcqRestrictions.mockReturnValue({ restrictions }); + useClaimsSend.mockReturnValue({ sendClaims }); + useCurrentUserTenants.mockReturnValue(tenants); + useHoldingItems.mockReturnValue({ itemsCount: 2 }); + useOkapiKy.mockReturnValue(kyMock); + useOrder.mockReturnValue({ order }); + useOrderLine.mockReturnValue({ orderLine }); + useLocationsQuery.mockReturnValue({ locations }); + usePieceMutator.mockReturnValue({ mutatePiece: mutatePieceMock }); + usePieces.mockReturnValue({ piecesCount: 2 }); + usePiecesStatusBatchUpdate.mockReturnValue({ updatePiecesStatus }); + usePieceQuickReceiving.mockReturnValue({ + onCancelReceive: jest.fn(), + onConfirmReceive: jest.fn(), + onQuickReceive: onQuickReceiveMock, + }); + useTitle.mockReturnValue({ title }); + useReceive.mockReturnValue({ receive: receiveMock }); + useUnreceive.mockReturnValue({ unreceive: unreceiveMock }); + }); + + afterEach(() => { + jest.clearAllMocks(); }); it('should display the piece form', () => { @@ -235,4 +233,47 @@ describe('PieceFormContainer', () => { }), })); }); + + it('should handle "Send claim" action with claiming integration', async () => { + kyMock.get.mockReturnValueOnce({ json: () => Promise.resolve({ configs: [{ value: 'val' }] }) }); + + renderPieceFormContainer(); + + await user.click(await screen.findByTestId('dropdown-trigger-button')); + await user.click(screen.getByTestId('send-claim-button')); + + expect(screen.getByText('ui-receiving.piece.sendClaim.withIntegration.message')).toBeInTheDocument(); + + await user.type(screen.getByRole('textbox', { name: /sendClaim.field.claimExpiryDate/ }), today.add(3, 'days').format(DATE_FORMAT)); + await user.click(await screen.findByRole('button', { name: 'stripes-acq-components.FormFooter.save' })); + + expect(sendClaims).toHaveBeenCalledWith({ + data: { + claimingInterval: 3, + externalNote: undefined, + internalNote: undefined, + claimingPieceIds: ['piece-id'], + }, + }); + }); + + it('should handle "Send claim" action without claiming integration', async () => { + renderPieceFormContainer(); + + await user.click(await screen.findByTestId('dropdown-trigger-button')); + await user.click(screen.getByTestId('send-claim-button')); + + expect(screen.getByText('ui-receiving.piece.sendClaim.withoutIntegration.message')).toBeInTheDocument(); + + await user.type(screen.getByRole('textbox', { name: /sendClaim.field.claimExpiryDate/ }), today.add(3, 'days').format(DATE_FORMAT)); + await user.click(await screen.findByRole('button', { name: 'stripes-acq-components.FormFooter.save' })); + + expect(updatePiecesStatus).toHaveBeenCalledWith({ + data: { + claimingInterval: 3, + pieceIds: ['piece-id'], + receivingStatus: PIECE_STATUS.claimSent, + }, + }); + }); }); diff --git a/src/Piece/hooks/usePieceClaimSend/usePieceClaimSend.js b/src/Piece/hooks/usePieceClaimSend/usePieceClaimSend.js index 8c7a4c85..7b8e7aad 100644 --- a/src/Piece/hooks/usePieceClaimSend/usePieceClaimSend.js +++ b/src/Piece/hooks/usePieceClaimSend/usePieceClaimSend.js @@ -23,7 +23,7 @@ export const usePieceClaimSend = (options = {}) => { const ky = useOkapiKy({ tenant: tenantId }); const confirmClaimSendPromise = useRef(Promise); const [isClaimSendModalOpen, toggleClaimSendModal] = useModalToggle(); - const [isIntegrationExists, setIntegrationExists] = useState(); + const [isIntegrationExists, setIsIntegrationExists] = useState(); const [claimSendModalProps, setClaimSendModalProps] = useState({}); /* Callback to handle claim send after form submit */ @@ -47,6 +47,7 @@ export const usePieceClaimSend = (options = {}) => { }; return ky.get(DATA_EXPORT_CONFIGS_API, { searchParams }) + .json() .then(({ configs }) => configs.length > 0) .catch(() => false); }, [ky, organizationId]); @@ -60,7 +61,7 @@ export const usePieceClaimSend = (options = {}) => { /> ); - setIntegrationExists(isExists); + setIsIntegrationExists(isExists); toggleClaimSendModal(); return new Promise((resolve, reject) => {