-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy patharray.spec.js
More file actions
73 lines (60 loc) · 2.44 KB
/
array.spec.js
File metadata and controls
73 lines (60 loc) · 2.44 KB
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
import { sampleArray } from '../../src/samplers/array.js';
describe('sampleArray', () => {
var res;
it('should return empty array by default', () => {
res = sampleArray({});
expect(res).to.deep.equal([]);
});
it('should return elements of correct type', () => {
res = sampleArray({items: {type: 'number'}});
expect(res).to.deep.equal([0]);
res = sampleArray({contains: {type: 'number'}});
expect(res).to.deep.equal([0]);
});
it('should return correct number of elements based on maxItems', () => {
res = sampleArray({items: {type: 'number'}, maxItems: 0});
expect(res).to.deep.equal([]);
});
it('should return correct number of elements based on minItems', () => {
res = sampleArray({items: {type: 'number'}, minItems: 3});
expect(res).to.deep.equal([0, 0, 0]);
});
it('should correctly sample tuples', () => {
res = sampleArray({items: [{type: 'number'}, {type: 'string'}, {}]});
expect(res).to.deep.equal([0, 'string', null]);
});
describe('disableNonRequiredAutoGen', () => {
it('should return null if omissible=true and primitive type item has no example', () => {
res = sampleArray({ items: { type: 'string' } }, { omissible: true, disableNonRequiredAutoGen: true });
expect(res).to.be.null;
});
it('should return null if omssible=true and object type item has no example', () => {
res = sampleArray({
items: {
type: 'object',
properties: {
a: { type: 'string' },
},
}
}, { omissible: true, disableNonRequiredAutoGen: true });
expect(res).to.be.null;
});
it('should return valid array samples if omissible=false and primitive type item has no example', () => {
// the sample must be valid to schema and show the array item type when the array is not omitted
res = sampleArray({ items: { type: 'string' }, minItems: 2 }, { disableNonRequiredAutoGen: true });
expect(res).to.deep.equal(['string', 'string']);
});
it('should return array of empty object if omssible=false and object type item has no example', () => {
// the sample must be valid to schema and show the array item type when the array is not omitted
res = sampleArray({
items: {
type: 'object',
properties: {
a: { type: 'string' },
},
}
}, { disableNonRequiredAutoGen: true });
expect(res).to.deep.equal([{}]);
});
});
});