Skip to content

Commit 6cc7a1f

Browse files
committed
test: add tests for HumanRenderer factory and finalize()
- Factory creates fresh renderer per config.human() call - finalize(hint) returns combined footer + hint string - stateless() wrapper has no finalize method
1 parent c58f49a commit 6cc7a1f

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

test/lib/formatters/output.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,4 +432,58 @@ describe("renderCommandOutput", () => {
432432
// jsonTransform output, not jsonExclude
433433
expect(parsed).toEqual({ transformed: true, id: 1 });
434434
});
435+
436+
test("human factory creates fresh renderer per resolve", () => {
437+
const calls: number[] = [];
438+
const config: OutputConfig<{ n: number }> = {
439+
json: true,
440+
human: () => ({
441+
render: (d) => {
442+
calls.push(d.n);
443+
return `#${d.n}`;
444+
},
445+
}),
446+
};
447+
448+
// First resolve + render
449+
const r1 = config.human();
450+
r1.render({ n: 1 });
451+
452+
// Second resolve = fresh renderer
453+
const r2 = config.human();
454+
r2.render({ n: 2 });
455+
456+
expect(calls).toEqual([1, 2]);
457+
});
458+
459+
test("finalize is called with hint and output is written", () => {
460+
const w = createTestWriter();
461+
const config: OutputConfig<{ value: string }> = {
462+
json: true,
463+
human: () => ({
464+
render: (d) => `[${d.value}]`,
465+
finalize: (hint) => `=== END ===${hint ? `\n${hint}` : ""}`,
466+
}),
467+
};
468+
469+
const renderer = config.human();
470+
renderCommandOutput(w, { value: "test" }, config, renderer, {
471+
json: false,
472+
});
473+
expect(w.output).toBe("[test]\n");
474+
475+
// Simulate finalize
476+
const footer = renderer.finalize?.("Done.");
477+
expect(footer).toBe("=== END ===\nDone.");
478+
});
479+
480+
test("stateless renderer has no finalize method", () => {
481+
const config: OutputConfig<string> = {
482+
json: true,
483+
human: stateless((s) => s.toUpperCase()),
484+
};
485+
const renderer = config.human();
486+
expect(renderer.render("hello")).toBe("HELLO");
487+
expect(renderer.finalize).toBeUndefined();
488+
});
435489
});

0 commit comments

Comments
 (0)