[ui/promise] added tests for Promise.fromNode
This commit is contained in:
parent
9a8ccc7e00
commit
e54f5c4606
2 changed files with 65 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);
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue