UNPKG

@jungvonmatt/sb-migrate

Version:

CLI tool for managing Storyblok schema and content migrations

111 lines 4.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const vitest_1 = require("vitest"); const create_1 = require("../../../handlers/componentGroup/create"); const api_1 = require("../../../utils/api"); // Mock the API vitest_1.vi.mock("../../../utils/api", () => ({ api: { componentGroups: { getAll: vitest_1.vi.fn(), create: vitest_1.vi.fn(), }, }, })); (0, vitest_1.describe)("handleCreateComponentGroup", () => { const mockMigration = { groups: [ { name: "test-group-1" }, { name: "test-group-2" }, ], }; const mockOptions = { isDryrun: false, }; (0, vitest_1.beforeEach)(() => { vitest_1.vi.clearAllMocks(); }); (0, vitest_1.it)("should create new component groups successfully", async () => { // Arrange const mockExistingGroups = { data: { component_groups: [], }, }; const mockCreateResponse = { data: { component_group: { id: 123, }, }, }; api_1.api.componentGroups.getAll.mockResolvedValue(mockExistingGroups); api_1.api.componentGroups.create.mockResolvedValue(mockCreateResponse); // Act await (0, create_1.handleCreateComponentGroup)(mockMigration, mockOptions); // Assert (0, vitest_1.expect)(api_1.api.componentGroups.getAll).toHaveBeenCalled(); (0, vitest_1.expect)(api_1.api.componentGroups.create).toHaveBeenCalledTimes(2); (0, vitest_1.expect)(api_1.api.componentGroups.create).toHaveBeenCalledWith({ name: "test-group-1", }); (0, vitest_1.expect)(api_1.api.componentGroups.create).toHaveBeenCalledWith({ name: "test-group-2", }); }); (0, vitest_1.it)("should handle dry run correctly", async () => { // Arrange const dryRunOptions = { isDryrun: true }; // Act await (0, create_1.handleCreateComponentGroup)(mockMigration, dryRunOptions); // Assert (0, vitest_1.expect)(api_1.api.componentGroups.getAll).not.toHaveBeenCalled(); (0, vitest_1.expect)(api_1.api.componentGroups.create).not.toHaveBeenCalled(); }); (0, vitest_1.it)("should skip existing component groups", async () => { // Arrange const mockExistingGroups = { data: { component_groups: [{ name: "test-group-1" }], }, }; const mockCreateResponse = { data: { component_group: { id: 123, }, }, }; api_1.api.componentGroups.getAll.mockResolvedValue(mockExistingGroups); api_1.api.componentGroups.create.mockResolvedValue(mockCreateResponse); // Act await (0, create_1.handleCreateComponentGroup)(mockMigration, mockOptions); // Assert (0, vitest_1.expect)(api_1.api.componentGroups.getAll).toHaveBeenCalled(); (0, vitest_1.expect)(api_1.api.componentGroups.create).toHaveBeenCalledTimes(1); (0, vitest_1.expect)(api_1.api.componentGroups.create).toHaveBeenCalledWith({ name: "test-group-2", }); }); (0, vitest_1.it)("should handle API errors gracefully", async () => { // Arrange const error = new Error("API Error"); api_1.api.componentGroups.getAll.mockRejectedValue(error); // Act & Assert await (0, vitest_1.expect)((0, create_1.handleCreateComponentGroup)(mockMigration, mockOptions)).rejects.toThrow("API Error"); }); (0, vitest_1.it)("should handle creation errors gracefully", async () => { // Arrange const mockExistingGroups = { data: { component_groups: [], }, }; const error = new Error("Creation Error"); api_1.api.componentGroups.getAll.mockResolvedValue(mockExistingGroups); api_1.api.componentGroups.create.mockRejectedValue(error); // Act & Assert await (0, vitest_1.expect)((0, create_1.handleCreateComponentGroup)(mockMigration, mockOptions)).rejects.toThrow("Creation Error"); }); }); //# sourceMappingURL=create.test.js.map