/
string.spec.ts
101 lines (89 loc) · 2.93 KB
/
string.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* eslint-disable unicorn/no-useless-undefined */
/* eslint-disable unicorn/no-null */
import { translate } from "../validation";
import {
coercedString,
endsWith,
length,
maxLength,
minLength,
nonEmptyString,
pattern,
startsWith,
string,
} from "./string";
it("accepts", () => {
expect(string().accepts("")).toBeTruthy();
expect(string().accepts(undefined)).toBeFalsy();
expect(string().accepts(null)).toBeFalsy();
});
it("validates", () => {
expect(string().validate("")).toBeUndefined();
expect(string({ required: "message" }).validate(undefined)).toMatchObject({
message: "message",
code: "required",
value: undefined,
});
});
describe("coercedString", () => {
it("parses", () => {
expect(coercedString().parse("a").parsedValue).toEqual("a");
expect(coercedString().parse(undefined).parsedValue).toEqual("undefined");
expect(coercedString().parse(true).parsedValue).toEqual("true");
expect(coercedString().parse(false).parsedValue).toEqual("false");
expect(coercedString().parse(null).parsedValue).toEqual("null");
expect(coercedString().parse([1, "a", 2]).parsedValue).toEqual("1,a,2");
expect(coercedString().parse({}).parsedValue).toEqual("[object Object]");
expect(coercedString().parse(() => 1).parsedValue).toEqual("() => 1");
});
});
describe("minLength", () => {
const schema = minLength(string(), 2);
it("validates", () => {
expect(schema.validate("12")).toBeUndefined();
expect(translate(schema.validate("1"))).toEqual("minLength(2)");
});
});
describe("maxLength", () => {
const schema = maxLength(string(), 2);
it("validates", () => {
expect(schema.validate("12")).toBeUndefined();
expect(translate(schema.validate("123"))).toEqual("maxLength(2)");
});
});
describe("minLength", () => {
const schema = length(string(), 2);
it("validates", () => {
expect(schema.validate("12")).toBeUndefined();
expect(translate(schema.validate("1"))).toEqual("length(2)");
expect(translate(schema.validate("123"))).toEqual("length(2)");
});
});
describe("nonEmptyString", () => {
const schema = nonEmptyString(string());
it("validates", () => {
expect(schema.validate("12")).toBeUndefined();
expect(translate(schema.validate(""))).toEqual("minLength(1)");
});
});
describe("pattern", () => {
const schema = pattern(string(), /1/);
it("validates", () => {
expect(schema.validate("12")).toBeUndefined();
expect(translate(schema.validate("2"))).toEqual("pattern(/1/)");
});
});
describe("startsWith", () => {
const schema = startsWith(string(), "1");
it("validates", () => {
expect(schema.validate("12")).toBeUndefined();
expect(translate(schema.validate("2"))).toEqual("startsWith(1,)");
});
});
describe("endsWith", () => {
const schema = endsWith(string(), "2");
it("validates", () => {
expect(schema.validate("12")).toBeUndefined();
expect(translate(schema.validate("1"))).toEqual("endsWith(2,)");
});
});