@jungvonmatt/sb-migrate
Version:
CLI tool for managing Storyblok schema and content migrations
107 lines • 4.27 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const delete_1 = require("../../../handlers/story/delete");
const api_1 = require("../../../utils/api");
// Mock the API
vitest_1.vi.mock("../../../utils/api", () => ({
api: {
stories: {
get: vitest_1.vi.fn(),
getBySlug: vitest_1.vi.fn(),
delete: vitest_1.vi.fn(),
},
},
}));
(0, vitest_1.describe)("handleDeleteStory", () => {
const mockStory = {
id: 123,
name: "Test Story",
slug: "test-story",
full_slug: "test-story",
};
const mockMigration = {
id: 123,
};
const mockOptions = {
isDryrun: false,
};
(0, vitest_1.beforeEach)(() => {
vitest_1.vi.clearAllMocks();
});
(0, vitest_1.it)("should delete a story by ID successfully", async () => {
// Arrange
api_1.api.stories.get.mockResolvedValue({
data: { story: mockStory },
});
api_1.api.stories.delete.mockResolvedValue({});
// Act
await (0, delete_1.handleDeleteStory)(mockMigration, mockOptions);
// Assert
(0, vitest_1.expect)(api_1.api.stories.get).toHaveBeenCalledWith(123);
(0, vitest_1.expect)(api_1.api.stories.delete).toHaveBeenCalledWith(123);
});
(0, vitest_1.it)("should delete a story by slug successfully", async () => {
// Arrange
const migrationWithSlug = {
id: "test-story",
};
api_1.api.stories.getBySlug.mockResolvedValue(mockStory);
api_1.api.stories.delete.mockResolvedValue({});
// Act
await (0, delete_1.handleDeleteStory)(migrationWithSlug, mockOptions);
// Assert
(0, vitest_1.expect)(api_1.api.stories.getBySlug).toHaveBeenCalledWith("test-story");
(0, vitest_1.expect)(api_1.api.stories.delete).toHaveBeenCalledWith(123);
});
(0, vitest_1.it)("should handle dry run correctly", async () => {
// Arrange
const dryRunOptions = { isDryrun: true };
// Act
await (0, delete_1.handleDeleteStory)(mockMigration, dryRunOptions);
// Assert
(0, vitest_1.expect)(api_1.api.stories.get).not.toHaveBeenCalled();
(0, vitest_1.expect)(api_1.api.stories.delete).not.toHaveBeenCalled();
});
(0, vitest_1.it)("should throw error if story not found", async () => {
// Arrange
api_1.api.stories.get.mockResolvedValue({
data: { story: null },
});
// Act & Assert
await (0, vitest_1.expect)((0, delete_1.handleDeleteStory)(mockMigration, mockOptions)).rejects.toThrow('Story "123" not found');
(0, vitest_1.expect)(api_1.api.stories.delete).not.toHaveBeenCalled();
});
(0, vitest_1.it)("should throw error if story has no ID", async () => {
// Arrange
const storyWithoutId = {
name: "Test Story",
slug: "test-story",
full_slug: "test-story",
};
api_1.api.stories.get.mockResolvedValue({
data: { story: storyWithoutId },
});
// Act & Assert
await (0, vitest_1.expect)((0, delete_1.handleDeleteStory)(mockMigration, mockOptions)).rejects.toThrow('Story "123" has no ID');
(0, vitest_1.expect)(api_1.api.stories.delete).not.toHaveBeenCalled();
});
(0, vitest_1.it)("should handle API errors gracefully", async () => {
// Arrange
const error = new Error("API Error");
api_1.api.stories.get.mockRejectedValue(error);
// Act & Assert
await (0, vitest_1.expect)((0, delete_1.handleDeleteStory)(mockMigration, mockOptions)).rejects.toThrow("API Error");
});
(0, vitest_1.it)("should handle delete errors gracefully", async () => {
// Arrange
api_1.api.stories.get.mockResolvedValue({
data: { story: mockStory },
});
const error = new Error("Delete Error");
api_1.api.stories.delete.mockRejectedValue(error);
// Act & Assert
await (0, vitest_1.expect)((0, delete_1.handleDeleteStory)(mockMigration, mockOptions)).rejects.toThrow("Delete Error");
});
});
//# sourceMappingURL=delete.test.js.map