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

PSP-9815 Property Research Summary comments field doesn't have a limit of character when more than allowed are inserted. #4602

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,69 +1,47 @@
import { Formik } from 'formik';
import { Formik, FormikProps } from 'formik';
import { createMemoryHistory } from 'history';
import noop from 'lodash/noop';
import { createRef } from 'react';

import { mockLookups } from '@/mocks/lookups.mock';
import { ApiGen_Concepts_ResearchFileProperty } from '@/models/api/generated/ApiGen_Concepts_ResearchFileProperty';
import { getMockResearchFileProperty } from '@/mocks/researchFile.mock';
import { lookupCodesSlice } from '@/store/slices/lookupCodes';
import { render, RenderOptions } from '@/utils/test-utils';
import {
act,
fakeText,
fireEvent,
getByName,
render,
RenderOptions,
screen,
userEvent,
} from '@/utils/test-utils';

import { UpdatePropertyFormModel } from './models';
import UpdatePropertyForm from './UpdatePropertyForm';
import { getEmptyBaseAudit } from '@/models/defaultInitializers';

const testResearchFile: ApiGen_Concepts_ResearchFileProperty = {
id: 1,
propertyName: 'Corner of Nakya PL ',
propertyId: 495,

propertyResearchPurposeTypes: [
{
id: 22,
propertyResearchPurposeTypeCode: {
id: 'FORM12',
description: 'Form 12',
isDisabled: false,
displayOrder: null,
},
rowVersion: 1,
...getEmptyBaseAudit(),
},
{
id: 23,
propertyResearchPurposeTypeCode: {
id: 'DOTHER',
description: 'District Other',
isDisabled: false,
displayOrder: null,
},
rowVersion: 1,
...getEmptyBaseAudit(),
},
],
rowVersion: 10,
isLegalOpinionRequired: null,
isLegalOpinionObtained: null,
documentReference: null,
researchSummary: null,
file: null,
displayOrder: null,
property: null,
location: null,
fileId: 0,
};
import UpdatePropertyForm, { IUpdatePropertyResearchFormProps } from './UpdatePropertyForm';
import { UpdatePropertyYupSchema } from './UpdatePropertyYupSchema';

const history = createMemoryHistory();
const storeState = {
[lookupCodesSlice.name]: { lookupCodes: mockLookups },
};

const onSubmit = vi.fn();
const validationSchema = vi.fn().mockReturnValue(UpdatePropertyYupSchema);

describe('UpdatePropertyForm component', () => {
const setup = (renderOptions: RenderOptions & { initialValues: UpdatePropertyFormModel }) => {
// render component under test
const component = render(
<Formik onSubmit={noop} initialValues={renderOptions.initialValues}>
{formikProps => <UpdatePropertyForm formikProps={formikProps} />}
</Formik>,
// render component under test
const setup = (
renderOptions: RenderOptions & { props?: Partial<IUpdatePropertyResearchFormProps> } = {},
) => {
const formikRef = createRef<FormikProps<UpdatePropertyFormModel>>();

const utils = render(
<UpdatePropertyForm
formikRef={formikRef}
initialValues={renderOptions?.props?.initialValues ?? new UpdatePropertyFormModel()}
validationSchema={renderOptions?.props?.validationSchema ?? validationSchema}
onSubmit={renderOptions?.props?.onSubmit ?? onSubmit}
/>,
{
...renderOptions,
store: storeState,
Expand All @@ -72,17 +50,64 @@ describe('UpdatePropertyForm component', () => {
);

return {
component,
...utils,
formikRef,
};
};

let initialValues: UpdatePropertyFormModel;

beforeEach(() => {
initialValues = UpdatePropertyFormModel.fromApi(getMockResearchFileProperty());
});

afterEach(() => {
vi.resetAllMocks();
vi.clearAllMocks();
});

it('renders as expected when provided no research file', () => {
const initialValues = UpdatePropertyFormModel.fromApi(testResearchFile);
const { component } = setup({ initialValues });
expect(component.asFragment()).toMatchSnapshot();
it('renders as expected', () => {
const { asFragment } = setup({ props: { initialValues } });
expect(asFragment()).toMatchSnapshot();
});

it('displays property research purposes - multiselect', () => {
setup({ props: { initialValues } });

expect(screen.getByText('Form 12')).toBeVisible();
expect(screen.getByText('District Other')).toBeVisible();
});

it('validates max length for research summary', async () => {
setup({ props: { initialValues } });

const summary = getByName('researchSummary') as HTMLTextAreaElement;

await act(async () => {
userEvent.paste(summary, fakeText(4001));
});
await act(async () => {
fireEvent.blur(summary);
});

expect(
await screen.findByText(/Summary comments must be less than 4000 characters/i),
).toBeVisible();
expect(validationSchema).toHaveBeenCalled();
});

it('calls onSubmit when form is submitted', async () => {
const { formikRef } = setup({ props: { initialValues } });

const summary = getByName('researchSummary') as HTMLTextAreaElement;

await act(async () => {
userEvent.paste(summary, fakeText(100));
});
await act(async () => {
formikRef.current?.submitForm();
});

expect(validationSchema).toHaveBeenCalled();
expect(onSubmit).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { FormikProps, useFormikContext } from 'formik';
import Multiselect from 'multiselect-react-dropdown';
import { createRef, useState } from 'react';
import { FaTimes } from 'react-icons/fa';
import { Formik, FormikHelpers, FormikProps } from 'formik';

import { Input, Select, SelectOption, TextArea } from '@/components/common/form';
import { Input, Multiselect, Select, SelectOption, TextArea } from '@/components/common/form';
import { Section } from '@/components/common/Section/Section';
import { SectionField } from '@/components/common/Section/SectionField';
import { StyledSummarySection } from '@/components/common/Section/SectionStyles';
Expand All @@ -12,19 +9,21 @@ import useLookupCodeHelpers from '@/hooks/useLookupCodeHelpers';

import { PropertyResearchFilePurposeFormModel, UpdatePropertyFormModel } from './models';

interface MultiSelectOption {
id: string;
text: string;
export interface IUpdatePropertyResearchFormProps {
formikRef: React.RefObject<FormikProps<UpdatePropertyFormModel>>;
/** Initial values of the form */
initialValues: UpdatePropertyFormModel;
/** A Yup Schema or a function that returns a Yup schema */
validationSchema?: any | (() => any);
/** Submission handler */
onSubmit: (
values: UpdatePropertyFormModel,
formikHelpers: FormikHelpers<UpdatePropertyFormModel>,
) => void | Promise<any>;
}

export interface IUpdatePropertyFormProps {
formikProps: FormikProps<UpdatePropertyFormModel>;
}

const UpdatePropertyForm: React.FunctionComponent<
React.PropsWithChildren<IUpdatePropertyFormProps>
> = props => {
const { values } = useFormikContext<UpdatePropertyFormModel>();
const UpdatePropertyForm: React.FunctionComponent<IUpdatePropertyResearchFormProps> = props => {
const { formikRef, initialValues, validationSchema, onSubmit } = props;
const { getByType } = useLookupCodeHelpers();

const opinionOptions: SelectOption[] = [
Expand All @@ -33,86 +32,49 @@ const UpdatePropertyForm: React.FunctionComponent<
{ label: 'No', value: 'no' },
];

const propertyResearchPurposeOptions = getByType(API.PROPERTY_RESEARCH_PURPOSE_TYPES);

const purposeFilterOptions: MultiSelectOption[] =
propertyResearchPurposeOptions.map<MultiSelectOption>(x => {
return { id: x.id as string, text: x.name };
});

const initialPurposeList = purposeFilterOptions.filter(x =>
values.propertyResearchPurposeTypes?.map(x => x.propertyResearchPurposeTypeCode).includes(x.id),
const propertyResearchPurposeOptions = getByType(API.PROPERTY_RESEARCH_PURPOSE_TYPES).map(x =>
PropertyResearchFilePurposeFormModel.fromLookup(x),
);

const [selectedPurposes, setSelectedPurposes] = useState<MultiSelectOption[]>(initialPurposeList);

const multiselectProgramRef = createRef<Multiselect>();

function onSelectedPurposeChange(selectedList: MultiSelectOption[]) {
setSelectedPurposes(selectedList);
const mapped = selectedList.map<PropertyResearchFilePurposeFormModel>(x => {
const purposeType = new PropertyResearchFilePurposeFormModel();
purposeType.propertyResearchPurposeTypeCode = x.id;
purposeType.propertyPurposeTypeDescription = x.text;
return purposeType;
});
props.formikProps.setFieldValue('propertyResearchPurposeTypes', mapped);
}

return (
<StyledSummarySection>
<Section header="Property of Interest">
<SectionField label="Descriptive name">
<Input field="propertyName" />
</SectionField>
<SectionField label="Purpose">
<Multiselect
id="purpose-selector"
ref={multiselectProgramRef}
options={purposeFilterOptions}
onSelect={onSelectedPurposeChange}
onRemove={onSelectedPurposeChange}
selectedValues={selectedPurposes}
displayValue="text"
placeholder="Select Property Purpose"
customCloseIcon={<FaTimes size="18px" className="ml-3" />}
hidePlaceholder={true}
style={{
chips: {
background: '#F2F2F2',
borderRadius: '4px',
color: 'black',
fontSize: '16px',
marginRight: '1em',
},
multiselectContainer: {
width: 'auto',
color: 'black',
paddingBottom: '12px',
},
searchBox: {
background: 'white',
border: '1px solid #606060',
},
}}
/>
</SectionField>
<SectionField label="Legal opinion req'd?">
<Select field="isLegalOpinionRequired" options={opinionOptions} />
</SectionField>
<SectionField label="Legal opinion obtained?">
<Select field="isLegalOpinionObtained" options={opinionOptions} />
</SectionField>
<SectionField label="Document reference">
<Input field="documentReference" />
</SectionField>
</Section>
<Formik<UpdatePropertyFormModel>
enableReinitialize
innerRef={formikRef}
initialValues={initialValues}
validationSchema={validationSchema}
onSubmit={onSubmit}
>
<StyledSummarySection>
<Section header="Property of Interest">
<SectionField label="Descriptive name">
<Input field="propertyName" />
</SectionField>
<SectionField label="Purpose">
<Multiselect
field="propertyResearchPurposeTypes"
displayValue="propertyPurposeTypeDescription"
placeholder="Select Property Purpose"
options={propertyResearchPurposeOptions}
hidePlaceholder
/>
</SectionField>
<SectionField label="Legal opinion req'd?">
<Select field="isLegalOpinionRequired" options={opinionOptions} />
</SectionField>
<SectionField label="Legal opinion obtained?">
<Select field="isLegalOpinionObtained" options={opinionOptions} />
</SectionField>
<SectionField label="Document reference">
<Input field="documentReference" />
</SectionField>
</Section>

<Section header="Research Summary">
<SectionField label="Summary comments" />
<TextArea field="researchSummary" />
</Section>
</StyledSummarySection>
<Section header="Research Summary">
<SectionField label="Summary comments" />
<TextArea field="researchSummary" />
</Section>
</StyledSummarySection>
</Formik>
);
};

Expand Down
Loading
Loading