|
1 | 1 | 'use strict'; |
| 2 | + |
| 3 | +const maybePromise = require('./../../lib/utils').maybePromise; |
2 | 4 | var expect = require('chai').expect; |
3 | 5 |
|
4 | | -describe('BYO Promises', function() { |
5 | | - it('should Correctly Use Blurbird promises library', { |
6 | | - metadata: { |
7 | | - requires: { |
8 | | - topology: ['single', 'ssl', 'wiredtiger'] |
9 | | - } |
10 | | - }, |
11 | | - |
12 | | - // The actual test we wish to run |
13 | | - test: function(done) { |
14 | | - var self = this; |
15 | | - const configuration = this.configuration; |
16 | | - var Promise = require('bluebird'); |
17 | | - |
18 | | - const client = configuration.newClient( |
19 | | - {}, |
20 | | - { |
21 | | - promiseLibrary: Promise, |
22 | | - sslValidate: false |
23 | | - } |
24 | | - ); |
25 | | - |
26 | | - client.connect().then(function(client) { |
27 | | - var db = client.db(self.configuration.db); |
28 | | - var promise = db.collection('test').insert({ a: 1 }); |
29 | | - expect(promise).to.be.an.instanceOf(Promise); |
30 | | - |
31 | | - promise.then(function() { |
32 | | - client.close(done); |
33 | | - }); |
34 | | - }); |
35 | | - } |
| 6 | +class CustomPromise extends Promise {} |
| 7 | +CustomPromise.prototype.isCustomMongo = true; |
| 8 | + |
| 9 | +const parent = { s: { promiseLibrary: CustomPromise } }; |
| 10 | + |
| 11 | +describe('Optional PromiseLibrary / maybePromise', function() { |
| 12 | + it('should correctly implement custom dependency-less promise', function(done) { |
| 13 | + const getCustomPromise = v => new CustomPromise(resolve => resolve(v)); |
| 14 | + const getNativePromise = v => new Promise(resolve => resolve(v)); |
| 15 | + expect(getNativePromise()).to.not.have.property('isCustomMongo'); |
| 16 | + expect(getCustomPromise()).to.have.property('isCustomMongo'); |
| 17 | + expect(getNativePromise()).to.have.property('then'); |
| 18 | + expect(getCustomPromise()).to.have.property('then'); |
| 19 | + done(); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should return a promise with extra property CustomMongo', function() { |
| 23 | + const prom = maybePromise(parent, undefined, () => 'example'); |
| 24 | + expect(prom).to.have.property('isCustomMongo'); |
| 25 | + expect(prom).to.have.property('then'); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should return a native promise with no parent', function(done) { |
| 29 | + const prom = maybePromise(undefined, undefined, () => 'example'); |
| 30 | + expect(prom).to.not.have.property('isCustomMongo'); |
| 31 | + expect(prom).to.have.property('then'); |
| 32 | + done(); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should return a native promise with empty parent', function(done) { |
| 36 | + const prom = maybePromise({}, undefined, () => 'example'); |
| 37 | + expect(prom).to.not.have.property('isCustomMongo'); |
| 38 | + expect(prom).to.have.property('then'); |
| 39 | + done(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should return a native promise with emtpy "s"', function(done) { |
| 43 | + const prom = maybePromise({ s: {} }, undefined, () => 'example'); |
| 44 | + expect(prom).to.not.have.property('isCustomMongo'); |
| 45 | + expect(prom).to.have.property('then'); |
| 46 | + done(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should have cursor return native promise', function(done) { |
| 50 | + const configuration = this.configuration; |
| 51 | + const client = this.configuration.newClient({ w: 1 }, { poolSize: 1 }); |
| 52 | + client.connect((err, client) => { |
| 53 | + expect(err).to.not.exist; |
| 54 | + const db = client.db(configuration.db); |
| 55 | + const collection = db.collection('test'); |
| 56 | + const cursor = collection.find({}); |
| 57 | + const isPromise = cursor.toArray(); |
| 58 | + expect(isPromise).to.not.have.property('isCustomMongo'); |
| 59 | + expect(isPromise).to.have.property('then'); |
| 60 | + isPromise.then(() => client.close(done)); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should have cursor return custom promise from new client options', function(done) { |
| 65 | + const configuration = this.configuration; |
| 66 | + const client = this.configuration.newClient( |
| 67 | + { w: 1 }, |
| 68 | + { poolSize: 1, promiseLibrary: CustomPromise } |
| 69 | + ); |
| 70 | + client.connect((err, client) => { |
| 71 | + const db = client.db(configuration.db); |
| 72 | + expect(err).to.be.null; |
| 73 | + const collection = db.collection('test'); |
| 74 | + const cursor = collection.find({}); |
| 75 | + const isPromise = cursor.toArray(); |
| 76 | + expect(isPromise).to.have.property('isCustomMongo'); |
| 77 | + expect(isPromise).to.have.property('then'); |
| 78 | + isPromise.then(() => client.close(done)); |
| 79 | + }); |
36 | 80 | }); |
37 | 81 | }); |
0 commit comments