Browse Source

Merge pull request #17384 from overleaf/dp-mongoose-callback-publisher-helper

Promisify Publisher acceptance test helper

GitOrigin-RevId: cce447234e32bfb93f8ce30deaf7fa21838e9176
David 2 years ago
parent
commit
887a404fdd
1 changed files with 31 additions and 11 deletions
  1. 31 11
      services/web/test/acceptance/src/helpers/Publisher.js

+ 31 - 11
services/web/test/acceptance/src/helpers/Publisher.js

@@ -1,9 +1,10 @@
 const { ObjectId } = require('mongodb')
 const { ObjectId } = require('mongodb')
 const PublisherModel = require('../../../../app/src/models/Publisher').Publisher
 const PublisherModel = require('../../../../app/src/models/Publisher').Publisher
+const { callbackify } = require('util')
 
 
 let count = parseInt(Math.random() * 999999)
 let count = parseInt(Math.random() * 999999)
 
 
-class Publisher {
+class PromisifiedPublisher {
   constructor(options = {}) {
   constructor(options = {}) {
     this.slug = options.slug || `publisher-slug-${count}`
     this.slug = options.slug || `publisher-slug-${count}`
     this.managerIds = []
     this.managerIds = []
@@ -11,22 +12,41 @@ class Publisher {
     count += 1
     count += 1
   }
   }
 
 
-  ensureExists(callback) {
+  async ensureExists() {
     const filter = { slug: this.slug }
     const filter = { slug: this.slug }
     const options = { upsert: true, new: true, setDefaultsOnInsert: true }
     const options = { upsert: true, new: true, setDefaultsOnInsert: true }
-    PublisherModel.findOneAndUpdate(filter, {}, options, (error, publisher) => {
-      this._id = publisher._id
-      callback(error)
-    })
+    const publisher = await PublisherModel.findOneAndUpdate(
+      filter,
+      {},
+      options
+    ).exec()
+
+    this._id = publisher._id
   }
   }
 
 
-  setManagerIds(managerIds, callback) {
-    return PublisherModel.findOneAndUpdate(
+  async setManagerIds(managerIds) {
+    return await PublisherModel.findOneAndUpdate(
       { _id: new ObjectId(this._id) },
       { _id: new ObjectId(this._id) },
-      { managerIds },
-      callback
-    )
+      { managerIds }
+    ).exec()
   }
   }
 }
 }
 
 
+class Publisher extends PromisifiedPublisher {}
+Publisher.promises = class extends PromisifiedPublisher {}
+
+// callbackify publisher class methods
+const nonPromiseMethods = ['constructor']
+Object.getOwnPropertyNames(PromisifiedPublisher.prototype).forEach(
+  methodName => {
+    const method = PromisifiedPublisher.prototype[methodName]
+    if (
+      typeof method === 'function' &&
+      !nonPromiseMethods.includes(methodName)
+    ) {
+      Publisher.prototype[methodName] = callbackify(method)
+    }
+  }
+)
+
 module.exports = Publisher
 module.exports = Publisher