Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Commit

Permalink
Create unit tests for delegates controller checkPendingVotes()
Browse files Browse the repository at this point in the history
  • Loading branch information
slaweet committed May 4, 2017
1 parent e017f5a commit c856f04
Showing 1 changed file with 65 additions and 1 deletion.
66 changes: 65 additions & 1 deletion src/test/components/delegates/delegates.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ describe('delegates component controller', () => {
let $peers;
let delegates;
let $q;
let $timeout;

beforeEach(inject((_$componentController_, _$rootScope_, _$q_, _$peers_) => {
beforeEach(inject((_$componentController_, _$rootScope_, _$q_, _$peers_, _$timeout_) => {
$componentController = _$componentController_;
$rootScope = _$rootScope_;
$peers = _$peers_;
$q = _$q_;
$timeout = _$timeout_;
}));

beforeEach(() => {
Expand Down Expand Up @@ -240,6 +242,68 @@ describe('delegates component controller', () => {
});
});

describe('checkPendingVotes()', () => {
let delegateServiceMock;
let accountDelegtatesDeferred;
let delegate41;
let delegate42;

beforeEach(() => {
accountDelegtatesDeferred = $q.defer();
delegateServiceMock = sinon.mock(controller.delegateService);
delegateServiceMock.expects('listAccountDelegates').returns(accountDelegtatesDeferred.promise);
delegate41 = { username: 'genesis_41', status: {} };
delegate42 = { username: 'genesis_42', status: {} };
});

afterEach(() => {
delegateServiceMock.verify();
delegateServiceMock.restore();
});

it('calls delegateService.listAccountDelegates and then removes all returned delegates from this.votePendingList', () => {
controller.votePendingList = [delegate41, delegate42];
controller.unvotePendingList = [];

controller.checkPendingVotes();

$timeout.flush();
accountDelegtatesDeferred.resolve({ success: true, delegates: [delegate42] });
$scope.$apply();

expect(controller.votePendingList.length).to.equal(1);
expect(controller.votePendingList[0]).to.deep.equal(delegate41);
});

it('calls delegateService.listAccountDelegates and then removes all NOT returned delegates from this.unvotePendingList', () => {
controller.votePendingList = [];
controller.unvotePendingList = [delegate41, delegate42];

controller.checkPendingVotes();

$timeout.flush();
accountDelegtatesDeferred.resolve({ success: true, delegates: [delegate42] });
$scope.$apply();

expect(controller.unvotePendingList.length).to.equal(1);
expect(controller.unvotePendingList[0]).to.deep.equal(delegate42);
});

it('calls delegateService.listAccountDelegates and if in the end there are still some votes pending calls itself again', () => {
controller.votePendingList = [];
controller.unvotePendingList = [delegate41, delegate42];

controller.checkPendingVotes();

$timeout.flush();
const selfMock = sinon.mock(controller);
selfMock.expects('checkPendingVotes');
accountDelegtatesDeferred.resolve({ success: true, delegates: [] });

$scope.$apply();
});
});

describe('parseVoteListFromInput(list)', () => {
let delegateServiceMock;

Expand Down

0 comments on commit c856f04

Please sign in to comment.