/** * Created by mkumar1 on 23-03-2018. */ describe('Test date widget directive', function () { var compile, scope, $httpBackend, ticket, field, events, editTicketDatesService, objectValueMapperService; beforeEach(module('myitsmApp','templates')); beforeEach(function(){ inject(function($compile, $rootScope, $injector, _events_, _editTicketDatesService_, _objectValueMapperService_, _userModel_){ $httpBackend = $injector.get('$httpBackend'); var getLocale = function(){ return readJSON('scripts/app/i18n/resources-locale_en.json'); }, getSupportGroupPersonResponse = function () { return [ { items: [ { id: 'test', loginId: 'test' } ] } ] }; _userModel_.userId = 'Allen'; $httpBackend.whenGET(/^scripts\/app\/i18n\/resources-locale_en.*$/).respond(getLocale()); $httpBackend.whenGET('/smartit/rest/v2/metadata?type=global').respond(200, readJSON('mocks/metadata-global.json')); $httpBackend.whenGET('/smartit/rest/serverstates').respond(200); $httpBackend.whenGET('/smartit/rest/sessionstatus?getLicenseKey=true').respond(200); $httpBackend.whenGET("/smartit/restapi/person/supportgroupperson").respond(200, getSupportGroupPersonResponse()); $httpBackend.whenGET("/smartit/restapi/person/supportgroupperson/Allen").respond(200); compile = $compile; scope = $rootScope.$new(); ticket = { "type": "change", "crossLaunchURL": "", "timing": {name: "Latent"}, "timingReason": "", "manager": { "customFields": {} }, "status": { "value": "Draft" }, "noCategories": 0, "ticketType": "change", "feed": [], "taskPhaseName": "Review", "attachments": [], "earliestStartDate": 'Fri May 18 2018 00:00:00 GMT+0530 (India Standard Time)' }; field = { "name": "scheduledDates", "label": "Scheduled Dates", "arFieldName": "", "members": [ { "name": "scheduledStartDate", "label": "Scheduled Start Date", "arFieldName": "Scheduled Start Date", "type": "dateTimeField", "dataType": "datetime", "required": false, "hasValue": false, "isDynamic": false, "widgetMember": false, "value": null, "extension": [], "isRequired": false, "isReadOnly": false, "isHidden": false, "isMapped": false, "selectionDisabled": false, "mappedAction": null, "selectedIcon": "", "setValueFlag": "#$#", "id": "AGGAA5V0HG8SIAOWZ00NAA7S5DX4QM", "itsmFieldId": 1000000350, "ootb": true, "parentId": "AGGAA5V0HG8SIAOWZI7WAA7QM2X488" }, { "name": "scheduledEndDate", "label": "Scheduled End Date", "arFieldName": "Scheduled End Date", "type": "dateTimeField", "dataType": "datetime", "required": false, "requiredCondition": "", "requiredConditionFlag": 0, "itsmRequired": false, "editable": true, "readOnly": false, "readOnlyCondition": "", "readOnlyConditionFlag": 0, "ootb": true, "parentId": "AGGAA5V0HG8SIAOWZI7WAA7QM2X488" } ], "value": { "scheduledEndDate": 'Fri May 18 2018 00:00:00 GMT+0530 (India Standard Time)', "scheduledStartDate": 'Thu May 17 2018 00:00:00 GMT+0530 (India Standard Time)' } }; events = _events_; editTicketDatesService = _editTicketDatesService_; objectValueMapperService = _objectValueMapperService_; }); }); beforeEach(inject(function ($q, $rootScope) { var deferred = $q.defer(), success = {id: 'AG00123F73CF5EDVYTSQN8FaAAZsUA'}; spyOn(editTicketDatesService, 'scheduledStartDateDisabled').and.callFake(function () { deferred.resolve(success); return deferred.promise; }); spyOn(editTicketDatesService, 'scheduledEndDateDisabled').and.callFake(function () { deferred.resolve(success); return deferred.promise; }); spyOn(editTicketDatesService, 'actualStartDateDisabled').and.callFake(function () { deferred.resolve(success); return deferred.promise; }); spyOn(editTicketDatesService, 'actualEndDateDisabled').and.callFake(function () { deferred.resolve(success); return deferred.promise; }); spyOn(objectValueMapperService, 'getValueByFieldName').and.callFake(function () { deferred.resolve(success); return deferred.promise; }); })); function getCompiledElement(){ var element = angular.element(''); var compiledElement = compile(element)(scope); scope.$digest(); return compiledElement; } it('should compile', function() { scope.field = new FieldVO().build(field); scope.ticket = ticket; var directiveElem = getCompiledElement(); var divElem = directiveElem[0]; expect(divElem).toBeDefined(); }); it('should set actual dates as required', function() { scope.field = new FieldVO().build(field); scope.field.name = 'actualDates'; scope.ticket = ticket; getCompiledElement(); expect(scope.field.isRequired).toBeTruthy(); }); it('should open start date calender', function() { scope.field = new FieldVO().build(field); scope.ticket = ticket; var directiveElem = getCompiledElement(); var isoScope = directiveElem.isolateScope(); isoScope.openStart(new Event('click')); expect(isoScope.status.openedStart).toBeTruthy(); }); it('should open end date calender', function() { scope.field = new FieldVO().build(field); scope.ticket = ticket; var directiveElem = getCompiledElement(); var isoScope = directiveElem.isolateScope(); isoScope.openEnd(new Event('click')); expect(isoScope.status.openedEnd).toBeTruthy(); }); it('should change date edit view', function() { scope.field = new FieldVO().build(field); scope.ticket = ticket; var directiveElem = getCompiledElement(); var isoScope = directiveElem.isolateScope(); isoScope.editDatesView(); expect(scope.ticket.timing.name).toBe('Latent'); }); it('should validate all dates', function() { scope.field = new FieldVO().build(field); scope.ticket = ticket; var directiveElem = getCompiledElement(), name = 'scheduledStartDate'; var isoScope = directiveElem.isolateScope(); isoScope.validator(name); scope.$apply(); expect(editTicketDatesService.scheduledStartDateDisabled).toHaveBeenCalled(); name = 'scheduledEndDate'; isoScope.validator(name); scope.$apply(); expect(editTicketDatesService.scheduledEndDateDisabled).toHaveBeenCalled(); name = 'targetDate'; isoScope.validator(name); scope.$apply(); name = 'actualStartDate'; isoScope.validator(name); scope.$apply(); expect(editTicketDatesService.actualStartDateDisabled).toHaveBeenCalled(); name = 'actualEndDate'; isoScope.validator(name); scope.$apply(); expect(editTicketDatesService.actualEndDateDisabled).toHaveBeenCalled(); }); it('should change date edit view and should fire Widget Value Change event', inject(function($rootScope) { scope.field = new FieldVO().build(field); scope.ticket = ticket; var directiveElem = getCompiledElement(), identifier = 'start'; var isoScope = directiveElem.isolateScope(); spyOn(editTicketDatesService, 'updateDateTime'); spyOn(editTicketDatesService, 'updateTargetDateTime'); spyOn($rootScope, '$broadcast'); isoScope.$digest(); isoScope.onFieldValueChange (scope.field, scope.field.members[0].name, identifier); expect(editTicketDatesService.updateDateTime).toHaveBeenCalledWith(isoScope.dateForm, scope.field.value, 'scheduled'); expect($rootScope.$broadcast).toHaveBeenCalledWith(events.WIDGET_VALUE_CHANGE, {fieldName: scope.field.name, processed: true, memberName: scope.field.members[0].name}); scope.field.name = 'actualDates'; isoScope.onFieldValueChange (scope.field, scope.field.members[0].name, identifier); expect(editTicketDatesService.updateDateTime).toHaveBeenCalledWith(isoScope.dateForm, scope.field.value, 'actual'); expect($rootScope.$broadcast).toHaveBeenCalledWith(events.WIDGET_VALUE_CHANGE, {fieldName: scope.field.name, processed: true, memberName: scope.field.members[0].name}); scope.field.name = 'targetDate'; isoScope.onFieldValueChange (scope.field, scope.field.members[1].name, identifier); expect(editTicketDatesService.updateTargetDateTime).toHaveBeenCalledWith(isoScope.dateForm, isoScope.context); expect($rootScope.$broadcast).toHaveBeenCalledWith(events.WIDGET_VALUE_CHANGE, {fieldName: scope.field.name, processed: true, memberName: scope.field.members[1].name}); })); it('should show error message for invalid target date', function() { scope.field = new FieldVO().build(field); scope.ticket = ticket; var directiveElem = getCompiledElement(); var isoScope = directiveElem.isolateScope(); isoScope.dates = {targetDate: {$invalid: true}}; var errTarget = isoScope.targetDateErrorMessageVisible(); expect(errTarget).toBeTruthy(); }); it('should listen to WIDGET_VALUE_CHANGE and assign data', inject(function ($rootScope) { scope.field = new FieldVO().build(field); scope.ticket = ticket; scope.field.fieldName = 'status'; scope.$digest(); getCompiledElement(); scope.$parent.$broadcast(events.WIDGET_VALUE_CHANGE, scope.field); expect(objectValueMapperService.getValueByFieldName).toHaveBeenCalled(); })); it('should listen to AFTER_SAVED_CHANGES and assign data', inject(function ($rootScope) { scope.field = new FieldVO().build(field); scope.ticket = ticket; scope.$digest(); getCompiledElement(); scope.$parent.$broadcast(events.AFTER_SAVED_CHANGES); expect(scope.field.value.scheduledStartDate).toBe('Thu May 17 2018 00:00:00 GMT+0530 (India Standard Time)'); })); it('should listen to DISCARD_CHANGES and assign data', inject(function ($rootScope) { scope.field = new FieldVO().build(field); scope.ticket = ticket; scope.$digest(); getCompiledElement(); scope.$parent.$broadcast(events.DISCARD_CHANGES); expect(scope.field.value.scheduledEndDate).toBe('Fri May 18 2018 00:00:00 GMT+0530 (India Standard Time)'); })); it('should listen to CLEAR_CHANGE_DATES and assign data', inject(function ($rootScope) { scope.field = new FieldVO().build(field); scope.ticket = ticket; scope.$digest(); getCompiledElement(); scope.$parent.$broadcast(events.CLEAR_CHANGE_DATES, true); expect(scope.field.value.scheduledEndDate).toBe(null); scope.field.name = 'actualDates'; scope.$parent.$broadcast(events.CLEAR_CHANGE_DATES, true); expect(scope.field.value.actualStartDate).toBe(null); scope.field.name = 'targetDate'; scope.$parent.$broadcast(events.CLEAR_CHANGE_DATES, true); expect(scope.field.value.targetDate).toBe(null); })); it('should invoke watch for setValueFlag', function(){ scope.field = new FieldVO().build(field); scope.ticket = ticket; scope.field.setValueFlag = "Some text"; scope.$digest(); getCompiledElement(); expect(scope.field.setValueFlag).toEqual('#$#'); }); });