UNPKG

@jungvonmatt/sb-migrate

Version:

CLI tool for managing Storyblok schema and content migrations

211 lines 8.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const vitest_1 = require("vitest"); const update_1 = require("../../../handlers/component/update"); const api_1 = require("../../../utils/api"); const migration_1 = require("../../../utils/migration"); const storyblok_1 = require("../../../utils/storyblok"); // Mock the API and helper functions vitest_1.vi.mock("../../../utils/api", () => ({ api: { components: { getAll: vitest_1.vi.fn(), }, componentGroups: { getAll: vitest_1.vi.fn(), }, }, })); vitest_1.vi.mock("../../../utils/migration", () => ({ helper: { updateComponent: vitest_1.vi.fn(), }, })); vitest_1.vi.mock("../../../utils/storyblok", () => ({ createRollbackFile: vitest_1.vi.fn(), })); // Create spy functions for Component methods const addOrUpdateFieldsSpy = vitest_1.vi.fn(); const saveSpy = vitest_1.vi.fn(); // Mock the Component instance const mockComponent = { addOrUpdateFields: addOrUpdateFieldsSpy, save: saveSpy, instance: { id: 123, name: "test-component", schema: {}, display_name: "Old Display Name", is_root: false, is_nestable: true, component_group_uuid: "old-group-uuid", }, }; (0, vitest_1.describe)("handleUpdateComponent", () => { const mockMigration = { schema: { name: "test-component", display_name: "New Display Name", is_root: true, is_nestable: false, component_group_name: "new-group", schema: { field1: { type: "text", display_name: "Field 1", }, field2: { type: "text", display_name: "Field 2", }, }, tabs: { general: ["field1"], advanced: ["field2"], }, }, }; const mockOptions = { isDryrun: false, }; (0, vitest_1.beforeEach)(() => { vitest_1.vi.clearAllMocks(); // Reset the mock component instance before each test mockComponent.instance = { id: 123, name: "test-component", schema: {}, display_name: "Old Display Name", is_root: false, is_nestable: true, component_group_uuid: "old-group-uuid", }; migration_1.helper.updateComponent.mockResolvedValue(mockComponent); }); (0, vitest_1.it)("should update a component successfully", async () => { // Arrange const mockComponents = { data: { components: [{ name: "test-component" }], }, }; const mockComponentGroups = { data: { component_groups: [{ name: "new-group", uuid: "new-group-uuid" }], }, }; api_1.api.components.getAll.mockResolvedValue(mockComponents); api_1.api.componentGroups.getAll.mockResolvedValue(mockComponentGroups); // Act await (0, update_1.handleUpdateComponent)(mockMigration, mockOptions); // Assert (0, vitest_1.expect)(api_1.api.components.getAll).toHaveBeenCalled(); (0, vitest_1.expect)(migration_1.helper.updateComponent).toHaveBeenCalledWith("test-component"); (0, vitest_1.expect)(addOrUpdateFieldsSpy).toHaveBeenCalledWith(mockMigration.schema.schema, { general: ["field1"], advanced: ["field2"], }); (0, vitest_1.expect)(saveSpy).toHaveBeenCalled(); (0, vitest_1.expect)(storyblok_1.createRollbackFile).toHaveBeenCalled(); }); (0, vitest_1.it)("should handle dry run correctly", async () => { // Arrange const dryRunOptions = { isDryrun: true }; // Act await (0, update_1.handleUpdateComponent)(mockMigration, dryRunOptions); // Assert (0, vitest_1.expect)(api_1.api.components.getAll).not.toHaveBeenCalled(); (0, vitest_1.expect)(migration_1.helper.updateComponent).not.toHaveBeenCalled(); (0, vitest_1.expect)(addOrUpdateFieldsSpy).not.toHaveBeenCalled(); (0, vitest_1.expect)(saveSpy).not.toHaveBeenCalled(); (0, vitest_1.expect)(storyblok_1.createRollbackFile).not.toHaveBeenCalled(); }); (0, vitest_1.it)("should throw error if component does not exist", async () => { // Arrange const mockComponents = { data: { components: [], }, }; api_1.api.components.getAll.mockResolvedValue(mockComponents); // Act & Assert await (0, vitest_1.expect)((0, update_1.handleUpdateComponent)(mockMigration, mockOptions)).rejects.toThrow('Component "test-component" not found'); (0, vitest_1.expect)(migration_1.helper.updateComponent).not.toHaveBeenCalled(); (0, vitest_1.expect)(saveSpy).not.toHaveBeenCalled(); }); (0, vitest_1.it)("should handle component group updates correctly", async () => { // Arrange const mockComponents = { data: { components: [{ name: "test-component" }], }, }; const mockComponentGroups = { data: { component_groups: [{ name: "new-group", uuid: "new-group-uuid" }], }, }; api_1.api.components.getAll.mockResolvedValue(mockComponents); api_1.api.componentGroups.getAll.mockResolvedValue(mockComponentGroups); // Act await (0, update_1.handleUpdateComponent)(mockMigration, mockOptions); // Assert (0, vitest_1.expect)(mockComponent.instance.component_group_uuid).toBe("new-group-uuid"); }); (0, vitest_1.it)("should handle non-existent component group gracefully", async () => { // Arrange const mockComponents = { data: { components: [{ name: "test-component" }], }, }; const mockComponentGroups = { data: { component_groups: [], // Empty array to simulate no matching group }, }; api_1.api.components.getAll.mockResolvedValue(mockComponents); api_1.api.componentGroups.getAll.mockResolvedValue(mockComponentGroups); // Act await (0, update_1.handleUpdateComponent)(mockMigration, mockOptions); // Assert (0, vitest_1.expect)(mockComponent.instance.component_group_uuid).toBe("old-group-uuid"); }); (0, vitest_1.it)("should create rollback file with correct data", async () => { // Arrange const mockComponents = { data: { components: [{ name: "test-component" }], }, }; const mockComponentGroups = { data: { component_groups: [{ name: "new-group", uuid: "new-group-uuid" }], }, }; // Create a copy of the original state for rollback const originalState = { id: mockComponent.instance.id, name: mockComponent.instance.name, schema: { ...mockComponent.instance.schema }, display_name: mockComponent.instance.display_name, is_root: mockComponent.instance.is_root, is_nestable: mockComponent.instance.is_nestable, component_group_uuid: mockComponent.instance.component_group_uuid, }; api_1.api.components.getAll.mockResolvedValue(mockComponents); api_1.api.componentGroups.getAll.mockResolvedValue(mockComponentGroups); // Act await (0, update_1.handleUpdateComponent)(mockMigration, mockOptions); // Assert (0, vitest_1.expect)(storyblok_1.createRollbackFile).toHaveBeenCalledWith([originalState], "component_test_component", "update"); }); (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, update_1.handleUpdateComponent)(mockMigration, mockOptions)).rejects.toThrow("API Error"); }); }); //# sourceMappingURL=update.test.js.map