@jungvonmatt/sb-migrate
Version:
CLI tool for managing Storyblok schema and content migrations
130 lines • 6.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const login_1 = require("../../commands/login");
const config_1 = require("../../utils/config");
const child_process_1 = require("child_process");
vitest_1.vi.mock("../../utils/config");
vitest_1.vi.mock("child_process");
vitest_1.vi.mock("picocolors", () => ({
default: {
green: vitest_1.vi.fn((str) => str),
red: vitest_1.vi.fn((str) => str),
blue: vitest_1.vi.fn((str) => str),
},
}));
(0, vitest_1.describe)("login command", () => {
let consoleLogSpy;
let consoleErrorSpy;
let processExitSpy;
(0, vitest_1.beforeEach)(() => {
consoleLogSpy = vitest_1.vi.spyOn(console, "log").mockImplementation(() => { });
consoleErrorSpy = vitest_1.vi.spyOn(console, "error").mockImplementation(() => { });
processExitSpy = vitest_1.vi
.spyOn(process, "exit")
.mockImplementation((code) => {
throw new Error(`process.exit(${code})`);
});
vitest_1.vi.resetAllMocks();
});
(0, vitest_1.afterEach)(() => {
consoleLogSpy.mockRestore();
consoleErrorSpy.mockRestore();
processExitSpy.mockRestore();
});
(0, vitest_1.describe)("isStoryblokCliInstalled", () => {
(0, vitest_1.it)("should return true when Storyblok CLI is installed", () => {
vitest_1.vi.mocked(child_process_1.spawnSync).mockReturnValue({
status: 0,
stdout: Buffer.from(""),
stderr: Buffer.from(""),
output: ["", "", ""],
pid: 0,
signal: null,
});
(0, vitest_1.expect)((0, login_1.isStoryblokCliInstalled)()).toBe(true);
(0, vitest_1.expect)(child_process_1.spawnSync).toHaveBeenCalledWith("storyblok", ["--version"]);
});
(0, vitest_1.it)("should return false when Storyblok CLI is not installed", () => {
vitest_1.vi.mocked(child_process_1.spawnSync).mockReturnValue({
status: 1,
stdout: Buffer.from(""),
stderr: Buffer.from(""),
output: ["", "", ""],
pid: 0,
signal: null,
});
(0, vitest_1.expect)((0, login_1.isStoryblokCliInstalled)()).toBe(false);
(0, vitest_1.expect)(child_process_1.spawnSync).toHaveBeenCalledWith("storyblok", ["--version"]);
});
});
(0, vitest_1.describe)("loginToStoryblok", () => {
(0, vitest_1.it)("should login successfully when CLI is installed and config exists", async () => {
const mockConfig = {
spaceId: "test-space",
oauthToken: "test-token",
region: "eu",
};
vitest_1.vi.mocked(config_1.loadConfig).mockResolvedValue(mockConfig);
vitest_1.vi.mocked(child_process_1.spawnSync).mockReturnValue({
status: 0,
stdout: Buffer.from(""),
stderr: Buffer.from(""),
output: ["", "", ""],
pid: 0,
signal: null,
});
await (0, login_1.loginToStoryblok)();
(0, vitest_1.expect)(child_process_1.execSync).toHaveBeenCalledWith("storyblok login --token test-token --region eu", { stdio: "inherit" });
(0, vitest_1.expect)(consoleLogSpy).toHaveBeenCalledWith(vitest_1.expect.stringContaining("Logging in to Storyblok"));
(0, vitest_1.expect)(consoleLogSpy).toHaveBeenCalledWith(vitest_1.expect.stringContaining("Successfully logged in to Storyblok"));
});
(0, vitest_1.it)("should handle missing CLI installation", async () => {
vitest_1.vi.mocked(child_process_1.spawnSync).mockReturnValue({
status: 1,
stdout: Buffer.from(""),
stderr: Buffer.from(""),
output: ["", "", ""],
pid: 0,
signal: null,
});
await (0, vitest_1.expect)((0, login_1.loginToStoryblok)()).rejects.toThrow("process.exit unexpectedly called with");
(0, vitest_1.expect)(consoleErrorSpy).toHaveBeenCalledWith(vitest_1.expect.stringContaining("Storyblok CLI not found"));
});
(0, vitest_1.it)("should handle missing OAuth token", async () => {
vitest_1.vi.mocked(child_process_1.spawnSync).mockReturnValue({
status: 0,
stdout: Buffer.from(""),
stderr: Buffer.from(""),
output: ["", "", ""],
pid: 0,
signal: null,
});
vitest_1.vi.mocked(config_1.loadConfig).mockResolvedValue({});
await (0, vitest_1.expect)((0, login_1.loginToStoryblok)()).rejects.toThrow("process.exit unexpectedly called with");
(0, vitest_1.expect)(consoleErrorSpy).toHaveBeenCalledWith(vitest_1.expect.stringContaining("No Storyblok OAuth token found"));
});
(0, vitest_1.it)("should handle login failure", async () => {
const mockConfig = {
spaceId: "test-space",
oauthToken: "test-token",
region: "eu",
};
vitest_1.vi.mocked(config_1.loadConfig).mockResolvedValue(mockConfig);
vitest_1.vi.mocked(child_process_1.spawnSync).mockReturnValue({
status: 0,
stdout: Buffer.from(""),
stderr: Buffer.from(""),
output: ["", "", ""],
pid: 0,
signal: null,
});
vitest_1.vi.mocked(child_process_1.execSync).mockImplementation(() => {
throw new Error("Login failed");
});
await (0, vitest_1.expect)((0, login_1.loginToStoryblok)()).rejects.toThrow("process.exit unexpectedly called with");
(0, vitest_1.expect)(consoleErrorSpy).toHaveBeenCalledWith(vitest_1.expect.stringContaining("Failed to login to Storyblok"));
});
});
});
//# sourceMappingURL=login.test.js.map