quilon
Version:
Generate ERDs from your entity files automagically
56 lines (55 loc) • 2.83 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const config_1 = require("../../global/config");
const types_1 = require("../../global/types");
const Init_1 = require("./Init");
jest.mock("fs");
describe("InitCommand", () => {
let initCommand;
beforeEach(() => {
initCommand = new Init_1.InitCommand();
jest.clearAllMocks();
});
test("should not create the config file if it already exists", () => {
fs_1.default.existsSync.mockReturnValue(true);
const consoleErrorSpy = jest.spyOn(console, "error").mockImplementation();
const executeSpy = jest.spyOn(initCommand, "execute");
initCommand.execute();
expect(fs_1.default.existsSync).toHaveBeenCalledWith(config_1.GlobalConfig.CONFIG_PATH);
expect(fs_1.default.writeFileSync).not.toHaveBeenCalled();
expect(consoleErrorSpy).toHaveBeenCalledWith(`${config_1.GlobalConfig.CONFIG_FILE} already exists.`);
expect(executeSpy).toHaveReturned();
consoleErrorSpy.mockRestore();
});
test("should create the config file if it does not exist", () => {
fs_1.default.existsSync.mockReturnValue(false);
const consoleLogSpy = jest.spyOn(console, "log").mockImplementation();
initCommand.execute();
expect(fs_1.default.existsSync).toHaveBeenCalledWith(config_1.GlobalConfig.CONFIG_PATH);
expect(fs_1.default.writeFileSync).toHaveBeenCalledWith(config_1.GlobalConfig.CONFIG_PATH, JSON.stringify({
$schema: "https://raw.githubusercontent.com/quilon-tool/quilon/main/src/config/config-schema.json",
entities: [],
orm: types_1.ORMs.TypeORM,
diagramLanguage: types_1.DiagramLanguages.Mermaid,
outputDir: config_1.GlobalConfig.OUTPUT_DIR,
}, null, 2));
expect(consoleLogSpy).toHaveBeenCalledWith(`${config_1.GlobalConfig.CONFIG_FILE} successfully created.`);
consoleLogSpy.mockRestore();
});
test("should handle errors during file write gracefully", () => {
fs_1.default.existsSync.mockReturnValue(false);
fs_1.default.writeFileSync.mockImplementation(() => {
throw new Error("Write error");
});
const consoleErrorSpy = jest.spyOn(console, "error").mockImplementation();
expect(() => initCommand.execute()).toThrow("Write error");
expect(fs_1.default.existsSync).toHaveBeenCalledWith(config_1.GlobalConfig.CONFIG_PATH);
expect(fs_1.default.writeFileSync).toHaveBeenCalled();
expect(consoleErrorSpy).not.toHaveBeenCalled();
consoleErrorSpy.mockRestore();
});
});
;