Skip to content

Commit aca93fd

Browse files
test(ui): add test cases for frame and update snapshot
1 parent 540290f commit aca93fd

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed

test/api/__snapshots__/ui-renderer.test.js.snap.webpack5

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

3+
exports[`frame should match snapshot 1`] = `
4+
[
5+
"",
6+
" ⬡ webpack info",
7+
" ──────────────────────────────────────────────────────────────────────────",
8+
" System and environment information.",
9+
"",
10+
" some content",
11+
" ℹ Run 'webpack --help=verbose' to see all available commands and options.",
12+
"",
13+
" Webpack documentation: https://webpack.js.org/",
14+
" CLI documentation: https://webpack.js.org/api/cli/",
15+
" Made with ♥ by the webpack team",
16+
"",
17+
]
18+
`;
19+
320
exports[`parseEnvinfoSections should match snapshot 1`] = `
421
[
522
{

test/api/ui-renderer.test.js

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
const {
22
INDENT,
33
MAX_WIDTH,
4+
frame,
45
parseEnvinfoSections,
56
renderAliasHelp,
7+
renderCommandHeader,
68
renderCommandHelp,
79
renderError,
810
renderFooter,
@@ -662,3 +664,134 @@ describe("renderFooter", () => {
662664
expect(captured).toMatchSnapshot();
663665
});
664666
});
667+
668+
describe("renderCommandHeader", () => {
669+
it("should render 'webpack <name>' in the header", () => {
670+
const { captured, opts } = makeOpts();
671+
renderCommandHeader({ name: "version", description: "Installed package versions." }, opts);
672+
expect(getOutput(captured)).toContain("webpack version");
673+
});
674+
675+
it("should render the description", () => {
676+
const { captured, opts } = makeOpts();
677+
renderCommandHeader({ name: "version", description: "Installed package versions." }, opts);
678+
expect(getOutput(captured)).toContain("Installed package versions.");
679+
});
680+
681+
it("should render a divider", () => {
682+
const { captured, opts } = makeOpts();
683+
renderCommandHeader({ name: "version", description: "Installed package versions." }, opts);
684+
expect(captured.some((l) => /{10,}/.test(l))).toBe(true);
685+
});
686+
687+
it("should include the ⬡ icon", () => {
688+
const { captured, opts } = makeOpts();
689+
renderCommandHeader({ name: "version", description: "Installed package versions." }, opts);
690+
expect(getOutput(captured)).toContain("⬡");
691+
});
692+
});
693+
694+
describe("frame", () => {
695+
it("should render the command header", async () => {
696+
const { captured, opts } = makeOpts();
697+
await frame(
698+
{ name: "info", description: "System and environment information." },
699+
() => {},
700+
opts,
701+
);
702+
expect(getOutput(captured)).toContain("webpack info");
703+
});
704+
705+
it("should render the description in the header", async () => {
706+
const { captured, opts } = makeOpts();
707+
await frame(
708+
{ name: "info", description: "System and environment information." },
709+
() => {},
710+
opts,
711+
);
712+
expect(getOutput(captured)).toContain("System and environment information.");
713+
});
714+
715+
it("should render the footer", async () => {
716+
const { captured, opts } = makeOpts();
717+
await frame(
718+
{ name: "info", description: "System and environment information." },
719+
() => {},
720+
opts,
721+
);
722+
expect(getOutput(captured)).toContain("https://webpack.js.org/");
723+
});
724+
725+
it("should call the fn callback", async () => {
726+
const { opts } = makeOpts();
727+
let called = false;
728+
await frame(
729+
{ name: "info", description: "System and environment information." },
730+
() => {
731+
called = true;
732+
},
733+
opts,
734+
);
735+
expect(called).toBe(true);
736+
});
737+
738+
it("should pass opts to the fn callback", async () => {
739+
const { opts } = makeOpts();
740+
let receivedOpts;
741+
await frame(
742+
{ name: "info", description: "System and environment information." },
743+
(option) => {
744+
receivedOpts = option;
745+
},
746+
opts,
747+
);
748+
expect(receivedOpts).toBe(opts);
749+
});
750+
751+
it("should await an async fn callback", async () => {
752+
const { captured, opts } = makeOpts();
753+
await frame(
754+
{ name: "info", description: "System and environment information." },
755+
async (option) => {
756+
await Promise.resolve();
757+
option.log("async content");
758+
},
759+
opts,
760+
);
761+
expect(getOutput(captured)).toContain("async content");
762+
});
763+
764+
it("should render header before fn output and footer after", async () => {
765+
const { captured, opts } = makeOpts();
766+
await frame(
767+
{ name: "info", description: "System and environment information." },
768+
(option) => {
769+
option.log("fn output");
770+
},
771+
opts,
772+
);
773+
const headerIdx = captured.findIndex((l) => l.includes("webpack info"));
774+
const fnIdx = captured.findIndex((l) => l.includes("fn output"));
775+
const footerIdx = captured.findIndex((l) => l.includes("https://webpack.js.org/"));
776+
expect(headerIdx).toBeLessThan(fnIdx);
777+
expect(fnIdx).toBeLessThan(footerIdx);
778+
});
779+
780+
it("should work without a description", async () => {
781+
const { captured, opts } = makeOpts();
782+
await frame({ name: "info" }, () => {}, opts);
783+
expect(getOutput(captured)).toContain("webpack info");
784+
});
785+
786+
it("should match snapshot", async () => {
787+
const { captured, opts } = makeOpts();
788+
await frame(
789+
{ name: "info", description: "System and environment information." },
790+
(option) => {
791+
option.log(" some content");
792+
},
793+
opts,
794+
);
795+
expect(captured).toMatchSnapshot();
796+
});
797+
});

0 commit comments

Comments
 (0)