-
Notifications
You must be signed in to change notification settings - Fork 582
/
time.test.ts
190 lines (168 loc) · 5.3 KB
/
time.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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import timeLtePlugin from "../../src/content/plugins/validators/timeLte";
import timeGtePlugin from "../../src/content/plugins/validators/timeGte";
import { CmsContentModelFieldValidation } from "../../src/types";
const createValidator = (args): CmsContentModelFieldValidation => {
return {
name: "test-validator",
message: "test validation message",
settings: {
...args
}
};
};
describe("time validators", () => {
const context: any = {};
const gtePlugin = timeGtePlugin();
const ltePlugin = timeLtePlugin();
const gteValidationCorrectValues = [
["11:22:33", "11:22:33"],
["11:22:34", "11:22:33"],
["11:23:33", "11:22:33"],
["12:22:33", "11:22:33"],
["11:22", "11:21:59"],
["11:22:33", "11:22"],
["11:23", "11:22"]
];
test.each(gteValidationCorrectValues)("should pass gte validation", async (value, gteValue) => {
const validator = createValidator({
value: gteValue
});
const result = await gtePlugin.validator.validate({
value,
validator,
context,
field: {} as any,
contentModel: {} as any
});
expect(result).toEqual(true);
});
const gteValidationIncorrectValues = [
["11:22:33", "11:22:34"],
["11:22:33", "11:23:33"],
["11:22:33", "12:22:33"],
["11:22", "11:22:33"],
["11:22", "11:22:01"]
];
test.each(gteValidationIncorrectValues)(
"should not pass gte validation",
async (value, gteValue) => {
const validator = createValidator({
value: gteValue
});
const result = await gtePlugin.validator.validate({
value,
validator,
context,
field: {} as any,
contentModel: {} as any
});
expect(result).toEqual(false);
}
);
const lteValidationCorrectValues = [
["11:22:33", "11:22:33"],
["11:22:33", "11:22:34"],
["11:22:33", "11:23:33"],
["11:22:33", "12:22:33"],
["11:21:59", "12:22"],
["11:22:33", "12:23"]
];
test.each(lteValidationCorrectValues)(
"name should pass lte validation",
async (value, lteValue) => {
const validator = createValidator({
value: lteValue
});
const result = await ltePlugin.validator.validate({
value,
validator,
context,
field: {} as any,
contentModel: {} as any
});
expect(result).toEqual(true);
}
);
const lteValidationIncorrectValues = [
["11:22:33", "11:22:32"],
["11:22:33", "11:21:33"],
["11:22:33", "11:22"],
["11:23", "11:22:33"]
];
test.each(lteValidationIncorrectValues)(
"name should not pass lte validation",
async (value, lteValue) => {
const validator = createValidator({
value: lteValue
});
const result = await ltePlugin.validator.validate({
value,
validator,
context,
field: {} as any,
contentModel: {} as any
});
expect(result).toEqual(false);
}
);
const rangeValidationCorrectValues = [
["11:22:33", "10:00:00", "22:00:00"],
["10:00:00", "10:00:00", "22:00:00"],
["22:00:00", "10:00:00", "22:00:00"]
];
const validate = async ({ value, lteValidator, gteValidator }) => {
const lteValid = await ltePlugin.validator.validate({
value,
validator: lteValidator,
context,
field: {} as any,
contentModel: {} as any
});
const gteValid = await gtePlugin.validator.validate({
value,
validator: gteValidator,
context,
field: {} as any,
contentModel: {} as any
});
return lteValid && gteValid;
};
test.each(rangeValidationCorrectValues)(
"time should pass validation for being in given range",
async (value, gte, lte) => {
const lteValidator = createValidator({
value: lte
});
const gteValidator = createValidator({
value: gte
});
const result = await validate({
value,
lteValidator,
gteValidator
});
expect(result).toEqual(true);
}
);
const rangeValidationIncorrectValues = [
["11:22:33", "12:00:00", "22:00:00"],
["22:00:00", "10:00:00", "21:00:00"]
];
test.each(rangeValidationIncorrectValues)(
"time should not pass validation because it is not in range",
async (value, gte, lte) => {
const lteValidator = createValidator({
value: lte
});
const gteValidator = createValidator({
value: gte
});
const result = await validate({
value,
lteValidator,
gteValidator
});
expect(result).toEqual(false);
}
);
});