@jungvonmatt/sb-migrate
Version:
CLI tool for managing Storyblok schema and content migrations
205 lines • 7.71 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const run_1 = require("../../commands/run");
const api_1 = require("../../utils/api");
const handlers = __importStar(require("../../handlers"));
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
// Store the mock migration object so we can update it in tests
const mockMigration = { type: "create-component" };
// Mock dependencies
vitest_1.vi.mock("../../utils/api");
vitest_1.vi.mock("../../handlers");
vitest_1.vi.mock("fs");
vitest_1.vi.mock("path");
vitest_1.vi.mock("jiti", () => ({
createJiti: () => ({
import: async () => ({ default: mockMigration }),
}),
}));
vitest_1.vi.mock("picocolors", () => ({
default: {
green: (str) => str,
red: (str) => str,
blue: (str) => str,
yellow: (str) => str,
},
}));
(0, vitest_1.describe)("run", () => {
const mockFilePath = "test-migration.ts";
const mockResolvedPath = "/absolute/path/to/test-migration.ts";
(0, vitest_1.beforeEach)(() => {
vitest_1.vi.clearAllMocks();
// Reset migration type for each test
mockMigration.type = "create-component";
// Common mocks setup
vitest_1.vi.mocked(path_1.default.resolve).mockReturnValue(mockResolvedPath);
vitest_1.vi.mocked(fs_1.default.existsSync).mockReturnValue(true);
// Mock process.exit
vitest_1.vi.spyOn(process, "exit").mockImplementation((code) => {
throw new Error(`Process.exit called with code ${code}`);
});
// Make all handlers succeed by default
Object.values(handlers).forEach((handler) => {
vitest_1.vi.mocked(handler).mockResolvedValue(undefined);
});
});
(0, vitest_1.afterEach)(() => {
vitest_1.vi.resetAllMocks();
});
(0, vitest_1.it)("should set default rate limit when no throttle is provided", async () => {
try {
await (0, run_1.run)(mockFilePath);
}
catch {
// Expected to throw due to process.exit
}
(0, vitest_1.expect)(api_1.setRequestsPerSecond).toHaveBeenCalledWith(3);
});
(0, vitest_1.it)("should set custom rate limit when throttle is provided", async () => {
try {
await (0, run_1.run)(mockFilePath, { throttle: 500 });
}
catch {
// Expected to throw due to process.exit
}
(0, vitest_1.expect)(api_1.setRequestsPerSecond).toHaveBeenCalledWith(2);
});
(0, vitest_1.it)("should exit when migration file is not found", async () => {
vitest_1.vi.mocked(fs_1.default.existsSync).mockReturnValue(false);
await (0, vitest_1.expect)((0, run_1.run)(mockFilePath)).rejects.toThrow("Process.exit called with code 1");
});
(0, vitest_1.describe)("migration type handling", () => {
const testCases = [
{
type: "create-component-group",
handler: handlers.handleCreateComponentGroup,
},
{
type: "update-component-group",
handler: handlers.handleUpdateComponentGroup,
},
{
type: "delete-component-group",
handler: handlers.handleDeleteComponentGroup,
},
{
type: "create-component",
handler: handlers.handleCreateComponent,
},
{
type: "update-component",
handler: handlers.handleUpdateComponent,
},
{
type: "delete-component",
handler: handlers.handleDeleteComponent,
},
{
type: "create-story",
handler: handlers.handleCreateStory,
},
{
type: "update-story",
handler: handlers.handleUpdateStory,
},
{
type: "delete-story",
handler: handlers.handleDeleteStory,
},
{
type: "create-datasource",
handler: handlers.handleCreateDatasource,
},
{
type: "update-datasource",
handler: handlers.handleUpdateDatasource,
},
{
type: "delete-datasource",
handler: handlers.handleDeleteDatasource,
},
{
type: "transform-entries",
handler: handlers.handleTransformEntries,
},
];
testCases.forEach(({ type, handler }) => {
(0, vitest_1.it)(`should call ${handler.name} for ${type} migration`, async () => {
mockMigration.type = type;
try {
await (0, run_1.run)(mockFilePath);
}
catch {
// Expected to throw due to process.exit
}
(0, vitest_1.expect)(handler).toHaveBeenCalledWith({ type }, {
isDryrun: undefined,
publish: undefined,
publishLanguages: undefined,
});
});
});
});
(0, vitest_1.it)("should pass options to migration handlers", async () => {
const options = {
dryRun: true,
publish: "all",
languages: "en,de",
throttle: 1000,
};
try {
await (0, run_1.run)(mockFilePath, options);
}
catch {
// Expected to throw due to process.exit
}
(0, vitest_1.expect)(handlers.handleCreateComponent).toHaveBeenCalledWith({ type: "create-component" }, {
isDryrun: true,
publish: "all",
publishLanguages: "en,de",
});
});
(0, vitest_1.it)("should handle unknown migration types", async () => {
mockMigration.type = "unknown-type";
await (0, vitest_1.expect)((0, run_1.run)(mockFilePath)).rejects.toThrow("Process.exit called with code 1");
});
});
//# sourceMappingURL=run.test.js.map