-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add basic cases for getCommand and usageCommand
- Loading branch information
Showing
2 changed files
with
103 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: /); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters