Skip to content

Commit

Permalink
test: add utils test case (#188)
Browse files Browse the repository at this point in the history
* add utils test case

* fix prettier

---------

Co-authored-by: lotteam004 <lotteam004@proton.me>
  • Loading branch information
lotteam004 and lotteam004 authored Sep 4, 2024
1 parent 5eef565 commit f3a31db
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion packages/core/tests/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defaultBech32Config } from "../src/utils";
import { defaultBech32Config, objectToBase64, nonNullable } from "../src/utils";

describe("defaultBech32Config", () => {
test("returns the correct config", () => {
Expand All @@ -14,3 +14,44 @@ describe("defaultBech32Config", () => {
});
});
});

describe("objectToBase64", () => {
test("should convert an object to a Base64 string", () => {
const obj = { name: "John", age: 30 };
const base64String = objectToBase64(obj);
expect(base64String).toBe(Buffer.from(JSON.stringify(obj)).toString("base64"));
});

test("should return an empty object as a Base64 string", () => {
const obj = {};
const base64String = objectToBase64(obj);
expect(base64String).toBe(Buffer.from(JSON.stringify(obj)).toString("base64"));
});

test("should handle nested objects", () => {
const obj = { user: { name: "John", age: 30 }, active: true };
const base64String = objectToBase64(obj);
expect(base64String).toBe(Buffer.from(JSON.stringify(obj)).toString("base64"));
});
});

describe("nonNullable", () => {
test("should return true for non-nullable values", () => {
expect(nonNullable("Hello")).toBe(true);
expect(nonNullable(42)).toBe(true);
expect(nonNullable({})).toBe(true);
expect(nonNullable([])).toBe(true);
});

test("should return false for null or undefined values", () => {
expect(nonNullable(null)).toBe(false);
expect(nonNullable(undefined)).toBe(false);
});

test("should work correctly wtesth type guards", () => {
const value: string | null = "Hello";
if (nonNullable(value)) {
expect(typeof value).toBe("string");
}
});
});

0 comments on commit f3a31db

Please sign in to comment.