-
Notifications
You must be signed in to change notification settings - Fork 1
/
validation.spec.ts
66 lines (63 loc) · 1.66 KB
/
validation.spec.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
/* eslint-disable unicorn/no-useless-undefined */
import { mergeValidations, ValidationIssue } from "./validation";
type S = {
array: string[];
set: Set<string>;
map: Map<string, string>;
nestedArray: { field: string }[];
nestedObject: { field: string };
};
type T = {
array: string[];
set: Set<string>;
map: Map<string, string>;
nestedArray: { field: string }[];
nestedObject: { field: string; field2: number };
};
const error = new ValidationIssue("additionalProperty", undefined, undefined);
describe("mergeValidations", () => {
it("returns right if left is success", () => {
expect(mergeValidations<S, T>(undefined, error)).toEqual(error);
});
it("returns left if it is a validation error", () => {
expect(mergeValidations<S, T>(error, { nestedObject: error })).toEqual(
error
);
});
it("merges arrays", () => {
expect(
mergeValidations<S, T>({ array: [error] }, { array: [error] })
).toEqual({ array: [error, error] });
});
it("merges sets", () => {
expect(
mergeValidations<S, T>(
{ set: new Set([error]) },
{ set: new Set([error]) }
)
).toEqual({ set: new Set([error, error]) });
});
it("merges maps", () => {
expect(
mergeValidations<S, T>(
{
map: new Map([
["1", error],
["2", error],
]),
},
{ map: new Map([["2", error]]) }
)
).toEqual({
map: new Map([
["1", error],
["2", error],
]),
});
});
it("merges objects", () => {
expect(
mergeValidations<S, T>({ array: [error] }, { array: [error] })
).toEqual({ array: [error, error] });
});
});