UNPKG

@jungvonmatt/sb-migrate

Version:

CLI tool for managing Storyblok schema and content migrations

93 lines 3.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const vitest_1 = require("vitest"); const delete_1 = require("../../../handlers/component/delete"); const api_1 = require("../../../utils/api"); // Mock the API vitest_1.vi.mock("../../../utils/api", () => ({ api: { components: { getAll: vitest_1.vi.fn(), delete: vitest_1.vi.fn(), }, }, })); (0, vitest_1.describe)("handleDeleteComponent", () => { const mockMigration = { name: "test-component", }; const mockOptions = { isDryrun: false, }; (0, vitest_1.beforeEach)(() => { vitest_1.vi.clearAllMocks(); }); (0, vitest_1.it)("should delete a component successfully", async () => { // Arrange const mockComponents = { data: { components: [ { id: 123, name: "test-component" }, { id: 456, name: "other-component" }, ], }, }; api_1.api.components.getAll.mockResolvedValue(mockComponents); api_1.api.components.delete.mockResolvedValue({}); // Act await (0, delete_1.handleDeleteComponent)(mockMigration, mockOptions); // Assert (0, vitest_1.expect)(api_1.api.components.getAll).toHaveBeenCalled(); (0, vitest_1.expect)(api_1.api.components.delete).toHaveBeenCalledWith(123); }); (0, vitest_1.it)("should handle dry run correctly", async () => { // Arrange const dryRunOptions = { isDryrun: true }; // Act await (0, delete_1.handleDeleteComponent)(mockMigration, dryRunOptions); // Assert (0, vitest_1.expect)(api_1.api.components.getAll).not.toHaveBeenCalled(); (0, vitest_1.expect)(api_1.api.components.delete).not.toHaveBeenCalled(); }); (0, vitest_1.it)("should throw error if component does not exist", async () => { // Arrange const mockComponents = { data: { components: [{ id: 456, name: "other-component" }], }, }; api_1.api.components.getAll.mockResolvedValue(mockComponents); // Act & Assert await (0, vitest_1.expect)((0, delete_1.handleDeleteComponent)(mockMigration, mockOptions)).rejects.toThrow('Component "test-component" not found'); (0, vitest_1.expect)(api_1.api.components.delete).not.toHaveBeenCalled(); }); (0, vitest_1.it)("should handle API errors gracefully", async () => { // Arrange const error = new Error("API Error"); api_1.api.components.getAll.mockRejectedValue(error); // Act & Assert await (0, vitest_1.expect)((0, delete_1.handleDeleteComponent)(mockMigration, mockOptions)).rejects.toThrow("API Error"); }); (0, vitest_1.it)("should find component by ID if name is a number", async () => { // Arrange const mockMigrationWithId = { name: 123, }; const mockComponents = { data: { components: [ { id: 123, name: "test-component" }, { id: 456, name: "other-component" }, ], }, }; api_1.api.components.getAll.mockResolvedValue(mockComponents); api_1.api.components.delete.mockResolvedValue({}); // Act await (0, delete_1.handleDeleteComponent)(mockMigrationWithId, mockOptions); // Assert (0, vitest_1.expect)(api_1.api.components.getAll).toHaveBeenCalled(); (0, vitest_1.expect)(api_1.api.components.delete).toHaveBeenCalledWith(123); }); }); //# sourceMappingURL=delete.test.js.map