Merge "Add unit tests to edit-image to improve coverage"

This commit is contained in:
Jenkins 2016-08-18 04:55:36 +00:00 committed by Gerrit Code Review
commit ca9919722c

View File

@ -18,13 +18,14 @@
'use strict'; 'use strict';
describe('horizon.app.core.images.actions.edit.service', function() { describe('horizon.app.core.images.actions.edit.service', function() {
var service, $scope, $q, deferred, testImage, $timeout, updateImageDeferred;
var existingMetadata = {p1: '1', p2: '2'}; var existingMetadata = {p1: '1', p2: '2'};
var metadataService = { var metadataService = {
getMetadata: function() { getMetadata: function() {
return { return {
then: function(callback) { then: function(callback) {
callback({ return callback({
data: existingMetadata data: existingMetadata
}); });
} }
@ -33,7 +34,7 @@
editMetadata: function() { editMetadata: function() {
return { return {
then: function(callback) { then: function(callback) {
callback(); return callback();
} }
}; };
} }
@ -46,16 +47,12 @@
}; };
var glanceAPI = { var glanceAPI = {
updateImage: function(image) { updateImage: function() {
return { return updateImageDeferred.promise;
then: function(callback) {
callback({data: image});
}
};
}, },
getImage: function() { getImage: function() {
var imageLoad = $q.defer(); var imageLoad = $q.defer();
imageLoad.resolve({data: testImage}); imageLoad.resolve({data: {id: 1, name: 'Test'}});
return imageLoad.promise; return imageLoad.promise;
} }
}; };
@ -77,8 +74,6 @@
} }
}; };
var service, $scope, $q, deferred, testImage, $timeout;
/////////////////////// ///////////////////////
beforeEach(module('horizon.framework')); beforeEach(module('horizon.framework'));
@ -98,6 +93,7 @@
service = $injector.get('horizon.app.core.images.actions.edit.service'); service = $injector.get('horizon.app.core.images.actions.edit.service');
service.initScope($scope); service.initScope($scope);
deferred = $q.defer(); deferred = $q.defer();
updateImageDeferred = $q.defer();
$timeout = _$timeout_; $timeout = _$timeout_;
})); }));
@ -133,6 +129,50 @@
$scope.$apply(); $scope.$apply();
}); });
describe('submit', function() {
beforeEach(function() {
var image = {id: 1, name: 'Original'};
spyOn(glanceAPI, 'updateImage').and.callThrough();
spyOn(wizardModalService, 'modal').and.callThrough();
service.initScope($scope);
service.perform(image);
$timeout.flush();
});
it('passes the image from the model to updateImage', function() {
var modalArgs = wizardModalService.modal.calls.argsFor(0)[0];
modalArgs.submit();
updateImageDeferred.resolve();
$timeout.flush();
expect(glanceAPI.updateImage.calls.argsFor(0)[0]).toEqual({id: 1, name: 'Test'});
});
it('returns a failed result if API call fails', function() {
var modalArgs = wizardModalService.modal.calls.argsFor(0)[0];
var result = modalArgs.submit();
updateImageDeferred.reject();
result.then(function err(data) {
expect(data.failed.length).toBe(1);
});
$timeout.flush();
});
it('updates metadata on event', function() {
$scope.$emit('horizon.app.core.images.IMAGE_METADATA_CHANGED', {i_am: 'metadata'});
$scope.$apply();
spyOn(metadataService, 'editMetadata').and.callThrough();
var modalArgs = wizardModalService.modal.calls.argsFor(0)[0];
modalArgs.submit();
expect(metadataService.editMetadata.calls.argsFor(0)[2]).toEqual({i_am: 'metadata'});
});
});
function permissionShouldFail(permissions) { function permissionShouldFail(permissions) {
permissions.then( permissions.then(
function() { function() {