-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: adds can budget form * feat: adds CANBudgetForm * feat: starts can budget submit * feat: adds post patch logic * feat: adds validation * feat: adds reset to validation suite * test: adds e2e for can budget form * test: adds can budget form component test * style: match figma design * chore: updates tag on CAN funding calls --------- Co-authored-by: Santi-3rd <[email protected]> Co-authored-by: maiyerlee <[email protected]>
- Loading branch information
1 parent
4ad202d
commit 1bf6b5c
Showing
18 changed files
with
660 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
frontend/src/components/CANs/CANBudgetForm/CANBudgetForm.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import CurrencyInput from "../../UI/Form/CurrencyInput"; | ||
import icons from "../../../uswds/img/sprite.svg"; | ||
|
||
/** | ||
* @typedef {Object} CANBudgetFormProps | ||
* @property {string} budgetAmount | ||
* @property {(arg: string) => string} cn | ||
* @property {Object} res | ||
* @property {number} fiscalYear | ||
* @property {(e: React.FormEvent<HTMLFormElement>) => void} handleAddBudget | ||
* @property {(name: string, value: string) => void} runValidate | ||
* @property { React.Dispatch<React.SetStateAction<string>>} setBudgetAmount | ||
*/ | ||
|
||
/** | ||
* @component - The CAN Budget Form component. | ||
* @param {CANBudgetFormProps} props | ||
* @returns {JSX.Element} - The component JSX. | ||
*/ | ||
const CANBudgetForm = ({ budgetAmount, cn, res, fiscalYear, handleAddBudget, runValidate, setBudgetAmount }) => { | ||
const fillColor = budgetAmount ? "#005ea2" : "#757575"; | ||
|
||
return ( | ||
<form | ||
onSubmit={(e) => { | ||
handleAddBudget(e); | ||
setBudgetAmount(""); | ||
}} | ||
> | ||
<div style={{ width: "383px" }}> | ||
<CurrencyInput | ||
name="budget-amount" | ||
label={`FY ${fiscalYear} CAN Budget`} | ||
onChange={(name, value) => { | ||
runValidate("budget-amount", value); | ||
}} | ||
setEnteredAmount={setBudgetAmount} | ||
value={budgetAmount || ""} | ||
messages={res.getErrors("budget-amount")} | ||
className={cn("budget-amount")} | ||
/> | ||
</div> | ||
<button | ||
id="add-fy-budget" | ||
className="usa-button usa-button--outline margin-top-4" | ||
disabled={!budgetAmount} | ||
data-cy="add-fy-budget" | ||
> | ||
<svg | ||
className="height-2 width-2 margin-right-05 cursor-pointer" | ||
style={{ fill: fillColor }} | ||
> | ||
<use xlinkHref={`${icons}#add`}></use> | ||
</svg> | ||
Add FY Budget | ||
</button> | ||
</form> | ||
); | ||
}; | ||
export default CANBudgetForm; |
72 changes: 72 additions & 0 deletions
72
frontend/src/components/CANs/CANBudgetForm/CANBudgetForm.test.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { describe, test, expect, vi } from "vitest"; | ||
import { render, screen, fireEvent } from "@testing-library/react"; | ||
import userEvent from "@testing-library/user-event"; | ||
import CANBudgetForm from "./CANBudgetForm"; | ||
|
||
describe("CANBudgetForm", () => { | ||
const defaultProps = { | ||
budgetAmount: "", | ||
cn: (name) => name, | ||
res: { getErrors: () => [] }, | ||
fiscalYear: 2024, | ||
handleAddBudget: vi.fn(), | ||
runValidate: vi.fn(), | ||
setBudgetAmount: vi.fn() | ||
}; | ||
|
||
test("renders with required props", () => { | ||
render(<CANBudgetForm {...defaultProps} />); | ||
expect(screen.getByLabelText(/FY 2024 CAN Budget/i)).toBeInTheDocument(); | ||
expect(screen.getByRole("button", { name: /add fy budget/i })).toBeInTheDocument(); | ||
}); | ||
|
||
test("button is disabled when budgetAmount is empty", () => { | ||
render(<CANBudgetForm {...defaultProps} />); | ||
expect(screen.getByRole("button", { name: /add fy budget/i })).toBeDisabled(); | ||
}); | ||
|
||
test("button is enabled when budgetAmount has value", () => { | ||
render( | ||
<CANBudgetForm | ||
{...defaultProps} | ||
budgetAmount="1000" | ||
/> | ||
); | ||
expect(screen.getByRole("button", { name: /add fy budget/i })).toBeEnabled(); | ||
}); | ||
|
||
test("calls handleAddBudget and setBudgetAmount on form submission", async () => { | ||
const user = userEvent.setup(); | ||
render( | ||
<CANBudgetForm | ||
{...defaultProps} | ||
budgetAmount="1000" | ||
/> | ||
); | ||
|
||
await user.click(screen.getByRole("button", { name: /add fy budget/i })); | ||
|
||
expect(defaultProps.handleAddBudget).toHaveBeenCalled(); | ||
expect(defaultProps.setBudgetAmount).toHaveBeenCalledWith(""); | ||
}); | ||
|
||
test("calls runValidate when currency input changes", () => { | ||
render(<CANBudgetForm {...defaultProps} />); | ||
|
||
fireEvent.change(screen.getByLabelText(/FY 2024 CAN Budget/i), { | ||
target: { value: "1000" } | ||
}); | ||
|
||
expect(defaultProps.runValidate).toHaveBeenCalledWith("budget-amount", "1,000"); | ||
}); | ||
|
||
test("displays validation errors when present", () => { | ||
const propsWithError = { | ||
...defaultProps, | ||
res: { getErrors: () => ["This is required information"] } | ||
}; | ||
|
||
render(<CANBudgetForm {...propsWithError} />); | ||
expect(screen.getByText("This is required information")).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {default} from "./CANBudgetForm" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { create, test, enforce, only } from "vest"; | ||
|
||
const suite = create((data = {}, fieldName) => { | ||
only(fieldName); | ||
|
||
test("budget-amount", "This is required information", () => { | ||
enforce(data["budget-amount"]).isNotBlank(); | ||
}); | ||
}); | ||
|
||
export default suite; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.