/
test.js
288 lines (250 loc) · 10.7 KB
/
test.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import expect from 'expect';
import React from 'react';
const nodeInstance = <div></div>;
import { check, types, createCustomChecker, recursive } from './';
const {
any,
array,
arrayOf,
bool,
exactShape,
func,
instanceOf,
node,
number,
object,
objectOf,
oneOf,
oneOfType,
shape,
string,
} = types;
var zan = require('./');
describe('zan', () => {
it('is curry-able', () => {
expect( check(number)(123) ).toBeFalsy();
});
it('can do optional checks',() => {
expect( check(number.isOptional, 123) ).toBeFalsy();
expect( check(number.isOptional, undefined) ).toBeFalsy();
expect( check(number.isOptional, null) ).toBeFalsy();
expect( check(number.isOptional, 'x') ).toBeAn(Error);
});
it('can convert optional checks to required checks', () => {
expect(check(number.isOptional.isRequired, null)).toBeAn(Error);
expect(check(shape({num: number}).isOptional.isRequired, null)).toBeAn(Error);
expect(check(exactShape({num: number}).isOptional.isRequired, null)).toBeAn(Error);
});
it('can convert optional checks to required checks and back again', () => {
expect(check(number.isOptional.isRequired.isOptional, null)).toBeFalsy();
expect(check(shape({num: number}).isOptional.isRequired.isOptional, null)).toBeFalsy();
expect(check(exactShape({num: number}).isOptional.isRequired.isOptional, null)).toBeFalsy();
});
it ('can statically inspect if a type checker is requried or optional', () => {
expect(number.inspectIsOptional()).toEqual(false);
expect(number.isOptional.inspectIsOptional()).toEqual(true);
expect(number.isOptional.isRequired.inspectIsOptional()).toEqual(false);
expect(shape({num: number}).inspectIsOptional()).toEqual(false);
expect(shape({num: number}).isOptional.inspectIsOptional()).toEqual(true);
expect(shape({num: number}).isOptional.isRequired.inspectIsOptional()).toEqual(false);
expect(exactShape({num: number}).inspectIsOptional()).toEqual(false);
expect(exactShape({num: number}).isOptional.inspectIsOptional()).toEqual(true);
expect(exactShape({num: number}).isOptional.isRequired.inspectIsOptional()).toEqual(false);
});
it ('can statically inspect the arguments passed to a checker', () => {
expect(number.inspectArgs()).toEqual([]);
expect(shape({num: number}).inspectArgs()).toEqual([{num: number}]);
expect(exactShape({num: number}).inspectArgs()).toEqual([{num: number}]);
});
it ('can predictably inspect type type of an inspector', () => {
expect(number.inspectType()).toEqual(number);
expect(shape({num: number}).inspectType()).toEqual(shape);
expect(shape({num: number}.isOptional).inspectType()).toEqual(shape);
expect(shape({num: number}).isOptional.isRequired.inspectType()).toEqual(shape);
expect(exactShape({num: number}).inspectType()).toEqual(exactShape);
expect(exactShape({num: number}).isOptional.inspectType()).toEqual(exactShape);
});
it('can do optional shapes',() => {
expect( check(shape({num: number}), {num: 22} )).toBeFalsy();
expect( check(shape({num: number}), {num: 'x'} )).toBeAn(Error);
expect( check(shape({num: number}), null )).toBeAn(Error);
expect( check(shape({num: number}).isOptional, null )).toBeFalsy();
expect( check(exactShape({num: number}), null )).toBeAn(Error);
});
describe('simple types', () => {
it('can check if a value is a boolean', () => {
expect(check(bool, true)).toBeFalsy();
expect(check(bool, 1)).toBeAn(Error);
});
it('can check if a value is a function', () => {
expect(check(func, () => {})).toBeFalsy();
expect(check(func, 'str')).toBeAn(Error);
expect(check(func.isOptional, null)).toBeFalsy();
});
it('can check if a value is a number', () => {
expect(check(number, 123)).toBeFalsy();
expect(check(number, '123')).toBeAn(Error);
});
it('can check if a value is a node', () => {
expect(check(node, nodeInstance)).toBeFalsy();
expect(check(node, {})).toBeAn(Error);
});
it('can check if a value is a string', () => {
expect(check(string, 'foo')).toBeFalsy();
expect(check(string, /foo/)).toBeAn(Error);
});
it('is able to produce custom checkers', function() {
var customValidator = createCustomChecker((props, propName) => {
if (!(/(foo|bar)g?/.test(props[propName]))) {
return new Error('Value is not "foo" or "bar"');
}
});
expect(check(customValidator, 'foo')).toBeFalsy();
expect(check(customValidator, 'foog')).toBeFalsy();
expect(check(customValidator, 'bar')).toBeFalsy();
expect(check(customValidator, 'barg')).toBeFalsy();
expect(check(customValidator, 'fog')).toBeAn(Error);
});
});
describe('custom validators', () => {
it ('should have isOptional and isRequired extentions', () => {
var customValidator = createCustomChecker((props, propName) => {
if (!(/(foo|bar)g?/.test(props[propName]))) {
return new Error('Value is not "foo" or "bar"');
}
});
expect(check(customValidator, null)).toBeAn(Error);
expect(check(customValidator.isRequired, null)).toBeAn(Error);
expect(check(customValidator.isOptional, null)).toBeFalsy();
expect(check(customValidator.isOptional.isRequired, null)).toBeAn(Error);
expect(check(customValidator.isOptional.isRequired.isOptional, null)).toBeFalsy();
});
});
describe('complex types', function() {
describe('has a type `array` that', function() {
it('can check if an object is an array', function() {
expect(check(array, ['a', 22])).toBeFalsy();
expect(check(array, {a: 22})).toBeAn(Error)
});
});
describe('has a type `object` that', function() {
it('can check if an object is an object', function() {
expect(check(object, {a: 22})).toBeFalsy();
expect(check(object, ['a', 22])).toBeAn(Error)
});
});
describe('has a type `objectOf` that', function() {
it('can check if an object is an objectOf', function() {
expect(check(objectOf(number), {id: 22})).toBeFalsy();
expect(check(objectOf(number), {id: '22'})).toBeAn(Error)
});
});
describe('has a type `arrayOf` that', function() {
it('can check if an object is an arrayOf', function() {
expect(check(arrayOf(number), [22])).toBeFalsy();
expect(check(arrayOf(number), ['22'])).toBeAn(Error)
expect(check(arrayOf(number), [22, '22'])).toBeAn(Error)
});
});
describe('has a type `instanceOf` that', function() {
it('can check if an object is an instanceOf', function() {
var C = function C() {};
var c = new C();
expect(check(instanceOf(C), c)).toBeFalsy();
expect(check(instanceOf(C), {})).toBeAn(Error)
});
});
describe('has a type `oneOf` that', function() {
it('can check if an object is oneOf a list of specific value', function() {
expect(check(oneOf(['foo', 'bar']), 'bar')).toBeFalsy();
expect(check(oneOf(['foo', 'bar']), 'baz')).toBeAn(Error)
});
});
describe('has a type `any` that', function() {
it('can check if an object anything', function() {
expect(check(any, 'bar')).toBeFalsy();
expect(check(any, null)).toBeAn(Error)
});
});
describe('has a type `oneOfType` that', function() {
it('can check if an object is oneOfType a list of specific value', function() {
var stringOrFunction = oneOfType([string, func]);
expect(check(stringOrFunction, 'foo')).toBeFalsy();
expect(check(stringOrFunction, function() {})).toBeFalsy();
expect(check(stringOrFunction, 123)).toBeAn(Error)
});
});
describe('has a type `oneOfType` that', function() {
it('can check if an object is oneOfType a list of specific value', function() {
var stringOrFunction = oneOfType([string, func]);
expect(check(stringOrFunction, 'foo')).toBeFalsy();
expect(check(stringOrFunction, function() {})).toBeFalsy();
expect(check(stringOrFunction, 123)).toBeAn(Error)
});
});
describe('has a type `shape` that', function() {
it('can check if an object matches a shape', function() {
var myShape = shape({
name: string,
age: number,
address: shape({
street: string
})
});
expect(check(myShape, {extra: 123, name: 'Bob', age: 99, address: {street: '2nd Ave'}})).toBeFalsy();
expect(check(myShape, {extra: 123, name: 'Bob', age: 99, address: {street: 2}})).toBeAn(Error)
expect(check(shape({name: string.isOptional}), {})).toBeFalsy();
});
});
});
describe('has a type `exactShape` that', function() {
it('can check if an object exactly matches a exactShape', function() {
var myExactShape = exactShape({
name: string,
age: number,
address: exactShape({
street: string
})
});
expect(check(myExactShape, {name: 'Bob', age: 99, address: {street: '2nd Ave'}})).toBeFalsy();
expect(check(myExactShape, {extra: 123, name: 'Bob', age: 99, address: {street: '2nd Ave'}})).toBeAn(Error)
expect(check(myExactShape, {age: 99, address: {street: '2nd Ave'}})).toBeAn(Error)
expect(check(myExactShape, {name: 'Bob', age: 99, address: {street: 2}})).toBeAn(Error)
});
});
describe('recursive', () => {
it("doesn't alter 1 level deep objects", () => {
const shallowObject = {a: 1, b: '2', c: null, d: () => {}};
expect( recursive(shallowObject) ).toEqual(shallowObject);
});
it('can check at least one level', () => {
const deepChecker = recursive({
value: {
level1: string
}
}).value;
expect( check(deepChecker, {level1: 123} )).toBeAn(Error);
expect( check(deepChecker, {level1: '123'} )).toBeFalsy();
});
it('can check until the last turtle', () => {
const deepChecker = recursive({
value: {
foo: number,
level1: {
level2: [{
level3: [[string]]
}]
}
}
}).value;
expect( check(deepChecker, {foo: '3', level1: { level2: [{ level3: [['abc'], [], ['xyz']] }] }} )).toBeAn(Error);
expect( check(deepChecker, {foo: 3, level1: { level99: [{ level3: [['abc'], [], ['xyz']] }] }} )).toBeAn(Error);
expect( check(deepChecker, {foo: 3, level1: { level2: [{ level3: ['abc'] }] }} )).toBeAn(Error);
expect( check(deepChecker, {foo: 3, level1: { level2: [{ level3: [] }] }} )).toBeFalsy();
expect( check(deepChecker, {foo: 3, level1: { level2: [{ level3: [['abc'], [], ['xyz']] }] }} )).toBeFalsy();
});
});
it("doesn't alter React.PropTypes", () => {
expect(React.PropTypes.number.isRequired.isOptional).toBeFalsy();
});
});