Browse Source

fix: add confirm button to date sheet and add tests for date error handling

master
mortezaei 1 week ago
parent
commit
93ea3b281c
  1. 16
      src/components/Componentes/question-date-sheet.tsx
  2. 15
      src/components/Componentes/question-date.test.tsx

16
src/components/Componentes/question-date-sheet.tsx

@ -11,6 +11,7 @@ import {
import { createPortal } from "react-dom"; import { createPortal } from "react-dom";
import type { QuestionField } from "@/lib/schema-adapter"; import type { QuestionField } from "@/lib/schema-adapter";
import { useI18n } from "@/translations/provider"; import { useI18n } from "@/translations/provider";
import { Button } from "./button";
import { useSheetScrollLock } from "./use-sheet-scroll-lock"; import { useSheetScrollLock } from "./use-sheet-scroll-lock";
const EXIT_ANIMATION_MS = 300; const EXIT_ANIMATION_MS = 300;
@ -349,6 +350,21 @@ export function QuestionDateSheet({
/> />
</div> </div>
</div> </div>
<div className="pt-5 pb-2">
<Button
onClick={() => {
const maxDay = daysInMonth(selectedYear, selectedMonth);
const dayNum = Math.min(Number.parseInt(selectedDay, 10), maxDay);
const formattedDay = String(dayNum).padStart(2, "0");
onApply(
`${selectedYear}-${selectedMonth.padStart(2, "0")}-${formattedDay}`,
);
closeSheet();
}}
>
{t.Confirm || (locale === "fa" ? "تأیید" : "Confirm")}
</Button>
</div>
</div> </div>
</section> </section>
</div>, </div>,

15
src/components/Componentes/question-date.test.tsx

@ -1,4 +1,4 @@
import { fireEvent, render, screen } from "@testing-library/react";
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest"; import { beforeEach, describe, expect, it, vi } from "vitest";
import type { QuestionField } from "@/lib/schema-adapter"; import type { QuestionField } from "@/lib/schema-adapter";
import { QuestionDate } from "./question-date"; import { QuestionDate } from "./question-date";
@ -36,6 +36,7 @@ const question = {
describe("QuestionDate", () => { describe("QuestionDate", () => {
beforeEach(() => { beforeEach(() => {
cleanup();
answerValue = null; answerValue = null;
}); });
@ -57,4 +58,16 @@ describe("QuestionDate", () => {
const yearWheel = screen.getByLabelText("Year"); const yearWheel = screen.getByLabelText("Year");
expect(yearWheel.textContent).not.toMatch(/[,٬]/); expect(yearWheel.textContent).not.toMatch(/[,٬]/);
}); });
it("handles invalid date strings gracefully without throwing RangeError", () => {
answerValue = "Mortezaei";
const { rerender } = render(<QuestionDate question={question} />);
expect(screen.queryByText("Age")).toBeNull();
expect(screen.getByText("Select date")).toBeDefined();
answerValue = "invalid-date-format";
rerender(<QuestionDate question={question} />);
expect(screen.queryByText("Age")).toBeNull();
expect(screen.getByText("Select date")).toBeDefined();
});
}); });
Loading…
Cancel
Save