Skip to content

Commit

Permalink
test(cli/unstable): add promptSelect() ETX test (#6267)
Browse files Browse the repository at this point in the history
Co-authored-by: Yoshiya Hinosawa <[email protected]>
  • Loading branch information
timreichen and kt3k authored Dec 17, 2024
1 parent 7921baa commit 66ebe6c
Showing 1 changed file with 63 additions and 1 deletion.
64 changes: 63 additions & 1 deletion cli/unstable_prompt_select_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ Deno.test("promptSelect() handles clear option", () => {
restore();
});

Deno.test("promptMultipleSelect() returns null if Deno.stdin.isTerminal() is false", () => {
Deno.test("promptSelect() returns null if Deno.stdin.isTerminal() is false", () => {
stub(Deno.stdin, "setRaw");
stub(Deno.stdin, "isTerminal", () => false);

Expand Down Expand Up @@ -402,3 +402,65 @@ Deno.test("promptMultipleSelect() returns null if Deno.stdin.isTerminal() is fal
assertEquals(expectedOutput, actualOutput);
restore();
});

Deno.test("promptSelect() handles ETX", () => {
stub(Deno.stdin, "setRaw");
stub(Deno.stdin, "isTerminal", () => true);

let called = false;
stub(
Deno,
"exit",
(() => {
called = true;
}) as never,
);

const expectedOutput = [
"\x1b[?25l",
"Please select a browser:\r\n",
"❯ safari\r\n",
" chrome\r\n",
" firefox\r\n",
"\x1b[?25h",
];

const actualOutput: string[] = [];

stub(
Deno.stdout,
"writeSync",
(data: Uint8Array) => {
const output = decoder.decode(data);
actualOutput.push(output);
return data.length;
},
);

let readIndex = 0;

const inputs = [
"\x03",
];

stub(
Deno.stdin,
"readSync",
(data: Uint8Array) => {
const input = inputs[readIndex++];
const bytes = encoder.encode(input);
data.set(bytes);
return bytes.length;
},
);

const _browser = promptSelect("Please select a browser:", [
"safari",
"chrome",
"firefox",
]);

assertEquals(called, true);
assertEquals(expectedOutput, actualOutput);
restore();
});

0 comments on commit 66ebe6c

Please sign in to comment.