Skip to content

Commit 64c237b

Browse files
committed
Added and regrouped tests for expected compile errors. Added test suites for multiple arrays in the root schema, objects without props and arrays without items.
1 parent b027692 commit 64c237b

File tree

1 file changed

+129
-9
lines changed

1 file changed

+129
-9
lines changed

test/validator.spec.js

Lines changed: 129 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,16 @@ describe("Test resolveMessage", () => {
103103
expect(res).toBe("Field country and again country. Expected: London, actual: 350.");
104104
});
105105

106+
it("should not resolve unknown errors", () => {
107+
let res = v.resolveMessage({ type: "XXX"});
108+
expect(res).toBeUndefined();
109+
});
106110
});
107111

108112
describe("Test compile (unit test)", () => {
109113

110114
const v = new Validator();
111-
v._processRule = jest.fn();
115+
v._processRule = jest.fn(v._processRule.bind(v));
112116

113117
it("should call processRule", () => {
114118
v.compile({
@@ -131,8 +135,9 @@ describe("Test compile (unit test)", () => {
131135
{ type: "string", min: 2 }
132136
]);
133137

134-
expect(v._processRule).toHaveBeenCalledTimes(2);
138+
expect(v._processRule).toHaveBeenCalledTimes(3);
135139
expect(v._processRule).toHaveBeenCalledWith({"type": "array", items: "number"}, null, false);
140+
expect(v._processRule).toHaveBeenCalledWith("number", null, false);
136141
expect(v._processRule).toHaveBeenCalledWith({"type": "string", min: 2 }, null, false);
137142
});
138143

@@ -163,6 +168,33 @@ describe("Test compile (unit test)", () => {
163168
v.compile([], []);
164169
}).toThrowError();
165170
});
171+
172+
it("should throw error if the type is invalid", () => {
173+
expect(() => {
174+
v.compile({ id: { type: "unknow" } });
175+
}).toThrowError("Invalid 'unknow' type in validator schema!");
176+
});
177+
178+
it.skip("should throw error if object has array props", () => {
179+
// TODO: This schema compiles, but never matches anything
180+
const schema = {
181+
invalid: { type: "object", props: [ { type: "string" }, { type: "number" } ] }
182+
};
183+
184+
expect(() => {
185+
v.compile(schema);
186+
}).toThrowError();
187+
});
188+
189+
it("should throw error if object has string props", () => {
190+
const schema = {
191+
invalid: { type: "object", props: "string" }
192+
};
193+
194+
expect(() => {
195+
v.compile(schema);
196+
}).toThrowError();
197+
});
166198
});
167199

168200
describe("Test _processRule", () => {
@@ -195,12 +227,6 @@ describe("Test _processRule", () => {
195227
expect(res[0].iterate).toBe(false);
196228
});
197229

198-
it("should throw error if the type is invalid", () => {
199-
expect(() => {
200-
v._processRule({ type: "unknow" }, "id", false);
201-
}).toThrowError("Invalid 'unknow' type in validator schema!");
202-
});
203-
204230
it("should call compile if type is object", () => {
205231
let res = v._processRule({ type: "object", props: {
206232
id: "number"
@@ -956,4 +982,98 @@ describe("Test multiple rules with arrays", () => {
956982

957983
});
958984

959-
});
985+
});
986+
987+
describe("Test multiple array in root", () => {
988+
const v = new Validator();
989+
990+
let schema = [
991+
{
992+
type: "array",
993+
items: "string"
994+
},
995+
{
996+
type: "array",
997+
items: "number"
998+
}
999+
];
1000+
1001+
let check = v.compile(schema);
1002+
1003+
it("should give true if first array is given", () => {
1004+
let obj = ["hello", "there", "this", "is", "a", "test"];
1005+
1006+
let res = check(obj);
1007+
1008+
expect(res).toBe(true);
1009+
});
1010+
1011+
it("should give true if second array is given", () => {
1012+
let obj = [1, 3, 3, 7];
1013+
1014+
let res = check(obj);
1015+
1016+
expect(res).toBe(true);
1017+
});
1018+
1019+
it("should give error if the array is broken", () => {
1020+
let obj = ["hello", 3];
1021+
1022+
let res = check(obj);
1023+
1024+
expect(res).toBeInstanceOf(Array);
1025+
expect(res.length).toBe(2);
1026+
expect(res[0].type).toBe("string");
1027+
expect(res[0].field).toBe("[1]");
1028+
1029+
expect(res[1].type).toBe("number");
1030+
expect(res[1].field).toBe("[0]");
1031+
});
1032+
1033+
it("should give error if the array is broken", () => {
1034+
let obj = [true, false];
1035+
let res = check(obj);
1036+
1037+
expect(res).toBeInstanceOf(Array);
1038+
expect(res.length).toBe(4);
1039+
expect(res[0].type).toBe("string");
1040+
expect(res[0].field).toBe("[0]");
1041+
1042+
expect(res[1].type).toBe("string");
1043+
expect(res[1].field).toBe("[1]");
1044+
1045+
});
1046+
1047+
});
1048+
1049+
describe("Test object without props", () => {
1050+
const v = new Validator();
1051+
1052+
it("should compile and validate", () => {
1053+
const schema = {
1054+
valid: { type: "object" }
1055+
};
1056+
1057+
const check = v.compile(schema);
1058+
expect(check).toBeInstanceOf(Function);
1059+
1060+
const res = check({ valid: { a: "b" } });
1061+
expect(res).toBe(true);
1062+
});
1063+
});
1064+
1065+
describe("Test array without items", () => {
1066+
const v = new Validator();
1067+
1068+
it("should compile and validate", () => {
1069+
const schema = {
1070+
valid: { type: "array" }
1071+
};
1072+
1073+
const check = v.compile(schema);
1074+
expect(check).toBeInstanceOf(Function);
1075+
1076+
const res = check({ valid: [1, 2, 3] });
1077+
expect(res).toBe(true);
1078+
});
1079+
});

0 commit comments

Comments
 (0)