|
7 | 7 | runMain, |
8 | 8 | showUsage, |
9 | 9 | } from "../src"; |
10 | | -import * as mainModule from "../src/main"; |
11 | 10 | import * as commandModule from "../src/command"; |
12 | 11 |
|
13 | 12 | describe("runMain", () => { |
@@ -120,6 +119,70 @@ describe("runMain", () => { |
120 | 119 | }); |
121 | 120 | }); |
122 | 121 |
|
| 122 | +describe("sub command", () => { |
| 123 | + it("runs the sub command", async () => { |
| 124 | + const setupMock = vi.fn(); |
| 125 | + const runMock = vi.fn(); |
| 126 | + const cleanupMock = vi.fn(); |
| 127 | + |
| 128 | + const command = defineCommand({ |
| 129 | + subCommands: { |
| 130 | + foo: { |
| 131 | + args: { |
| 132 | + bar: { |
| 133 | + type: "positional", |
| 134 | + }, |
| 135 | + }, |
| 136 | + setup: async ({ args }) => { |
| 137 | + setupMock(args.bar); |
| 138 | + }, |
| 139 | + cleanup: async ({ args }) => { |
| 140 | + cleanupMock(args.bar); |
| 141 | + }, |
| 142 | + run: async ({ args }) => { |
| 143 | + runMock(args.bar); |
| 144 | + }, |
| 145 | + }, |
| 146 | + }, |
| 147 | + }); |
| 148 | + |
| 149 | + await runMain(command, { rawArgs: ["foo", "bar"] }); |
| 150 | + |
| 151 | + expect(setupMock).toHaveBeenCalledOnce(); |
| 152 | + expect(setupMock).toHaveBeenCalledWith("bar"); |
| 153 | + expect(runMock).toHaveBeenCalledOnce(); |
| 154 | + expect(runMock).toHaveBeenCalledWith("bar"); |
| 155 | + expect(cleanupMock).toHaveBeenCalledOnce(); |
| 156 | + expect(cleanupMock).toHaveBeenCalledWith("bar"); |
| 157 | + }); |
| 158 | +}); |
| 159 | + |
| 160 | +describe("resolveSubCommand", () => { |
| 161 | + it("resolves the sub command", async () => { |
| 162 | + const command = defineCommand({ |
| 163 | + subCommands: { |
| 164 | + foo: { |
| 165 | + args: { |
| 166 | + bar: { |
| 167 | + type: "positional", |
| 168 | + }, |
| 169 | + }, |
| 170 | + }, |
| 171 | + }, |
| 172 | + }); |
| 173 | + |
| 174 | + const [subCommand] = await commandModule.resolveSubCommand(command, [ |
| 175 | + "foo", |
| 176 | + "bar", |
| 177 | + ]); |
| 178 | + |
| 179 | + expect(subCommand).toBeDefined(); |
| 180 | + expect(subCommand.args).toEqual({ |
| 181 | + bar: { type: "positional" }, |
| 182 | + }); |
| 183 | + }); |
| 184 | +}); |
| 185 | + |
123 | 186 | describe("createMain", () => { |
124 | 187 | it("creates and returns a function", () => { |
125 | 188 | const main = createMain(defineCommand({})); |
|
0 commit comments