Skip to content

Commit c025ec3

Browse files
Merge branch 'CSE-25:main' into main
2 parents 2d83016 + 67b8747 commit c025ec3

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

test/clear.test.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import path from "node:path";
2+
import {
3+
existsSync,
4+
mkdirSync,
5+
readdirSync,
6+
writeFileSync,
7+
rmSync,
8+
} from "node:fs";
9+
import { fileURLToPath } from "url";
10+
import { jest, expect } from "@jest/globals";
11+
12+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
13+
const tempDir = path.join(__dirname, "test-temp-dir");
14+
const mockConfirm = jest.fn();
15+
16+
jest.unstable_mockModule("@inquirer/prompts", () => ({
17+
confirm: mockConfirm,
18+
}));
19+
20+
let clearCWD;
21+
22+
beforeAll(async () => {
23+
const clearModule = await import("../bin/util/clear.js");
24+
clearCWD = clearModule.clearCWD;
25+
});
26+
27+
function createTempDir() {
28+
if (existsSync(tempDir)) {
29+
rmSync(tempDir, { recursive: true, force: true });
30+
}
31+
mkdirSync(tempDir, { recursive: true });
32+
writeFileSync(path.join(tempDir, "test1"), "console.log('test1');");
33+
writeFileSync(path.join(tempDir, "test2.js"), "console.log('test2');");
34+
35+
const testDirPath = path.join(tempDir, "nested-dir");
36+
mkdirSync(testDirPath);
37+
writeFileSync(path.join(testDirPath, "test3.txt"), "console.log('test3');");
38+
}
39+
40+
function listDirContents() {
41+
if (existsSync(tempDir)) {
42+
return readdirSync(tempDir);
43+
} else {
44+
return [];
45+
}
46+
}
47+
48+
function removeTempDir() {
49+
if (existsSync(tempDir)) {
50+
rmSync(tempDir, { recursive: true, force: true });
51+
}
52+
}
53+
54+
describe("qse clear command function", () => {
55+
beforeEach(() => {
56+
jest.resetModules();
57+
createTempDir();
58+
jest.spyOn(process, "cwd").mockReturnValue(tempDir);
59+
mockConfirm.mockReset();
60+
});
61+
62+
afterEach(() => {
63+
jest.restoreAllMocks();
64+
});
65+
66+
afterAll(() => {
67+
jest.clearAllMocks();
68+
removeTempDir();
69+
});
70+
71+
test("remove files when user confirms", async () => {
72+
expect(listDirContents().length).toBeGreaterThan(0);
73+
mockConfirm.mockResolvedValue(true);
74+
75+
await clearCWD();
76+
77+
expect(listDirContents().length).toBe(0);
78+
expect(mockConfirm).toHaveBeenCalled();
79+
});
80+
81+
test("does not remove files when user canceles", async () => {
82+
const initialContents = listDirContents();
83+
expect(initialContents.length).toBeGreaterThan(0);
84+
mockConfirm.mockResolvedValue(false);
85+
86+
await clearCWD();
87+
88+
expect(listDirContents()).toEqual(initialContents);
89+
expect(mockConfirm).toHaveBeenCalled();
90+
});
91+
});

0 commit comments

Comments
 (0)