-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConfigManager.initialization.test.js
More file actions
75 lines (60 loc) · 2.28 KB
/
Copy pathConfigManager.initialization.test.js
File metadata and controls
75 lines (60 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import fs from "fs";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { ConfigManager } from "../configManager.js";
// Mock fs module
vi.mock("fs");
describe("ConfigManager - Initialization", () => {
const templatePath = "/path/to/template.toml";
const userConfigPath = "/path/to/user.toml";
const versionFilePath = "/path/to/version.toml";
const appVersion = "1.0.0";
const appVersionReturnValue = `version = "${appVersion}"`;
beforeEach(() => {
vi.clearAllMocks();
});
it("should create config manager instance", () => {
fs.existsSync.mockReturnValue(true);
fs.readFileSync.mockReturnValue(appVersionReturnValue);
const manager = new ConfigManager(
userConfigPath,
templatePath,
versionFilePath,
appVersion,
);
expect(manager.userConfigPath).toBe(userConfigPath);
expect(manager.templatePath).toBe(templatePath);
});
it("should create directory if it does not exist", () => {
fs.existsSync.mockImplementation((path) => {
if (path === "/path/to") return false;
return true;
});
fs.mkdirSync.mockImplementation(() => {});
fs.readFileSync.mockReturnValue(appVersionReturnValue);
new ConfigManager(userConfigPath, templatePath, versionFilePath, appVersion);
expect(fs.mkdirSync).toHaveBeenCalledWith("/path/to", {
recursive: true,
});
});
it("should copy template if user config does not exist", () => {
fs.existsSync.mockImplementation((path) => {
if (path === userConfigPath) return false;
return true;
});
fs.copyFileSync.mockImplementation(() => {});
fs.writeFileSync.mockImplementation(() => {});
const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {});
new ConfigManager(userConfigPath, templatePath, versionFilePath, appVersion);
expect(fs.copyFileSync).toHaveBeenCalledWith(templatePath, userConfigPath);
expect(consoleSpy).toHaveBeenCalledWith(
expect.stringContaining("Created config from template"),
);
consoleSpy.mockRestore();
});
it("should throw error if template does not exist", () => {
fs.existsSync.mockReturnValue(false);
expect(() => {
new ConfigManager(userConfigPath, templatePath, versionFilePath, appVersion);
}).toThrow("Template not found");
});
});