Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UIREC-294 Check if a holding exists during the abandonment check #458

Merged
merged 5 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* *BREAKING* Upgrade React to v18. Refs UIREC-280.
* *BREAKING* bump `react-intl` to `v6.4.4`. Refs UIREC-286.
* Bump optional plugins to their `@folio/stripes` `v9` compatible versions. Refs UIREC-290.
* Check if a holding exists during the abandonment check. Refs UIREC-294.

## [3.0.0](https://github.com/folio-org/ui-receiving/tree/v3.0.0) (2023-02-22)
[Full Changelog](https://github.com/folio-org/ui-receiving/compare/v2.3.1...v3.0.0)
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,9 @@
"react-router-prop-types": "^1.0.4"
},
"optionalDependencies": {
"@folio/plugin-find-instance": "^7.0.0",
"@folio/plugin-find-organization": "^5.0.0",
"@folio/plugin-find-po-line": "^5.0.0"
"@folio/plugin-find-instance": "^6.3.0",
"@folio/plugin-find-organization": "^4.0.0",
"@folio/plugin-find-po-line": "^4.0.0"
usavkov-epam marked this conversation as resolved.
Show resolved Hide resolved
},
"peerDependencies": {
"@folio/stripes": "^9.0.0",
Expand Down
50 changes: 31 additions & 19 deletions src/TitleDetails/AddPieceModal/AddPieceModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
TextField,
checkScope,
} from '@folio/stripes/components';
import { useOkapiKy } from '@folio/stripes/core';
import stripesFinalForm from '@folio/stripes/final-form';
import {
usavkov-epam marked this conversation as resolved.
Show resolved Hide resolved
FieldDatepickerFinal,
Expand All @@ -33,6 +34,7 @@
CreateItemField,
LineLocationsView,
} from '../../common/components';
import { HOLDINGS_API } from '../../common/constants';
import { DeletePieceModal } from '../DeletePieceModal';
import { DeleteHoldingsModal } from '../DeleteHoldingsModal';

Expand Down Expand Up @@ -62,10 +64,11 @@
const [isDeleteConfirmation, toggleDeleteConfirmation] = useModalToggle();
const [isDeleteHoldingsConfirmation, toggleDeleteHoldingsConfirmation] = useModalToggle();

const ky = useOkapiKy();
const intl = useIntl();
const modalLabel = intl.formatMessage({ id: labelId });

const initialHoldingId = useMemo(() => getState().initialValues?.holdingId, []);

Check warning on line 71 in src/TitleDetails/AddPieceModal/AddPieceModal.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

React Hook useMemo has a missing dependency: 'getState'. Either include it or remove the dependency array. If 'getState' changes too often, find the parent component that defines it and wrap that definition in useCallback

Check warning on line 71 in src/TitleDetails/AddPieceModal/AddPieceModal.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

React Hook useMemo has a missing dependency: 'getState'. Either include it or remove the dependency array. If 'getState' changes too often, find the parent component that defines it and wrap that definition in useCallback

const disabled = (initialValues.isCreateAnother && pristine) || hasValidationErrors;

Expand All @@ -88,33 +91,42 @@
if (!checked) change('discoverySuppress', checked);
};

const onSave = useCallback((e) => {
const checkHoldingAbandonment = useCallback((holdingId) => {
return ky.get(`${HOLDINGS_API}/${holdingId}`)
.json()
.then((holding) => getHoldingsItemsAndPieces(holding.id, { limit: 1 }))
.then(({ pieces, items }) => {
const willAbandoned = Boolean(
pieces && items
&& (pieces.totalRecords === 1)
&& ((items.totalRecords === 1 && itemId) || items.totalRecords === 0),
);

return { willAbandoned };
})
.catch(() => ({ willAbandoned: false }));
}, [getHoldingsItemsAndPieces, itemId, ky]);

const onSave = useCallback(async (e) => {
const holdingId = getState().values?.holdingId;

if ((id && initialHoldingId) && (holdingId !== initialHoldingId)) {
return getHoldingsItemsAndPieces(initialHoldingId, { limit: 1 })
.then(({ pieces, items }) => {
const canDeleteHolding = Boolean(
pieces && items
&& (pieces.totalRecords === 1)
&& ((items.totalRecords === 1 && itemId) || items.totalRecords === 0),
);

if (canDeleteHolding) {
return toggleDeleteHoldingsConfirmation();
}

return handleSubmit(e);
});
const shouldCheckHoldingAbandonment = (id && initialHoldingId) && (holdingId !== initialHoldingId);

if (shouldCheckHoldingAbandonment) {
return checkHoldingAbandonment(initialHoldingId)
.then(({ willAbandoned }) => (
willAbandoned
? toggleDeleteHoldingsConfirmation()
: handleSubmit(e)
));
}

return handleSubmit(e);
}, []);
}, [checkHoldingAbandonment, getState, handleSubmit, id, initialHoldingId, toggleDeleteHoldingsConfirmation]);

const onDeleteHoldings = useCallback(() => {
change('deleteHolding', true);
handleSubmit();
}, []);
}, [change, handleSubmit]);

const start = (
<Button
Expand Down
95 changes: 75 additions & 20 deletions src/TitleDetails/AddPieceModal/AddPieceModal.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import React from 'react';
import { MemoryRouter } from 'react-router-dom';
import { render, screen } from '@folio/jest-config-stripes/testing-library/react';

import user from '@folio/jest-config-stripes/testing-library/user-event';
import { act, render, screen } from '@folio/jest-config-stripes/testing-library/react';

import { useOkapiKy } from '@folio/stripes/core';
usavkov-epam marked this conversation as resolved.
Show resolved Hide resolved
import {
FieldInventory,
INVENTORY_RECORDS_TYPE,
PIECE_FORMAT,
} from '@folio/stripes-acq-components';

import AddPieceModal from './AddPieceModal';
Expand Down Expand Up @@ -41,9 +44,17 @@ const defaultProps = {
values: {},
poLine: { locations: [{ locationId: '001' }] },
setSearchParams: jest.fn(),
getHoldingsItemsAndPieces: jest.fn().mockReturnValue({
then: () => ({}),
}),
getHoldingsItemsAndPieces: jest.fn(),
};

const holding = {
id: 'holdingId',
};

const kyMock = {
get: jest.fn(() => ({
json: () => Promise.resolve(holding),
})),
};

const renderAddPieceModal = (props = defaultProps) => (render(
Expand All @@ -53,11 +64,21 @@ const renderAddPieceModal = (props = defaultProps) => (render(
{ wrapper: MemoryRouter },
));

const findButton = (name) => screen.findByRole('button', { name });

const createNewHoldingForThePiece = (newHoldingId = 'newHoldingUUID') => {
return act(async () => FieldInventory.mock.calls[0][0].onChange(null, 'locationId', 'holdingId', newHoldingId));
};

describe('AddPieceModal', () => {
beforeEach(() => {
defaultProps.close.mockClear();
defaultProps.onCheckIn.mockClear();
defaultProps.onSubmit.mockClear();
defaultProps.getHoldingsItemsAndPieces.mockClear();
FieldInventory.mockClear();
kyMock.get.mockClear();
useOkapiKy.mockClear().mockReturnValue(kyMock);
});

it('should display Add piece modal', () => {
Expand All @@ -70,15 +91,15 @@ describe('AddPieceModal', () => {
it('should close Add piece modal', async () => {
renderAddPieceModal();

await user.click(screen.getByText('ui-receiving.piece.actions.cancel'));
await user.click(await findButton('ui-receiving.piece.actions.cancel'));

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

describe('Check display on holding', () => {
it.skip('should enable discovery suppress when clicked', async () => {
const format = 'Electronic';
const format = PIECE_FORMAT.electronic;

renderAddPieceModal({
...defaultProps,
Expand Down Expand Up @@ -107,24 +128,63 @@ describe('AddPieceModal', () => {

describe('Save piece', () => {
it('should call \'onSubmit\' when save button was clicked', async () => {
const format = 'Electronic';
const format = PIECE_FORMAT.electronic;

renderAddPieceModal({
...defaultProps,
createInventoryValues: { [format]: INVENTORY_RECORDS_TYPE.instanceAndHolding },
initialValues: {
format,
id: 'pieceId',
holdingId: 'holdingId',
holdingId: holding.id,
},
});

const saveAndCloseBtn = await screen.findByRole('button', {
name: 'ui-receiving.piece.actions.saveAndClose',
await user.click(await findButton('ui-receiving.piece.actions.saveAndClose'));
expect(defaultProps.onSubmit).toHaveBeenCalled();
});

describe('Abandoned holdings', () => {
it('should display the modal for deleting abandoned holding when the original holding is empty after changing to a new one', async () => {
defaultProps.getHoldingsItemsAndPieces.mockResolvedValue({
pieces: { totalRecords: 1 },
items: { totalRecords: 0 },
});

renderAddPieceModal({
...defaultProps,
initialValues: {
id: 'pieceId',
format: PIECE_FORMAT.physical,
holdingId: holding.id,
},
});

await createNewHoldingForThePiece();
await user.click(await findButton('ui-receiving.piece.actions.saveAndClose'));

expect(await screen.findByText('ui-receiving.piece.actions.edit.deleteHoldings.message')).toBeInTheDocument();
expect(defaultProps.onSubmit).not.toHaveBeenCalled();
});

await user.click(saveAndCloseBtn);
expect(defaultProps.onSubmit).toHaveBeenCalled();
it('should NOT display the modal for deleting abandoned holding if it has already been deleted', async () => {
kyMock.get.mockReturnValue(({ json: () => Promise.reject(new Error('404')) }));

renderAddPieceModal({
...defaultProps,
initialValues: {
id: 'pieceId',
format: PIECE_FORMAT.physical,
holdingId: holding.id,
},
});

await createNewHoldingForThePiece();
await user.click(await findButton('ui-receiving.piece.actions.saveAndClose'));

expect(screen.queryByText('ui-receiving.piece.actions.edit.deleteHoldings.message')).not.toBeInTheDocument();
expect(defaultProps.onSubmit).toHaveBeenCalled();
});
});
});

Expand All @@ -135,13 +195,8 @@ describe('AddPieceModal', () => {
initialValues: { isCreateAnother: true },
});

const saveBtn = await screen.findByRole('button', {
name: 'stripes-core.button.save',
});

const quickReceiveBtn = await screen.findByRole('button', {
name: 'ui-receiving.piece.actions.quickReceive',
});
const saveBtn = await findButton('ui-receiving.piece.actions.quickReceive');
const quickReceiveBtn = await findButton('ui-receiving.piece.actions.quickReceive');

expect(saveBtn).toBeInTheDocument();
expect(quickReceiveBtn.classList.contains('primary')).toBeTruthy();
Expand Down
Loading