diff --git a/lib/index.js b/lib/index.js index 7cb7bae..918515e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -28,7 +28,7 @@ _mongoose2.default.createConnection = jest.fn().mockReturnValue({ model: _mongoose2.default.model.bind(_mongoose2.default) }); -var ops = ['find', 'findOne', 'count', 'distinct', 'findOneAndUpdate', 'findOneAndRemove', 'remove', 'deleteOne', 'deleteMany']; +var ops = ['find', 'findOne', 'count', 'distinct', 'findOneAndUpdate', 'findOneAndRemove', 'remove', 'update', 'deleteOne', 'deleteMany']; var mockedReturn = function mockedReturn(cb) { var op = this.op, @@ -68,6 +68,7 @@ var mockedReturn = function mockedReturn(cb) { ops.forEach(function (op) { _mongoose2.default.Query.prototype[op] = jest.fn().mockImplementation(function (criteria, doc, options, callback) { switch (arguments.length) { + case 4: case 3: if (typeof options === 'function') { callback = options; @@ -104,6 +105,23 @@ _mongoose2.default.Query.prototype.exec = jest.fn().mockImplementation(function return mockedReturn.call(this, cb); }); +_mongoose2.default.Aggregate.prototype.exec = jest.fn().mockImplementation(function cb(cb) { + var modelName = this._model.modelName; + + + var mock = mockingoose.__mocks[modelName] && mockingoose.__mocks[modelName].aggregate; + + var err = null; + + if (mock instanceof Error) err = mock; + + if (cb) return cb(err, mock); + + if (err) return Promise.reject(err); + + return Promise.resolve(mock); +}); + var instance = ['remove', 'save']; instance.forEach(function (methodName) { diff --git a/lib/index.js.map b/lib/index.js.map index 1c1efdc..73eb22b 100644 --- a/lib/index.js.map +++ b/lib/index.js.map @@ -1 +1 @@ -{"version":3,"sources":["../src/index.js"],"names":["test","version","Promise","connect","jest","fn","mockImplementation","resolve","createConnection","mockReturnValue","on","once","then","catch","model","bind","ops","mockedReturn","cb","op","modelName","_mongooseOptions","Model","mock","mockingoose","__mocks","err","Error","includes","Array","isArray","map","item","lean","toObject","reject","forEach","Query","prototype","criteria","doc","options","callback","arguments","length","undefined","exec","call","instance","methodName","constructor","Object","assign","doMock","target","resetAll","toJSON","traps","get","prop","hasOwnProperty","Refl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;AAEA,IAAMC,SAAS;AACb7B,WAAS,EADI;AAEb8B,UAFa,sBAEF;AAAE,SAAK9B,OAAL,GAAe,EAAf;AAAoB,GAFpB;AAGb+B,QAHa,oBAGJ;AAAE,WAAO,KAAK/B,OAAZ;AAAsB;AAHpB,CAAf;;AAMA,IAAMgC,QAAQ;AACZC,KADY,eACRJ,MADQ,EACAK,IADA,EACM;AAChB,QAAIL,OAAOM,cAAP,CAAsBD,IAAtB,CAAJ,EAAiC,OAAOE,QAAQH,GAAR,CAAYJ,MAAZ,EAAoBK,IAApB,CAAP;;AAEjC,WAAO;AACLG,cADK,oBACIC,CADJ,EACoB;AAAA,YAAb5C,EAAa,uEAAR,MAAQ;;AACvBmC,eAAO7B,OAAP,CAAemC,cAAf,CAA8BD,IAA9B,IACIL,OAAO7B,OAAP,CAAekC,IAAf,EAAqBxC,EAArB,IAA2B4C,CAD/B,GAEIT,OAAO7B,OAAP,CAAekC,IAAf,wBAA0BxC,EAA1B,EAA+B4C,CAA/B,CAFJ;;AAIA,eAAO,IAAP;AACD,OAPI;AASLC,WATK,iBASC7C,EATD,EASK;AACRA,cAAM,OAAOmC,OAAO7B,OAAP,CAAekC,IAAf,EAAqBxC,EAArB,CAAb,IAAyC,OAAOmC,OAAO7B,OAAP,CAAekC,IAAf,CAAhD;;AAEA,eAAO,IAAP;AACD,OAbI;AAeLH,YAfK,oBAeI;AACP,eAAOF,OAAO7B,OAAP,CAAekC,IAAf,KAAwB,EAA/B;AACD;AAjBI,KAAP;AAmBD;AAvBW,CAAd;;AA0BA,IAAMnC,cAAc,IAAIyC,KAAJ,CAAUX,MAAV,EAAkBG,KAAlB,CAApB;;kBAEejC,W","file":"index.js","sourcesContent":["import mongoose from 'mongoose';\r\n\r\nif(!/^5/.test(mongoose.version)) mongoose.Promise = Promise;\r\n\r\nmongoose.connect = jest.fn().mockImplementation(() => Promise.resolve());\r\nmongoose.createConnection = jest\r\n .fn()\r\n .mockReturnValue({\r\n on: jest.fn(),\r\n once: jest.fn(),\r\n then(resolve) { return Promise.resolve(resolve(this)); },\r\n catch() {},\r\n model: mongoose.model.bind(mongoose),\r\n });\r\n\r\nconst ops = [\r\n 'find',\r\n 'findOne',\r\n 'count',\r\n 'distinct',\r\n 'findOneAndUpdate',\r\n 'findOneAndRemove',\r\n 'remove',\r\n 'deleteOne',\r\n 'deleteMany',\r\n];\r\n\r\nconst mockedReturn = function (cb) {\r\n const { op, model: { modelName }, _mongooseOptions = {} } = this;\r\n const Model = mongoose.model(modelName);\r\n\r\n let mock = mockingoose.__mocks[modelName] && mockingoose.__mocks[modelName][op];\r\n\r\n let err = null;\r\n\r\n if (mock instanceof Error) err = mock;\r\n\r\n if (!mock && op === 'save') { mock = this;}\r\n\r\n if (mock && mock instanceof Model === false && (!['update', 'count'].includes(op))) {\r\n mock = Array.isArray(mock) ? mock.map(item => new Model(item)) : new Model(mock);\r\n\r\n if (_mongooseOptions.lean) mock = Array.isArray(mock) ? mock.map(item => item.toObject()) : mock.toObject();\r\n }\r\n\r\n if (cb) return cb(err, mock);\r\n\r\n if (err) return Promise.reject(err);\r\n\r\n return Promise.resolve(mock);\r\n};\r\n\r\nops.forEach(op => {\r\n mongoose.Query.prototype[op] = jest.fn().mockImplementation(function (criteria, doc, options, callback) {\r\n switch (arguments.length) {\r\n case 3:\r\n if (typeof options === 'function') {\r\n callback = options;\r\n options = {};\r\n }\r\n break;\r\n case 2:\r\n if (typeof doc === 'function') {\r\n callback = doc;\r\n doc = criteria;\r\n criteria = undefined;\r\n }\r\n options = undefined;\r\n break;\r\n case 1:\r\n if (typeof criteria === 'function') {\r\n callback = criteria;\r\n criteria = options = doc = undefined;\r\n } else {\r\n doc = criteria;\r\n criteria = options = undefined;\r\n }\r\n }\r\n\r\n this.op = op;\r\n\r\n if (!callback) return this;\r\n\r\n return this.exec.call(this, callback);\r\n });\r\n});\r\n\r\nmongoose.Query.prototype.exec = jest.fn().mockImplementation(function cb(cb) {\r\n return mockedReturn.call(this, cb);\r\n});\r\n\r\nconst instance = [\r\n 'remove',\r\n 'save'\r\n];\r\n\r\ninstance.forEach(methodName => {\r\n mongoose.Model.prototype[methodName] = jest.fn().mockImplementation(function (options, cb) {\r\n const op = methodName;\r\n const { modelName } = this.constructor;\r\n\r\n if (typeof options === 'function') cb = options;\r\n\r\n Object.assign(this, { op, model: { modelName } });\r\n\r\n return mockedReturn.call(this, cb);\r\n })\r\n});\r\n\r\njest.doMock('mongoose', () => mongoose);\r\n\r\nconst target = {\r\n __mocks: {},\r\n resetAll() { this.__mocks = {}; },\r\n toJSON() { return this.__mocks; },\r\n};\r\n\r\nconst traps = {\r\n get(target, prop) {\r\n if (target.hasOwnProperty(prop)) return Reflect.get(target, prop);\r\n\r\n return {\r\n toReturn(o, op = 'find') {\r\n target.__mocks.hasOwnProperty(prop)\r\n ? target.__mocks[prop][op] = o\r\n : target.__mocks[prop] = { [op]: o };\r\n\r\n return this;\r\n },\r\n\r\n reset(op) {\r\n op && delete target.__mocks[prop][op] || delete target.__mocks[prop];\r\n\r\n return this;\r\n },\r\n\r\n toJSON() {\r\n return target.__mocks[prop] || {};\r\n },\r\n };\r\n },\r\n};\r\n\r\nconst mockingoose = new Proxy(target, traps);\r\n\r\nexport default mockingoose;\r\n"]} \ No newline at end of file +{"version":3,"sources":["../src/index.js"],"names":["test","version","Promise","connect","jest","fn","mockImplementation","resolve","createConnection","mockReturnValue","on","once","then","catch","model","bind","ops","mockedReturn","cb","op","modelName","_mongooseOptions","Model","mock","mockingoose","__mocks","err","Error","includes","Array","isArray","map","item","lean","toObject","reject","forEach","Query","prototype","criteria","doc","options","callback","arguments","length","undefined","exec","call","Aggregate","_model","aggregate","instance","methodName","constructor","Object","assign","doMock","target","resetAll","toJSON","traps","get","prop","hasOwnProperty","Reflect","toReturn","o","reset","Proxy"],"mappings":";;;;;;AAAA;;;;;;;;AAEA,IAAG,CAAC,KAAKA,IAAL,CAAU,mBAASC,OAAnB,CAAJ,EAAiC,mBAASC,OAAT,GAAmBA,OAAn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ArB,CAAb,IAAyC,OAAOsC,OAAOhC,OAAP,CAAeqC,IAAf,CAAhD;;AAEA,eAAO,IAAP;AACD,OAbI;AAeLH,YAfK,oBAeI;AACP,eAAOF,OAAOhC,OAAP,CAAeqC,IAAf,KAAwB,EAA/B;AACD;AAjBI,KAAP;AAmBD;AAvBW,CAAd;;AA0BA,IAAMtC,cAAc,IAAI4C,KAAJ,CAAUX,MAAV,EAAkBG,KAAlB,CAApB;;kBAEepC,W","file":"index.js","sourcesContent":["import mongoose from 'mongoose';\r\n\r\nif(!/^5/.test(mongoose.version)) mongoose.Promise = Promise;\r\n\r\nmongoose.connect = jest.fn().mockImplementation(() => Promise.resolve());\r\nmongoose.createConnection = jest\r\n .fn()\r\n .mockReturnValue({\r\n on: jest.fn(),\r\n once: jest.fn(),\r\n then(resolve) { return Promise.resolve(resolve(this)); },\r\n catch() {},\r\n model: mongoose.model.bind(mongoose),\r\n });\r\n\r\nconst ops = [\r\n 'find',\r\n 'findOne',\r\n 'count',\r\n 'distinct',\r\n 'findOneAndUpdate',\r\n 'findOneAndRemove',\r\n 'remove',\r\n 'update',\r\n 'deleteOne',\r\n 'deleteMany',\r\n];\r\n\r\nconst mockedReturn = function (cb) {\r\n const { op, model: { modelName }, _mongooseOptions = {} } = this;\r\n const Model = mongoose.model(modelName);\r\n\r\n let mock = mockingoose.__mocks[modelName] && mockingoose.__mocks[modelName][op];\r\n\r\n let err = null;\r\n\r\n if (mock instanceof Error) err = mock;\r\n\r\n if (!mock && op === 'save') { mock = this;}\r\n\r\n if (mock && mock instanceof Model === false && (!['update', 'count'].includes(op))) {\r\n mock = Array.isArray(mock) ? mock.map(item => new Model(item)) : new Model(mock);\r\n\r\n if (_mongooseOptions.lean) mock = Array.isArray(mock) ? mock.map(item => item.toObject()) : mock.toObject();\r\n }\r\n\r\n if (cb) return cb(err, mock);\r\n\r\n if (err) return Promise.reject(err);\r\n\r\n return Promise.resolve(mock);\r\n};\r\n\r\nops.forEach(op => {\r\n mongoose.Query.prototype[op] = jest.fn().mockImplementation(function (criteria, doc, options, callback) {\r\n switch (arguments.length) {\r\n case 4:\r\n case 3:\r\n if (typeof options === 'function') {\r\n callback = options;\r\n options = {};\r\n }\r\n break;\r\n case 2:\r\n if (typeof doc === 'function') {\r\n callback = doc;\r\n doc = criteria;\r\n criteria = undefined;\r\n }\r\n options = undefined;\r\n break;\r\n case 1:\r\n if (typeof criteria === 'function') {\r\n callback = criteria;\r\n criteria = options = doc = undefined;\r\n } else {\r\n doc = criteria;\r\n criteria = options = undefined;\r\n }\r\n }\r\n\r\n this.op = op;\r\n\r\n if (!callback) return this;\r\n\r\n return this.exec.call(this, callback);\r\n });\r\n});\r\n\r\nmongoose.Query.prototype.exec = jest.fn().mockImplementation(function cb(cb) {\r\n return mockedReturn.call(this, cb);\r\n});\r\n\r\nmongoose.Aggregate.prototype.exec = jest.fn().mockImplementation(function cb(cb) {\r\n\tconst { _model: { modelName } } = this;\r\n\r\n\tlet mock = mockingoose.__mocks[modelName] && mockingoose.__mocks[modelName].aggregate;\r\n\r\n\tlet err = null;\r\n\r\n\tif (mock instanceof Error) err = mock;\r\n\r\n\tif (cb) return cb(err, mock);\r\n\r\n\tif (err) return Promise.reject(err);\r\n\r\n\treturn Promise.resolve(mock);\r\n});\r\n\r\nconst instance = [\r\n 'remove',\r\n 'save'\r\n];\r\n\r\ninstance.forEach(methodName => {\r\n mongoose.Model.prototype[methodName] = jest.fn().mockImplementation(function (options, cb) {\r\n const op = methodName;\r\n const { modelName } = this.constructor;\r\n\r\n if (typeof options === 'function') cb = options;\r\n\r\n Object.assign(this, { op, model: { modelName } });\r\n\r\n return mockedReturn.call(this, cb);\r\n })\r\n});\r\n\r\njest.doMock('mongoose', () => mongoose);\r\n\r\nconst target = {\r\n __mocks: {},\r\n resetAll() { this.__mocks = {}; },\r\n toJSON() { return this.__mocks; },\r\n};\r\n\r\nconst traps = {\r\n get(target, prop) {\r\n if (target.hasOwnProperty(prop)) return Reflect.get(target, prop);\r\n\r\n return {\r\n toReturn(o, op = 'find') {\r\n target.__mocks.hasOwnProperty(prop)\r\n ? target.__mocks[prop][op] = o\r\n : target.__mocks[prop] = { [op]: o };\r\n\r\n return this;\r\n },\r\n\r\n reset(op) {\r\n op && delete target.__mocks[prop][op] || delete target.__mocks[prop];\r\n\r\n return this;\r\n },\r\n\r\n toJSON() {\r\n return target.__mocks[prop] || {};\r\n },\r\n };\r\n },\r\n};\r\n\r\nconst mockingoose = new Proxy(target, traps);\r\n\r\nexport default mockingoose;\r\n"]} \ No newline at end of file