-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use correct component for skipprograms param
- Loading branch information
ismay
committed
Mar 13, 2024
1 parent
e5b18d1
commit 5b4768d
Showing
7 changed files
with
157 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
import { MultiSelectFieldFF, ReactFinalForm, MultiSelectField } from '@dhis2/ui' | ||
import i18n from '@dhis2/d2-i18n' | ||
import { useParameterOption } from '../../hooks/parameter-options' | ||
|
||
const { Field } = ReactFinalForm | ||
|
||
// This field has options that have both an id and a label. | ||
const ListFieldMulti = ({ label, name, parameterName }) => { | ||
const { loading, error, data } = useParameterOption(parameterName) | ||
const disabledProps = { disabled: true, label } | ||
|
||
if (loading) { | ||
return ( | ||
<MultiSelectField | ||
{...disabledProps} | ||
helpText={i18n.t('Loading options')} | ||
/> | ||
) | ||
} | ||
|
||
if (error) { | ||
return ( | ||
<MultiSelectField | ||
{...disabledProps} | ||
helpText={i18n.t('Something went wrong whilst loading options')} | ||
/> | ||
) | ||
} | ||
|
||
if (data.length === 0) { | ||
return ( | ||
<MultiSelectField | ||
{...disabledProps} | ||
helpText={i18n.t('No options available')} | ||
/> | ||
) | ||
} | ||
|
||
const labeledOptions = data.map(({ id, displayName }) => ({ | ||
value: id, | ||
label: displayName, | ||
})) | ||
|
||
return ( | ||
<Field | ||
name={name} | ||
component={MultiSelectFieldFF} | ||
options={labeledOptions} | ||
label={label} | ||
/> | ||
) | ||
} | ||
|
||
const { string } = PropTypes | ||
|
||
ListFieldMulti.propTypes = { | ||
label: string.isRequired, | ||
name: string.isRequired, | ||
parameterName: string.isRequired, | ||
} | ||
|
||
export default ListFieldMulti |
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
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,73 @@ | ||
import React from 'react' | ||
import { mount } from 'enzyme' | ||
import { ReactFinalForm } from '@dhis2/ui' | ||
import { useParameterOption } from '../../hooks/parameter-options' | ||
import ListFieldSingle from './ListFieldSingle' | ||
|
||
const { Form } = ReactFinalForm | ||
|
||
jest.mock('../../hooks/parameter-options', () => ({ | ||
useParameterOption: jest.fn(), | ||
})) | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks() | ||
}) | ||
|
||
describe('<ListFieldSingle>', () => { | ||
it('shows a message when there are no options', () => { | ||
useParameterOption.mockImplementation(() => ({ | ||
loading: false, | ||
error: undefined, | ||
data: [], | ||
})) | ||
const props = { | ||
label: 'label', | ||
name: 'name', | ||
parameterName: 'parameterName', | ||
} | ||
const wrapper = mount( | ||
<Form onSubmit={() => {}}> | ||
{() => ( | ||
<form> | ||
<ListFieldSingle {...props} /> | ||
</form> | ||
)} | ||
</Form> | ||
) | ||
|
||
const actual = wrapper | ||
.find({ | ||
'data-test': 'dhis2-uiwidgets-singleselectfield-help', | ||
}) | ||
.text() | ||
|
||
expect(actual).toEqual(expect.stringContaining('No options available')) | ||
}) | ||
|
||
it('renders the field when there are options', () => { | ||
useParameterOption.mockImplementation(() => ({ | ||
loading: false, | ||
error: undefined, | ||
data: [{ id: 'id', displayName: 'displayName' }], | ||
})) | ||
const props = { | ||
label: 'label', | ||
name: 'fieldName', | ||
parameterName: 'parameterName', | ||
} | ||
const wrapper = mount( | ||
<Form onSubmit={() => {}}> | ||
{() => ( | ||
<form> | ||
<ListFieldSingle {...props} /> | ||
</form> | ||
)} | ||
</Form> | ||
) | ||
|
||
const actual = wrapper.find('ListFieldSingle') | ||
|
||
expect(actual).toHaveLength(1) | ||
}) | ||
}) |
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
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