Skip to content

Commit 9de22b4

Browse files
committed
Added tests for custom log path
1 parent 067fdb2 commit 9de22b4

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

setup.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export async function accessAndRefreshTokens(
140140
}
141141
}
142142

143-
function transactionLogPath(logPath: string | null): void {
143+
export function transactionLogPath(logPath: string | null): void {
144144
const config = dotenv.parse(fs.readFileSync(".env"));
145145
const currDir = dirname(fileURLToPath(import.meta.url));
146146
config.LOG = "true";
@@ -169,20 +169,20 @@ function transactionLogPath(logPath: string | null): void {
169169
}
170170
}
171171

172-
async function askForTransactionLogPath(): Promise<string> {
172+
export async function askForTransactionLogPath(): Promise<string> {
173173
let logPath: string | null = null;
174174
let isValidPath = false;
175175

176176
while (!isValidPath) {
177-
const response = await inquirer.prompt([
177+
const { logpath } = await inquirer.prompt([
178178
{
179179
type: "input",
180180
name: "logPath",
181181
message: "Enter absolute path of log file:",
182182
default: null,
183183
},
184184
]);
185-
logPath = response.logPath;
185+
logPath = logpath;
186186

187187
if (logPath && fs.existsSync(logPath)) {
188188
try {
@@ -468,7 +468,7 @@ export async function mongoDB(): Promise<void> {
468468
For Docker setup
469469
*/
470470

471-
function getDockerComposeCommand(): { command: string; args: string[] } {
471+
export function getDockerComposeCommand(): { command: string; args: string[] } {
472472
let dockerComposeCmd = "docker-compose"; // Default to v1
473473
let args = ["-f", "docker-compose.dev.yaml", "up", "--build", "-d"];
474474

tests/setup/transactionLog.spec.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { it, expect, vi, describe, beforeEach } from "vitest";
2+
import inquirer from "inquirer";
3+
import fs from "fs";
4+
import { askForTransactionLogPath } from "../../setup";
5+
6+
/*
7+
Test Case 1:
8+
Description: function askForSuperAdminEmail should return email as entered.
9+
Expected Behavior: When the askForSuperAdminEmail function is called, it should prompt the user to enter an email address, and upon entering, it should return the entered email address.
10+
11+
Test Case 2:
12+
Description: superAdmin prompts user, updates .env_test, and does not throw errors.
13+
Expected Behavior: When the superAdmin function is called, it should prompt the user to enter an email address for the last resort super admin, update the .env_test file with the entered email address, and it should not throw any errors during execution.
14+
15+
Note: Each test case involves mocking user input using inquirer, executing the relevant function, and asserting the expected behavior by checking the returned email or the updated .env_test file.
16+
*/
17+
describe("Setup -> transactionLogPath", () => {
18+
beforeEach(() => {
19+
vi.clearAllMocks();
20+
});
21+
22+
it("should return the transaction log path as entered by the user", async () => {
23+
const currDir = process.cwd();
24+
const testPath = `${currDir}/tests/setup/test-transaction.log`;
25+
vi.spyOn(inquirer, "prompt").mockResolvedValueOnce({ logpath: testPath });
26+
vi.spyOn(fs, "existsSync").mockReturnValueOnce(true);
27+
vi.spyOn(fs, "accessSync").mockReturnValueOnce(undefined);
28+
29+
const result = await askForTransactionLogPath();
30+
expect(result).toEqual(testPath);
31+
});
32+
33+
it("should handle invalid path and prompt user again", async () => {
34+
const currDir = process.cwd();
35+
const testPath = `${currDir}/tests/setup/test-transaction.log`;
36+
vi.spyOn(inquirer, "prompt")
37+
.mockResolvedValueOnce({ logpath: "invalidpath" })
38+
.mockResolvedValueOnce({ logpath: testPath });
39+
vi.spyOn(fs, "existsSync")
40+
.mockReturnValueOnce(false)
41+
.mockReturnValueOnce(true);
42+
vi.spyOn(fs, "accessSync").mockReturnValueOnce(undefined);
43+
const result = await askForTransactionLogPath();
44+
expect(result).toEqual(testPath);
45+
});
46+
});

0 commit comments

Comments
 (0)