-
Notifications
You must be signed in to change notification settings - Fork 209
/
Copy pathencodeKey.test.ts
46 lines (42 loc) · 1.35 KB
/
encodeKey.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
import { attest } from "@ark/attest";
import { describe, it } from "vitest";
import { encodeKey } from "./encodeKey";
import { defineTable } from "@latticexyz/store/config/v2";
describe("encodeKey", () => {
it("should encode a key to a string", () => {
const table = defineTable({
label: "test",
schema: { field1: "uint32", field2: "uint256", field3: "string" },
key: ["field1", "field2"],
});
attest(encodeKey({ table, key: { field1: 1, field2: 2n } })).snap("1|2");
});
it("should throw a type error if an invalid key is provided", () => {
const table = defineTable({
label: "test",
schema: { field1: "uint32", field2: "uint256", field3: "string" },
key: ["field1", "field2"],
});
attest(() =>
encodeKey({
table,
// @ts-expect-error Property 'field2' is missing in type '{ field1: number; }'
key: {
field1: 1,
},
}),
)
.throws(`Provided key is missing field field2.`)
.type.errors(`Property 'field2' is missing in type '{ field1: number; }'`);
attest(
encodeKey({
table,
key: {
field1: 1,
// @ts-expect-error Type 'string' is not assignable to type 'bigint'.
field2: "invalid",
},
}),
).type.errors(`Type 'string' is not assignable to type 'bigint'.`);
});
});