generated from JoshuaKGoldberg/create-typescript-app
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
temporal.test.ts
62 lines (51 loc) · 1.39 KB
/
temporal.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { Temporal } from "@js-temporal/polyfill";
import { expect, test } from "vitest";
import { TsonType, createTson } from "../index.js";
const plainDate: TsonType<Temporal.PlainDate, string> = {
deserialize: (v) => Temporal.PlainDate.from(v),
key: "PlainDate",
serialize: (v) => v.toString(),
test: (v) => v instanceof Temporal.PlainDate,
};
const instant: TsonType<Temporal.Instant, string> = {
deserialize: (v) => Temporal.Instant.from(v),
key: "Instant",
serialize: (v) => v.toString(),
test: (v) => v instanceof Temporal.Instant,
};
const tson = createTson({
nonce: () => "__tson",
types: [plainDate, instant],
});
test("PlainDate", () => {
const expected = Temporal.PlainDate.from("2021-01-01");
const serialized = tson.serialize(expected);
expect(serialized).toMatchInlineSnapshot(`
{
"json": [
"PlainDate",
"2021-01-01",
"__tson",
],
"nonce": "__tson",
}
`);
const deserialized = tson.deserialize(serialized);
expect(deserialized).toEqual(expected);
});
test("Instant", () => {
const expected = Temporal.Instant.from("2021-01-01T00:00:00Z");
const serialized = tson.serialize(expected);
expect(serialized).toMatchInlineSnapshot(`
{
"json": [
"Instant",
"2021-01-01T00:00:00Z",
"__tson",
],
"nonce": "__tson",
}
`);
const deserialized = tson.deserialize(serialized);
expect(deserialized).toEqual(expected);
});