Skip to content

Commit

Permalink
test: add basic cases for getCommand and usageCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsquest committed Nov 15, 2024
1 parent b00fb24 commit bc7fbb6
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 15 deletions.
87 changes: 87 additions & 0 deletions src/commands.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import {
type Mock,
afterEach,
beforeEach,
describe,
expect,
spyOn,
test,
} from "bun:test";
import { getCommand, usageCommand } from "./commands.ts";

describe("getCommand", () => {
test("show config", () => {
const args = ["config"];

const command = getCommand(args);

expect(command.type).toEqual("config");
});

test("show loans", () => {
const args = ["loans"];

const command = getCommand(args);

expect(command.type).toEqual("loans");
});

test("show usage", () => {
const args = ["usage"];

const command = getCommand(args);

expect(command.type).toEqual("usage");
});

test("unknown command", () => {
const args = ["unknown"];

const command = getCommand(args);

expect(command.type).toEqual("usage");
});

test("no args", () => {
const args: string[] = [];

const command = getCommand(args);

expect(command.type).toEqual("usage");
});
});

describe("usage", () => {
let consoleLog: Mock<() => void>;
let consoleError: Mock<() => void>;

beforeEach(() => {
consoleLog = spyOn(console, "log");
consoleError = spyOn(console, "error");
});

afterEach(() => {
consoleLog.mockRestore();
consoleError.mockRestore();
});

test("no error message", async () => {
const cmd = usageCommand();

const result = await cmd();

expect(result.success).toEqual(true);
expect(consoleError).toHaveBeenCalledTimes(0);
expect(consoleLog.mock.calls.join(" ")).toMatch(/Usage: /);
});

test("with error message", async () => {
const cmd = usageCommand("OMG");

const result = await cmd();

expect(result.success).toEqual(true);
expect(consoleError.mock.calls.join(" ")).toMatch(/OMG/);
expect(consoleLog.mock.calls.join(" ")).toMatch(/Usage: /);
});
});
31 changes: 16 additions & 15 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ export type Command = {
run: () => Promise<Result>;
};

const commands = (errorMessage?: string) => async (): Promise<Result> => {
if (errorMessage) {
console.error(errorMessage);
console.error(); // blank line
}
console.log("Usage: npx mediathequeroubaix <command>");
console.log("Commands:");
console.log(" loans: List all loans");
console.log(" usage: Show this usage information");
return success(undefined);
};
export const usageCommand =
(errorMessage?: string) => async (): Promise<Result> => {
if (errorMessage) {
console.error(errorMessage);
console.error(); // blank line
}
console.log("Usage: npx mediathequeroubaix <command>");
console.log("Commands:");
console.log(" loans: List all loans");
console.log(" usage: Show this usage information");
return success(undefined);
};

const loanCommand = async (): Promise<Result> => {
console.error("Not yet implemented");
Expand Down Expand Up @@ -58,7 +59,7 @@ const printConfig = (config: object): Promise<void> => {
return Promise.resolve();
};

const showConfigCommand = async (): Promise<Result> => {
export const showConfigCommand = async (): Promise<Result> => {
const showConfig = pipe(
getHomeDir,
getConfigFilename,
Expand All @@ -75,21 +76,21 @@ export const getCommand = (args: string[]): Command => {
const firstArg = args[0];
switch (firstArg) {
case "usage":
return { type: "usage", run: commands() };
return { type: "usage", run: usageCommand() };
case "loans":
return { type: "loans", run: loanCommand };
case "config":
return { type: "config", run: showConfigCommand };
default:
return {
type: "usage",
run: commands(`Unknown command: ${firstArg}`),
run: usageCommand(`Unknown command: ${firstArg}`),
};
}
}

return {
type: "usage",
run: commands("Missing argument, none provided"),
run: usageCommand("Missing argument, none provided"),
};
};

0 comments on commit bc7fbb6

Please sign in to comment.