Skip to content

Commit

Permalink
Merge pull request bcgov#1704 from djnunez-aot/unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
djnunez-aot authored Jan 19, 2024
2 parents 883c624 + 6c59b08 commit f58f964
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { PIPOrgType } from "../../models/pipOrgType";
import ControlledSwitch from "../shared/controlledInputComponents/ControlledSwitch";
import RichTextEditor from "../shared/richTextEditor";
import codeService, { Code } from "../../services/codeService";
import { showNotification } from "components/shared/notificationProvider";
import { COMMON_ERROR_MESSAGE } from "constants/application-constant";

const schema = yup.object().shape({
name: yup
Expand Down Expand Up @@ -85,9 +87,15 @@ export default function IndigenousNationForm({ ...props }) {
}, [ctx.item]);

const getStaffs = async () => {
const staffsResult = await staffService.getAll();
if (staffsResult.status === 200) {
setStaffs(staffsResult.data as never);
try {
const staffsResult = await staffService.getAll();
if (staffsResult.status === 200) {
setStaffs(staffsResult.data as Staff[]);
}
} catch (e) {
showNotification(COMMON_ERROR_MESSAGE, {
type: "error",
});
}
};

Expand All @@ -96,9 +104,15 @@ export default function IndigenousNationForm({ ...props }) {
};

const getCodes = async (code: Code) => {
const codeResult = await codeService.getCodes(code);
if (codeResult.status === 200) {
codeTypes[code]((codeResult.data as never)["codes"]);
try {
const codeResult = await codeService.getCodes(code);
if (codeResult.status === 200) {
codeTypes[code]((codeResult.data as never)["codes"]);
}
} catch (e) {
showNotification(COMMON_ERROR_MESSAGE, {
type: "error",
});
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import indigenousNationService from "../../services/indigenousNationService/indi
import { FirstNation } from "../../models/firstNation";
import MasterTrackTable from "../shared/MasterTrackTable";
import { ETGridTitle, ETPageContainer } from "../shared";
import IndigenousNationForm from "./IndigneousNationForm";
import IndigenousNationForm from "./IndigenousNationForm";
import { Staff } from "../../models/staff";
import staffService from "../../services/staffService/staffService";
import { MasterContext } from "../shared/MasterContext";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { MasterContext } from "components/shared/MasterContext";
import { defaultFirstNation } from "models/firstNation";
import { MasterBase } from "models/type";
import IndigenousNationForm from "../IndigenousNationForm";
import { Staff } from "models/staff";

const staffs: Staff[] = [
{
id: 1,
full_name: "John Doe",
first_name: "", // Add the missing property
last_name: "", // Add the missing property
phone: "",
email: "",
is_active: false,
position_id: 2 /* add more missing properties here */,
position: { name: "test", id: 1 }, // Add the missing property
},
{
id: 2,
full_name: "John Doe",
first_name: "", // Add the missing property
last_name: "", // Add the missing property
phone: "",
email: "",
is_active: false,
position_id: 2 /* add more missing properties here */,
position: { name: "test", id: 1 }, // Add the missing property
},
// Add more mock Staff objects as needed
];

const endpoints = [
{
method: "OPTIONS",
url: "http://localhost:3200/api/v1/staffs?is_active=false",
},
{
method: "OPTIONS",
url: "http://localhost:3200/api/v1/codes/pip_org_types",
},
{ method: "OPTIONS", url: "http://localhost:3200/api/v1/first_nations" },
{
method: "GET",
url: "http://localhost:3200/api/v1/staffs?is_active=false",
body: { data: staffs },
},
{
method: "GET",
url: "http://localhost:3200/api/v1/codes/pip_org_types",
body: [],
},
{
method: "GET",
url: "http://localhost:3200/api/v1/first_nations",
body: [],
},
];

function createMockContext() {
return {
item: defaultFirstNation,
setFormId: cy.stub(),
setTitle: cy.stub(),
setId: cy.stub(),
onSave: cy.stub(),
title: "",
data: [] as MasterBase[],
loading: false,
setItem: cy.stub(),
setShowDeleteDialog: cy.stub(),
setShowModalForm: cy.stub(),
getData: cy.stub(),
setService: cy.stub(),
setForm: cy.stub(),
onDialogClose: cy.stub(),
setFormStyle: cy.stub(),
getById: cy.stub(),
setDialogProps: cy.stub(),
};
}
function setupIntercepts(endpoints: any[]) {
endpoints.forEach(({ method, url, body }) => {
cy.intercept(method, url, { body });
});
}

describe("IndigenousNationForm", () => {
beforeEach(() => {
const mockContext = createMockContext();
setupIntercepts(endpoints);

cy.mount(
<MasterContext.Provider value={mockContext}>
<IndigenousNationForm />
</MasterContext.Provider>
);
});

it("renders the form", () => {
cy.get("form").should("be.visible");
});

it("renders the name field", () => {
cy.get('input[name="name"]');
});

it("renders the pip url field", () => {
cy.get('input[name="pip_link"]');
});

it("renders the rich text editor", () => {
cy.get(".DraftEditor-editorContainer");
});

it("renders the relationship holder field", () => {
cy.get('input[name="relationship_holder_id"]');
});
it("renders the pip organization type field", () => {
cy.get('input[name="pip_org_type_id"]');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const mockApprovedIssueUpdate: WorkIssueUpdate = {
is_deleted: false,
approved_by: faker.person.fullName(),
is_approved: true,
posted_date: "",
};

const mockIssue: WorkIssue = {
Expand Down Expand Up @@ -46,6 +47,7 @@ const generateMockIssueUpdate = ({
is_deleted: false,
approved_by: faker.person.fullName(),
is_approved: approved,
posted_date: "",
};
};

Expand Down

0 comments on commit f58f964

Please sign in to comment.