UNPKG

@jungvonmatt/sb-migrate

Version:

CLI tool for managing Storyblok schema and content migrations

132 lines 5.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const vitest_1 = require("vitest"); const delete_1 = require("../../../handlers/componentGroup/delete"); const api_1 = require("../../../utils/api"); // Mock the API vitest_1.vi.mock("../../../utils/api", () => ({ api: { componentGroups: { getAll: vitest_1.vi.fn(), delete: vitest_1.vi.fn(), }, }, })); (0, vitest_1.describe)("handleDeleteComponentGroup", () => { const mockMigration = { id: 123, }; const mockOptions = { isDryrun: false, }; (0, vitest_1.beforeEach)(() => { vitest_1.vi.clearAllMocks(); }); (0, vitest_1.it)("should delete a component group successfully by ID", async () => { // Arrange const mockExistingGroups = { data: { component_groups: [ { id: 123, name: "test-group", }, ], }, }; api_1.api.componentGroups.getAll.mockResolvedValue(mockExistingGroups); api_1.api.componentGroups.delete.mockResolvedValue({}); // Act await (0, delete_1.handleDeleteComponentGroup)(mockMigration, mockOptions); // Assert (0, vitest_1.expect)(api_1.api.componentGroups.getAll).toHaveBeenCalled(); (0, vitest_1.expect)(api_1.api.componentGroups.delete).toHaveBeenCalledWith(123); }); (0, vitest_1.it)("should delete a component group successfully by name", async () => { // Arrange const mockMigrationByName = { id: "test-group", }; const mockExistingGroups = { data: { component_groups: [ { id: 123, name: "test-group", }, ], }, }; api_1.api.componentGroups.getAll.mockResolvedValue(mockExistingGroups); api_1.api.componentGroups.delete.mockResolvedValue({}); // Act await (0, delete_1.handleDeleteComponentGroup)(mockMigrationByName, mockOptions); // Assert (0, vitest_1.expect)(api_1.api.componentGroups.getAll).toHaveBeenCalled(); (0, vitest_1.expect)(api_1.api.componentGroups.delete).toHaveBeenCalledWith(123); }); (0, vitest_1.it)("should handle dry run correctly", async () => { // Arrange const dryRunOptions = { isDryrun: true }; // Act await (0, delete_1.handleDeleteComponentGroup)(mockMigration, dryRunOptions); // Assert (0, vitest_1.expect)(api_1.api.componentGroups.getAll).not.toHaveBeenCalled(); (0, vitest_1.expect)(api_1.api.componentGroups.delete).not.toHaveBeenCalled(); }); (0, vitest_1.it)("should throw error if component group does not exist", async () => { // Arrange const mockExistingGroups = { data: { component_groups: [], }, }; api_1.api.componentGroups.getAll.mockResolvedValue(mockExistingGroups); // Act & Assert await (0, vitest_1.expect)((0, delete_1.handleDeleteComponentGroup)(mockMigration, mockOptions)).rejects.toThrow('Component group "123" not found'); (0, vitest_1.expect)(api_1.api.componentGroups.delete).not.toHaveBeenCalled(); }); (0, vitest_1.it)("should throw error if component group has no ID", async () => { // Arrange const mockExistingGroups = { data: { component_groups: [ { name: "test-group", // Missing id }, ], }, }; api_1.api.componentGroups.getAll.mockResolvedValue(mockExistingGroups); // Act & Assert await (0, vitest_1.expect)((0, delete_1.handleDeleteComponentGroup)({ id: "test-group" }, mockOptions)).rejects.toThrow('Component group "test-group" has no ID'); (0, vitest_1.expect)(api_1.api.componentGroups.delete).not.toHaveBeenCalled(); }); (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, delete_1.handleDeleteComponentGroup)(mockMigration, mockOptions)).rejects.toThrow("API Error"); }); (0, vitest_1.it)("should handle delete errors gracefully", async () => { // Arrange const mockExistingGroups = { data: { component_groups: [ { id: 123, name: "test-group", }, ], }, }; const error = new Error("Delete Error"); api_1.api.componentGroups.getAll.mockResolvedValue(mockExistingGroups); api_1.api.componentGroups.delete.mockRejectedValue(error); // Act & Assert await (0, vitest_1.expect)((0, delete_1.handleDeleteComponentGroup)(mockMigration, mockOptions)).rejects.toThrow("Delete Error"); }); }); //# sourceMappingURL=delete.test.js.map