/** * Created by vdhakre on 06/04/2018. */ describe('StatusBarController', function () { var $httpBackend, q; beforeEach(module('myitsmApp','templates')); beforeEach(inject(function ($injector, $rootScope, events, $controller, $state, $modal, $q, objectValueMapperService, fieldValidationModel, ticketModel, customFieldLinkFunction, screenConfigurationModel) { $httpBackend = $injector.get('$httpBackend'); this.$rootScope = $rootScope; this.scope = $rootScope.$new(); this.events = events; this.controller = $controller; this.$state = $state; this.$modal = $modal; this.objectValueMapperService = objectValueMapperService; this.fieldValidationModel = fieldValidationModel; this.ticketModel = ticketModel; this.customFieldLinkFunction =customFieldLinkFunction; this.screenConfigurationModel = screenConfigurationModel; q = $q; 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/serverstates').respond(200); $httpBackend.whenGET('/smartit/rest/sessionstatus?getLicenseKey=true').respond(200); $httpBackend.whenGET('views/dashboard/index.html').respond(200); this.scope.ticket = { "type": "incident", "resolution": 'test resolution', "autoAssign": true, "customFields": {}, "serviceType": "User Service Restoration", "status": { "value": "New" }, "impact": "4-Minor/Localized", "urgency": "4-Low", "priority": "Low", "id": "IDGAA5V0GEWV1APGR3HNPFUQG1G0NF", "displayId": "INC5725", "ticketType": "incident", "selectedCompany": "", "attachments": [], "desc": "" }; this.scope.metadata = { "metadatatype": "incident", "statuses": [ { "isValidForCreate": true, "index": 0, "name": "New", "label": "New" }, { "invalidStatusTransitions": [ "New" ], "isValidForCreate": true, "index": 1, "name": "Assigned", "label": "Assigned" }, { "invalidStatusTransitions": [ "New" ], "isValidForCreate": true, "index": 2, "name": "In Progress", "label": "In Progress" }, { "statusReasons": [ { "displayOrder": 0, "index": 19000, "name": "Automated Resolution Reported", "label": "Automated Resolution Reported" } ], "invalidStatusTransitions": [ "New" ], "isValidForCreate": true, "index": 3, "name": "Pending", "label": "Pending" } ] }; this.scope.status = { "isValidForCreate": true, "index": 0, "name": "New", "label": "New" }; this.scope.status2 = { "isValidForCreate": true, "index": 4, "name": "Resolved", "label": "Resolved" }; this.controllerInstance = this.controller('StatusBarController', { $scope: this.scope }); })); it('should be defined', function () { expect(this.controllerInstance).toBeDefined(); }); it('should check for status reason in screen metadata', function () { this.scope.ticket.status.value = 'Pending'; this.scope.ticket.status.reason = 'Pending Original Incident'; spyOn(this.screenConfigurationModel, 'getScreenNameByTicketType').and.callFake(function () { return 'incidentViewScreen'; }); this.screenConfigurationModel.screensCacheByName = { incidentViewScreen: { panels: [ { fields: [ { name: 'status', members: [ { name: 'statusReason', options: [ { name: 'Pending Original Incident', label: 'Pending Original Incident' } ] } ] } ] } ] } }; this.controllerInstance = this.controller('StatusBarController', { $scope: this.scope }); expect(this.controllerInstance).toBeDefined(); }); it('should keep changes to resolutionNote as it is when status is changed', function () { this.scope.selected.resolutionNote = "test value"; this.scope.changeStatus(this.scope.status); this.scope.$apply(); expect(this.scope.selected.resolutionNote).toBe('test value'); }); it('should not send resolutionNote value to backend', function () { spyOn(this.scope, 'changeResolutionNote').and.callFake(function () { return {value: {}}; }); this.scope.selected.resolutionNote = "test value"; this.scope.changeStatus(this.scope.status); this.scope.$apply(); expect(this.scope.changeResolutionNote).not.toHaveBeenCalled(); }); it(' should not call changeResolutionNote when the status is resolved', function () { spyOn(this.scope, 'changeResolutionNote').and.callFake(function () { return {value: {}}; }); this.scope.selected.resolutionNote = "This is a very long resolution note. " + "This is a very long resolution note. This is a very long resolution note. " + "This is a very long resolution note. This is a very long resolution note. " + "This is a very long resolution note. "; this.scope.changeStatus(this.scope.status2); this.scope.$apply(); expect(this.scope.changeResolutionNote).not.toHaveBeenCalled(); }); it(' should test load more function', function () { this.scope.selected.expanded = undefined; this.scope.loadMore(); this.scope.$apply(); expect(this.scope.selected.expanded).toBeTruthy(); this.scope.selected.expanded = true; this.scope.loadMore(); this.scope.$apply(); expect(this.scope.selected.expanded).toBeFalsy(); }); it('needResolutionNote method should return true when resolution note has value', function () { this.scope.selected.resolutionNote = "test value"; this.scope.ticket.resolution = "some value"; this.scope.needResolutionNote(); this.scope.$apply(); expect(this.scope.needResolutionNote).toBeTruthy(); }); it('should return true if status is disabled', function () { var status; this.scope.ticket.accessMappings = { fieldMappings: { status: 'read' } }; status = this.scope.isStatusDisabled(); this.scope.$apply(); expect(status).toBeTruthy(); }); it('should return true if status reason is disabled', function () { var statusReason; this.scope.ticket.accessMappings = { fieldMappings: { statusReason: 'read' } }; statusReason = this.scope.isStatusReasonDisabled(); this.scope.$apply(); expect(statusReason).toBeTruthy(); }); it('should return true if resolution is disabled', function () { var resolution; this.scope.ticket.accessMappings = { fieldMappings: { statusReason: 'read' } }; resolution = this.scope.isResolutionDisabled(); this.scope.$apply(); expect(resolution).toBeTruthy(); }); it('should sucessfully watch statusField.setValueFlag', function () { this.scope.ticket.type = 'change'; this.scope.statusField.setValueFlag = 'Completed'; this.scope.metadata = { statuses: [ { "isValidForCreate": true, "index": 3, "name": "Completed", "label": "Completed" } ] }; this.scope.$apply(); expect(this.scope.statusField.setValueFlag).toBe('#$#'); }); it('should successfully change status reason', function () { var statusReason = { "name": "Client Action Required", "label": "Client Action Required" }; spyOn(this.objectValueMapperService, 'getFieldByName').and.returnValue({name: 'statusReason'}); spyOn(this.scope, '$emit'); this.scope.changeStatusReason(statusReason); this.scope.$apply(); expect(this.scope.$emit).toHaveBeenCalledWith(this.events.WIDGET_VALUE_CHANGE, {fieldName: 'statusReason', memberName: 'statusReason' }); }); it('should successfully change resolution note', function () { spyOn(this.objectValueMapperService, 'getFieldByName').and.returnValue({name: 'resolution'}); spyOn(this.scope, '$emit'); this.scope.changeResolutionNote(); this.scope.$apply(); expect(this.scope.$emit).toHaveBeenCalledWith(this.events.WIDGET_VALUE_CHANGE, {fieldName: 'resolution', memberName: 'resolution' }); }); it('should return true while checking isFieldRequied if ticket type is incident', function () { var fieldStatus; spyOn(this.fieldValidationModel, 'isFieldRequired').and.returnValue(true); fieldStatus = this.scope.isFieldRequired('statusReason'); this.scope.$apply(); expect(this.fieldValidationModel.isFieldRequired).toHaveBeenCalled(); expect(fieldStatus).toBeTruthy(); }); it('should return false while checking isFieldRequied if ticket type is workorder', function () { var fieldStatus; this.scope.ticket.type = 'workorder'; fieldStatus = this.scope.isFieldRequired('statusReason'); this.scope.$apply(); expect(fieldStatus).toBeFalsy(); }); it('should return true while checking isFieldRequied if ticket type is other', function () { var fieldStatus; this.scope.ticket.type = 'knowledge'; fieldStatus = this.scope.isFieldRequired('statusReason'); this.scope.$apply(); expect(fieldStatus).toBeTruthy(); }); it('should return false while checking isFieldRequied if fieldName is not status Reason', function () { var fieldStatus; spyOn(this.fieldValidationModel, 'isFieldRequired').and.returnValue(false); fieldStatus = this.scope.isFieldRequired('status'); this.scope.$apply(); expect(this.fieldValidationModel.isFieldRequired).toHaveBeenCalled(); expect(fieldStatus).toBeFalsy(); }); it('should change the status on click of next button and change the status accordingly in case of change request', function () { this.scope.selected.status = { index: 0 }; this.scope.availableStatuses = [ { "index": 0, "name": "Planning In Progress", "label": "Planning In Progress", }, { "index": 1, "name": "Pending", "label": "Pending" }, { "index": 2, "name": "New", "label": "New" } ]; this.scope.switchState('next', 'change'); this.scope.$apply(); expect(this.scope.selected.status.name).toBe('New'); }); it('should change the status on click of prev button and change the status accordingly in case of change request', function () { this.scope.selected.status = { index: 2 }; this.scope.availableStatuses = [ { "index": 0, "name": "Planning In Progress", "label": "Planning In Progress", }, { "index": 1, "name": "Pending", "label": "Pending" }, { "index": 2, "name": "Completed", "label": "Completed" } ]; this.scope.switchState('prev', 'change'); this.scope.$apply(); expect(this.scope.selected.status.name).toBe('Planning In Progress'); }); it('should change the status on click of next button and change the status accordingly in case of release', function () { this.scope.selected.milestone = { index: 0 }; this.scope.availableMilestoneStatuses = [ { "index": 0, "name": "Planning In Progress", "label": "Planning In Progress", }, { "index": 1, "name": "Pending", "label": "Pending" }, { "index": 2, "name": "New", "label": "New" } ]; this.scope.changeMileStone = angular.noop; spyOn(this.scope, 'changeMileStone'); this.scope.switchState('next', 'release'); this.scope.$apply(); expect(this.scope.changeMileStone).toHaveBeenCalledWith(this.scope.availableMilestoneStatuses[1], 'release'); }); it('should change the status on click of prev button and change the status accordingly in case of release', function () { this.scope.selected.milestone = { index: 2 }; this.scope.availableMilestoneStatuses = [ { "index": 0, "name": "Planning In Progress", "label": "Planning In Progress", }, { "index": 1, "name": "Pending", "label": "Pending" }, { "index": 2, "name": "Completed", "label": "Completed" } ]; this.scope.changeMileStone = angular.noop; spyOn(this.scope, 'changeMileStone'); this.scope.switchState('prev', 'release'); this.scope.$apply(); expect(this.scope.changeMileStone).toHaveBeenCalledWith(this.scope.availableMilestoneStatuses[1], 'release'); }); it('should check next status switch is enabled in case of change request', function () { this.scope.selected.status = { index: 0 }; this.scope.availableStatuses = [ { "index": 0, "name": "Planning In Progress", "label": "Planning In Progress", }, { "index": 1, "name": "Pending", "label": "Pending" }, { "index": 2, "name": "New", "label": "New" } ]; var result = this.scope.checkStatusSwitcherIsDisabled('next', 'change'); this.scope.$apply(); expect(result).toBeFalsy(); }); it('should check next status switch is enabled in case of work order', function () { this.scope.selected.status = { index: 0, name: 'Completed' }; this.scope.availableStatuses = [ { "index": 0, "name": "Planning In Progress", "label": "Planning In Progress", }, { "index": 1, "name": "Closed", "label": "Closed" }, { "index": 2, "name": "New", "label": "New" } ]; var result = this.scope.checkStatusSwitcherIsDisabled('next', 'workorder'); this.scope.$apply(); expect(result).toBeFalsy(); }); it('should check prev status switch is enabled in case of change request', function () { this.scope.selected.status = { index: 2 }; this.scope.availableStatuses = [ { "index": 0, "name": "Planning In Progress", "label": "Planning In Progress", }, { "index": 1, "name": "Pending", "label": "Pending" }, { "index": 2, "name": "Completed", "label": "Completed" } ]; var result = this.scope.checkStatusSwitcherIsDisabled('prev', 'change'); this.scope.$apply(); expect(result).toBeFalsy(); }); it('should check next status switch is disabled in case of release', function () { this.scope.selected.milestone = { index: 2 }; this.scope.availableMilestoneStatuses = [ { "index": 0, "name": "Planning In Progress", "label": "Planning In Progress", }, { "index": 1, "name": "Pending", "label": "Pending" }, { "index": 2, "name": "New", "label": "New" } ]; var result = this.scope.checkStatusSwitcherIsDisabled('next', 'release'); this.scope.$apply(); expect(result).toBeTruthy(); }); it('should check prev status switch is enabled in case of release', function () { this.scope.selected.milestone = { index: 2 }; this.scope.availableMilestoneStatuses = [ { "index": 0, "name": "Planning In Progress", "label": "Planning In Progress", }, { "index": 1, "name": "Pending", "label": "Pending" }, { "index": 2, "name": "Completed", "label": "Completed" } ]; var result = this.scope.checkStatusSwitcherIsDisabled('prev', 'release'); this.scope.$apply(); expect(result).toBeFalsy(); }); it('should transit to edit mode on click of edit button', function () { this.scope.editMode = false; this.$rootScope.$broadcast(this.events.TOGGLE_EDIT_MODE); this.scope.$apply(); expect(this.scope.editMode).toBeTruthy(); }); it('should save data and transit to view mode on click of save button', function () { spyOn(this.objectValueMapperService, 'getFieldByName'); this.scope.editMode = true; this.scope.handleEditableFlagManually = true; this.$rootScope.$broadcast(this.events.AFTER_SAVED_CHANGES); this.scope.$apply(); expect(this.scope.editMode).toBeFalsy(); }); it('should revert all the data and transit to view mode on click of cancel button', function () { this.scope.editMode = true; this.scope.handleEditableFlagManually = true; this.$rootScope.$broadcast(this.events.DISCARD_CHANGES); this.scope.$apply(); expect(this.scope.editMode).toBeFalsy(); expect(this.scope.selected.status.name).toBe('New'); }); it('should watch for scheduled start date and check for valid dates', function () { this.scope.ticket.scheduledStartDate = new Date('Mon Jul 23 2018 00:00:00 GMT+0530 (India Standard Time)'); this.scope.ticket.scheduledEndDate = new Date('Wed Jul 25 2018 00:00:00 GMT+0530 (India Standard Time)'); this.scope.ticket.type = 'change'; var status = { "isValidForCreate": true, "index": 1, "name": "Scheduled For Review", "label": "Scheduled For Review" }; this.scope.changeStatus(status); this.scope.$apply(); expect(this.scope.fixScheduledDates).toBeFalsy(); }); it('should emit the edit event on click of status dropdown', function () { spyOn(this.scope, '$emit'); this.scope.editTicketStatus(); this.scope.$apply(); expect(this.scope.$emit).toHaveBeenCalledWith(this.events.EDIT_STATUS_CLICK); }); it('should handle the TICKET_TEMPLATE_UPDATED event handler and update the ticket', function () { var value = { selectedStatus: { name: 'Resolved' }, selectedStatusReason: 'Fixed', resolution: 'test' }; this.scope.$emit(this.events.TICKET_TEMPLATE_UPDATED, value); this.scope.$apply(); expect(this.scope.selected.resolutionNote).toBe('test'); expect(this.scope.selected.statusReason).toBe('Fixed'); expect(this.scope.selected.status.name).toBe('Resolved'); }); it('should destroy the scope and unbind the event handler', function () { var value = { selectedStatus: { name: 'Resolved' }, selectedStatusReason: 'Fixed', resolution: 'test' }; this.scope.$destroy(); this.scope.$apply(); this.scope.$emit(this.events.TICKET_TEMPLATE_UPDATED, value); this.scope.$apply(); expect(this.scope.selected.resolutionNote).not.toBe('test'); expect(this.scope.selected.statusReason).not.toBe('Fixed'); expect(this.scope.selected.status.name).not.toBe('Resolved'); }); it('should set statusReason to null on initialization ', function () { this.scope.metadata.statuses = []; this.controller('StatusBarController', { $scope: this.scope }); expect(this.scope.selected.statusReason).toBeNull(); }); it('should set displayTruncatedText to true if resolution is more than 250 characters', function () { this.scope.ticket.resolution = 'This sentence is more than 250 characters This sentence ' + 'is more than 250 charactersThis sentence is more than 250 charactersThis sentence is more than 250 characters' + 'This sentence is more than 250 charactersThis sentence is more than 250 characters......'; this.controller('StatusBarController', { $scope: this.scope }); expect(this.scope.displayTruncatedText).toBeTruthy(); }); it('should set availableStatuses accordingly if validKnowledgeTransitions is available', function () { this.scope.metadata.statuses[0].validKnowledgeTransitions = [ { "index": 0, "validStatus": "Planning In Progress", "label": "Planning In Progress", }, { "index": 1, "validStatus": "Pending", "label": "Pending" }, { "index": 2, "validStatus": "Completed", "label": "Completed" } ]; this.scope.ticket.status = { value: 'New' }; this.controller('StatusBarController', { $scope: this.scope }); expect(this.scope.availableStatuses[0].name).toBe('Pending'); }); it('should set availableStatuses to empty array if validKnowledgeTransitions is empty', function () { this.scope.metadata.statuses[0].validKnowledgeTransitions = []; this.scope.ticket.status = { value: 'New' }; this.controller('StatusBarController', { $scope: this.scope }); expect(this.scope.availableStatuses.length).toBe(0); }); it('should successfully fetch available status in case of change request', function () { var validStatus = [ "Cancelled", "Draft", "Pending", "Request For Authorization" ]; spyOn(this.ticketModel, 'getAvailableStatuses').and.callFake(function () { var deferred = q.defer(); deferred.resolve(validStatus); return deferred.promise; }); this.scope.ticket.type = 'change'; this.scope.ticket.status = { value: 'New' }; this.controller('StatusBarController', { $scope: this.scope }); this.scope.$apply(); expect(this.scope.availableStatuses[0].name).toBe('Pending'); }); it('should set the status field value of initialization if scope data is present', function () { this.scope.data = { "name": "status", "label": "", "dataType": "text", "setValueFlag": "#$#", "isWidgetMember": angular.noop, "isWidget": angular.noop, "members": [] }; this.controller('StatusBarController', { $scope: this.scope }); this.scope.$apply(); expect(this.scope.statusField.name).toBe('status'); }); });