UNPKG

@jungvonmatt/sb-migrate

Version:

CLI tool for managing Storyblok schema and content migrations

189 lines 7.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const vitest_1 = require("vitest"); const update_1 = require("../../../handlers/story/update"); const api_1 = require("../../../utils/api"); const storyblok_1 = require("../../../utils/storyblok"); // Mock the API and utility functions vitest_1.vi.mock("../../../utils/api", () => ({ api: { stories: { get: vitest_1.vi.fn(), getBySlug: vitest_1.vi.fn(), update: vitest_1.vi.fn(), }, }, })); vitest_1.vi.mock("../../../utils/storyblok", () => ({ createRollbackFile: vitest_1.vi.fn(), })); (0, vitest_1.describe)("handleUpdateStory", () => { const mockStory = { id: 123, name: "Original Story", slug: "original-story", full_slug: "original-story", parent_id: 0, content: { component: "test-component", field1: "value1", field2: "value2", }, }; const mockStoryUpdate = { name: "Updated Story", slug: "updated-story", parent_id: 456, content: { component: "test-component", field1: "new-value1", field3: "value3", }, }; const mockMigration = { id: 123, story: mockStoryUpdate, }; const mockOptions = { isDryrun: false, }; (0, vitest_1.beforeEach)(() => { vitest_1.vi.clearAllMocks(); }); (0, vitest_1.it)("should update a story by ID successfully", async () => { // Arrange api_1.api.stories.get.mockResolvedValue({ data: { story: mockStory }, }); api_1.api.stories.update.mockResolvedValue({}); storyblok_1.createRollbackFile.mockResolvedValue(undefined); // Act await (0, update_1.handleUpdateStory)(mockMigration, mockOptions); // Assert (0, vitest_1.expect)(api_1.api.stories.get).toHaveBeenCalledWith(123); (0, vitest_1.expect)(api_1.api.stories.update).toHaveBeenCalledWith({ ...mockStory, name: "Updated Story", slug: "updated-story", parent_id: 456, content: { component: "test-component", field1: "new-value1", field2: "value2", field3: "value3", }, }, {}); }); (0, vitest_1.it)("should update a story by slug successfully", async () => { // Arrange const migrationWithSlug = { id: "test-story", story: mockStoryUpdate, }; api_1.api.stories.getBySlug.mockResolvedValue(mockStory); api_1.api.stories.update.mockResolvedValue({}); storyblok_1.createRollbackFile.mockResolvedValue(undefined); // Act await (0, update_1.handleUpdateStory)(migrationWithSlug, mockOptions); // Assert (0, vitest_1.expect)(api_1.api.stories.getBySlug).toHaveBeenCalledWith("test-story"); (0, vitest_1.expect)(api_1.api.stories.update).toHaveBeenCalled(); (0, vitest_1.expect)(storyblok_1.createRollbackFile).toHaveBeenCalledWith([ { id: mockStory.id, full_slug: mockStory.full_slug, content: mockStory.content, name: mockStory.name, slug: mockStory.slug, parent_id: mockStory.parent_id, }, ], "story_test_story", "update"); }); (0, vitest_1.it)("should handle publish option", async () => { // Arrange const storyWithPublish = { ...mockStoryUpdate, publish: true, }; api_1.api.stories.get.mockResolvedValue({ data: { story: mockStory }, }); api_1.api.stories.update.mockResolvedValue({}); // Act await (0, update_1.handleUpdateStory)({ id: 123, story: storyWithPublish }, mockOptions); // Assert (0, vitest_1.expect)(api_1.api.stories.update).toHaveBeenCalledWith(vitest_1.expect.any(Object), { publish: "1", }); }); (0, vitest_1.it)("should handle release_id option", async () => { // Arrange const storyWithRelease = { ...mockStoryUpdate, release_id: 789, }; api_1.api.stories.get.mockResolvedValue({ data: { story: mockStory }, }); api_1.api.stories.update.mockResolvedValue({}); // Act await (0, update_1.handleUpdateStory)({ id: 123, story: storyWithRelease }, mockOptions); // Assert (0, vitest_1.expect)(api_1.api.stories.update).toHaveBeenCalledWith(vitest_1.expect.any(Object), { release_id: 789, }); }); (0, vitest_1.it)("should handle lang option", async () => { // Arrange const storyWithLang = { ...mockStoryUpdate, lang: "de", }; api_1.api.stories.get.mockResolvedValue({ data: { story: mockStory }, }); api_1.api.stories.update.mockResolvedValue({}); // Act await (0, update_1.handleUpdateStory)({ id: 123, story: storyWithLang }, mockOptions); // Assert (0, vitest_1.expect)(api_1.api.stories.update).toHaveBeenCalledWith(vitest_1.expect.any(Object), { lang: "de", }); }); (0, vitest_1.it)("should handle dry run correctly", async () => { // Arrange const dryRunOptions = { isDryrun: true }; // Act await (0, update_1.handleUpdateStory)(mockMigration, dryRunOptions); // Assert (0, vitest_1.expect)(api_1.api.stories.get).not.toHaveBeenCalled(); (0, vitest_1.expect)(api_1.api.stories.update).not.toHaveBeenCalled(); (0, vitest_1.expect)(storyblok_1.createRollbackFile).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, update_1.handleUpdateStory)(mockMigration, mockOptions)).rejects.toThrow('Story "123" not found'); }); (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, update_1.handleUpdateStory)(mockMigration, mockOptions)).rejects.toThrow("API Error"); }); (0, vitest_1.it)("should handle update errors gracefully", async () => { // Arrange api_1.api.stories.get.mockResolvedValue({ data: { story: mockStory }, }); const error = new Error("Update Error"); api_1.api.stories.update.mockRejectedValue(error); // Act & Assert await (0, vitest_1.expect)((0, update_1.handleUpdateStory)(mockMigration, mockOptions)).rejects.toThrow("Update Error"); }); }); //# sourceMappingURL=update.test.js.map