UNPKG

@jungvonmatt/sb-migrate

Version:

CLI tool for managing Storyblok schema and content migrations

197 lines 6.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const vitest_1 = require("vitest"); const create_1 = require("../../../handlers/story/create"); const api_1 = require("../../../utils/api"); // Mock the API vitest_1.vi.mock("../../../utils/api", () => ({ api: { stories: { getAll: vitest_1.vi.fn(), create: vitest_1.vi.fn(), }, }, })); (0, vitest_1.describe)("handleCreateStory", () => { const mockStory = { name: "Test Story", slug: "test-story", content: { component: "test-component", field1: "value1", field2: "value2", }, }; const mockMigration = { story: mockStory, }; const mockOptions = { isDryrun: false, }; const mockCreatedStory = { id: 123, name: "Test Story", slug: "test-story", full_slug: "test-story", }; (0, vitest_1.beforeEach)(() => { vitest_1.vi.clearAllMocks(); }); (0, vitest_1.it)("should create a new story successfully", async () => { // Arrange api_1.api.stories.getAll.mockResolvedValue({ data: { stories: [] }, }); api_1.api.stories.create.mockResolvedValue({ data: { story: mockCreatedStory }, }); // Act await (0, create_1.handleCreateStory)(mockMigration, mockOptions); // Assert (0, vitest_1.expect)(api_1.api.stories.getAll).toHaveBeenCalledWith({ starts_with: "test-story", }); (0, vitest_1.expect)(api_1.api.stories.create).toHaveBeenCalledWith({ name: "Test Story", slug: "test-story", content: { component: "test-component", field1: "value1", field2: "value2", }, }, {}); }); (0, vitest_1.it)("should create a story with publish option", async () => { // Arrange const storyWithPublish = { ...mockStory, publish: true, }; api_1.api.stories.getAll.mockResolvedValue({ data: { stories: [] }, }); api_1.api.stories.create.mockResolvedValue({ data: { story: mockCreatedStory }, }); // Act await (0, create_1.handleCreateStory)({ story: storyWithPublish }, mockOptions); // Assert (0, vitest_1.expect)(api_1.api.stories.create).toHaveBeenCalledWith({ name: "Test Story", slug: "test-story", content: { component: "test-component", field1: "value1", field2: "value2", }, }, { publish: "1" }); }); (0, vitest_1.it)("should create a story with release_id", async () => { // Arrange const storyWithRelease = { ...mockStory, release_id: 456, }; api_1.api.stories.getAll.mockResolvedValue({ data: { stories: [] }, }); api_1.api.stories.create.mockResolvedValue({ data: { story: mockCreatedStory }, }); // Act await (0, create_1.handleCreateStory)({ story: storyWithRelease }, mockOptions); // Assert (0, vitest_1.expect)(api_1.api.stories.create).toHaveBeenCalledWith({ name: "Test Story", slug: "test-story", content: { component: "test-component", field1: "value1", field2: "value2", }, }, { release_id: 456 }); }); (0, vitest_1.it)("should handle dry run correctly", async () => { // Arrange const dryRunOptions = { isDryrun: true }; // Act await (0, create_1.handleCreateStory)(mockMigration, dryRunOptions); // Assert (0, vitest_1.expect)(api_1.api.stories.getAll).not.toHaveBeenCalled(); (0, vitest_1.expect)(api_1.api.stories.create).not.toHaveBeenCalled(); }); (0, vitest_1.it)("should skip creation if story exists by slug", async () => { // Arrange api_1.api.stories.getAll.mockResolvedValue({ data: { stories: [ { name: "Different Name", slug: "test-story", full_slug: "test-story", }, ], }, }); // Act await (0, create_1.handleCreateStory)(mockMigration, mockOptions); // Assert (0, vitest_1.expect)(api_1.api.stories.getAll).toHaveBeenCalled(); (0, vitest_1.expect)(api_1.api.stories.create).not.toHaveBeenCalled(); }); (0, vitest_1.it)("should skip creation if story exists by name", async () => { // Arrange api_1.api.stories.getAll.mockResolvedValue({ data: { stories: [ { name: "Test Story", slug: "different-slug", full_slug: "different-slug", }, ], }, }); // Act await (0, create_1.handleCreateStory)(mockMigration, mockOptions); // Assert (0, vitest_1.expect)(api_1.api.stories.getAll).toHaveBeenCalled(); (0, vitest_1.expect)(api_1.api.stories.create).not.toHaveBeenCalled(); }); (0, vitest_1.it)("should generate slug from name if not provided", async () => { // Arrange const storyWithoutSlug = { ...mockStory, slug: undefined, }; api_1.api.stories.getAll.mockResolvedValue({ data: { stories: [] }, }); api_1.api.stories.create.mockResolvedValue({ data: { story: mockCreatedStory }, }); // Act await (0, create_1.handleCreateStory)({ story: storyWithoutSlug }, mockOptions); // Assert (0, vitest_1.expect)(api_1.api.stories.getAll).toHaveBeenCalledWith({ starts_with: "test-story", }); (0, vitest_1.expect)(api_1.api.stories.create).toHaveBeenCalledWith({ name: "Test Story", slug: "test-story", content: { component: "test-component", field1: "value1", field2: "value2", }, }, {}); }); (0, vitest_1.it)("should handle API errors", async () => { // Arrange const error = new Error("API Error"); api_1.api.stories.getAll.mockRejectedValue(error); // Act & Assert await (0, vitest_1.expect)((0, create_1.handleCreateStory)(mockMigration, mockOptions)).rejects.toThrow("API Error"); }); }); //# sourceMappingURL=create.test.js.map