Browse Source

fix: restrict option_id to choice types and update tests

master
mortezaei 4 days ago
parent
commit
cc68e1dc27
  1. 10
      src/components/Componentes/question-answer-storage.tsx
  2. 30
      src/components/Componentes/question-sheet.test.tsx

10
src/components/Componentes/question-answer-storage.tsx

@ -132,7 +132,13 @@ function createQuestionField(
): MarriageField { ): MarriageField {
let option_id: string | string[] | undefined; let option_id: string | string[] | undefined;
if (question.options && Array.isArray(question.options) && question.options.length > 0) {
const isChoiceType =
question.type === "dropdown" ||
question.type === "radio" ||
question.type === "checkbox" ||
question.type === "scale";
if (isChoiceType && question.options && Array.isArray(question.options) && question.options.length > 0) {
if (Array.isArray(value)) { if (Array.isArray(value)) {
option_id = value; option_id = value;
} else if (typeof value === "string" && value) { } else if (typeof value === "string" && value) {
@ -159,7 +165,7 @@ function createQuestionField(
type: question.type, type: question.type,
value, value,
private: question.private, private: question.private,
option_id: option_id,
option_id: isChoiceType ? option_id : undefined,
} as MarriageField; } as MarriageField;
} }

30
src/components/Componentes/question-sheet.test.tsx

@ -15,6 +15,13 @@ vi.mock("@/translations/provider", () => ({
useI18n: vi.fn(() => ({ locale: "fa", dictionary: { Confirm: "تایید" } })), useI18n: vi.fn(() => ({ locale: "fa", dictionary: { Confirm: "تایید" } })),
})); }));
vi.mock("@/hooks/marriage/use-profile-main", () => ({
useMarriageProfileQuery: vi.fn(() => ({
data: { id: 1, can_edit_profile: true },
isLoading: false,
})),
}));
vi.mock("@/hooks/marriage/use-section-data", () => ({ vi.mock("@/hooks/marriage/use-section-data", () => ({
applyProfilePatchResultToCache: vi.fn(), applyProfilePatchResultToCache: vi.fn(),
useMarriageSectionDataQuery: vi.fn(() => ({ useMarriageSectionDataQuery: vi.fn(() => ({
@ -91,16 +98,19 @@ describe("QuestionSheet component", () => {
fireEvent.click(iranBtn); fireEvent.click(iranBtn);
// Trigger should now show Iran // Trigger should now show Iran
await waitFor(() => {
expect(screen.getByText("ایران")).toBeDefined();
expect(document.body.classList.contains("dropdown-open")).toBe(false);
expect(document.body.classList.contains("question-sheet-open")).toBe(
false,
);
expect(
(document.querySelector(".app-shell") as HTMLElement).style.overflowY,
).toBe("");
});
await waitFor(
() => {
expect(screen.getByText("ایران")).toBeDefined();
expect(document.body.classList.contains("dropdown-open")).toBe(false);
expect(document.body.classList.contains("question-sheet-open")).toBe(
false,
);
expect(
(document.querySelector(".app-shell") as HTMLElement).style.overflowY,
).toBe("");
},
{ timeout: 1000 },
);
}); });
it("supports multi-selection with confirm button", async () => { it("supports multi-selection with confirm button", async () => {

Loading…
Cancel
Save