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

UISACQCOMP-220: ECS - expand ConsortiumFieldInventory component with additionalAffiliationIds prop to display affiliation name for User without affiliation in specific tenant #818

Merged
merged 2 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -24,6 +24,7 @@
* ECS - Add `isLoading` prop for `ConsortiumFieldInventory` component. Refs UISACQCOMP-215.
* Add "Amount must be a positive number" validation for "Set exchange rate" field. Refs UISACQCOMP-218.
* Create common utilities for managing response errors. Refs UISACQCOMP-219.
* ECS - expand `ConsortiumFieldInventory` component with `additionalAffiliationIds` prop to display affiliation name for User without affiliation in specific tenant. Refs UISACQCOMP-220.

## [5.1.2](https://github.com/folio-org/stripes-acq-components/tree/v5.1.2) (2024-09-13)
[Full Changelog](https://github.com/folio-org/stripes-acq-components/compare/v5.1.1...v5.1.2)
Expand Down
20 changes: 18 additions & 2 deletions lib/ConsortiumFieldInventory/ConsortiumFieldInventory.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ import {
import { FieldInventory } from '../FieldInventory';
import {
useAffiliationsSelectionOptions,
useConsortiumTenants,
useCurrentUserTenants,
} from '../hooks';
import { FieldAffiliation } from '../FieldAffiliation';

import css from './ConsortiumFieldInventory.css';

export const ConsortiumFieldInventory = ({
// Additional affiliations to be displayed in the dropdown if the current user is not affiliated with them which is defined by UIOR-1311
additionalAffiliationIds = [],
affiliationLabel,
affiliationName,
disabled = false,
Expand All @@ -43,14 +46,26 @@ export const ConsortiumFieldInventory = ({
const { batch, change } = useForm();
const { values } = useFormState();
const currUserTenants = useCurrentUserTenants();
const { tenants } = useConsortiumTenants();

const affiliations = useMemo(() => {
return currUserTenants?.map(({ id, name, isPrimary }) => ({
let tenantList = currUserTenants;
const isAdditionalAffiliationsMissing = additionalAffiliationIds.some((id) => {
return !currUserTenants?.find(({ id: tenantId }) => tenantId === id);
});

if (additionalAffiliationIds.length && isAdditionalAffiliationsMissing) {
const additionalAffiliations = tenants?.filter(({ id }) => additionalAffiliationIds.includes(id));

tenantList = [...additionalAffiliations, ...currUserTenants];
}

return tenantList?.map(({ id, name, isPrimary }) => ({
tenantId: id,
tenantName: name,
isPrimary,
}));
}, [currUserTenants]);
}, [additionalAffiliationIds, currUserTenants, tenants]);

const { dataOptions } = useAffiliationsSelectionOptions(affiliations);

Expand Down Expand Up @@ -113,6 +128,7 @@ export const ConsortiumFieldInventory = ({
};

ConsortiumFieldInventory.propTypes = {
additionalAffiliationIds: PropTypes.arrayOf(PropTypes.string),
affiliationLabel: PropTypes.node,
affiliationName: PropTypes.string.isRequired,
disabled: PropTypes.bool,
Expand Down
40 changes: 39 additions & 1 deletion lib/ConsortiumFieldInventory/ConsortiumFieldInventory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import {
screen,
} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import {
QueryClient,
QueryClientProvider,
} from 'react-query';
import { MemoryRouter } from 'react-router-dom';

import { useStripes } from '@folio/stripes/core';
Expand All @@ -12,8 +16,14 @@ import {
tenants,
} from '../../test/jest/fixtures';
import { StripesFinalFormHelper } from '../../test/jest/helpers';
import { useConsortiumTenants } from '../hooks';
import { ConsortiumFieldInventory } from './ConsortiumFieldInventory';

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

const stripes = {
user: {
user: { tenants },
Expand All @@ -27,6 +37,20 @@ const defaultProps = {
locationIds: [location.id],
locations: [location],
};
const additionalAffiliation = {
id: 'college',
name: 'College',
};

const queryClient = new QueryClient();

const wrapper = ({ children }) => (
<MemoryRouter>
<QueryClientProvider client={queryClient}>
{children}
</QueryClientProvider>
</MemoryRouter>
);

const renderConsortiumFieldInventory = (props = {}) => render(
<StripesFinalFormHelper
Expand All @@ -38,14 +62,18 @@ const renderConsortiumFieldInventory = (props = {}) => render(
{...props}
/>
</StripesFinalFormHelper>,
{ wrapper: MemoryRouter },
{ wrapper },
);

describe('ConsortiumFieldInventory', () => {
beforeEach(() => {
useStripes
.mockClear()
.mockReturnValue(stripes);
useConsortiumTenants
.mockReturnValue({
tenants: [...tenants, additionalAffiliation],
});
});

it('should render affiliation field', () => {
Expand All @@ -64,4 +92,14 @@ describe('ConsortiumFieldInventory', () => {

expect(onAffiliationChange).toHaveBeenCalled();
});

it('should render affiliations with additionalAffiliationIds', async () => {
renderConsortiumFieldInventory({
additionalAffiliationIds: [additionalAffiliation.id],
});

await userEvent.click(screen.getAllByText('stripes-components.selection.controlLabel')[0]);

expect(await screen.findByText(additionalAffiliation.name)).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
CONSORTIA_CONSORTIUM_TENANTS_API,
} from '../../../constants';

const DEFAULT_DATA = [];

export const useConsortiumTenants = (options = {}) => {
const stripes = useStripes();
const [namespace] = useNamespace({ key: 'consortium-tenants' });
Expand Down Expand Up @@ -45,7 +47,7 @@ export const useConsortiumTenants = (options = {}) => {
});

return ({
tenants: data?.tenants,
tenants: data?.tenants || DEFAULT_DATA,
totalRecords: data?.totalRecords,
isFetching,
isLoading,
Expand Down
Loading