Skip to content

Commit

Permalink
Add specs and fix Promise handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ZitaNemeckova committed Nov 26, 2018
1 parent 50ec898 commit 65fb15d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 15 deletions.
Expand Up @@ -126,14 +126,14 @@ function mainCustomButtonFormController(API, miqService, $q, $http) {

vm.saveClicked = function() {
var saveMsg = sprintf(__('%s "%s" has been successfully saved.'), vm.entity, vm.customButtonModel.name);
API.put('/api/custom_buttons/' + vm.customButtonRecordId, vm.prepSaveObject(), {skipErrors: [400]})
return API.put('/api/custom_buttons/' + vm.customButtonRecordId, vm.prepSaveObject(), {skipErrors: [400]})
.then(miqService.redirectBack.bind(vm, saveMsg, 'success', vm.redirectUrl))
.catch(handleErrorMessages);
};

vm.addClicked = function() {
var saveMsg = sprintf(__('%s "%s" has been successfully added.'), vm.entity, vm.customButtonModel.name);
API.post('/api/custom_buttons/', vm.prepSaveObject(), {skipErrors: [400]})
return API.post('/api/custom_buttons/', vm.prepSaveObject(), {skipErrors: [400]})
.then(miqService.redirectBack.bind(vm, saveMsg, 'success', vm.redirectUrl))
.catch(handleErrorMessages);
};
Expand Down Expand Up @@ -214,14 +214,16 @@ function mainCustomButtonFormController(API, miqService, $q, $http) {
errorMessages.forEach(function(message) {
if (message.includes("Name has already been taken")) {
add_flash(__("Name has already been taken"), "error");
return Promise.reject();
} else if (message.includes("Description has already been taken")) {
add_flash(__("Description has already been taken"), "error");
return Promise.reject();
} else {
miqService.handleFailure;
return miqService.handleFailure();
}
});
} else {
miqService.handleFailure;
return miqService.handleFailure();
}
}

Expand Down
@@ -1,33 +1,70 @@
describe('main-custom-button-form-component', function() {
var $componentController, vm, miqService, API, $scope;
beforeEach(module('ManageIQ'));
beforeEach(inject(function (_$componentController_, _API_, _miqService_, $rootScope) {
beforeEach(inject(function (_$componentController_, _API_, _$httpBackend_, _miqService_, $rootScope, $q) {
$componentController = _$componentController_;
API = _API_;
miqService = _miqService_;
$scope = $rootScope.$new();
spyOn(miqService, 'miqFlash');
$httpBackend = _$httpBackend_;

var bindings = {genericObjectDefinitionRecordId: '1', customButtonGroupRecordId: '2', customButtonRecordId: '3', redirectUrl: '/redirect/url'};
vm = $componentController('mainCustomButtonFormController', null, bindings);
var volumeChoicesResponse = {volume_choices: [{ name : "original value" }, { name: "changed value" }]};
API.whenGET('/cloud_volume_backup/volume_form_choices').respond(volumeChoicesResponse);
vm = $componentController("mainCustomButtonFormComponent", null, bindings);
var deferred = $q.defer();
spyOn(API, 'get').and.callFake(function() {return deferred.promise;});
var domainsResponse = {
data:{
distinct_instances_across_domains: ["Automation", "Event", "GenericObject", "MiqEvent", "Request", "parse_automation_request", "parse_event_stream", "parse_provider_category"]
}
};
$httpBackend.whenGET('/generic_object_definition/retrieve_distinct_instances_across_domains').respond(domainsResponse);
vm.$onInit();
$httpBackend.flush();
}));

describe('#saveClick', function() {
beforeEach(function(){
vm.saveClicked();
spyOn(window, 'add_flash');
});
it('delegates miqService ajaxButton', function() {
expect(miqService.miqAjaxButton).toHaveBeenCalledWith('/cloud_volume_backup/backup_restore/1111?button=restore', vm.cloudVolumeBackupModel, { complete: false });

it('catch 400 and call add_flash if error is name/description taken', function(done) {
var rejectionData = {status: 400, data: {error: {message: "Name has already been taken, Description has already been taken"}}};
spyOn(API, 'put').and.returnValue(Promise.reject(rejectionData));
vm.saveClicked()
.then( function() {
expect(add_flash).toHaveBeenCalledWith("Name has already been taken", "error");
expect(add_flash).toHaveBeenCalledWith("Description has already been taken", "error");
done();
})
.catch(function() {
expect(add_flash).toHaveBeenCalledWith("Name has already been taken", "error");
expect(add_flash).toHaveBeenCalledWith("Description has already been taken", "error");

done();
});
});
});

describe('#addClick', function() {
beforeEach(function(){
vm.addClicked();
spyOn(window, 'add_flash');
spyOn(miqService, 'handleFailure').and.callFake(function() {debugger;return Promise.reject()});
});
it('delegates miqService ajaxButton', function() {
expect(miqService.miqAjaxButton).toHaveBeenCalledWith('/cloud_volume_backup/backup_restore/1111?button=restore', vm.cloudVolumeBackupModel, { complete: false });

it('catch 400 and call add_flash', function(done) {
var rejectionData = {status: 400, data: {error: {message: "Name has already been taken, Description has already been taken"}}};
spyOn(API, 'post').and.returnValue(Promise.reject(rejectionData));
vm.addClicked()
.then( function() {
expect(add_flash).toHaveBeenCalledWith("Name has already been taken", "error");
expect(add_flash).toHaveBeenCalledWith("Description has already been taken", "error");
done();
})
.catch(function () {
expect(add_flash).toHaveBeenCalledWith("Name has already been taken", "error");
expect(add_flash).toHaveBeenCalledWith("Description has already been taken", "error");
done();
});
});
});
});

0 comments on commit 65fb15d

Please sign in to comment.