@jungvonmatt/sb-migrate
Version:
CLI tool for managing Storyblok schema and content migrations
174 lines • 6.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const create_1 = require("../../../handlers/datasource/create");
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: {
datasources: {
getAll: vitest_1.vi.fn(),
},
},
}));
vitest_1.vi.mock("../../../utils/storyblok", () => ({
addOrUpdateDatasource: vitest_1.vi.fn(),
}));
(0, vitest_1.describe)("handleCreateDatasource", () => {
const mockDatasource = {
name: "test-datasource",
slug: "test-datasource",
};
const mockEntries = [
{
name: "entry1",
value: "value1",
dimension_value: "dimension1",
},
{
name: "entry2",
value: "value2",
dimension_value: "dimension1",
},
];
const mockMigration = {
datasource: mockDatasource,
entries: mockEntries,
};
const mockOptions = {
isDryrun: false,
};
(0, vitest_1.beforeEach)(() => {
vitest_1.vi.clearAllMocks();
});
(0, vitest_1.it)("should create a new datasource with entries successfully", async () => {
// Arrange
const mockExistingDatasources = {
data: {
datasources: [],
},
};
const mockCreatedDatasource = {
id: 123,
name: "test-datasource",
slug: "test-datasource",
};
const mockCreatedEntries = [
{ id: 1, name: "entry1" },
{ id: 2, name: "entry2" },
];
api_1.api.datasources.getAll.mockResolvedValue(mockExistingDatasources);
storyblok_1.addOrUpdateDatasource.mockResolvedValue([
mockCreatedDatasource,
mockCreatedEntries,
]);
// Act
await (0, create_1.handleCreateDatasource)(mockMigration, mockOptions);
// Assert
(0, vitest_1.expect)(api_1.api.datasources.getAll).toHaveBeenCalled();
(0, vitest_1.expect)(storyblok_1.addOrUpdateDatasource).toHaveBeenCalledWith(mockDatasource, mockEntries.map((entry) => ({
name: entry.name,
value: entry.value,
dimension_value: entry.dimension_value,
})));
});
(0, vitest_1.it)("should create a new datasource without entries successfully", async () => {
// Arrange
const mockExistingDatasources = {
data: {
datasources: [],
},
};
const mockCreatedDatasource = {
id: 123,
name: "test-datasource",
slug: "test-datasource",
};
const mockCreatedEntries = [];
api_1.api.datasources.getAll.mockResolvedValue(mockExistingDatasources);
storyblok_1.addOrUpdateDatasource.mockResolvedValue([
mockCreatedDatasource,
mockCreatedEntries,
]);
// Act
await (0, create_1.handleCreateDatasource)({ datasource: mockDatasource }, mockOptions);
// Assert
(0, vitest_1.expect)(api_1.api.datasources.getAll).toHaveBeenCalled();
(0, vitest_1.expect)(storyblok_1.addOrUpdateDatasource).toHaveBeenCalledWith(mockDatasource, []);
});
(0, vitest_1.it)("should handle dry run correctly", async () => {
// Arrange
const dryRunOptions = { isDryrun: true };
// Act
await (0, create_1.handleCreateDatasource)(mockMigration, dryRunOptions);
// Assert
(0, vitest_1.expect)(api_1.api.datasources.getAll).not.toHaveBeenCalled();
(0, vitest_1.expect)(storyblok_1.addOrUpdateDatasource).not.toHaveBeenCalled();
});
(0, vitest_1.it)("should skip creation if datasource already exists by name", async () => {
// Arrange
const mockExistingDatasources = {
data: {
datasources: [
{
name: "test-datasource",
slug: "different-slug",
},
],
},
};
api_1.api.datasources.getAll.mockResolvedValue(mockExistingDatasources);
// Act
await (0, create_1.handleCreateDatasource)(mockMigration, mockOptions);
// Assert
(0, vitest_1.expect)(api_1.api.datasources.getAll).toHaveBeenCalled();
(0, vitest_1.expect)(storyblok_1.addOrUpdateDatasource).not.toHaveBeenCalled();
});
(0, vitest_1.it)("should skip creation if datasource already exists by slug", async () => {
// Arrange
const mockExistingDatasources = {
data: {
datasources: [
{
name: "different-name",
slug: "test-datasource",
},
],
},
};
api_1.api.datasources.getAll.mockResolvedValue(mockExistingDatasources);
// Act
await (0, create_1.handleCreateDatasource)(mockMigration, mockOptions);
// Assert
(0, vitest_1.expect)(api_1.api.datasources.getAll).toHaveBeenCalled();
(0, vitest_1.expect)(storyblok_1.addOrUpdateDatasource).not.toHaveBeenCalled();
});
(0, vitest_1.it)("should handle API errors gracefully", async () => {
// Arrange
const error = new Error("API Error");
api_1.api.datasources.getAll.mockRejectedValue(error);
// Act
await (0, create_1.handleCreateDatasource)(mockMigration, mockOptions);
// Assert
(0, vitest_1.expect)(api_1.api.datasources.getAll).toHaveBeenCalled();
(0, vitest_1.expect)(storyblok_1.addOrUpdateDatasource).not.toHaveBeenCalled();
});
(0, vitest_1.it)("should handle creation errors gracefully", async () => {
// Arrange
const mockExistingDatasources = {
data: {
datasources: [],
},
};
const error = new Error("Creation Error");
api_1.api.datasources.getAll.mockResolvedValue(mockExistingDatasources);
storyblok_1.addOrUpdateDatasource.mockRejectedValue(error);
// Act
await (0, create_1.handleCreateDatasource)(mockMigration, mockOptions);
// Assert
(0, vitest_1.expect)(api_1.api.datasources.getAll).toHaveBeenCalled();
(0, vitest_1.expect)(storyblok_1.addOrUpdateDatasource).toHaveBeenCalled();
});
});
//# sourceMappingURL=create.test.js.map