-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.ts
74 lines (56 loc) · 2.21 KB
/
index.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
63
64
65
66
67
68
69
70
71
72
73
74
import { Buffer } from "buffer";
import { UUIDSchemaType } from "./index";
describe("UUIDSchemaType", () => {
it("should cast a valid UUID string to Buffer", () => {
const uuidString = "7b7e8257-36d4-4a7f-b1f7-5d8e8de5e646";
const uuidBuffer = Buffer.from(uuidString, "hex");
const uuidSchemaType = new UUIDSchemaType("uuid");
const castValue = uuidSchemaType.cast(uuidString);
expect(castValue).toEqual(uuidBuffer);
});
it("should throw an error for an invalid UUID string", () => {
const invalidUuidString = "invalid-uuid-string";
const uuidSchemaType = new UUIDSchemaType("uuid");
expect(() => {
uuidSchemaType.cast(invalidUuidString);
}).toThrowError(`Value is not a valid UUID string: ${invalidUuidString}`);
});
it("should return the original Buffer value for castForQuery", () => {
const uuidBuffer = Buffer.from(
"7b7e8257-36d4-4a7f-b1f7-5d8e8de5e646",
"hex"
);
const uuidSchemaType = new UUIDSchemaType("uuid");
const castValue = uuidSchemaType.castForQuery(uuidBuffer);
expect(castValue).toEqual(uuidBuffer);
});
it("should throw an error for an invalid value in castForQuery", () => {
const invalidValue = 123;
const uuidSchemaType = new UUIDSchemaType("uuid");
expect(() => {
uuidSchemaType.castForQuery(invalidValue);
}).toThrowError(
`UUID must be a string or buffer, got ${typeof invalidValue}`
);
});
it("should convert a Buffer value to a UUID string using the getter", () => {
const uuidString = "7b7e8257-36d4-4a7f-b1f7-5d8e8de5e646";
const uuidBuffer = Buffer.from(
"7b7e8257-36d4-4a7f-b1f7-5d8e8de5e646",
"hex"
);
const uuidSchemaType = new UUIDSchemaType("uuid");
uuidSchemaType.toString = jest.fn(() => uuidString);
const result = uuidSchemaType.get((value: any) => value);
expect(result).toEqual(uuidString);
});
it("should return true for checkRequired if the value is a Buffer", () => {
const uuidBuffer = Buffer.from(
"7b7e8257-36d4-4a7f-b1f7-5d8e8de5e646",
"hex"
);
const uuidSchemaType = new UUIDSchemaType("uuid");
const isRequired = uuidSchemaType.checkRequired(uuidBuffer);
expect(isRequired).toBe(true);
});
});