-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.js
174 lines (141 loc) · 4.24 KB
/
index.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
'use strict';
const { assert, refute, sinon } = require('@sinonjs/referee-sinon');
const { schema, object, boolean } = require('./index');
/**
* @template I
* @typedef {import('./index').Infer<I>} Infer
*/
describe('schema', () => {
afterEach(() => {
sinon.restore();
});
it('returns a function with read, write and verify', () => {
const validate = schema(object({}));
assert.isFunction(validate);
assert.isFunction(validate.read);
assert.isFunction(validate.write);
assert.isFunction(validate.verify);
});
it('exposes type Object for object validator', () => {
const test = object({ test: schema.number });
const validate = schema(test);
assert.same(validate.type, 'Object');
assert.equals(validate.properties, { test: schema.number });
// @ts-expect-error
assert.isUndefined(validate.items);
});
it('exposes type Array', () => {
const test = schema.array(schema.number);
const validate = schema(test);
assert.same(validate.type, 'Array');
assert.same(validate.items, schema.number);
// @ts-expect-error
assert.isUndefined(validate.properties);
});
it('exposes type number', () => {
const validate = schema(schema.number);
assert.same(validate.type, 'number');
// @ts-expect-error
assert.isUndefined(validate.properties);
// @ts-expect-error
assert.isUndefined(validate.items);
});
it('exposes type string', () => {
const validate = schema(schema.string);
assert.same(validate.type, 'string');
});
it('does not invoke the given validator property accessors', () => {
const obj = schema.object({});
const read = sinon.replaceGetter(obj['verify'], 'read', sinon.fake());
const write = sinon.replaceGetter(obj['verify'], 'write', sinon.fake());
schema(obj);
refute.called(read);
refute.called(write);
});
it('returns the validated object', () => {
const validate = schema(object({ test: boolean }));
const result = validate({ test: true });
assert.isObject(result);
assert.isTrue(result.test); // shouldn't file type check
});
it('fails with default error code', () => {
const validate = schema(object({}));
assert.exception(
() => {
// @ts-expect-error
validate(null);
},
{ code: 'E_SCHEMA' }
);
});
it('fails with error code from schema option', () => {
const validate = schema(object({}), { error_code: 'INVALID' });
assert.exception(
() => {
// @ts-expect-error
validate(null);
},
{ code: 'INVALID' }
);
});
it('fails in reader with error code from schema option', () => {
const validate = schema(object({ test: boolean }), {
error_code: 'INVALID'
});
assert.exception(
() => {
// @ts-expect-error
validate.read(null);
},
{ code: 'INVALID' }
);
const valid = validate.read({ test: true });
assert.exception(
() => {
// @ts-expect-error
return valid.invalid;
},
{ code: 'INVALID' }
);
});
it('fails in writer with error code from schema option', () => {
const validate = schema(object({}), { error_code: 'INVALID' });
assert.exception(
() => {
// @ts-expect-error
validate.write(null);
},
{ code: 'INVALID' }
);
});
context('Infer', () => {
it('creates a type with Infer for a validator', () => {
/* eslint-disable-next-line jsdoc/no-undefined-types */
/** @typedef {Infer<validate>} Test */
const validate = object({ test: boolean });
assert.isTrue(validate(/** @type {Test} */ ({ test: true })));
assert.isFalse(
validate(
// @ts-expect-error
/** @type {Test} */ ({
test: 'test'
})
)
);
});
it('creates a type with Infer for a schema', () => {
/* eslint-disable-next-line jsdoc/no-undefined-types */
/** @typedef {Infer<validate>} Test */
const validate = schema(object({ test: boolean }));
assert.equals(validate({ test: true }), { test: true });
assert.exception(() =>
validate(
// @ts-expect-error
/** @type {Test} */ ({
test: 'test'
})
)
);
});
});
});