UNPKG

@joktec/mongo

Version:

JokTec - Mongo Service

200 lines 8.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const globals_1 = require("@jest/globals"); const helpers_1 = require("../helpers"); const models_1 = require("../models"); (0, globals_1.describe)('MongoHelper class', () => { (0, globals_1.describe)('flatten function', () => { (0, globals_1.it)('should flattening a simple object without nesting', () => { const condition = { _id: '507f1f77bcf86cd799439011', parentId: new models_1.ObjectId('656c096ad77a68cf9c495e28'), name: 'Alice', age: 25, }; const result = helpers_1.MongoHelper.flatten(condition); (0, globals_1.expect)(result).toMatchObject({ _id: models_1.ObjectId.create('507f1f77bcf86cd799439011'), parentId: models_1.ObjectId.create('656c096ad77a68cf9c495e28'), name: 'Alice', age: 25, }); }); (0, globals_1.it)('should flattening an object with nested properties', () => { const condition = { person: { name: 'Bob', age: null, birthday: new Date() }, location: { city: 'Los Angeles', state: null }, }; const result = helpers_1.MongoHelper.flatten(condition); (0, globals_1.expect)(result).toMatchObject({ 'person.name': 'Bob', 'person.age': null, 'person.birthday': globals_1.expect.any(Date), 'location.city': 'Los Angeles', 'location.state': null, }); }); (0, globals_1.it)('should flattening an object with null values', () => { const condition = { name: null, address: { street: null, city: 'Chicago' }, }; const result = helpers_1.MongoHelper.flatten(condition); (0, globals_1.expect)(result).toEqual({ name: null, 'address.street': null, 'address.city': 'Chicago', }); }); (0, globals_1.it)('should flattening an object with Mongoose operators', () => { const condition = { title: 'John Doe', author: { user: { age: { $gte: 18 }, deletedAt: null, }, }, }; const result = helpers_1.MongoHelper.flatten(condition); (0, globals_1.expect)(result).toEqual({ title: 'John Doe', 'author.user.age': { $gte: 18 }, 'author.user.deletedAt': null, }); }); }); (0, globals_1.describe)('parseFilter function', () => { (0, globals_1.it)('should handle empty object', () => { const condition = {}; const result = helpers_1.MongoHelper.parseFilter(condition); (0, globals_1.expect)(result).toEqual(condition); }); (0, globals_1.it)('should not modify a simple condition', () => { const result = helpers_1.MongoHelper.parseFilter({ status: 'activated', $text: { $search: 'Tìm đồ' }, parentId: '656c096ad77a68cf9c495e28', }); const condition = { status: 'activated', $text: { $search: 'Tìm đồ' }, parentId: models_1.ObjectId.create('656c096ad77a68cf9c495e28'), }; (0, globals_1.expect)(result).toEqual(condition); }); (0, globals_1.it)('should handle nested objects', () => { const condition = { _id: '123', foo: 'bar', nested: { _id: '123', baz: 'qux', foo: 'foo1', deeper: { _id: '456', foo: 'foo1', quux: 'corge' } }, 'nested.foo': 'foo', 'nested.deeper.foo': 'foo', }; const result = helpers_1.MongoHelper.parseFilter(condition); (0, globals_1.expect)(result).toEqual({ _id: '123', foo: 'bar', 'nested.foo': 'foo', 'nested._id': '123', 'nested.baz': 'qux', 'nested.deeper._id': '456', 'nested.deeper.quux': 'corge', 'nested.deeper.foo': 'foo', }); }); (0, globals_1.it)('should handle complex objects', () => { const condition = { type: 'service', status: 'activated', deletedAt: { $eq: null }, name: { $like: 'jo' }, nullableValue: { $nil: true }, defaultValue: { $empty: true }, date: new Date(), regex: /foo/i, nested: { _id: '123', foo: 'foo1', name: { $begin: 'jo' }, deeper: { _id: '456', foo: { $eq: 'foo1' }, name: { $end: 'jo' }, num: { $gte: 789 }, }, }, 'nested.foo': { $eq: 'foo' }, 'nested.deeper.foo': { $eq: 'foo' }, }; const result = helpers_1.MongoHelper.parseFilter(condition); (0, globals_1.expect)(result).toEqual({ type: 'service', status: 'activated', deletedAt: { $eq: null }, name: { $regex: /jo/i }, nullableValue: null, defaultValue: '', date: condition.date, regex: condition.regex, 'nested._id': '123', 'nested.name': { $regex: /^jo/i }, 'nested.deeper._id': '456', 'nested.deeper.name': { $regex: /jo$/i }, 'nested.deeper.num': { $gte: 789 }, 'nested.foo': { $eq: 'foo' }, 'nested.deeper.foo': { $eq: 'foo' }, }); }); }); (0, globals_1.describe)('parsePopulate function', () => { (0, globals_1.it)('should convert populate object to mongoose populate options', () => { const populate = { author: { model: 'User', select: 'name', populate: { comments: { model: 'Comment', select: 'text', populate: { user: { model: 'User', select: 'email', }, }, }, }, }, }; const expected = [ { path: 'author', model: 'User', select: 'name', populate: [ { path: 'comments', model: 'Comment', select: 'text', populate: [ { path: 'user', model: 'User', select: 'email', }, ], }, ], }, ]; const result = helpers_1.MongoHelper.parsePopulate(populate); (0, globals_1.expect)(result).toEqual(expected); }); (0, globals_1.it)('should return empty array when populate object is empty', () => { const result = helpers_1.MongoHelper.parsePopulate(); (0, globals_1.expect)(result).toEqual([]); }); }); }); //# sourceMappingURL=mongo.helper.spec.js.map