Skip to content

Commit

Permalink
Merge pull request #908 from chalettu/snapshot-component-tests
Browse files Browse the repository at this point in the history
[Finishes #150819088] updated snapshot component unit tests
  • Loading branch information
chriskacerguis authored Sep 6, 2017
2 parents 24af57a + 458c9d6 commit 7996b1a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 7 deletions.
12 changes: 7 additions & 5 deletions client/app/services/vms/snapshots.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ function ComponentController (VmsService, sprintf, EventNotifications, ListView,
// Functions
resolveSnapshots: resolveSnapshots,
deleteSnapshots: deleteSnapshots,
deleteSnapshot: deleteSnapshot,
revertSnapshot: revertSnapshot,
cancelDelete: cancelDelete,
updateMenuActionForItemFn: updateMenuActionForItemFn,

resolveVm: resolveVm,
// Config
listConfig: getListConfig(),
menuActions: getMenuActions(),
Expand Down Expand Up @@ -140,7 +142,7 @@ function ComponentController (VmsService, sprintf, EventNotifications, ListView,

function deleteSnapshots () {
cancelDelete()
VmsService.deleteSnapshots(vm.vm.id, vm.snapshotsToRemove).then(success, failure)
return VmsService.deleteSnapshots(vm.vm.id, vm.snapshotsToRemove).then(success, failure)

function success (response) {
EventNotifications.batch(response.results, __('Deleting snapshot.'), __('Error deleting snapshot.'))
Expand Down Expand Up @@ -195,7 +197,7 @@ function ComponentController (VmsService, sprintf, EventNotifications, ListView,
}

function resolveVm () {
VmsService.getVm(vm.vmId).then(success, failure)
return VmsService.getVm(vm.vmId).then(success, failure)

function success (response) {
vm.vm = response
Expand Down Expand Up @@ -230,10 +232,10 @@ function ComponentController (VmsService, sprintf, EventNotifications, ListView,
}

function revertSnapshot (_action, item) {
VmsService.revertSnapshot(vm.vm.id, item.id).then(success, failure)
return VmsService.revertSnapshot(vm.vm.id, item.id).then(success, failure)

function success (response) {
EventNotifications.batch({results: [response]}, __('Reverting snapshot.'), __('Error reverting snapshot.'))
EventNotifications.batch([response], __('Reverting snapshot.'), __('Error reverting snapshot.'))
resolveSnapshots()
}

Expand Down
72 changes: 70 additions & 2 deletions client/app/services/vms/snapshots.component.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('Component: snapshots', function() {
returnObj.length = 0;
return returnObj
};
bard.inject('VmsService');
ctrl = $componentController('vmSnapshots', {$transclude: transclude}, bindings);
})
);
Expand All @@ -41,11 +42,74 @@ describe('Component: snapshots', function() {
ctrl.deleteSnapshots("action", {item: "item"})
expect(ctrl.deleteSnapshots()).to.be.defined;
});

it('can succesfully delete snapshots', () => {
notificationsSpy = sinon.spy(EventNotifications, 'batch');
collectionsApiSpy = sinon.stub(VmsService, 'deleteSnapshots').returns(Promise.resolve({results: ['test', 'test2']}));
return ctrl.deleteSnapshots().then((data) => {
expect(notificationsSpy).to.have.been.called;
})
})
it('can handle failing to delete snapshots', () => {
notificationsSpy = sinon.spy(EventNotifications, 'error');
const errorObject = {
data: {
error: {
message: 'failure'
}
}
};

collectionsApiSpy = sinon.stub(VmsService, 'deleteSnapshots').returns(Promise.reject( errorObject));
return ctrl.deleteSnapshots().then((data) => {
expect(notificationsSpy).to.have.been.called;
})
});
it('can handle deleting a single snapshot', () => {
ctrl.deleteSnapshot('', {href:'/test', name: 'test'});
expect(ctrl.snapshotsToRemove).to.deep.eq([{ href: '/test' }]);
});
it('can handle deleting all snapshots', () => {
ctrl.deleteSnapshot(null);
expect(ctrl.deleteTitle).to.eq('Delete All Snapshots on VM undefined');
});
it('can handle reverting a snapshot successfully', () => {
notificationsSpy = sinon.spy(EventNotifications, 'batch');
const vmAPISpy = sinon.stub(VmsService, 'revertSnapshot').returns(Promise.resolve('test'));
return ctrl.revertSnapshot('',1).then( (data) => {
expect(notificationsSpy).to.have.been.calledOnce;
});
});
it('can try to revert a snapshot and fail', () => {
notificationsSpy = sinon.spy(EventNotifications, 'error');
const errorObject = {
data: {
error: {
message: 'failure'
}
}
};
const vmAPISpy = sinon.stub(VmsService, 'revertSnapshot').returns(Promise.reject(errorObject));
return ctrl.revertSnapshot('',1).then( (data) => {
expect(notificationsSpy).to.have.been.calledWith('failure');
});
});
it('has a cancelDelete()', function() {
expect(ctrl.cancelDelete()).to.be.defined;
});

it('can resolve getting a VM', () => {
const vmSpy = sinon.stub(VmsService, 'getVm').returns(Promise.resolve({results: ['test', 'test2']}));
return ctrl.resolveVm().then((data) => {
expect(vmSpy).to.have.been.calledWith('1');
})
});
it('can handle failing to resolve a VM', () => {
const vmSpy = sinon.stub(VmsService, 'getVm').returns(Promise.reject({}));
notificationsSpy = sinon.spy(EventNotifications, 'error');
return ctrl.resolveVm().then((data) => {
expect(notificationsSpy).to.have.been.calledWith('There was an error loading the vm.');
});
});
beforeEach(function() {
bard.inject('CollectionsApi', 'EventNotifications');
notificationsSpy = sinon.stub(EventNotifications, 'result').returns(null);
Expand All @@ -57,7 +121,11 @@ describe('Component: snapshots', function() {
ctrl.resolveSnapshots();
expect(collectionsApiSpy).to.have.been.called;
});

it('should show a failure if the API cant resolve snapshots', () => {
collectionsApiSpy = sinon.stub(CollectionsApi, 'query').returns(Promise.reject(successResponse));
ctrl.resolveSnapshots();
expect(collectionsApiSpy).to.have.been.called;
})
it('should call CollectionsApi post to delete snapshots', function() {
collectionsApiSpy = sinon.stub(CollectionsApi, 'post').returns(Promise.resolve(successResponse));
ctrl.deleteSnapshots();
Expand Down

0 comments on commit 7996b1a

Please sign in to comment.