Skip to content

Commit

Permalink
testing disabled statuses with jest, not quite done
Browse files Browse the repository at this point in the history
  • Loading branch information
taraepp committed Nov 7, 2024
1 parent df468b1 commit 033344e
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 22 deletions.
5 changes: 2 additions & 3 deletions services/common/src/components/forms/RenderOrgBookSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { BaseInputProps, getFormItemLabel } from "./BaseInput";

interface OrgBookSearchProps extends BaseInputProps {
data?: any;
isDisabled?: boolean;
setCredential: (credential: IOrgbookCredential) => void;
}

Expand All @@ -29,7 +28,7 @@ const RenderOrgBookSearch: FC<OrgBookSearchProps> = ({
input,
meta,
required,
isDisabled = false,
disabled = false,
setCredential,
}) => {
const dispatch = useDispatch();
Expand Down Expand Up @@ -133,7 +132,7 @@ const RenderOrgBookSearch: FC<OrgBookSearchProps> = ({
onChange={handleChange}
onSelect={handleSelect}
style={{ width: "100%" }}
disabled={isDisabled}
disabled={disabled}
value={options.length === 1 ? { key: options[0].text } : null}
>
{options.map((option) => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,7 @@ export const AuthorizationsInvolved: FC<ProjectSummaryFormComponentProps> = ({ f
component={RenderHiddenField}
required
validate={[requiredList]}
disabled={fieldsDisabled}
label={<Typography.Title level={4}>Regulatory Approval Type</Typography.Title>}
>
<FormSection name="authorizations">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import React from "react";
import { render } from "@testing-library/react";
import FormWrapper from "../forms/FormWrapper";
import { FORM, SystemFlagEnum } from "@mds/common/constants";
import { ProjectManagement } from "./ProjectManagement";
import { LegalLandOwnerInformation } from "./LegalLandOwnerInformation";
import { ReduxWrapper } from "@mds/common/tests/utils/ReduxWrapper";
import * as MOCK from "@mds/common/tests/mocks/dataMocks";
import { AUTHENTICATION, PROJECTS, STATIC_CONTENT } from "@mds/common/constants/reducerTypes";
import { formatProjectSummary } from "@mds/common/redux/selectors/projectSelectors";
import BasicInformation from "./BasicInformation";
import ProjectLinks from "./ProjectLinks";
import ProjectContacts from "./ProjectContacts";
import ProjectDates from "./ProjectDates";
import Applicant from "./Applicant";
import { Agent } from "./Agent";
import { ApplicationSummary } from "./ApplicationSummary";
import AuthorizationsInvolved from "./AuthorizationsInvolved";
import Declaration from "./Declaration";
import DocumentUpload from "./DocumentUpload";
import { FacilityOperator } from "./FacilityOperator";
import { BrowserRouter } from "react-router-dom";

const amsAuthTypes = ['AIR_EMISSIONS_DISCHARGE_PERMIT', 'EFFLUENT_DISCHARGE_PERMIT', 'REFUSE_DISCHARGE_PERMIT'];
const project = { project_lead_party_guid: "project_lead_party_guid" };
const formattedProjectSummary = formatProjectSummary(MOCK.PROJECT_SUMMARY, project, amsAuthTypes);

const initialState = {
form: {
[FORM.ADD_EDIT_PROJECT_SUMMARY]: {
values: formattedProjectSummary,
},
},
[PROJECTS]: {
projectSummary: MOCK.PROJECT_SUMMARY,
projects: MOCK.PROJECTS.records,
project: MOCK.PROJECT,
},
[STATIC_CONTENT]: {
projectSummaryAuthorizationTypes:
MOCK.BULK_STATIC_CONTENT_RESPONSE.projectSummaryAuthorizationTypes,
},
[AUTHENTICATION]: {
systemFlag: SystemFlagEnum.core,
userAccessData: MOCK.USER_ACCESS_DATA,
userInfo: { preferred_username: "USERNAME" }
},
};

const mockFields = jest.fn();
const mockDocFields = jest.fn();
const mockAuthFields = jest.fn();
const mockEnvFields = jest.fn();

jest.mock("@mds/common/components/projects/projectUtils", () => ({
areFieldsDisabled: () => mockFields(),
areDocumentFieldsDisabled: () => mockDocFields(),
areAuthFieldsDisabled: () => mockAuthFields(),
areAuthEnvFieldsDisabled: () => mockEnvFields(),
getProjectStatusDescription: () => jest.fn().mockReturnValue("Status Description")
}));

const allowedEnabledWhenDisabled = [
"status_code", // core user can edit status
"project_lead_party_guid", // and the project lead
"ADD_EDIT_PROJECT_SUMMARY_ams_terms_agreed", // still debating these 2, but harmless to leave open
"ADD_EDIT_PROJECT_SUMMARY_confirmation_of_submission",
];

const filterOutAllowedEnabledIds = (idArray: string[]) => {
return idArray.filter((id) => !allowedEnabledWhenDisabled.includes(id));
};
describe("ProjectSummaryForm components disable accurately accoring to functions", () => {
const renderedComponents = ({ fieldsDisabled, authFieldsDisabled, docFieldsDisabled, envFieldsDisabled }) => (
<BrowserRouter>
<FormWrapper name={FORM.ADD_EDIT_PROJECT_SUMMARY} onSubmit={jest.fn()}>
<ProjectManagement />
<BasicInformation fieldsDisabled={fieldsDisabled} />
<LegalLandOwnerInformation fieldsDisabled={fieldsDisabled} />
<ProjectLinks fieldsDisabled={fieldsDisabled} viewProject={jest.fn()} />
<ProjectContacts fieldsDisabled={fieldsDisabled} />
<ProjectDates fieldsDisabled={fieldsDisabled} />
<Applicant fieldsDisabled={fieldsDisabled} />
<Agent fieldsDisabled={fieldsDisabled} />
<FacilityOperator fieldsDisabled={fieldsDisabled} />
<AuthorizationsInvolved fieldsDisabled={authFieldsDisabled} />
<DocumentUpload docFieldsDisabled={docFieldsDisabled} />
<ApplicationSummary fieldsDisabled={fieldsDisabled} />
<Declaration />
</FormWrapper>
</BrowserRouter>
);

test("shows all disabled", () => {
const params = {
fieldsDisabled: true,
authFieldsDisabled: true,
docFieldsDisabled: true,
envFieldsDisabled: true
};
mockFields.mockReturnValue(params.fieldsDisabled);
mockDocFields.mockReturnValue(params.docFieldsDisabled);
mockAuthFields.mockReturnValue(params.authFieldsDisabled);
mockEnvFields.mockReturnValue(params.envFieldsDisabled);

const { container } = render(
<ReduxWrapper initialState={initialState}>
{renderedComponents(params)}
</ReduxWrapper>
);

const enabledInputs = container.querySelectorAll(`input:not(:disabled)`);
const enabledInputIds = Array.from(enabledInputs).map((input) => input.id);
const filteredIds = filterOutAllowedEnabledIds(enabledInputIds);
expect(filteredIds).toEqual([]);
});

test("shows all enabled", () => {
const params = {
fieldsDisabled: false,
authFieldsDisabled: false,
docFieldsDisabled: false,
envFieldsDisabled: false
};
mockFields.mockReturnValue(params.fieldsDisabled);
mockDocFields.mockReturnValue(params.docFieldsDisabled);
mockAuthFields.mockReturnValue(params.authFieldsDisabled);
mockEnvFields.mockReturnValue(params.envFieldsDisabled);

const { container } = render(
<ReduxWrapper initialState={initialState}>
{renderedComponents(params)}
</ReduxWrapper>
);

const disabledInputs = container.querySelectorAll(`input:disabled`);
const disabledInputIds = Array.from(disabledInputs).map((input) => input.id);
expect(disabledInputIds).toEqual([]);
});
});
42 changes: 23 additions & 19 deletions services/common/src/redux/selectors/projectSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,27 +138,31 @@ export const getFormattedProjectApplication = createSelector(
}
);

export const formatProjectSummary = (summary, project, amsAuthTypes) => {
const documents = formatProjectSummaryDocuments(summary.documents);
const contacts = formatProjectContact(project.contacts);
const agent = formatProjectSummaryParty(summary.agent);
const facility_operator = formatProjectSummaryParty(summary.facility_operator);
const confirmation_of_submission = Boolean(summary.submission_date);

const formattedSummary = {
...summary,
contacts,
agent,
facility_operator,
confirmation_of_submission,
...formatAuthorizations(amsAuthTypes, confirmation_of_submission, summary.authorizations),
...documents,
};

formattedSummary.project_lead_party_guid = project.project_lead_party_guid;
return formattedSummary;
}

export const getFormattedProjectSummary = createSelector(
[getProjectSummary, getProject, getAmsAuthorizationTypes],
(summary, project, amsAuthTypes) => {
const documents = formatProjectSummaryDocuments(summary.documents);
const contacts = formatProjectContact(project.contacts);
const agent = formatProjectSummaryParty(summary.agent);
const facility_operator = formatProjectSummaryParty(summary.facility_operator);
const confirmation_of_submission = Boolean(summary.submission_date);

const formattedSummary = {
...summary,
contacts,
agent,
facility_operator,
confirmation_of_submission,
...formatAuthorizations(amsAuthTypes, confirmation_of_submission, summary.authorizations),
...documents,
};

formattedSummary.project_lead_party_guid = project.project_lead_party_guid;

return formattedSummary;
console.log(amsAuthTypes);
return formatProjectSummary(summary, project, amsAuthTypes);
}
);

0 comments on commit 033344e

Please sign in to comment.