|
1 | 1 | const { |
2 | 2 | INDENT, |
3 | 3 | MAX_WIDTH, |
| 4 | + frame, |
4 | 5 | parseEnvinfoSections, |
5 | 6 | renderAliasHelp, |
| 7 | + renderCommandHeader, |
6 | 8 | renderCommandHelp, |
7 | 9 | renderError, |
8 | 10 | renderFooter, |
@@ -662,3 +664,134 @@ describe("renderFooter", () => { |
662 | 664 | expect(captured).toMatchSnapshot(); |
663 | 665 | }); |
664 | 666 | }); |
| 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