24 lines
810 B
TypeScript
24 lines
810 B
TypeScript
import { expect, test } from "vite-plus/test";
|
|
|
|
import { field, isMapping, isObject } from "./json.ts";
|
|
|
|
test("reads a named field of a parsed object", () => {
|
|
const value: unknown = JSON.parse('{"login":"bot","user":{"login":"nested"}}');
|
|
expect(field(value, "login")).toBe("bot");
|
|
expect(field(field(value, "user"), "login")).toBe("nested");
|
|
expect(field(value, "missing")).toBeUndefined();
|
|
});
|
|
|
|
test("reads no field of a value that is not an object", () => {
|
|
for (const value of [null, undefined, "text", 1, true]) {
|
|
expect(field(value, "login")).toBeUndefined();
|
|
}
|
|
});
|
|
|
|
test("counts arrays as objects but not as mappings", () => {
|
|
expect(isObject([])).toBe(true);
|
|
expect(isMapping([])).toBe(false);
|
|
expect(isMapping({})).toBe(true);
|
|
expect(field(["first"], "0")).toBe("first");
|
|
});
|