Skip to content

Commit

Permalink
Added tests for custom log path
Browse files Browse the repository at this point in the history
  • Loading branch information
Aad1tya27 committed Jan 11, 2025
1 parent 067fdb2 commit 9de22b4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
10 changes: 5 additions & 5 deletions setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export async function accessAndRefreshTokens(
}
}

function transactionLogPath(logPath: string | null): void {
export function transactionLogPath(logPath: string | null): void {
const config = dotenv.parse(fs.readFileSync(".env"));
const currDir = dirname(fileURLToPath(import.meta.url));
config.LOG = "true";
Expand Down Expand Up @@ -169,20 +169,20 @@ function transactionLogPath(logPath: string | null): void {
}
}

async function askForTransactionLogPath(): Promise<string> {
export async function askForTransactionLogPath(): Promise<string> {
let logPath: string | null = null;
let isValidPath = false;

while (!isValidPath) {
const response = await inquirer.prompt([
const { logpath } = await inquirer.prompt([
{
type: "input",
name: "logPath",
message: "Enter absolute path of log file:",
default: null,
},
]);
logPath = response.logPath;
logPath = logpath;

if (logPath && fs.existsSync(logPath)) {
try {
Expand Down Expand Up @@ -468,7 +468,7 @@ export async function mongoDB(): Promise<void> {
For Docker setup
*/

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

Expand Down
46 changes: 46 additions & 0 deletions tests/setup/transactionLog.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { it, expect, vi, describe, beforeEach } from "vitest";
import inquirer from "inquirer";
import fs from "fs";
import { askForTransactionLogPath } from "../../setup";

/*
Test Case 1:
Description: function askForSuperAdminEmail should return email as entered.
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.
Test Case 2:
Description: superAdmin prompts user, updates .env_test, and does not throw errors.
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.
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.
*/
describe("Setup -> transactionLogPath", () => {
beforeEach(() => {
vi.clearAllMocks();
});

it("should return the transaction log path as entered by the user", async () => {
const currDir = process.cwd();
const testPath = `${currDir}/tests/setup/test-transaction.log`;
vi.spyOn(inquirer, "prompt").mockResolvedValueOnce({ logpath: testPath });
vi.spyOn(fs, "existsSync").mockReturnValueOnce(true);
vi.spyOn(fs, "accessSync").mockReturnValueOnce(undefined);

const result = await askForTransactionLogPath();
expect(result).toEqual(testPath);
});

it("should handle invalid path and prompt user again", async () => {
const currDir = process.cwd();
const testPath = `${currDir}/tests/setup/test-transaction.log`;
vi.spyOn(inquirer, "prompt")
.mockResolvedValueOnce({ logpath: "invalidpath" })
.mockResolvedValueOnce({ logpath: testPath });
vi.spyOn(fs, "existsSync")
.mockReturnValueOnce(false)
.mockReturnValueOnce(true);
vi.spyOn(fs, "accessSync").mockReturnValueOnce(undefined);
const result = await askForTransactionLogPath();
expect(result).toEqual(testPath);
});
});

0 comments on commit 9de22b4

Please sign in to comment.