* Fix Pi ask_user optional input handling * Clarify Pi ask_user optional comment prompt * Combine Pi ask_user optional comment UI
80 lines
2.6 KiB
TypeScript
80 lines
2.6 KiB
TypeScript
import { describe, expect, test } from "vitest";
|
|
import {
|
|
areQuestionsAnswered,
|
|
buildQuestionFormAnswers,
|
|
parseQuestionFormQuestions,
|
|
questionShowsTextInput,
|
|
resolveDismissLabel,
|
|
shouldSubmitEmptyOnDismiss,
|
|
} from "./question-form-card-core";
|
|
|
|
describe("question form card core", () => {
|
|
test("treats optional input prompts as skippable empty answers", () => {
|
|
const questions = parseQuestionFormQuestions({
|
|
questions: [
|
|
{
|
|
question: "Optional comment?",
|
|
header: "Response",
|
|
options: [],
|
|
multiSelect: false,
|
|
placeholder: "Optional comment (press Enter to skip)...",
|
|
allowEmpty: true,
|
|
dismissLabel: "Skip",
|
|
},
|
|
],
|
|
});
|
|
|
|
if (!questions) throw new Error("questions did not parse");
|
|
expect(areQuestionsAnswered(questions, {}, {})).toBe(true);
|
|
expect(buildQuestionFormAnswers(questions, {}, {})).toEqual({ Response: "" });
|
|
expect(shouldSubmitEmptyOnDismiss(questions)).toBe(true);
|
|
expect(resolveDismissLabel(questions)).toBe("Skip");
|
|
});
|
|
|
|
test("requires a selection for option-only questions", () => {
|
|
const questions = parseQuestionFormQuestions({
|
|
questions: [
|
|
{
|
|
question: "Pick one",
|
|
header: "Response",
|
|
options: [{ label: "A" }, { label: "B" }],
|
|
multiSelect: false,
|
|
},
|
|
],
|
|
});
|
|
|
|
if (!questions) throw new Error("questions did not parse");
|
|
const [question] = questions;
|
|
if (!question) throw new Error("question missing");
|
|
expect(questionShowsTextInput(question)).toBe(false);
|
|
expect(areQuestionsAnswered(questions, {}, { 0: "freeform" })).toBe(false);
|
|
expect(areQuestionsAnswered(questions, { 0: new Set([1]) }, {})).toBe(true);
|
|
expect(buildQuestionFormAnswers(questions, { 0: new Set([1]) }, {})).toEqual({
|
|
Response: "B",
|
|
});
|
|
});
|
|
|
|
test("shows text input for explicit other questions", () => {
|
|
const questions = parseQuestionFormQuestions({
|
|
questions: [
|
|
{
|
|
question: "Pick or type",
|
|
header: "Response",
|
|
options: [{ label: "A" }],
|
|
isOther: true,
|
|
multiSelect: false,
|
|
},
|
|
],
|
|
});
|
|
|
|
if (!questions) throw new Error("questions did not parse");
|
|
const [question] = questions;
|
|
if (!question) throw new Error("question missing");
|
|
expect(questionShowsTextInput(question)).toBe(true);
|
|
expect(areQuestionsAnswered(questions, {}, { 0: "custom" })).toBe(true);
|
|
expect(buildQuestionFormAnswers(questions, {}, { 0: "custom" })).toEqual({
|
|
Response: "custom",
|
|
});
|
|
});
|
|
});
|