|
| 1 | +const path = require("path"); |
| 2 | + |
| 3 | +const utilsDirectory = { |
| 4 | + cli: "../../../packages/webpack-cli/lib/utils", |
| 5 | + generators: "../../../packages/generators/src/utils", |
| 6 | +}; |
| 7 | + |
| 8 | +jest.setMock(path.join(utilsDirectory.cli, "get-available-installers"), jest.fn()); |
| 9 | +jest.mock(path.join(utilsDirectory.generators, "scaffold-utils"), () => ({ |
| 10 | + List: jest.fn(), |
| 11 | +})); |
| 12 | + |
| 13 | +const getAvailableInstallers = require(path.join(utilsDirectory.cli, "get-available-installers")); |
| 14 | +const getPackageManager = require(path.join(utilsDirectory.cli, "get-package-manager")); |
| 15 | +const logger = require(path.join(utilsDirectory.cli, "logger")); |
| 16 | + |
| 17 | +const { getInstaller, getTemplate } = require(path.join(utilsDirectory.generators, "helpers")); |
| 18 | +const { List } = require(path.join(utilsDirectory.generators, "scaffold-utils")); |
| 19 | + |
| 20 | +const context = { |
| 21 | + prompt: () => {}, |
| 22 | + supportedTemplates: ["default"], |
| 23 | + utils: { |
| 24 | + getAvailableInstallers, |
| 25 | + getPackageManager, |
| 26 | + logger, |
| 27 | + }, |
| 28 | +}; |
| 29 | + |
| 30 | +describe("helpers", () => { |
| 31 | + it("getInstaller() returns the available installer", async () => { |
| 32 | + // Multiple installers are not available |
| 33 | + getAvailableInstallers.mockReturnValue(["npm"]); |
| 34 | + |
| 35 | + // Invoke the helper function |
| 36 | + const installer = await getInstaller.call(context); |
| 37 | + expect(installer).toBe("npm"); |
| 38 | + }); |
| 39 | + |
| 40 | + it("getInstaller() invokes a List prompt if multiple installers are available", async () => { |
| 41 | + // Multiple installers are available |
| 42 | + getAvailableInstallers.mockReturnValue(["npm", "yarn", "pnpm"]); |
| 43 | + |
| 44 | + // User chose "pnpm" |
| 45 | + List.mockReturnValue({ packager: "pnpm" }); |
| 46 | + |
| 47 | + // Invoke the helper function |
| 48 | + const installer = await getInstaller.call(context); |
| 49 | + expect(installer).toBe("pnpm"); |
| 50 | + }); |
| 51 | + |
| 52 | + it("getTemplate() returns with the valid template", async () => { |
| 53 | + context.template = "default"; |
| 54 | + |
| 55 | + // Invoke the helper function |
| 56 | + const template = await getTemplate.call(context); |
| 57 | + expect(template).toBe("default"); |
| 58 | + }); |
| 59 | + |
| 60 | + it("getTemplate() invokes a List prompt on supplying an invalid template", async () => { |
| 61 | + context.template = "unknown"; |
| 62 | + |
| 63 | + // User chose "default" |
| 64 | + List.mockReturnValue({ selectedTemplate: "default" }); |
| 65 | + |
| 66 | + const loggerMock = jest.spyOn(logger, "warn").mockImplementation(() => {}); |
| 67 | + |
| 68 | + // Invoke the helper function` |
| 69 | + const template = await getTemplate.call(context); |
| 70 | + expect(template).toBe("default"); |
| 71 | + expect(loggerMock).toHaveBeenCalled(); |
| 72 | + }); |
| 73 | +}); |
0 commit comments