@jungvonmatt/sb-migrate
Version:
CLI tool for managing Storyblok schema and content migrations
152 lines • 6.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const create_1 = require("../../../handlers/component/create");
const api_1 = require("../../../utils/api");
const migration_1 = require("../../../utils/migration");
// Mock the API and Component class
vitest_1.vi.mock("../../../utils/api", () => ({
api: {
components: {
getAll: vitest_1.vi.fn(),
},
componentGroups: {
getAll: vitest_1.vi.fn(),
},
},
}));
// Create spy functions for Component methods
const createSpy = vitest_1.vi.fn();
const saveSpy = vitest_1.vi.fn();
const addOrUpdateFieldsSpy = vitest_1.vi.fn();
// Mock the Component class with proper prototype methods
vitest_1.vi.mock("../../../utils/migration", () => ({
Component: vitest_1.vi.fn().mockImplementation(() => ({
create: createSpy,
save: saveSpy,
addOrUpdateFields: addOrUpdateFieldsSpy,
instance: {
display_name: "",
is_root: false,
is_nestable: false,
component_group_uuid: "",
},
})),
}));
(0, vitest_1.describe)("handleCreateComponent", () => {
const mockMigration = {
schema: {
name: "test-component",
display_name: "Test Component",
is_root: true,
is_nestable: false,
component_group_name: "test-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();
});
(0, vitest_1.it)("should create a new component successfully", async () => {
// Arrange
const mockComponents = { data: { components: [] } };
const mockComponentGroups = {
data: {
component_groups: [{ name: "test-group", uuid: "group-123" }],
},
};
api_1.api.components.getAll.mockResolvedValue(mockComponents);
api_1.api.componentGroups.getAll.mockResolvedValue(mockComponentGroups);
// Act
await (0, create_1.handleCreateComponent)(mockMigration, mockOptions);
// Assert
(0, vitest_1.expect)(api_1.api.components.getAll).toHaveBeenCalled();
(0, vitest_1.expect)(api_1.api.componentGroups.getAll).toHaveBeenCalled();
(0, vitest_1.expect)(migration_1.Component).toHaveBeenCalled();
(0, vitest_1.expect)(createSpy).toHaveBeenCalledWith("test-component");
(0, vitest_1.expect)(saveSpy).toHaveBeenCalled();
});
(0, vitest_1.it)("should not create component if it already exists", async () => {
// Arrange
const mockComponents = {
data: {
components: [{ name: "test-component" }],
},
};
api_1.api.components.getAll.mockResolvedValue(mockComponents);
// Act
await (0, create_1.handleCreateComponent)(mockMigration, mockOptions);
// Assert
(0, vitest_1.expect)(api_1.api.components.getAll).toHaveBeenCalled();
(0, vitest_1.expect)(createSpy).not.toHaveBeenCalled();
(0, vitest_1.expect)(saveSpy).not.toHaveBeenCalled();
});
(0, vitest_1.it)("should handle dry run correctly", async () => {
// Arrange
const dryRunOptions = { isDryrun: true };
// Act
await (0, create_1.handleCreateComponent)(mockMigration, dryRunOptions);
// Assert
(0, vitest_1.expect)(api_1.api.components.getAll).not.toHaveBeenCalled();
(0, vitest_1.expect)(createSpy).not.toHaveBeenCalled();
(0, vitest_1.expect)(saveSpy).not.toHaveBeenCalled();
});
(0, vitest_1.it)("should handle component group assignment correctly", async () => {
// Arrange
const mockComponents = { data: { components: [] } };
const mockComponentGroups = {
data: {
component_groups: [{ name: "test-group", uuid: "group-123" }],
},
};
api_1.api.components.getAll.mockResolvedValue(mockComponents);
api_1.api.componentGroups.getAll.mockResolvedValue(mockComponentGroups);
// Act
await (0, create_1.handleCreateComponent)(mockMigration, mockOptions);
// Assert
const componentInstance = migration_1.Component.mock.results[0]?.value;
(0, vitest_1.expect)(componentInstance?.instance.component_group_uuid).toBe("group-123");
});
(0, vitest_1.it)("should handle schema fields and tabs correctly", async () => {
// Arrange
const mockComponents = { data: { components: [] } };
const mockComponentGroups = {
data: {
component_groups: [{ name: "test-group", uuid: "group-123" }],
},
};
api_1.api.components.getAll.mockResolvedValue(mockComponents);
api_1.api.componentGroups.getAll.mockResolvedValue(mockComponentGroups);
// Act
await (0, create_1.handleCreateComponent)(mockMigration, mockOptions);
// Assert
(0, vitest_1.expect)(addOrUpdateFieldsSpy).toHaveBeenCalledWith(mockMigration.schema.schema, {
general: ["field1"],
advanced: ["field2"],
});
});
(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, create_1.handleCreateComponent)(mockMigration, mockOptions)).rejects.toThrow("API Error");
});
});
//# sourceMappingURL=create.test.js.map