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

refactor: fix logic for agreement types count, add component tests #3206

Merged
merged 1 commit into from
Dec 11, 2024
Merged
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
10 changes: 5 additions & 5 deletions frontend/cypress/e2e/canDetail.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ describe("CAN detail page", () => {
cy.get("#edit").should("exist");
cy.get("#edit").click();
cy.get("h2").should("contain", "Edit CAN Details");
cy.get("#can_nick_name").invoke("val").should("equal", can502Nickname);
cy.get("#can_nick_name").clear();
cy.get("#can-nickName").invoke("val").should("equal", can502Nickname);
cy.get("#can-nickName").clear();
cy.get(".usa-error-message").should("exist").contains("This is required information");
cy.get("#save-changes").should("be.disabled");
cy.get("#can_nick_name").type("Test Can Nickname");
cy.get("#can-nickName").type("Test Can Nickname");
cy.get("#save-changes").should("not.be.disabled");
cy.get(".usa-error-message").should("not.exist");
cy.get("#description").invoke("val").should("equal", can502Description);
Expand All @@ -50,8 +50,8 @@ describe("CAN detail page", () => {
cy.get("dd").should("contain", "Test description.");
// revert back to original values
cy.get("#edit").click();
cy.get("#can_nick_name").clear();
cy.get("#can_nick_name").type(can502Nickname);
cy.get("#can-nickName").clear();
cy.get("#can-nickName").type(can502Nickname);
cy.get("#description").clear();
cy.get("#description").type(can502Description);
cy.get("#save-changes").click();
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/components/CANs/CANDetailForm/CANDetailForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ const CANDetailForm = ({ canId, canNumber, canNickname, canDescription, portfoli
/>
)}
<Input
name="can_nick_name"
name="can-nickName"
label="CAN Nickname"
onChange={(name, value) => {
runValidate("can_nick_name", value);
runValidate("can-nickName", value);
setNickName(value);
}}
value={nickName}
isRequired
messages={res.getErrors("can_nick_name")}
className={cn("can_nick_name")}
messages={res.getErrors("can-nickName")}
className={cn("can-nickName")}
/>
<TextArea
maxLength={1000}
Expand Down
87 changes: 87 additions & 0 deletions frontend/src/components/CANs/CANDetailForm/CANDetailForm.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { configureStore } from "@reduxjs/toolkit";
import { fireEvent, render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { Provider } from "react-redux";
import { beforeEach, describe, expect, test, vi } from "vitest";
import { opsApi } from "../../../api/opsAPI";
import CANDetailForm from "./CANDetailForm";

// Mock data
const mockCan = {
id: 123,
number: "CAN-001",
nick_name: "Test CAN",
description: "Test Description",
portfolio_id: 456
};

// Mock store setup
const store = configureStore({
reducer: {
[opsApi.reducerPath]: opsApi.reducer
},
middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(opsApi.middleware)
});

vi.mock("react-redux", async () => {
const actual = await vi.importActual("react-redux");
return {
...actual,
useSelector: vi.fn((selector) => {
// Mock the auth state
const mockState = {
alert: {
isActive: false
}
};
return selector(mockState);
})
};
});

const renderComponent = () => {
return render(
<Provider store={store}>
<CANDetailForm
canId={mockCan.id}
canNumber={mockCan.number}
canNickname={mockCan.nick_name}
canDescription={mockCan.description}
portfolioId={mockCan.portfolio_id}
toggleEditMode={vi.fn()}
/>
</Provider>
);
};

describe("CANDetailForm", () => {
beforeEach(() => {
vi.clearAllMocks();
});

test("renders form with initial values", () => {
renderComponent();

expect(screen.getByLabelText(/nickname/i)).toHaveValue(mockCan.nick_name);
expect(screen.getByLabelText(/description/i)).toHaveValue(mockCan.description);
});

test("validates required nickname field", async () => {
renderComponent();
const nicknameInput = screen.getByLabelText(/nickname/i);

await userEvent.clear(nicknameInput);
fireEvent.blur(nicknameInput);

expect(await screen.findByText("This is required information")).toBeInTheDocument();
});

test("shows confirmation modal when canceling", async () => {
renderComponent();

const cancelButton = screen.getByText(/cancel/i);
await userEvent.click(cancelButton);

expect(screen.getByText(/are you sure you want to cancel editing/i)).toBeInTheDocument();
});
});
4 changes: 2 additions & 2 deletions frontend/src/components/CANs/CANDetailForm/suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { create, test, enforce, only } from "vest";
const suite = create((data = {}, fieldName) => {
only(fieldName);

test("can_nick_name", "This is required information", () => {
enforce(data.can_nick_name).isNotBlank();
test("can-nickName", "This is required information", () => {
enforce(data["can-nickName"]).isNotBlank();
});
});

Expand Down
66 changes: 66 additions & 0 deletions frontend/src/components/CANs/CANDetailView/CANDetailView.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { describe, it, expect } from "vitest";
import { render, screen } from "@testing-library/react";
import CANDetailView from "./CANDetailView";

const mockProps = {
description: "Test CAN Description",
number: "CAN-123",
nickname: "Test Nickname",
portfolioName: "Test Portfolio",
teamLeaders: [
{ id: 1, full_name: "John Doe" },
{ id: 2, full_name: "Jane Smith" }
],
divisionDirectorFullName: "Director Name",
divisionName: "Test Division"
};

describe("CANDetailView", () => {
it("renders all CAN details correctly", () => {
render(
<dl>
<CANDetailView {...mockProps} />
</dl>
);

// Check for basic text content
expect(screen.getByText("Test CAN Description")).toBeInTheDocument();
expect(screen.getByText("CAN-123")).toBeInTheDocument();
expect(screen.getByText("Test Nickname")).toBeInTheDocument();
expect(screen.getByText("Test Portfolio")).toBeInTheDocument();
expect(screen.getByText("Test Division")).toBeInTheDocument();

// Check for team leaders
expect(screen.getByText("John Doe")).toBeInTheDocument();
expect(screen.getByText("Jane Smith")).toBeInTheDocument();

// Check for division director
expect(screen.getByText("Director Name")).toBeInTheDocument();
});

it("renders history section", () => {
render(
<dl>
<CANDetailView {...mockProps} />
</dl>
);

expect(screen.getByText("History")).toBeInTheDocument();
expect(screen.getByText("Not yet implemented")).toBeInTheDocument();
});

it("renders without team leaders", () => {
render(
<dl>
<CANDetailView
{...mockProps}
teamLeaders={[]}
/>
</dl>
);

// Verify other content still renders
expect(screen.getByText("Test CAN Description")).toBeInTheDocument();
expect(screen.getByText("Team Leader")).toBeInTheDocument();
});
});
15 changes: 13 additions & 2 deletions frontend/src/pages/cans/detail/Can.hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,21 @@ export default function useCan() {
[budgetLineItemsByFiscalYear]
);

const budgetLineAgreements = budgetLineItemsByFiscalYear?.map((item) => item.agreement) ?? [];
const budgetLinesAgreements = budgetLineItemsByFiscalYear?.map((item) => item.agreement) ?? [];

/**
* @type {import("../../../components/Agreements/AgreementTypes").SimpleAgreement[]} - Array of unique budget line agreements
*/
const uniqueBudgetLineAgreements =
budgetLinesAgreements?.reduce((acc, item) => {
if (!acc.some((existingItem) => existingItem.name === item.name)) {
acc.push(item);
}
return acc;
}, []) ?? [];

const agreementTypesCount = React.useMemo(
() => getTypesCounts(budgetLineAgreements, "agreement_type"),
() => getTypesCounts(uniqueBudgetLineAgreements, "agreement_type"),
[fiscalYear, can]
);

Expand Down
Loading