UNPKG

@jungvonmatt/sb-migrate

Version:

CLI tool for managing Storyblok schema and content migrations

74 lines 3.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const vitest_1 = require("vitest"); const pull_components_1 = require("../../commands/pull-components"); const config_1 = require("../../utils/config"); const login_1 = require("../../commands/login"); const child_process_1 = require("child_process"); // Mock dependencies vitest_1.vi.mock("../../utils/config"); vitest_1.vi.mock("../../commands/login"); vitest_1.vi.mock("child_process"); vitest_1.vi.mock("picocolors", () => ({ default: { green: vitest_1.vi.fn((str) => str), red: vitest_1.vi.fn((str) => str), blue: vitest_1.vi.fn((str) => str), }, })); (0, vitest_1.describe)("pullComponents", () => { const mockSpaceId = "12345"; const mockConfig = { spaceId: mockSpaceId }; (0, vitest_1.beforeEach)(() => { vitest_1.vi.clearAllMocks(); login_1.isStoryblokCliInstalled.mockReturnValue(true); config_1.loadConfig.mockResolvedValue(mockConfig); child_process_1.execSync.mockReturnValue(Buffer.from("")); // Mock process.exit to throw an error instead of exiting vitest_1.vi.spyOn(process, "exit").mockImplementation((code) => { throw new Error(`Process.exit called with code ${code}`); }); }); (0, vitest_1.afterEach)(() => { vitest_1.vi.resetAllMocks(); }); (0, vitest_1.it)("should successfully pull components when all requirements are met", async () => { // Act & Assert await (0, pull_components_1.pullComponents)(); (0, vitest_1.expect)(login_1.isStoryblokCliInstalled).toHaveBeenCalled(); (0, vitest_1.expect)(config_1.loadConfig).toHaveBeenCalled(); (0, vitest_1.expect)(child_process_1.execSync).toHaveBeenCalledWith(`storyblok pull-components --space ${mockSpaceId}`, { stdio: "inherit" }); }); (0, vitest_1.it)("should exit with error when Storyblok CLI is not installed", async () => { // Arrange login_1.isStoryblokCliInstalled.mockReturnValue(false); // Act & Assert await (0, vitest_1.expect)((0, pull_components_1.pullComponents)()).rejects.toThrow("Process.exit called with code 1"); (0, vitest_1.expect)(config_1.loadConfig).not.toHaveBeenCalled(); (0, vitest_1.expect)(child_process_1.execSync).not.toHaveBeenCalled(); }); (0, vitest_1.it)("should exit with error when no Space ID is found in config", async () => { // Arrange config_1.loadConfig.mockResolvedValue({}); // Act & Assert await (0, vitest_1.expect)((0, pull_components_1.pullComponents)()).rejects.toThrow("Process.exit called with code 1"); (0, vitest_1.expect)(child_process_1.execSync).not.toHaveBeenCalled(); }); (0, vitest_1.it)("should exit with error when component pull operation fails", async () => { // Arrange const mockError = new Error("Pull failed"); child_process_1.execSync.mockImplementation(() => { throw mockError; }); // Act & Assert await (0, vitest_1.expect)((0, pull_components_1.pullComponents)()).rejects.toThrow("Process.exit called with code 1"); }); (0, vitest_1.it)("should handle config loading errors gracefully", async () => { // Arrange config_1.loadConfig.mockRejectedValue(new Error("Config error")); // Act & Assert await (0, vitest_1.expect)((0, pull_components_1.pullComponents)()).rejects.toThrow("Process.exit called with code 1"); (0, vitest_1.expect)(child_process_1.execSync).not.toHaveBeenCalled(); }); }); //# sourceMappingURL=pull-components.test.js.map