Debian Codegraph @

frontend/test/misc.test.ts

raw
1 import { test } from "uvu";
2 import assert from "uvu/assert";
3
4 import { is_non_empty, last_element } from "../src/lib/array";
5 import { errorWithCauses } from "../src/lib/errors";
6 import { getInterval } from "../src/lib/interval";
7 import { parseJSON } from "../src/lib/json";
8
9 test("validate interval", () => {
10 assert.equal(getInterval("year"), "year");
11 assert.equal(getInterval("yasdfaear"), "month");
12 });
13
14 test("parse json", () => {
15 assert.equal(parseJSON("{}").unwrap(), {});
16
17 const invalid = parseJSON("invalid").unwrap_err();
18 assert.instance(invalid, SyntaxError);
19 });
20
21 test("non-empty array helpers", () => {
22 assert.is(is_non_empty([]), false);
23 const a = [false, true];
24 assert.ok(is_non_empty(a));
25 assert.is(last_element(a), true);
26 });
27
28 test("print out error with causes", () => {
29 const err1 = new Error("a reason");
30 const err2 = new Error("b reason", { cause: err1 });
31
32 assert.equal(errorWithCauses(err1), "a reason");
33 assert.equal(errorWithCauses(err2), "b reason\n Caused by: a reason");
34 });
35
36 test.run();