Skip to content

Commit

Permalink
fix-code-quality
Browse files Browse the repository at this point in the history
  • Loading branch information
Suyash878 committed Feb 11, 2025
1 parent 55b27b6 commit 6840c3c
Showing 1 changed file with 46 additions and 46 deletions.
92 changes: 46 additions & 46 deletions test/fix-readme-links.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,59 +7,59 @@ vi.mock("node:fs");
vi.mock("node:path");

describe("replaceLinks", () => {
const mockConsole = {
log: vi.spyOn(console, "log").mockImplementation(() => {}),
error: vi.spyOn(console, "error").mockImplementation(() => {})
};
const mockConsole = {
log: vi.spyOn(console, "log").mockImplementation(() => {}),
error: vi.spyOn(console, "error").mockImplementation(() => {}),
};

beforeEach(() => {
vi.clearAllMocks();
process.env.DOCS_DIR = "./test-docs";
beforeEach(() => {
vi.clearAllMocks();
process.env.DOCS_DIR = "./test-docs";

// Mock all required fs functions with proper implementations
vi.mocked(fs.readdirSync).mockReturnValue(["file.md", "subdir"]);
vi.mocked(fs.lstatSync).mockImplementation((path) => ({
isDirectory: () => path.endsWith("subdir")
}));
vi.mocked(fs.readFileSync).mockReturnValue("[Test](../README.md)");
vi.mocked(fs.writeFileSync).mockImplementation(() => undefined);
// Mock all required fs functions with proper implementations
vi.mocked(fs.readdirSync).mockReturnValue(["file.md", "subdir"]);
vi.mocked(fs.lstatSync).mockImplementation((path) => ({
isDirectory: () => path.endsWith("subdir"),
}));
vi.mocked(fs.readFileSync).mockReturnValue("[Test](../README.md)");
vi.mocked(fs.writeFileSync).mockImplementation(() => undefined);

// Mock path functions
vi.mocked(path.resolve).mockReturnValue("/test/docs");
vi.mocked(path.join).mockImplementation((...args) => args.join("/"));
});
// Mock path functions
vi.mocked(path.resolve).mockReturnValue("/test/docs");
vi.mocked(path.join).mockImplementation((...args) => args.join("/"));
});

afterEach(() => {
process.env.DOCS_DIR = undefined;
});
afterEach(() => {
process.env.DOCS_DIR = undefined;
});

test("processes markdown files and replaces README links", () => {
replaceLinks("/test/docs");
test("processes markdown files and replaces README links", () => {
replaceLinks("/test/docs");

expect(fs.writeFileSync).toHaveBeenCalledWith(
"/test/docs/file.md",
"[Test](/)",
"utf8"
);
expect(mockConsole.log).toHaveBeenCalledWith(
"Processing directory: /test/docs"
);
});
expect(fs.writeFileSync).toHaveBeenCalledWith(
"/test/docs/file.md",
"[Test](/)",
"utf8",
);
expect(mockConsole.log).toHaveBeenCalledWith(
"Processing directory: /test/docs",
);
});

test("recursively processes subdirectories", () => {
replaceLinks("/test/docs");
test("recursively processes subdirectories", () => {
replaceLinks("/test/docs");

expect(fs.readdirSync).toHaveBeenCalledTimes(2);
expect(fs.readdirSync).toHaveBeenCalledWith("/test/docs");
expect(fs.readdirSync).toHaveBeenCalledWith("/test/docs/subdir");
});
expect(fs.readdirSync).toHaveBeenCalledTimes(2);
expect(fs.readdirSync).toHaveBeenCalledWith("/test/docs");
expect(fs.readdirSync).toHaveBeenCalledWith("/test/docs/subdir");
});

test("handles file system errors", () => {
vi.mocked(fs.readdirSync).mockImplementation(() => {
throw new Error("Test error");
});
test("handles file system errors", () => {
vi.mocked(fs.readdirSync).mockImplementation(() => {
throw new Error("Test error");
});

expect(() => replaceLinks("/test/docs")).toThrow("Test error");
expect(mockConsole.error).toHaveBeenCalled();
});
});
expect(() => replaceLinks("/test/docs")).toThrow("Test error");
expect(mockConsole.error).toHaveBeenCalled();
});
});

0 comments on commit 6840c3c

Please sign in to comment.