From da01b02745879df726bacc1c50992263dc1d154f Mon Sep 17 00:00:00 2001 From: Alon Valadji Date: Fri, 29 Sep 2017 17:13:29 +0300 Subject: [PATCH] :wrench: build --- lib/index.js | 2 +- lib/index.js.map | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index 6cabd77..92d0f4a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -29,7 +29,7 @@ var mockedReturn = function mockedReturn(cb) { mock = this; } - if (mock instanceof Model === false && op !== 'update') { + if (mock && mock instanceof Model === false && op !== 'update') { mock = Array.isArray(mock) ? mock.map(function (item) { return new Model(item); }) : new Model(mock); diff --git a/lib/index.js.map b/lib/index.js.map index 8da6fff..5969fd7 100644 --- a/lib/index.js.map +++ b/lib/index.js.map @@ -1 +1 @@ -{"version":3,"sources":["../src/index.js"],"names":["Promise","connect","jest","fn","ops","mockedReturn","cb","op","modelName","model","Model","mock","mockingoose","__mocks","Array","isArray","map","item","err","Error","reject","resolve","forEach","Query","prototype","mockImplementation","criteria","doc","options","callback","arguments","length","undefined","exec","call","save","constructor","Object","assign","doMock","target","resetAll","toJSON","traps","get","prop","hasOwnProperty","Reflect","toReturn","o","reset","Proxy"],"mappings":";;;;;;AAAA;;;;;;;;AAEA,mBAASA,OAAT,GAAmBA,OAAnB;AACA,mBAASC,OAAT,GAAmBC,KAAKC,EAAL,EAAnB;;AAEA,IAAMC,MAAM,CACV,MADU,EAEV,SAFU,EAGV,OAHU,EAIV,UAJU,EAKV,kBALU,EAMV,kBANU,EAOV,QAPU,EAQV,WARU,EASV,YATU,CAAZ;;AAYA,IAAMC,eAAe,SAAfA,YAAe,CAASC,EAAT,EAAa;AAAA,MACxBC,EADwB,GACI,IADJ,CACxBA,EADwB;AAAA,MACXC,SADW,GACI,IADJ,CACpBC,KADoB,CACXD,SADW;;AAEhC,MAAME,QAAQ,mBAASD,KAAT,CAAeD,SAAf,CAAd;;AAEA,MAAIG,OAAOC,YAAYC,OAAZ,CAAoB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,KADY,eACRJ,MADQ,EACAK,IADA,EACM;AAChB,QAAGL,OAAOM,cAAP,CAAsBD,IAAtB,CAAH,EAAgC,OAAOE,QAAQH,GAAR,CAAYJ,MAAZ,EAAoBK,IAApB,CAAP;;AAEhC,WAAO;AACLG,cADK,oBACIC,CADJ,EACoB;AAAA,YAAb1C,EAAa,uEAAR,MAAQ;;AACvBiC,eAAO3B,OAAP,CAAeiC,cAAf,CAA8BD,IAA9B,IACML,OAAO3B,OAAP,CAAegC,IAAf,EAAqBtC,EAArB,IAA2B0C,CADjC,GAEMT,OAAO3B,OAAP,CAAegC,IAAf,wBAA0BtC,EAA1B,EAA+B0C,CAA/B,CAFN;;AAIA,eAAO,IAAP;AACD,OAPI;AASLC,WATK,iBASC3C,EATD,EASK;AACRA,cAAM,OAAOiC,OAAO3B,OAAP,CAAegC,IAAf,EAAqBtC,EAArB,CAAb,IAAyC,OAAOiC,OAAO3B,OAAP,CAAegC,IAAf,CAAhD;;AAEA,eAAO,IAAP;AACD,OAbI;AAeLH,YAfK,oBAeI;AACP,eAAOF,OAAO3B,OAAP,CAAegC,IAAf,KAAwB,EAA/B;AACD;AAjBI,KAAP;AAmBD;AAvBW,CAAd;;AA0BA,IAAMjC,cAAc,IAAIuC,KAAJ,CAAUX,MAAV,EAAkBG,KAAlB,CAApB;;kBAEe/B,W","file":"index.js","sourcesContent":["import mongoose from 'mongoose';\r\n\r\nmongoose.Promise = Promise;\r\nmongoose.connect = jest.fn();\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 }} = 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 if(!mock && op === 'save') { mock = this;}\r\n\r\n if(mock instanceof Model === false && (op !== 'update')) {\r\n mock = Array.isArray(mock) ? mock.map(item => new Model(item)) : new Model(mock);\r\n }\r\n\r\n let err = null;\r\n\r\n if(mock instanceof Error) err = mock;\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\nmongoose.Model.prototype.save = function(options, cb) {\r\n const op = 'save';\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\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":["Promise","connect","jest","fn","ops","mockedReturn","cb","op","modelName","model","Model","mock","mockingoose","__mocks","Array","isArray","map","item","err","Error","reject","resolve","forEach","Query","prototype","mockImplementation","criteria","doc","options","callback","arguments","length","undefined","exec","call","save","constructor","Object","assign","doMock","target","resetAll","toJSON","traps","get","prop","hasOwnProperty","Reflect","toReturn","o","reset","Proxy"],"mappings":";;;;;;AAAA;;;;;;;;AAEA,mBAASA,OAAT,GAAmBA,OAAnB;AACA,mBAASC,OAAT,GAAmBC,KAAKC,EAAL,EAAnB;;AAEA,IAAMC,MAAM,CACV,MADU,EAEV,SAFU,EAGV,OAHU,EAIV,UAJU,EAKV,kBALU,EAMV,kBANU,EAOV,QAPU,EAQV,WARU,EASV,YATU,CAAZ;;AAYA,IAAMC,eAAe,SAAfA,YAAe,CAASC,EAAT,EAAa;AAAA,MACxBC,EADwB,GACI,IADJ,CACxBA,EADwB;AAAA,MACXC,SADW,GACI,IADJ,CACpBC,KADoB,CACXD,SADW;;AAEhC,MAAME,QAAQ,mBAASD,KAAT,CAAeD,SAAf,CAAd;;AAEA,MAAIG,OAAOC,YAAYC,OAAZ,CAAoBL,SAApB,KAAkCI,YAAYC,OAAZ,CAAoBL,SAApB,EAA+BD,EAA/B,CAA7C;;AAEA,MAAG,CAACI,IAAD,IAASJ,OAAO,MAAnB,EAA2B;AAAEI,WAAO,IAAP;AAAa;;AAE1C,MAAGA,QAAQA,gBAAgBD,KAAhB,KAA0B,KAAlC,IAA4CH,OAAO,QAAtD,EAAiE;AAC/DI,WAAOG,MAAMC,OAAN,CAAcJ,IAAd,IAAsBA,KAAKK,GAAL,CAAS;AAAA,aAAQ,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,IAAf,EAAqBtC,EAArB,IAA2B0C,CADjC,GAEMT,OAAO3B,OAAP,CAAegC,IAAf,wBAA0BtC,EAA1B,EAA+B0C,CAA/B,CAFN;;AAIA,eAAO,IAAP;AACD,OAPI;AASLC,WATK,iBASC3C,EATD,EASK;AACRA,cAAM,OAAOiC,OAAO3B,OAAP,CAAegC,IAAf,EAAqBtC,EAArB,CAAb,IAAyC,OAAOiC,OAAO3B,OAAP,CAAegC,IAAf,CAAhD;;AAEA,eAAO,IAAP;AACD,OAbI;AAeLH,YAfK,oBAeI;AACP,eAAOF,OAAO3B,OAAP,CAAegC,IAAf,KAAwB,EAA/B;AACD;AAjBI,KAAP;AAmBD;AAvBW,CAAd;;AA0BA,IAAMjC,cAAc,IAAIuC,KAAJ,CAAUX,MAAV,EAAkBG,KAAlB,CAApB;;kBAEe/B,W","file":"index.js","sourcesContent":["import mongoose from 'mongoose';\r\n\r\nmongoose.Promise = Promise;\r\nmongoose.connect = jest.fn();\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 }} = 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 if(!mock && op === 'save') { mock = this;}\r\n\r\n if(mock && mock instanceof Model === false && (op !== 'update')) {\r\n mock = Array.isArray(mock) ? mock.map(item => new Model(item)) : new Model(mock);\r\n }\r\n\r\n let err = null;\r\n\r\n if(mock instanceof Error) err = mock;\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\nmongoose.Model.prototype.save = function(options, cb) {\r\n const op = 'save';\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\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