|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { render, screen } from "@testing-library/react"; |
| 3 | +import type { Questionnaire } from "fhir/r5"; |
| 4 | + |
| 5 | +import { FormStore } from "../../../stores/form-store.ts"; |
| 6 | +import { isQuestionNode } from "../../../stores/question-store.ts"; |
| 7 | +import { QuestionNode } from "../../nodes/question-node.tsx"; |
| 8 | +import { EXT } from "../../../utils.ts"; |
| 9 | + |
| 10 | +function getQuestion(form: FormStore, linkId: string) { |
| 11 | + const node = form.scope.lookupNode(linkId); |
| 12 | + expect(node && isQuestionNode(node)).toBe(true); |
| 13 | + if (!node || !isQuestionNode(node)) { |
| 14 | + throw new Error("Expected question node"); |
| 15 | + } |
| 16 | + return node; |
| 17 | +} |
| 18 | + |
| 19 | +describe("keyboard extension", () => { |
| 20 | + it("applies tel input mode for phone keyboard type on string questions", () => { |
| 21 | + const questionnaire: Questionnaire = { |
| 22 | + resourceType: "Questionnaire", |
| 23 | + status: "active", |
| 24 | + item: [ |
| 25 | + { |
| 26 | + linkId: "contact-phone", |
| 27 | + text: "Contact phone", |
| 28 | + type: "string", |
| 29 | + extension: [ |
| 30 | + { |
| 31 | + url: EXT.SDC_KEYBOARD, |
| 32 | + valueCoding: { |
| 33 | + system: "http://hl7.org/fhir/uv/sdc/ValueSet/keyboardType", |
| 34 | + code: "phone", |
| 35 | + }, |
| 36 | + }, |
| 37 | + ], |
| 38 | + }, |
| 39 | + ], |
| 40 | + }; |
| 41 | + |
| 42 | + const form = new FormStore(questionnaire); |
| 43 | + const question = getQuestion(form, "contact-phone"); |
| 44 | + |
| 45 | + expect(question.keyboardType).toBe("tel"); |
| 46 | + |
| 47 | + render(<QuestionNode item={question} />); |
| 48 | + |
| 49 | + const input = screen.getByLabelText("Contact phone") as HTMLInputElement; |
| 50 | + expect(input.getAttribute("inputmode")).toBe("tel"); |
| 51 | + }); |
| 52 | + |
| 53 | + it("applies text input mode for chat keyboard type on text questions", () => { |
| 54 | + const questionnaire: Questionnaire = { |
| 55 | + resourceType: "Questionnaire", |
| 56 | + status: "active", |
| 57 | + item: [ |
| 58 | + { |
| 59 | + linkId: "chat-text", |
| 60 | + text: "Chat message", |
| 61 | + type: "text", |
| 62 | + extension: [ |
| 63 | + { |
| 64 | + url: EXT.SDC_KEYBOARD, |
| 65 | + valueCoding: { |
| 66 | + system: "http://hl7.org/fhir/uv/sdc/ValueSet/keyboardType", |
| 67 | + code: "chat", |
| 68 | + }, |
| 69 | + }, |
| 70 | + ], |
| 71 | + }, |
| 72 | + ], |
| 73 | + }; |
| 74 | + |
| 75 | + const form = new FormStore(questionnaire); |
| 76 | + const question = getQuestion(form, "chat-text"); |
| 77 | + |
| 78 | + expect(question.keyboardType).toBe("text"); |
| 79 | + |
| 80 | + render(<QuestionNode item={question} />); |
| 81 | + |
| 82 | + const textarea = screen.getByLabelText( |
| 83 | + "Chat message", |
| 84 | + ) as HTMLTextAreaElement; |
| 85 | + expect(textarea.getAttribute("inputmode")).toBe("text"); |
| 86 | + }); |
| 87 | + |
| 88 | + it("ignores unsupported keyboard codes", () => { |
| 89 | + const questionnaire: Questionnaire = { |
| 90 | + resourceType: "Questionnaire", |
| 91 | + status: "active", |
| 92 | + item: [ |
| 93 | + { |
| 94 | + linkId: "other", |
| 95 | + text: "Other", |
| 96 | + type: "string", |
| 97 | + extension: [ |
| 98 | + { |
| 99 | + url: EXT.SDC_KEYBOARD, |
| 100 | + valueCoding: { |
| 101 | + code: "unsupported", |
| 102 | + }, |
| 103 | + }, |
| 104 | + ], |
| 105 | + }, |
| 106 | + ], |
| 107 | + }; |
| 108 | + |
| 109 | + const form = new FormStore(questionnaire); |
| 110 | + const question = getQuestion(form, "other"); |
| 111 | + |
| 112 | + expect(question.keyboardType).toBeUndefined(); |
| 113 | + |
| 114 | + render(<QuestionNode item={question} />); |
| 115 | + |
| 116 | + const input = screen.getByLabelText("Other") as HTMLInputElement; |
| 117 | + expect(input.hasAttribute("inputmode")).toBe(false); |
| 118 | + }); |
| 119 | +}); |
0 commit comments