diff --git a/src/ui/public/promises/__tests__/promises.js b/src/ui/public/promises/__tests__/promises.js new file mode 100644 index 000000000000..874ae2946748 --- /dev/null +++ b/src/ui/public/promises/__tests__/promises.js @@ -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); + }); + }); +}); diff --git a/src/ui/public/promises.js b/src/ui/public/promises/promises.js similarity index 100% rename from src/ui/public/promises.js rename to src/ui/public/promises/promises.js