import { faker } from "@faker-js/faker"; import { describe, expect, it } from "vitest"; import { NonEmptyString } from "./non-empty-string.js"; describe("NonEmptyString", () => { const WHITESPACE_CHARACTERS = { NEWLINE: "\n", SPACE: " ", TAB: "\t", }; it("rejects true", () => // @ts-expect-error because only strings are valid inputs expect(NonEmptyString.safeDecode(true).success).toBe(false)); it("rejects false", () => // @ts-expect-error because only strings are valid inputs expect(NonEmptyString.safeDecode(false).success).toBe(false)); it("rejects integers", { repeats: 10 }, () => // @ts-expect-error because only strings are valid inputs expect(NonEmptyString.safeDecode(faker.number.int()).success).toBe(false), ); it("rejects bigints", { repeats: 10 }, () => // @ts-expect-error because only strings are valid inputs expect(NonEmptyString.safeDecode(faker.number.bigInt()).success).toBe( false, ), ); it("rejects floats/numbers", { repeats: 10 }, () => // @ts-expect-error because only strings are valid inputs expect(NonEmptyString.safeDecode(faker.number.float()).success).toBe(false), ); it("rejects arrays", () => // @ts-expect-error because only strings are valid inputs expect(NonEmptyString.safeDecode([]).success).toBe(false)); it("rejects plain objects", () => // @ts-expect-error because only strings are valid inputs expect(NonEmptyString.safeDecode({}).success).toBe(false)); it("rejects empty strings", () => expect(NonEmptyString.safeDecode("").success).toBe(false)); it("rejects strings containing only whitespace characters", () => expect( NonEmptyString.safeDecode( faker.helpers .multiple(() => faker.helpers.enumValue(WHITESPACE_CHARACTERS), { count: { min: 1, max: 100 }, }) .join(""), ).success, ).toBe(false)); it("accepts strings with at least 1 non-whitespsace character", () => expect( NonEmptyString.safeDecode( faker.string.alphanumeric({ length: { min: 1, max: 255 } }), ).success, ).toBe(true)); });