graphql-compose-mongoose
Version:
Plugin for `graphql-compose` which derive a graphql types from a mongoose model.
230 lines (227 loc) • 4.98 kB
JavaScript
"use strict";
var _mongoose = require("mongoose");
var _toMongoDottedObject = require("../toMongoDottedObject");
describe('toMongoDottedObject()', () => {
it('should dot nested objects', () => {
expect((0, _toMongoDottedObject.toMongoDottedObject)({
a: {
b: {
c: 1
}
}
})).toEqual({
'a.b.c': 1
});
});
it('should not dot query operators started with $', () => {
expect((0, _toMongoDottedObject.toMongoDottedObject)({
a: {
$in: [1, 2, 3]
}
})).toEqual({
a: {
$in: [1, 2, 3]
}
});
expect((0, _toMongoDottedObject.toMongoDottedObject)({
a: {
b: {
$in: [1, 2, 3]
}
}
})).toEqual({
'a.b': {
$in: [1, 2, 3]
}
});
expect((0, _toMongoDottedObject.toMongoDottedObject)({
$or: [{
age: 1
}, {
age: 2
}]
})).toEqual({
$or: [{
age: 1
}, {
age: 2
}]
});
});
it('should mix query operators started with $', () => {
expect((0, _toMongoDottedObject.toMongoDottedObject)({
a: {
$in: [1, 2, 3],
$exists: true
}
})).toEqual({
a: {
$in: [1, 2, 3],
$exists: true
}
});
});
it('should not mix query operators started with $ and regular fields', () => {
expect((0, _toMongoDottedObject.toMongoDottedObject)({
a: {
$exists: true,
b: 3
}
})).toEqual({
a: {
$exists: true
},
'a.b': 3
});
});
it('should handle date object values as scalars', () => {
expect((0, _toMongoDottedObject.toMongoDottedObject)({
dateField: new Date(100)
})).toEqual({
dateField: new Date(100)
});
});
it('should handle date object values when nested', () => {
expect((0, _toMongoDottedObject.toMongoDottedObject)({
a: {
dateField: new Date(100)
}
})).toEqual({
'a.dateField': new Date(100)
});
});
it('should keep BSON ObjectId untouched', () => {
const id = new _mongoose.Types.ObjectId();
expect((0, _toMongoDottedObject.toMongoDottedObject)({
a: {
someField: id
}
})).toEqual({
'a.someField': id
});
});
it('should dot array without index', () => {
expect((0, _toMongoDottedObject.toMongoDottedObject)({
a: [{
b: 1
}, {
c: 2
}]
})).toEqual({
'a.0.b': 1,
'a.1.c': 2
});
});
});
describe('toMongoFilterDottedObject()', () => {
it('should dot nested objects', () => {
expect((0, _toMongoDottedObject.toMongoFilterDottedObject)({
a: {
b: {
c: 1
}
}
})).toEqual({
'a.b.c': 1
});
});
it('should not dot query operators started with $', () => {
expect((0, _toMongoDottedObject.toMongoFilterDottedObject)({
a: {
$in: [1, 2, 3]
}
})).toEqual({
a: {
$in: [1, 2, 3]
}
});
expect((0, _toMongoDottedObject.toMongoFilterDottedObject)({
a: {
b: {
$in: [1, 2, 3]
}
}
})).toEqual({
'a.b': {
$in: [1, 2, 3]
}
});
expect((0, _toMongoDottedObject.toMongoFilterDottedObject)({
$or: [{
age: 1
}, {
age: 2
}]
})).toEqual({
$or: [{
age: 1
}, {
age: 2
}]
});
});
it('should mix query operators started with $', () => {
expect((0, _toMongoDottedObject.toMongoFilterDottedObject)({
a: {
$in: [1, 2, 3],
$exists: true
}
})).toEqual({
a: {
$in: [1, 2, 3],
$exists: true
}
});
});
it('should not mix query operators started with $ and regular fields', () => {
expect((0, _toMongoDottedObject.toMongoFilterDottedObject)({
a: {
$exists: true,
b: 3
}
})).toEqual({
a: {
$exists: true
},
'a.b': 3
});
});
it('should handle date object values as scalars', () => {
expect((0, _toMongoDottedObject.toMongoFilterDottedObject)({
dateField: new Date(100)
})).toEqual({
dateField: new Date(100)
});
});
it('should handle date object values when nested', () => {
expect((0, _toMongoDottedObject.toMongoFilterDottedObject)({
a: {
dateField: new Date(100)
}
})).toEqual({
'a.dateField': new Date(100)
});
});
it('should keep BSON ObjectId untouched', () => {
const id = new _mongoose.Types.ObjectId();
expect((0, _toMongoDottedObject.toMongoFilterDottedObject)({
a: {
someField: id
}
})).toEqual({
'a.someField': id
});
});
it('should dot array without index', () => {
expect((0, _toMongoDottedObject.toMongoFilterDottedObject)({
a: [{
b: 1
}, {
c: 2
}]
})).toEqual({
'a.b': 1,
'a.c': 2
});
});
});