-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
validator.spec.js
81 lines (73 loc) 路 2.15 KB
/
validator.spec.js
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
75
76
77
78
79
80
81
import { OptionValidator } from "../lib/index.js";
describe("OptionValidator", () => {
describe("validateTopLevelOptions", () => {
let v;
beforeAll(() => {
v = new OptionValidator("test-descriptor");
});
it("should throw when option key is not found", () => {
expect(() =>
v.validateTopLevelOptions(
{ unknown: "options" },
{ foo: "foo" },
"test",
),
).toThrow();
});
it("should throw when option key is an own property but not found", () => {
expect(() =>
v.validateTopLevelOptions(
{ hasOwnProperty: "foo" },
{
foo: "foo",
bar: "bar",
aLongPropertyKeyToSeeLevenPerformance: "a",
},
"test",
),
).toThrow();
});
});
describe("validateBooleanOption", () => {
let v;
beforeAll(() => {
v = new OptionValidator("test-descriptor");
});
it("`undefined` option returns false", () => {
expect(v.validateBooleanOption("test", undefined, false)).toBe(false);
});
it("`false` option returns false", () => {
expect(v.validateBooleanOption("test", false, false)).toBe(false);
});
it("`true` option returns true", () => {
expect(v.validateBooleanOption("test", true, false)).toBe(true);
});
it("array option is invalid", () => {
expect(() => {
v.validateBooleanOption("test", [], false);
}).toThrow();
});
});
describe("validateStringOption", () => {
let v;
beforeAll(() => {
v = new OptionValidator("test-descriptor");
});
it("`undefined` option default", () => {
expect(v.validateStringOption("test", undefined, "default")).toBe(
"default",
);
});
it("`value` option returns value", () => {
expect(v.validateStringOption("test", "value", "default")).toBe("value");
});
it("no default returns undefined", () => {
expect(v.validateStringOption("test", undefined)).toBe(undefined);
});
it("array option is invalid", () => {
expect(() => {
v.validateStringOption("test", [], "default");
}).toThrow();
});
});
});