UNPKG

@jungvonmatt/sb-migrate

Version:

CLI tool for managing Storyblok schema and content migrations

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