Browse Source
fix(question-number): normalize Persian/Arabic digits and use text inputMode numeric
master
fix(question-number): normalize Persian/Arabic digits and use text inputMode numeric
master
2 changed files with 143 additions and 26 deletions
-
80src/components/Componentes/question-number.test.tsx
-
89src/components/Componentes/question-number.tsx
@ -0,0 +1,80 @@ |
|||||
|
import { render, screen, fireEvent, cleanup } from "@testing-library/react"; |
||||
|
import { describe, expect, it, afterEach } from "vitest"; |
||||
|
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; |
||||
|
import QuestionNumber, { normalizeNumberString } from "./question-number"; |
||||
|
import { QuestionAnswersProvider } from "./question-answer-storage"; |
||||
|
import type { QuestionField } from "@/lib/schema-adapter"; |
||||
|
|
||||
|
describe("normalizeNumberString", () => { |
||||
|
it("should convert Persian digits to English digits", () => { |
||||
|
expect(normalizeNumberString("۱۲۳۴۵۶۷۸۹۰")).toBe("1234567890"); |
||||
|
expect(normalizeNumberString("۵")).toBe("5"); |
||||
|
expect(normalizeNumberString("۳٫۵")).toBe("3.5"); |
||||
|
}); |
||||
|
|
||||
|
it("should convert Arabic digits to English digits", () => { |
||||
|
expect(normalizeNumberString("١٢٣٤٥٦٧٨٩٠")).toBe("1234567890"); |
||||
|
expect(normalizeNumberString("٤")).toBe("4"); |
||||
|
}); |
||||
|
|
||||
|
it("should preserve English digits", () => { |
||||
|
expect(normalizeNumberString("12345")).toBe("12345"); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
describe("QuestionNumber Component", () => { |
||||
|
afterEach(() => { |
||||
|
cleanup(); |
||||
|
}); |
||||
|
|
||||
|
const queryClient = new QueryClient({ |
||||
|
defaultOptions: { |
||||
|
queries: { retry: false }, |
||||
|
mutations: { retry: false }, |
||||
|
}, |
||||
|
}); |
||||
|
|
||||
|
const mockQuestion: QuestionField = { |
||||
|
id: "q1_number_of_siblings", |
||||
|
title: "Number of Siblings", |
||||
|
type: "number", |
||||
|
order: 1, |
||||
|
required: true, |
||||
|
isVisible: true, |
||||
|
description: "", |
||||
|
tooltip: "", |
||||
|
extras: { placeHolder: "e.g. 3", range: [0, 20] }, |
||||
|
options: [], |
||||
|
ui_config: {}, |
||||
|
}; |
||||
|
|
||||
|
it("should allow entering Persian digits without clearing input", () => { |
||||
|
render( |
||||
|
<QueryClientProvider client={queryClient}> |
||||
|
<QuestionAnswersProvider slug="family_background" questions={[mockQuestion]}> |
||||
|
<QuestionNumber question={mockQuestion} /> |
||||
|
</QuestionAnswersProvider> |
||||
|
</QueryClientProvider>, |
||||
|
); |
||||
|
|
||||
|
const input = screen.getByPlaceholderText("e.g. 3") as HTMLInputElement; |
||||
|
fireEvent.change(input, { target: { value: "۴" } }); |
||||
|
|
||||
|
expect(input.value).toBe("4"); |
||||
|
}); |
||||
|
|
||||
|
it("should allow entering English digits", () => { |
||||
|
render( |
||||
|
<QueryClientProvider client={queryClient}> |
||||
|
<QuestionAnswersProvider slug="family_background" questions={[mockQuestion]}> |
||||
|
<QuestionNumber question={mockQuestion} /> |
||||
|
</QuestionAnswersProvider> |
||||
|
</QueryClientProvider>, |
||||
|
); |
||||
|
|
||||
|
const input = screen.getByPlaceholderText("e.g. 3") as HTMLInputElement; |
||||
|
fireEvent.change(input, { target: { value: "5" } }); |
||||
|
|
||||
|
expect(input.value).toBe("5"); |
||||
|
}); |
||||
|
}); |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue