Skip to content

Commit

Permalink
update jest tests
Browse files Browse the repository at this point in the history
  • Loading branch information
usavkov-epam committed Dec 24, 2024
1 parent d06d6cf commit 7841abd
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 72 deletions.
26 changes: 5 additions & 21 deletions src/Piece/PieceForm/PieceForm.test.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -90,7 +89,7 @@ const logs = [
];

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

const renderPieceForm = (props = {}) => renderWithRouter(
<PieceForm
Expand Down Expand Up @@ -345,9 +344,9 @@ describe('PieceForm', () => {

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();
});
Expand Down Expand Up @@ -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(
Expand All @@ -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(),
);
});
});
});
1 change: 1 addition & 0 deletions src/Piece/PieceForm/PieceFormActionButtons/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export const PIECE_ACTIONS = ({
),
[PIECE_ACTION_NAMES.unReceivable]: (
<MarkUnreceivableActionMenuItem
data-testid="unreceivable-button"
disabled={actionsDisabled[PIECE_ACTION_NAMES.unReceivable]}
onClick={(e) => {
onToggle(e);
Expand Down
8 changes: 4 additions & 4 deletions src/Piece/PieceForm/PieceFormActionButtons/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand All @@ -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);
});
Expand Down
131 changes: 86 additions & 45 deletions src/Piece/PieceForm/PieceFormContainer.test.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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', () => ({
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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,
},
});
});
});
5 changes: 3 additions & 2 deletions src/Piece/hooks/usePieceClaimSend/usePieceClaimSend.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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]);
Expand All @@ -60,7 +61,7 @@ export const usePieceClaimSend = (options = {}) => {
/>
);

setIntegrationExists(isExists);
setIsIntegrationExists(isExists);
toggleClaimSendModal();

return new Promise((resolve, reject) => {
Expand Down

0 comments on commit 7841abd

Please sign in to comment.