test: speed up styled select test

This commit is contained in:
Peter Steinberger
2026-04-28 10:02:14 +01:00
parent 8f277e4b7f
commit 0876ff481b
3 changed files with 71 additions and 49 deletions

View File

@@ -0,0 +1,30 @@
import { stylePromptHint, stylePromptMessage } from "./prompt-style.js";
type SelectParamsLike = {
message: string;
options: readonly object[];
};
type PromptSelectStylers = {
message: (value: string) => string;
hint: (value: string) => string | undefined;
};
const defaultStylers: PromptSelectStylers = {
message: stylePromptMessage,
hint: stylePromptHint,
};
export function styleSelectParams<TParams extends SelectParamsLike>(
params: TParams,
stylers: PromptSelectStylers = defaultStylers,
): TParams {
return {
...params,
message: stylers.message(params.message),
options: params.options.map((opt) => {
const hint = "hint" in opt && typeof opt.hint === "string" ? opt.hint : undefined;
return hint === undefined ? opt : { ...opt, hint: stylers.hint(hint) };
}),
} as TParams;
}

View File

@@ -1,45 +1,23 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { describe, expect, it } from "vitest";
import { styleSelectParams } from "./prompt-select-styled-params.js";
const { selectMock, stylePromptMessageMock, stylePromptHintMock } = vi.hoisted(() => ({
selectMock: vi.fn(),
stylePromptMessageMock: vi.fn((value: string) => `msg:${value}`),
stylePromptHintMock: vi.fn((value: string) => `hint:${value}`),
}));
vi.mock("@clack/prompts", () => ({
select: selectMock,
}));
vi.mock("./prompt-style.js", () => ({
stylePromptMessage: stylePromptMessageMock,
stylePromptHint: stylePromptHintMock,
}));
import { selectStyled } from "./prompt-select-styled.js";
describe("selectStyled", () => {
beforeEach(() => {
selectMock.mockClear();
stylePromptMessageMock.mockClear();
stylePromptHintMock.mockClear();
});
it("styles message and option hints before delegating to clack select", () => {
const expected = Symbol("selected");
selectMock.mockReturnValue(expected);
const result = selectStyled({
message: "Pick channel",
options: [
{ value: "stable", label: "Stable", hint: "Tagged releases" },
{ value: "dev", label: "Dev" },
],
});
expect(result).toBe(expected);
expect(stylePromptMessageMock).toHaveBeenCalledWith("Pick channel");
expect(stylePromptHintMock).toHaveBeenCalledWith("Tagged releases");
expect(selectMock).toHaveBeenCalledWith({
describe("styleSelectParams", () => {
it("styles message and option hints before select receives params", () => {
expect(
styleSelectParams(
{
message: "Pick channel",
options: [
{ value: "stable", label: "Stable", hint: "Tagged releases" },
{ value: "dev", label: "Dev" },
],
},
{
message: (value) => `msg:${value}`,
hint: (value) => `hint:${value}`,
},
),
).toEqual({
message: "msg:Pick channel",
options: [
{ value: "stable", label: "Stable", hint: "hint:Tagged releases" },
@@ -47,4 +25,24 @@ describe("selectStyled", () => {
],
});
});
it("keeps unhinted options unchanged", () => {
const option = { value: "dev", label: "Dev" };
const params = styleSelectParams(
{
message: "Pick channel",
options: [option],
},
{
message: (value) => `msg:${value}`,
hint: (value) => `hint:${value}`,
},
);
expect(params).toEqual({
message: "msg:Pick channel",
options: [{ value: "dev", label: "Dev" }],
});
expect(params.options[0]).toBe(option);
});
});

View File

@@ -1,12 +1,6 @@
import { select } from "@clack/prompts";
import { stylePromptHint, stylePromptMessage } from "./prompt-style.js";
import { styleSelectParams } from "./prompt-select-styled-params.js";
export function selectStyled<T>(params: Parameters<typeof select<T>>[0]) {
return select({
...params,
message: stylePromptMessage(params.message),
options: params.options.map((opt) =>
opt.hint === undefined ? opt : { ...opt, hint: stylePromptHint(opt.hint) },
),
});
return select(styleSelectParams(params));
}