Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

userInputAnswers.test.ts

48 lines1.9 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { formatUserInputAnswers, readUserInputQuestions } from "./userInputAnswers.ts";
4​
5const questions = readUserInputQuestions({
6 questions: [
7 { id: "approach", header: "Approach", question: "How should we proceed?", options: [] },
8 { id: "scope", header: "Scope", question: "Which areas?", options: [], multiSelect: true },
9 ],
10});
11​
12describe("formatUserInputAnswers", () => {
13 it("lists one line per question in the order asked, joining multi-select answers", () => {
14 expect(
15 formatUserInputAnswers(questions, {
16 scope: ["Server", "Web"],
17 approach: "Ship the minimal fix",
18 }),
19 ).toBe("Approach: Ship the minimal fix\nScope: Server, Web");
20 });
21​
22 it("keeps answers whose question is unknown and skips blank ones", () => {
23 expect(formatUserInputAnswers(questions, { approach: " ", extra: "Free text" })).toBe(
24 "extra: Free text",
25 );
26 expect(formatUserInputAnswers([], { note: "typed by hand" })).toBe("note: typed by hand");
27 });
28​
29 it("returns nothing for answers it cannot show", () => {
30 expect(formatUserInputAnswers(questions, undefined)).toBeUndefined();
31 expect(formatUserInputAnswers(questions, {})).toBeUndefined();
32 expect(readUserInputQuestions({ questions: "nope" })).toEqual([]);
33 });
34​
35 it("keeps the questions it can read when a request row carries a malformed one", () => {
36 const asked = readUserInputQuestions({
37 questions: [
38 { id: "approach", header: "Approach", question: "How should we proceed?" },
39 { header: "No id" },
40 { id: "scope", header: "Scope", question: "Which areas?", options: [], multiSelect: true },
41 ],
42 });
43 expect(asked.map((question) => question.id)).toEqual(["approach", "scope"]);
44 expect(formatUserInputAnswers(asked, { approach: "Minimal", scope: ["Web"] })).toBe(
45 "Approach: Minimal\nScope: Web",
46 );
47 });
48});