-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathobject.spec.js
More file actions
222 lines (206 loc) · 5.51 KB
/
object.spec.js
File metadata and controls
222 lines (206 loc) · 5.51 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
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
import { sampleObject} from '../../src/samplers/object.js';
describe('sampleObject', () => {
let res;
it('should return emtpy object by default', () => {
res = sampleObject({});
expect(res).to.deep.equal({});
});
it('should instantiate all properties', () => {
res = sampleObject({properties: {
a: {type: 'string'},
b: {type: 'integer'}
}});
expect(res).to.deep.equal({
a: 'string',
b: 0
});
});
it('should skip readonly properties if skipReadOnly=true', () => {
res = sampleObject({properties: {
a: {type: 'string'},
b: {type: 'integer', readOnly: true}
}}, {skipReadOnly: true});
expect(res).to.deep.equal({
a: 'string'
});
});
it('should skip readonly properties in nested objects if skipReadOnly=true', () => {
res = sampleObject({properties: {
a: {type: 'string'},
b: {type: 'object', properties: {
b1: { type: 'number', readOnly: true },
b2: { type: 'number'}
}}
}}, {skipReadOnly: true});
expect(res).to.deep.equal({
a: 'string',
b: {
b2: 0
}
});
});
it('should skip readonly properties in oneOfs if skipReadOnly=true', () => {
res = sampleObject(
{
properties: {
a: { type: 'string' },
b: {
type: 'object',
oneOf: [
{
type: 'object',
properties: {
c: {
type: 'string',
},
},
},
{
type: 'object',
properties: {
d: {
type: 'string',
},
},
},
],
readOnly: true,
},
},
},
{ skipReadOnly: true }
);
expect(res).to.deep.equal({
a: 'string',
});
});
it('should skip writeonly properties if writeonly=true', () => {
res = sampleObject({properties: {
a: {type: 'string'},
b: {type: 'integer', writeOnly: true}
}}, {skipWriteOnly: true});
expect(res).to.deep.equal({
a: 'string'
});
});
it('should skip writeonly properties in nested objects if writeonly=true', () => {
res = sampleObject({properties: {
a: {type: 'string'},
b: {type: 'object', properties: {
b1: { type: 'number', writeOnly: true },
b2: { type: 'number'}
}}
}}, {skipWriteOnly: true});
expect(res).to.deep.equal({
a: 'string',
b: {
b2: 0
}
});
});
it('should skip writeonly properties in oneOfs if skipReadOnly=true', () => {
res = sampleObject(
{
properties: {
a: { type: 'string' },
b: {
type: 'object',
oneOf: [
{
type: 'object',
properties: {
c: {
type: 'string',
},
},
},
{
type: 'object',
properties: {
d: {
type: 'string',
},
},
},
],
writeOnly: true,
},
},
},
{ skipWriteOnly: true }
);
expect(res).to.deep.equal({
a: 'string',
});
});
it('should should instantiate 2 additionalProperties', () => {
res = sampleObject({additionalProperties: {type: 'string'}});
expect(res).to.deep.equal({
property1: 'string',
property2: 'string'
});
});
it('should skip non-required properties if skipNonRequired=true', () => {
res = sampleObject({
properties: {
a: {type: 'string'},
b: {type: 'integer'}
},
required: ['a']
}, {skipNonRequired: true});
expect(res).to.deep.equal({
a: 'string'
});
});
it('should pass propertyName context to samplers', () => {
res = sampleObject({
properties: {
fooId: {type: 'string', format: 'uuid'},
barId: {type: 'string', format: 'uuid'},
}
});
expect(res).to.deep.equal({
fooId: 'fb4274c7-4fcd-4035-8958-a680548957ff',
barId: '3c966637-4898-4972-9a9d-baefa6cd6c89'
});
});
describe('disableNonRequiredAutoGen', () => {
it('should skip non-required properties without explicit example value', () => {
res = sampleObject({
required: ['e'],
properties: {
a: { type: 'string', enum: ['foo', 'bar'] },
b: { type: 'integer', default: 100 },
c: { type: 'string' },
d: { type: 'string', example: 'Example' },
e: { type: 'string', enum: ['foo', 'bar'] },
},
}, { disableNonRequiredAutoGen: true });
expect(res).to.deep.equal({
b: 100,
d: 'Example',
e: 'foo',
});
});
it('should skip additional properties', () => {
res = sampleObject({
properties: {
a: { type: 'string', example: 'Example' },
},
additionalProperties: { type: 'string' }
}, { disableNonRequiredAutoGen: true });
expect(res).to.deep.equal({
a: 'Example'
});
});
it('should return null if omissible=true and no property has example', () => {
res = sampleObject({
properties: {
a: { type: 'string' },
b: { type: 'integer' }
},
}, { disableNonRequiredAutoGen: true, omissible: true });
expect(res).to.be.null;
});
});
});