-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
268 lines (248 loc) · 8.17 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
/* eslint-disable no-unused-expressions */
/* eslint-disable no-undef */
const chai = require('chai');
const mongoose = require('mongoose');
const { generateMock } = require('./index');
describe('Basic Schema with all data types', () => {
it('Should throw error when valid schema is not used', () => {
const userSchema = {
firstName: String,
age: Number,
};
chai
.expect(() => generateMock(userSchema))
.to.throw('Valid mongoose schema is required to generate mock');
});
it('Should return mock object respecting mongoose schema types', () => {
const userSchema = new mongoose.Schema({
firstName: String,
lastName: String,
birthDate: Date,
isActive: Boolean,
age: Number,
salary: mongoose.Decimal128,
accountBalance: BigInt,
activeAccounts: [String],
bufferField: Buffer,
mixedField: mongoose.Schema.Types.Mixed,
});
const user = new mongoose.Document({}, userSchema);
const mockObj = generateMock(userSchema);
user.set(mockObj);
const error = user.validateSync();
chai.expect(error).to.be.undefined;
});
});
describe('Schema with nested schema', () => {
const addressSchema = new mongoose.Schema({
street: String,
city: String,
zipCode: String,
bufferField: Buffer,
mixedField: mongoose.Schema.Types.Mixed,
});
const userSchema = new mongoose.Schema({
firstName: String,
activeAccounts: [String],
address: addressSchema,
});
it('Should return mock obj conforming to both root and child schemas', () => {
const user = new mongoose.Document({}, userSchema);
const mockObj = generateMock(userSchema);
user.set(mockObj);
const error = user.validateSync();
chai.expect(error).to.be.undefined;
});
});
describe('Schema with enum values', () => {
const addressSchema = new mongoose.Schema({
street: String,
city: String,
zipCode: String,
country: {
type: String,
enum: ['India', 'USA', 'Others'],
},
});
const userSchema = new mongoose.Schema({
firstName: String,
lastName: String,
accountType: {
type: String,
enum: ['Savings', 'Personal'],
},
address: addressSchema,
});
it('Should return mock obj conforming to enum values in both root and nested schemas', () => {
const user = new mongoose.Document({}, userSchema);
const mockObj = generateMock(userSchema);
user.set(mockObj);
const error = user.validateSync();
chai.expect(error).to.be.undefined;
});
});
describe('Schema with min/max validators', () => {
const userSchema = new mongoose.Schema({
firstName: String,
});
describe('Min validation', () => {
const minTestSchema = userSchema.clone();
const minValue = 18;
minTestSchema.add({
age: { type: Number, min: minValue },
});
it('Should return mock obj with age field greater than equal to min', () => {
const mockObj = generateMock(minTestSchema);
chai.expect(mockObj.age).to.be.greaterThanOrEqual(minValue);
});
});
describe('Max validation', () => {
const maxTestSchema = userSchema.clone();
const maxValue = 200;
maxTestSchema.add({
age: { type: Number, max: maxValue },
});
it('Should return mock obj with age field lesser than equal to max', () => {
const mockObj = generateMock(maxTestSchema);
chai.expect(mockObj.age).to.be.lessThanOrEqual(maxValue);
});
});
describe('Min & Max validation', () => {
const minMaxTestSchema = userSchema.clone();
const minValue = 18;
const maxValue = 200;
minMaxTestSchema.add({
age: { type: Number, min: minValue, max: maxValue },
});
it('Should return mock obj with age field within min max range', () => {
const mockObj = generateMock(minMaxTestSchema);
chai.expect(mockObj.age).to.be.greaterThanOrEqual(minValue);
chai.expect(mockObj.age).to.be.lessThanOrEqual(maxValue);
});
});
describe('Nested Schema', () => {
const nestedSchema = {
field1: {
type: Number,
max: 55,
},
};
const nestedFieldSchema = userSchema.clone();
const minValue = 18;
const maxValue = 200;
nestedFieldSchema.add({
age: { type: Number, min: [minValue, 'Too young bruh!'], max: maxValue },
nestedField: nestedSchema,
nestedArray: [nestedSchema],
});
it('Should return mock obj with age field within min max range', () => {
const user = new mongoose.Document({}, nestedFieldSchema);
const mockObj = generateMock(nestedFieldSchema);
user.set(mockObj);
const error = user.validateSync();
chai.expect(error).to.be.undefined;
});
});
});
describe('Schema with minLength/maxLength validators', () => {
const userSchema = new mongoose.Schema({
firstName: String,
});
describe('MinLength validation', () => {
const minTestSchema = userSchema.clone();
const minLength = 10;
minTestSchema.add({
address: { type: String, minLength },
});
it('Should return mock obj with address field greater than 10 char length', () => {
const mockObj = generateMock(minTestSchema);
chai.expect(mockObj.address.length).to.be.greaterThanOrEqual(minLength);
});
});
describe('MaxLength validation', () => {
const maxTestSchema = userSchema.clone();
const maxLength = 25;
maxTestSchema.add({
address: { type: String, maxLength },
});
it('Should return mock obj with address field lesser than 25 char length', () => {
const mockObj = generateMock(maxTestSchema);
chai.expect(mockObj.address.length).to.be.lessThanOrEqual(maxLength);
});
});
describe('MinLength & MaxLength validation', () => {
const minMaxTestSchema = userSchema.clone();
const minLength = 5;
const maxLength = 25;
minMaxTestSchema.add({
address: { type: String, minLength, maxLength },
});
it('Should return mock obj with address field in range of minLength & maxLength', () => {
const mockObj = generateMock(minMaxTestSchema);
chai.expect(mockObj.address.length).to.be.greaterThanOrEqual(minLength);
chai.expect(mockObj.address.length).to.be.lessThanOrEqual(maxLength);
});
});
describe('Nested Schema', () => {
const nestedSchema = {
field1: {
type: String,
maxLength: 30,
minLength: 10,
},
};
const nestedFieldSchema = userSchema.clone();
const minLength = 5;
const maxLength = 15;
nestedFieldSchema.add({
address: { type: String, minLength: [minLength, 'Too short sir!Type more'], maxLength },
nestedField: nestedSchema,
nestedArray: [nestedSchema],
});
it('Should return mock obj with adress field within min max range', () => {
const user = new mongoose.Document({}, nestedFieldSchema);
const mockObj = generateMock(nestedFieldSchema);
user.set(mockObj);
const error = user.validateSync();
chai.expect(error).to.be.undefined;
});
});
});
describe('Options - requiredOnly', () => {
it('Should return mock object only with required fields, when requiredOnly is true', () => {
const addressSchema = new mongoose.Schema({
street: String,
city: {
type: String,
required: true,
},
zipCode: String,
});
const userSchema = new mongoose.Schema({
firstName: {
type: String,
required: true,
},
lastName: String,
age: Number,
activeAccounts: {
type: [String],
required: true,
},
address: {
type: addressSchema,
required: true,
},
});
const mockObj = generateMock(userSchema, { requiredOnly: true });
chai.expect(mockObj).to.haveOwnProperty('firstName');
chai.expect(mockObj).to.haveOwnProperty('activeAccounts');
chai.expect(mockObj.activeAccounts).to.be.an('array').to.have.lengthOf.at.least(1);
chai.expect(mockObj).to.not.haveOwnProperty('lastName');
chai.expect(mockObj).to.not.haveOwnProperty('age');
chai.expect(mockObj).to.haveOwnProperty('address');
chai.expect(mockObj.address).to.haveOwnProperty('city');
chai.expect(mockObj.address).to.not.haveOwnProperty('street');
chai.expect(mockObj.address).to.not.haveOwnProperty('zipCode');
});
});