@jungvonmatt/sb-migrate
Version:
CLI tool for managing Storyblok schema and content migrations
125 lines • 5.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable @typescript-eslint/no-explicit-any */
const vitest_1 = require("vitest");
const config_1 = require("../../utils/config");
const promises_1 = require("fs/promises");
const dotenv_1 = require("dotenv");
const cosmiconfig_1 = require("cosmiconfig");
const prompts_1 = require("@inquirer/prompts");
vitest_1.vi.mock("fs/promises");
vitest_1.vi.mock("dotenv");
vitest_1.vi.mock("cosmiconfig");
vitest_1.vi.mock("@inquirer/prompts");
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),
yellow: vitest_1.vi.fn((str) => str),
},
}));
(0, vitest_1.describe)("config utils", () => {
let consoleLogSpy;
let consoleErrorSpy;
(0, vitest_1.beforeEach)(() => {
consoleLogSpy = vitest_1.vi.spyOn(console, "log").mockImplementation(() => { });
consoleErrorSpy = vitest_1.vi.spyOn(console, "error").mockImplementation(() => { });
vitest_1.vi.resetAllMocks();
});
(0, vitest_1.afterEach)(() => {
consoleLogSpy.mockRestore();
consoleErrorSpy.mockRestore();
});
(0, vitest_1.describe)("loadEnvConfig", () => {
(0, vitest_1.it)("should load environment variables", async () => {
process.env.STORYBLOK_SPACE_ID = "test-space";
process.env.STORYBLOK_OAUTH_TOKEN = "test-token";
const result = await (0, config_1.loadEnvConfig)();
(0, vitest_1.expect)(dotenv_1.config).toHaveBeenCalled();
(0, vitest_1.expect)(result).toEqual({
spaceId: "test-space",
oauthToken: "test-token",
});
delete process.env.STORYBLOK_SPACE_ID;
delete process.env.STORYBLOK_OAUTH_TOKEN;
});
});
(0, vitest_1.describe)("saveConfig", () => {
(0, vitest_1.it)("should save config to file", async () => {
const testConfig = {
spaceId: "test-space",
oauthToken: "test-token",
region: "eu",
};
await (0, config_1.saveConfig)(testConfig);
(0, vitest_1.expect)(promises_1.writeFile).toHaveBeenCalledWith(vitest_1.expect.stringContaining(".storyblokrc.json"), JSON.stringify(testConfig, null, 2));
});
});
(0, vitest_1.describe)("loadConfig", () => {
(0, vitest_1.it)("should return null when no config is found", async () => {
vitest_1.vi.mocked(cosmiconfig_1.cosmiconfig).mockReturnValue({
search: () => Promise.resolve(null),
});
const result = await (0, config_1.loadConfig)();
(0, vitest_1.expect)(result).toBeNull();
});
(0, vitest_1.it)("should return config when found", async () => {
const testConfig = {
spaceId: "test-space",
oauthToken: "test-token",
region: "eu",
};
vitest_1.vi.mocked(cosmiconfig_1.cosmiconfig).mockReturnValue({
search: () => Promise.resolve({ config: testConfig }),
});
const result = await (0, config_1.loadConfig)();
(0, vitest_1.expect)(result).toEqual(testConfig);
});
});
(0, vitest_1.describe)("verifyExistingConfig", () => {
(0, vitest_1.it)("should return null if user doesn't confirm", async () => {
const mockConfirm = Promise.resolve(false);
mockConfirm.cancel = () => { };
vitest_1.vi.mocked(prompts_1.confirm).mockImplementation(() => mockConfirm);
const result = await (0, config_1.verifyExistingConfig)({ spaceId: "test" });
(0, vitest_1.expect)(result).toBeNull();
});
(0, vitest_1.it)("should prompt for missing values if user confirms", async () => {
const mockConfirm = Promise.resolve(true);
mockConfirm.cancel = () => { };
vitest_1.vi.mocked(prompts_1.confirm).mockImplementation(() => mockConfirm);
const mockInput = Promise.resolve("test-value");
mockInput.cancel = () => { };
vitest_1.vi.mocked(prompts_1.input).mockImplementation(() => mockInput);
const mockSelect = Promise.resolve("eu");
mockSelect.cancel = () => { };
vitest_1.vi.mocked(prompts_1.select).mockImplementation(() => mockSelect);
const result = await (0, config_1.verifyExistingConfig)({ spaceId: "existing-space" });
(0, vitest_1.expect)(result).toEqual({
spaceId: "existing-space",
oauthToken: "test-value",
region: "eu",
});
});
});
(0, vitest_1.describe)("promptForConfig", () => {
(0, vitest_1.it)("should prompt for all values with defaults", async () => {
vitest_1.vi.mocked(prompts_1.input).mockImplementation(({ message }) => {
const mockInput = Promise.resolve(message.includes("Space ID") ? "new-space" : "new-token");
mockInput.cancel = () => { };
return mockInput;
});
const mockSelect = Promise.resolve("eu");
mockSelect.cancel = () => { };
vitest_1.vi.mocked(prompts_1.select).mockImplementation(() => mockSelect);
const result = await (0, config_1.promptForConfig)({ spaceId: "old-space" });
(0, vitest_1.expect)(result).toEqual({
spaceId: "new-space",
oauthToken: "new-token",
region: "eu",
});
});
});
});
//# sourceMappingURL=config.test.js.map