SmartIT_Extensions/BMC/smart-it-full/test/app/common/edit-summary-directive.spec.js

355 lines
13 KiB
JavaScript

describe('Test Edit Summary directive', function () {
var compile, scope, $httpBackend, attachmentService, objectValueMapperService, $timeout, events, rootScope, deferred, $q;
beforeEach(module('myitsmApp', 'templates'));
beforeEach(function () {
inject(function ($compile, $rootScope, $injector, _attachmentService_, _objectValueMapperService_, _$timeout_, _events_, _$q_) {
$httpBackend = $injector.get('$httpBackend');
var getLocale = function () {
return readJSON('scripts/app/i18n/resources-locale_en.json');
};
$httpBackend.whenGET(/^scripts\/app\/i18n\/resources-locale_en.*$/).respond(getLocale());
$httpBackend.whenGET('/smartit/rest/v2/metadata?type=global').respond(200);
$httpBackend.whenGET('/smartit/rest/serverstates').respond(200);
$httpBackend.whenGET('/smartit/rest/sessionstatus?getLicenseKey=true').respond(200);
$httpBackend.whenPUT('/smartit/rest/task/all').respond([]);
compile = $compile;
rootScope = $rootScope;
scope = $rootScope.$new();
attachmentService = _attachmentService_;
objectValueMapperService = _objectValueMapperService_;
$timeout = _$timeout_;
events = _events_;
$q = _$q_;
deferred = $q.defer();
});
});
beforeEach(function () {
scope.context = 'detail';
scope.asset = {
type: 'task'
};
scope.data = {
members: [
{
name: 'desc',
type: 'textAreaField',
required: true
}
]
};
scope.updateIsHandledByParent = false;
});
function getCompiledElement() {
var element = angular.element('<edit-summary ticket="asset" context="context" attachment="false" class="editable-content-section-block" update-is-handled-by-parent="updateIsHandledByParent" is-desc-required="true" data="data" is-editable="isEditable" edit-mode="editMode" dropable="dropable" desc-limit="10"></edit-summary>');
var compiledElement = compile(element)(scope);
scope.$digest();
return compiledElement;
}
it('should compile', function () {
var directiveElem = getCompiledElement(),
divElem = directiveElem[0];
expect(divElem).toBeDefined();
});
it('should not be editable', function () {
scope.isEditable = false;
var directiveElem = getCompiledElement(),
isolateScope = directiveElem.isolateScope();
expect(isolateScope.isLocked).toBeTruthy();
});
it('should successfully upload the attachment', function () {
scope.editMode = true;
scope.dropable = false;
var directiveElem = getCompiledElement(),
isolateScope = directiveElem.isolateScope(),
attachment = {
pendingSave: true,
name: 'Dummy.xls',
size: 940544,
file: {},
fileExt: 'xls'
},
descField = {
name: 'desc',
value: {
attachmentToUpload: []
}
};
isolateScope.showAttachment = true;
isolateScope.$digest();
spyOn(attachmentService, 'prepareFileToUpload').and.returnValue(attachment);
spyOn(objectValueMapperService, 'getFieldByName').and.returnValue(descField);
isolateScope.handleFileChange(directiveElem.find('.attach-tool__file-input')[0]);
$timeout.flush();
expect(isolateScope.attachmentToUpload[0].name).toBe('Dummy.xls');
expect(descField.value.attachmentToUpload[0].name).toBe('Dummy.xls');
});
it('should dismiss the uploaded attachment', function () {
var directiveElem = getCompiledElement(),
isolateScope = directiveElem.isolateScope();
spyOn(attachmentService, 'dismissAttachment');
isolateScope.dismissAttachment();
expect(attachmentService.dismissAttachment).toHaveBeenCalled();
});
it('should open uploaded attachment on click', function () {
var directiveElem = getCompiledElement(),
isolateScope = directiveElem.isolateScope();
spyOn(attachmentService, 'getAttachmentFile');
isolateScope.handleAttachmentClick();
expect(attachmentService.getAttachmentFile).toHaveBeenCalled();
});
it('should not open uploaded attachment if it is in draft or create state', function () {
scope.context = 'draft';
var directiveElem = getCompiledElement(),
isolateScope = directiveElem.isolateScope(),
returnVal;
returnVal = isolateScope.handleAttachmentClick();
expect(returnVal).toBeFalsy();
});
it('should handle toggle Edit Mode event handler and successfully toggle to edit mode if data is already present in scope', function () {
scope.data.value = {
desc: 'dummy'
};
var directiveElem = getCompiledElement(),
isolateScope = directiveElem.isolateScope();
isolateScope.edit();
expect(isolateScope.editMode).toBeTruthy();
expect(isolateScope.descCopy).toBe('dummy');
});
it('should successfully toggle to edit mode if data is not present in scope', function () {
scope.asset.desc = 'dummy';
var directiveElem = getCompiledElement(),
isolateScope = directiveElem.isolateScope();
rootScope.$broadcast(events.TOGGLE_EDIT_MODE);
expect(isolateScope.editMode).toBeTruthy();
expect(isolateScope.descCopy).toBe('dummy');
});
it('should should handle cancel event handler and revert all changes on click of cancel', function () {
scope.asset.attachments = [{ name: 'Dummy.xls' }];
scope.descCopy = 'dummy test 1';
scope.asset.desc = 'dummy';
scope.context = 'draft';
scope.data.value = {
desc: ''
};
scope.data.name = 'description';
var directiveElem = getCompiledElement(),
isolateScope = directiveElem.isolateScope();
spyOn(isolateScope, '$emit');
rootScope.$broadcast(events.DISCARD_CHANGES);
expect(isolateScope.attachmentToDelete.length).toBe(0);
expect(isolateScope.attachmentToUpload.length).toBe(0);
expect(isolateScope.descCopy).toBe('dummy');
expect(isolateScope.attachments[0].name).toBe('Dummy.xls');
expect(isolateScope.ticket.attachments[0].name).toBe('Dummy.xls');
expect(isolateScope.$emit).toHaveBeenCalledWith(events.WIDGET_VALUE_CHANGE, { fieldName: 'description' });
});
it('should change the height and check for max length of the text area', function () {
var directiveElem = getCompiledElement(),
isolateScope = directiveElem.isolateScope();
isolateScope.data.name = 'description';
isolateScope.context = 'create';
isolateScope.descCopy = 'this is description of text box';
isolateScope.data.value = {
desc: ''
};
spyOn(isolateScope, '$emit');
isolateScope.onTicketDescriptionChange();
expect(isolateScope.ticket.desc).toBe('this is de');
expect(isolateScope.$emit).toHaveBeenCalledWith(events.WIDGET_VALUE_CHANGE, { fieldName: 'description', memberName: 'description' });
expect(isolateScope.$emit).toHaveBeenCalledWith(events.FIELD_FORM_IS_DIRTY);
});
it('should toggle summary on click of show/less link', function () {
var directiveElem = getCompiledElement(),
isolateScope = directiveElem.isolateScope();
isolateScope.summaryExpanded = false;
isolateScope.toggleSummary();
$timeout.flush();
expect(isolateScope.summaryExpanded).toBeTruthy();
});
it('should handle afterSaveChangesHandler event Handler on click of Save button', function () {
scope.asset.attachments = [{ name: 'Attach.xls' }];
var directiveElem = getCompiledElement(),
isolateScope = directiveElem.isolateScope(),
descField = {
value: {
attachmentToUpload: [{ name: 'Dummy.xls' }],
attachmentToDelete: [{ name: 'Delete.xls' }]
}
};
spyOn(objectValueMapperService, 'getFieldByName').and.returnValue(descField);
rootScope.$broadcast(events.AFTER_SAVED_CHANGES);
expect(descField.value.attachmentToUpload.length).toBe(0);
expect(isolateScope.attachments[0].name).toBe('Attach.xls');
});
it('should handle handleSaveChanges event Handler on click of Save button', function () {
var directiveElem = getCompiledElement(),
isolateScope = directiveElem.isolateScope(),
response = [{ name: 'Dummy1.xls', pendingSave: false }];
spyOn($q, 'all').and.callFake(function () {
deferred.resolve(response);
return deferred.promise;
});
spyOn(isolateScope, '$emit');
spyOn(attachmentService, 'deleteAttachment');
spyOn(attachmentService, 'uploadAttachment').and.callFake(function () {
deferred.resolve(response);
return deferred.promise;
});
isolateScope.state.descriptionChanged = true;
isolateScope.descCopy = 'Dummy Text';
isolateScope.attachmentToUpload = [{ name: 'Dummy.xls', pendingSave: false }];
isolateScope.attachmentToDelete = [{ name: 'Delete.xls', pendingSave: false, attachmentReference: {} }];
rootScope.$broadcast(events.SAVE_CHANGES);
isolateScope.$digest();
expect(isolateScope.attachments[0].name).toBe('Dummy1.xls');
expect(attachmentService.deleteAttachment).toHaveBeenCalled();
});
it('should handle handleSaveChanges event Handler and revert all changes in case of any error', function () {
var directiveElem = getCompiledElement(),
isolateScope = directiveElem.isolateScope();
spyOn($q, 'all').and.callFake(function () {
deferred.reject('');
return deferred.promise;
});
isolateScope.state.descriptionChanged = false;
isolateScope.descCopy = 'Dummy Text';
isolateScope.ticket.desc = 'old data';
isolateScope.data.value = {
desc: ''
};
rootScope.$broadcast(events.SAVE_CHANGES);
isolateScope.$digest();
expect(isolateScope.descCopy).toBe('old data');
});
it('should handle handleSaveAllChangesComplete event Handler', function () {
scope.updateIsHandledByParent = true;
var directiveElem = getCompiledElement(),
isolateScope = directiveElem.isolateScope();
rootScope.$broadcast(events.SAVE_ALL_CHANGES_COMPLETE);
expect(isolateScope.editMode).toBeFalsy();
expect(isolateScope.state.descriptionChanged).toBeFalsy();
});
it('should successfully watch setValueFlag', function () {
var directiveElem = getCompiledElement(),
isolateScope = directiveElem.isolateScope();
isolateScope.data.value = {
desc: ''
};
isolateScope.data.setValueFlag = 'Dummy Data';
isolateScope.$digest();
expect(isolateScope.data.setValueFlag).toBe('#$#');
});
it('should successfully hanlde DESC_CHANGED event handler and update the description on call', function () {
var directiveElem = getCompiledElement(),
isolateScope = directiveElem.isolateScope();
isolateScope.ticket.desc = 'Dummy Data';
rootScope.$broadcast(events.DESC_CHANGED);
expect(isolateScope.descCopy).toBe('Dummy Data');
});
it('should successfully hanlde CLEAR_DESC event handler and clear the description on call', function () {
var directiveElem = getCompiledElement(),
isolateScope = directiveElem.isolateScope();
isolateScope.ticket.desc = 'Dummy Data';
isolateScope.descCopy = 'Dummy Data';
rootScope.$broadcast(events.CLEAR_DESC);
expect(isolateScope.descCopy).toBe('');
expect(isolateScope.ticket.desc).toBe('');
});
it('should successfully handle TICKET_TEMPLATE_UPDATED event handler on update of ticket template', function () {
var directiveElem = getCompiledElement(),
isolateScope = directiveElem.isolateScope();
isolateScope.data = {
name: 'description',
value: {
desc: ''
}
};
isolateScope.$emit(events.TICKET_TEMPLATE_UPDATED, { desc: 'Dummy'});
expect(isolateScope.descCopy).toBe('Dummy');
expect(isolateScope.data.value.desc).toBe('Dummy');
});
it('should successfully destroy scope', function () {
var directiveElem = getCompiledElement(),
isolateScope = directiveElem.isolateScope();
expect(isolateScope.$parent.context).toBe('detail');
isolateScope.$destroy();
expect(isolateScope.$parent).toBeNull();
});
});