Skip to content

Commit

Permalink
test: add unit tests for internal utils (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Sato1995 committed Mar 18, 2024
1 parent 7ee947b commit 2a3b7d3
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { describe, it, expect } from "vitest";
import {
toArray,
formatLineColumns,
resolveValue,
CLIError,
} from "../src/_utils";
import type { Resolvable } from "../src/types";

describe("toArray()", () => {
it.concurrent("should return empty array if input is undefined", () => {
expect(toArray(undefined)).toEqual([]);
});

it.concurrent("should return the same array if input is an array", () => {
expect(toArray([1, 2, 3])).toEqual([1, 2, 3]);
});

it.concurrent(
"should return an array containing the input if it is not an array",
() => {
expect(toArray(1)).toEqual([1]);
expect(toArray("hello")).toEqual(["hello"]);
},
);
});
describe("formatLineColumns()", () => {
it.concurrent("should format lines correctly", () => {
const lines = [
["Name", "Age"],
["Alice", "30"],
["Bob", "40"],
];
const result = formatLineColumns(lines);
expect(result).toBe(" Name Age\nAlice 30 \n Bob 40 ");
});

it.concurrent("should prepend a line prefix if provided", () => {
const lines = [
["Name", "Age"],
["Alice", "30"],
["Bob", "40"],
];
const result = formatLineColumns(lines, "--");
expect(result).toBe("-- Name --Age\n--Alice --30 \n-- Bob --40 ");
});
});

describe("resolveValue()", () => {
it("should resolve a non-function value correctly", () => {
const value: Resolvable<number> = 42;
const result = resolveValue(value);
expect(result).toBe(42);
});

it("should resolve a function value correctly", () => {
// eslint-disable-next-line unicorn/consistent-function-scoping
const value: Resolvable<number> = () => 42;
const result = resolveValue(value);
expect(result).toBe(42);
});
});

describe("CLIError", () => {
it("should correctly create an instance with a message", () => {
const error = new CLIError("An error occurred");
expect(error.message).toBe("An error occurred");
expect(error.name).toBe("CLIError");
expect(error.code).toBeUndefined();
});

it("should correctly create an instance with a message and code", () => {
const error = new CLIError("An error occurred", "ERROR_CODE");
expect(error.message).toBe("An error occurred");
expect(error.name).toBe("CLIError");
expect(error.code).toBe("ERROR_CODE");
});
});

0 comments on commit 2a3b7d3

Please sign in to comment.