Skip to content

Commit 0aec17c

Browse files
UIOR-1569: Correct refresh of 'prefix' and 'suffix' field
We add a 'shouldRevalidatePoNumber' condition to the validators of 'FieldPrefix' and 'FieldSuffix' s.t. we only validate when the field 'poNumber' is actually available.
1 parent 40abc47 commit 0aec17c

4 files changed

Lines changed: 135 additions & 6 deletions

File tree

src/common/POFields/FieldPrefix.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import PropTypes from 'prop-types';
2+
import { useForm } from 'react-final-form';
23
import { FormattedMessage } from 'react-intl';
34

45
import {
@@ -13,13 +14,17 @@ const FieldPrefix = ({
1314
prefixes,
1415
...rest
1516
}) => {
17+
const { getFieldState } = useForm();
18+
const poNumberFieldState = getFieldState?.(PO_FORM_FIELDS.poNumber);
19+
const shouldRevalidatePoNumber = poNumberFieldState?.value;
20+
1621
return (
1722
<FieldSelect
1823
dataOptions={prefixes}
1924
isNonInteractive={isNonInteractive}
2025
label={<FormattedMessage id="ui-orders.orderDetails.orderNumberPrefix" />}
2126
name={PO_FORM_FIELDS.poNumberPrefix}
22-
validateFields={[PO_FORM_FIELDS.poNumber]}
27+
validateFields={shouldRevalidatePoNumber ? [PO_FORM_FIELDS.poNumber] : []}
2328
{...rest}
2429
/>
2530
);

src/common/POFields/FieldPrefix.test.js

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,31 @@
1-
import { Form } from 'react-final-form';
1+
import { Form, useForm } from 'react-final-form';
22

33
import { render, screen } from '@folio/jest-config-stripes/testing-library/react';
44

5+
import { PO_FORM_FIELDS } from '../constants';
56
import FieldPrefix from './FieldPrefix';
67

8+
jest.mock('react-final-form', () => {
9+
const actual = jest.requireActual('react-final-form');
10+
11+
return {
12+
...actual,
13+
useForm: jest.fn(),
14+
};
15+
});
16+
17+
const mockFieldSelectFinal = jest.fn(({ label }) => label);
18+
19+
jest.mock('@folio/stripes-acq-components', () => {
20+
const React = jest.requireActual('react');
21+
const PropTypes = jest.requireActual('prop-types');
22+
23+
return {
24+
FieldSelectFinal: (props) => mockFieldSelectFinal(props),
25+
fieldSelectOptionsShape: PropTypes.arrayOf(PropTypes.shape({})),
26+
};
27+
});
28+
729
const defaultProps = {
830
prefixes: [],
931
};
@@ -21,9 +43,47 @@ const renderFieldPrefix = (props = {}) => render(
2143
);
2244

2345
describe('FieldPrefix', () => {
24-
it('should render \'prefix\' field', () => {
46+
beforeEach(() => {
47+
jest.clearAllMocks();
48+
});
49+
50+
it('should render the prefix field', () => {
51+
useForm.mockReturnValue({ getFieldState: () => undefined });
52+
2553
renderFieldPrefix();
2654

2755
expect(screen.getByText('ui-orders.orderDetails.orderNumberPrefix')).toBeInTheDocument();
2856
});
57+
58+
it('should not revalidate the PO number field when it is not mounted', () => {
59+
useForm.mockReturnValue({ getFieldState: () => undefined });
60+
61+
renderFieldPrefix();
62+
63+
expect(mockFieldSelectFinal).toHaveBeenCalledWith(expect.objectContaining({
64+
validateFields: [],
65+
}));
66+
});
67+
68+
it('should not revalidate the PO number field when it is empty', () => {
69+
useForm.mockReturnValue({ getFieldState: () => ({ name: PO_FORM_FIELDS.poNumber, value: '' }) });
70+
71+
renderFieldPrefix();
72+
73+
expect(mockFieldSelectFinal).toHaveBeenCalledWith(expect.objectContaining({
74+
validateFields: [],
75+
}));
76+
});
77+
78+
it('should revalidate the PO number field when it is mounted and has a value', () => {
79+
useForm.mockReturnValue({
80+
getFieldState: () => ({ name: PO_FORM_FIELDS.poNumber, value: '1001' }),
81+
});
82+
83+
renderFieldPrefix();
84+
85+
expect(mockFieldSelectFinal).toHaveBeenCalledWith(expect.objectContaining({
86+
validateFields: [PO_FORM_FIELDS.poNumber],
87+
}));
88+
});
2989
});

src/common/POFields/FieldSuffix.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import PropTypes from 'prop-types';
2+
import { useForm } from 'react-final-form';
23
import { FormattedMessage } from 'react-intl';
34

45
import {
@@ -13,13 +14,17 @@ const FieldSuffix = ({
1314
suffixes,
1415
...rest
1516
}) => {
17+
const { getFieldState } = useForm();
18+
const poNumberFieldState = getFieldState?.(PO_FORM_FIELDS.poNumber);
19+
const shouldRevalidatePoNumber = poNumberFieldState?.value;
20+
1621
return (
1722
<FieldSelect
1823
dataOptions={suffixes}
1924
isNonInteractive={isNonInteractive}
2025
label={<FormattedMessage id="ui-orders.orderDetails.orderNumberSuffix" />}
2126
name={PO_FORM_FIELDS.poNumberSuffix}
22-
validateFields={[PO_FORM_FIELDS.poNumber]}
27+
validateFields={shouldRevalidatePoNumber ? [PO_FORM_FIELDS.poNumber] : []}
2328
{...rest}
2429
/>
2530
);

src/common/POFields/FieldSuffix.test.js

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
1-
import { Form } from 'react-final-form';
1+
import { Form, useForm } from 'react-final-form';
22

33
import { render, screen } from '@folio/jest-config-stripes/testing-library/react';
44

5+
import { PO_FORM_FIELDS } from '../constants';
56
import FieldSuffix from './FieldSuffix';
67

8+
jest.mock('react-final-form', () => {
9+
const actual = jest.requireActual('react-final-form');
10+
11+
return {
12+
...actual,
13+
useForm: jest.fn(),
14+
};
15+
});
16+
17+
const mockFieldSelectFinal = jest.fn(({ label }) => label);
18+
19+
jest.mock('@folio/stripes-acq-components', () => {
20+
const PropTypes = jest.requireActual('prop-types');
21+
22+
return {
23+
FieldSelectFinal: (props) => mockFieldSelectFinal(props),
24+
fieldSelectOptionsShape: PropTypes.arrayOf(PropTypes.shape({})),
25+
};
26+
});
27+
728
const defaultProps = {
829
suffixes: [],
930
};
@@ -21,9 +42,47 @@ const renderFieldSuffix = (props = {}) => render(
2142
);
2243

2344
describe('FieldSuffix', () => {
24-
it('should render \'suffix\' field', () => {
45+
beforeEach(() => {
46+
jest.clearAllMocks();
47+
});
48+
49+
it('should render the suffix field', () => {
50+
useForm.mockReturnValue({ getFieldState: () => undefined });
51+
2552
renderFieldSuffix();
2653

2754
expect(screen.getByText('ui-orders.orderDetails.orderNumberSuffix')).toBeInTheDocument();
2855
});
56+
57+
it('should not revalidate the PO number field when it is not mounted', () => {
58+
useForm.mockReturnValue({ getFieldState: () => undefined });
59+
60+
renderFieldSuffix();
61+
62+
expect(mockFieldSelectFinal).toHaveBeenCalledWith(expect.objectContaining({
63+
validateFields: [],
64+
}));
65+
});
66+
67+
it('should not revalidate the PO number field when it is empty', () => {
68+
useForm.mockReturnValue({ getFieldState: () => ({ name: PO_FORM_FIELDS.poNumber, value: '' }) });
69+
70+
renderFieldSuffix();
71+
72+
expect(mockFieldSelectFinal).toHaveBeenCalledWith(expect.objectContaining({
73+
validateFields: [],
74+
}));
75+
});
76+
77+
it('should revalidate the PO number field when it is mounted and has a value', () => {
78+
useForm.mockReturnValue({
79+
getFieldState: () => ({ name: PO_FORM_FIELDS.poNumber, value: '1001' }),
80+
});
81+
82+
renderFieldSuffix();
83+
84+
expect(mockFieldSelectFinal).toHaveBeenCalledWith(expect.objectContaining({
85+
validateFields: [PO_FORM_FIELDS.poNumber],
86+
}));
87+
});
2988
});

0 commit comments

Comments
 (0)