mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 09:48:58 -04:00
Merge pull request #5274 from spalger/implement/promiseFromNode
[promise] fromNode helper
This commit is contained in:
commit
081a22c48e
2 changed files with 74 additions and 0 deletions
65
src/ui/public/promises/__tests__/promises.js
Normal file
65
src/ui/public/promises/__tests__/promises.js
Normal file
|
@ -0,0 +1,65 @@
|
|||
import expect from 'expect.js';
|
||||
import ngMock from 'ngMock';
|
||||
import sinon from 'auto-release-sinon';
|
||||
|
||||
describe('Promise service', function () {
|
||||
let Promise;
|
||||
let $rootScope;
|
||||
|
||||
beforeEach(ngMock.module('kibana'));
|
||||
beforeEach(ngMock.inject(function ($injector) {
|
||||
Promise = $injector.get('Promise');
|
||||
$rootScope = $injector.get('$rootScope');
|
||||
}));
|
||||
|
||||
describe('Promise.fromNode', function () {
|
||||
it('creates a callback that controls a promise', function () {
|
||||
let callback;
|
||||
Promise.fromNode(cb => (callback = cb)());
|
||||
$rootScope.$apply();
|
||||
expect(callback).to.be.a('function');
|
||||
});
|
||||
|
||||
it('rejects if the callback receives an error', function () {
|
||||
let errback = sinon.stub();
|
||||
let err = new Error();
|
||||
Promise.fromNode(cb => cb(err)).catch(errback);
|
||||
$rootScope.$apply();
|
||||
|
||||
expect(errback.callCount).to.be(1);
|
||||
expect(errback.getCall(0).args[0]).to.be(err);
|
||||
});
|
||||
|
||||
it('resolves with the second argument', function () {
|
||||
let thenback = sinon.stub();
|
||||
let result = {};
|
||||
Promise.fromNode(cb => cb(null, result)).then(thenback);
|
||||
$rootScope.$apply();
|
||||
|
||||
expect(thenback.callCount).to.be(1);
|
||||
expect(thenback.getCall(0).args[0]).to.be(result);
|
||||
});
|
||||
|
||||
it('resolves with an array if multiple arguments are received', function () {
|
||||
let thenback = sinon.stub();
|
||||
let result1 = {};
|
||||
let result2 = {};
|
||||
Promise.fromNode(cb => cb(null, result1, result2)).then(thenback);
|
||||
$rootScope.$apply();
|
||||
|
||||
expect(thenback.callCount).to.be(1);
|
||||
expect(thenback.getCall(0).args[0][0]).to.be(result1);
|
||||
expect(thenback.getCall(0).args[0][1]).to.be(result2);
|
||||
});
|
||||
|
||||
it('resolves with an array if multiple undefined are received', function () {
|
||||
let thenback = sinon.stub();
|
||||
Promise.fromNode(cb => cb(null, undefined, undefined)).then(thenback);
|
||||
$rootScope.$apply();
|
||||
|
||||
expect(thenback.callCount).to.be(1);
|
||||
expect(thenback.getCall(0).args[0][0]).to.be(undefined);
|
||||
expect(thenback.getCall(0).args[0][1]).to.be(undefined);
|
||||
});
|
||||
});
|
||||
});
|
|
@ -86,6 +86,15 @@ define(function (require) {
|
|||
|
||||
return Promise.resolve(value);
|
||||
};
|
||||
Promise.fromNode = function (takesCbFn) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
takesCbFn(function (err, ...results) {
|
||||
if (err) reject(err);
|
||||
else if (results.length > 1) resolve(results);
|
||||
else resolve(results[0]);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
return Promise;
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue