-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UISACQCOMP-166: configure the plugin via props
- Loading branch information
1 parent
5bbcf17
commit a4193de
Showing
7 changed files
with
251 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,62 @@ | ||
import React, { useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { FieldArray } from 'react-final-form-arrays'; | ||
|
||
import { | ||
Col, | ||
Loading, | ||
Row, | ||
} from '@folio/stripes/components'; | ||
import { useStripes } from '@folio/stripes/core'; | ||
|
||
import AddDonorButton from './AddDonorButton'; | ||
import DonorsList from './DonorsList'; | ||
import { useFetchDonors } from './hooks'; | ||
|
||
function DonorsContainer({ name, donorOrganizationIds }) { | ||
const [donorIds, setDonorIds] = useState(donorOrganizationIds); | ||
const { donors, isLoading } = useFetchDonors(donorIds); | ||
|
||
const donorsMap = donors.reduce((acc, contact) => { | ||
acc[contact.id] = contact; | ||
|
||
return acc; | ||
}, {}); | ||
|
||
if (isLoading) { | ||
return <Loading />; | ||
} | ||
import { defaultVisibleColumns } from './constants'; | ||
|
||
function DonorsContainer({ | ||
donorsMap, | ||
fields, | ||
id, | ||
setDonorIds, | ||
showTriggerButton, | ||
visibleColumns, | ||
...rest | ||
}) { | ||
const stripes = useStripes(); | ||
|
||
return ( | ||
<Row> | ||
<Col xs={12}> | ||
<FieldArray | ||
name={name} | ||
id={name} | ||
component={DonorsList} | ||
setDonorIds={setDonorIds} | ||
donorsMap={donorsMap} | ||
/> | ||
</Col> | ||
</Row> | ||
<> | ||
<DonorsList | ||
fields={fields} | ||
donorsMap={donorsMap} | ||
id={id} | ||
stripes={stripes} | ||
visibleColumns={visibleColumns} | ||
/> | ||
<br /> | ||
{ | ||
showTriggerButton && ( | ||
<AddDonorButton | ||
onAddDonors={setDonorIds} | ||
fields={fields} | ||
stripes={stripes} | ||
name={id} | ||
{...rest} | ||
/> | ||
) | ||
} | ||
</> | ||
); | ||
} | ||
|
||
DonorsContainer.propTypes = { | ||
name: PropTypes.string.isRequired, | ||
donorOrganizationIds: PropTypes.arrayOf(PropTypes.string), | ||
columnWidths: PropTypes.object, | ||
donorsMap: PropTypes.object, | ||
fields: PropTypes.object, | ||
formatter: PropTypes.object, | ||
id: PropTypes.string.isRequired, | ||
searchLabel: PropTypes.node, | ||
setDonorIds: PropTypes.func.isRequired, | ||
showTriggerButton: PropTypes.bool, | ||
visibleColumns: PropTypes.arrayOf(PropTypes.string), | ||
}; | ||
|
||
DonorsContainer.defaultProps = { | ||
donorOrganizationIds: [], | ||
showTriggerButton: true, | ||
visibleColumns: defaultVisibleColumns, | ||
}; | ||
|
||
export default DonorsContainer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import React, { useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { FieldArray } from 'react-final-form-arrays'; | ||
|
||
import { | ||
Col, | ||
Loading, | ||
Row, | ||
} from '@folio/stripes/components'; | ||
|
||
import DonorsContainer from './DonorsContainer'; | ||
import { useFetchDonors } from './hooks'; | ||
|
||
function DonorsForm({ name, donorOrganizationIds, ...rest }) { | ||
const [donorIds, setDonorIds] = useState(donorOrganizationIds); | ||
const { donors, isLoading } = useFetchDonors(donorIds); | ||
|
||
const donorsMap = donors.reduce((acc, contact) => { | ||
acc[contact.id] = contact; | ||
|
||
return acc; | ||
}, {}); | ||
|
||
if (isLoading) { | ||
return <Loading />; | ||
} | ||
|
||
return ( | ||
<Row> | ||
<Col xs={12}> | ||
<FieldArray | ||
name={name} | ||
id={name} | ||
component={DonorsContainer} | ||
setDonorIds={setDonorIds} | ||
donorsMap={donorsMap} | ||
{...rest} | ||
/> | ||
</Col> | ||
</Row> | ||
); | ||
} | ||
|
||
DonorsForm.propTypes = { | ||
donorOrganizationIds: PropTypes.arrayOf(PropTypes.string), | ||
name: PropTypes.string.isRequired, | ||
searchLabel: PropTypes.node, | ||
showTriggerButton: PropTypes.bool, | ||
visibleColumns: PropTypes.arrayOf(PropTypes.string), | ||
}; | ||
|
||
DonorsForm.defaultProps = { | ||
donorOrganizationIds: [], | ||
}; | ||
|
||
export default DonorsForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { MemoryRouter } from 'react-router-dom'; | ||
import { render, screen } from '@testing-library/react'; | ||
|
||
import stripesFinalForm from '@folio/stripes/final-form'; | ||
|
||
import DonorsForm from './DonorsForm'; | ||
import { useFetchDonors } from './hooks'; | ||
|
||
jest.mock('@folio/stripes/components', () => ({ | ||
...jest.requireActual('@folio/stripes/components'), | ||
Loading: jest.fn(() => 'Loading'), | ||
})); | ||
|
||
jest.mock('./DonorsList', () => jest.fn(({ donorsMap }) => { | ||
if (!Object.values(donorsMap).length) { | ||
return 'stripes-components.tableEmpty'; | ||
} | ||
|
||
return Object.values(donorsMap).map(({ name }) => <div key={name}>{name}</div>); | ||
})); | ||
|
||
jest.mock('./hooks', () => ({ | ||
useFetchDonors: jest.fn().mockReturnValue({ | ||
donors: [], | ||
isLoading: false, | ||
}), | ||
})); | ||
|
||
const defaultProps = { | ||
name: 'donors', | ||
donorOrganizationIds: [], | ||
}; | ||
|
||
const renderForm = (props = {}) => ( | ||
<form> | ||
<DonorsForm | ||
{...defaultProps} | ||
{...props} | ||
/> | ||
<button type="submit">Submit</button> | ||
</form> | ||
); | ||
|
||
const FormCmpt = stripesFinalForm({})(renderForm); | ||
|
||
const renderComponent = (props = {}) => (render( | ||
<MemoryRouter> | ||
<FormCmpt onSubmit={() => { }} {...props} /> | ||
</MemoryRouter>, | ||
)); | ||
|
||
describe('DonorsForm', () => { | ||
beforeEach(() => { | ||
useFetchDonors.mockClear().mockReturnValue({ | ||
donors: [], | ||
isLoading: false, | ||
}); | ||
}); | ||
|
||
it('should render component', () => { | ||
renderComponent(); | ||
|
||
expect(screen.getByText('stripes-components.tableEmpty')).toBeDefined(); | ||
}); | ||
|
||
it('should render Loading component', () => { | ||
useFetchDonors.mockClear().mockReturnValue({ | ||
donors: [], | ||
isLoading: true, | ||
}); | ||
|
||
renderComponent(); | ||
|
||
expect(screen.getByText('Loading')).toBeDefined(); | ||
}); | ||
|
||
it('should call `useFetchDonors` with `donorOrganizationIds`', () => { | ||
const mockData = [{ name: 'Amazon', code: 'AMAZ', id: '1' }]; | ||
|
||
useFetchDonors.mockClear().mockReturnValue({ | ||
donors: mockData, | ||
isLoading: false, | ||
}); | ||
|
||
renderComponent({ donorOrganizationIds: ['1'] }); | ||
|
||
expect(screen.getByText(mockData[0].name)).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.