client-ui
Version:
Testing implementation of nodeJs Backend, angular frontend, and hopefully in a way that this can be deployed to s3/cloudfront
119 lines (114 loc) • 3.76 kB
JavaScript
/* global inject */
Function.prototype.bind = Function.prototype.bind || function (thisp) { //eslint-disable-line
var fn = this;
return function () {
return fn.apply(thisp, arguments);
};
};
describe('Promise Wrapper', function() {
var server = 'http://qloans:8080/v1/';
var promiseWrapper;
var promise;
var count;
var qDep;
var $timeout;
var $rootScope;
beforeEach(function() {
count = {
reject: 0,
resolve: 0
};
qDep = {
defer: function() {
return {
resolve: function() {
count.resolve++;
},
reject: function() {
count.reject++;
}
};
}
};
});
beforeEach(function() {
angular.mock.module('clientApp', function($provide) {
$provide.constant('rootConstants', {
'server': server
});
$provide.service('$state', function() {
});
});
});
beforeEach(function() {
inject(function($injector) {
promiseWrapper = $injector.get('promiseWrapper');
promise = new promiseWrapper(qDep);
$timeout = $injector.get("$timeout");
$rootScope = $injector.get("$rootScope");
});
});
describe('Standard usage', function() {
it('Should only call resolve once', function(){
promise.resolve();
$timeout.flush();
$rootScope.$digest();
assert.equal(count.reject, 0);
assert.equal(count.resolve, 1);
});
it('Should only call resolve once', function(){
promise.reject();
$timeout.flush();
$rootScope.$digest();
assert.equal(count.reject, 1);
assert.equal(count.resolve, 0);
});
});
describe('timeoutAfter', function() {
it('Should only call resolve once, if it doesnt timeout', function(){
promise.timeoutAfter({timeout: 0});
promise.resolve();
$timeout.flush();
$rootScope.$digest();
assert.equal(count.reject, 0);
assert.equal(count.resolve, 1);
});
it('Should only call reject once', function(){
promise.timeoutAfter({timeout: 0});
promise.reject();
$timeout.flush();
$rootScope.$digest();
assert.equal(count.reject, 1);
assert.equal(count.resolve, 0);
});
it('Should only call reject once if timed out, then rejected', function(){
promise.timeoutAfter({timeout: 0});
$timeout.flush();
$rootScope.$digest();
promise.reject();
assert.equal(count.reject, 1);
assert.equal(count.resolve, 0);
});
it('Should not resolve at all after already rejected', function(){
promise.timeoutAfter({timeout: 0});
$timeout.flush();
$rootScope.$digest();
promise.resolve();
assert.equal(count.reject, 1);
assert.equal(count.resolve, 0);
});
it('Should cancel timeout after resolved or rejected', function(){
promise.timeoutAfter({timeout: 0});
var called = false;
promise.reject = function() {
called = true;
};
promise.resolve();
$timeout.flush();
$rootScope.$digest();
assert.equal(count.resolve, 1);
assert.equal(count.reject, 0);
assert.equal(called, false);
});
});
});