describe('Test impacted areas directive', function () { var compile, scope, rootScope, $httpBackend, searchModel, field, ticket, deferred, customerCompanyData, createTicketModel, systemAlertService, events; beforeEach(module('myitsmApp','templates')); beforeEach(module('changeModule')); beforeEach(function(){ inject(function($compile, $rootScope, $injector, _searchModel_, $q, _createTicketModel_, _systemAlertService_, _events_){ $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/v2/metadata?type=change').respond(200); $httpBackend.whenGET(/^\/smartit\/rest\/foundation\/items*/).respond(200); compile = $compile; rootScope = $rootScope; scope = $rootScope.$new(); searchModel = _searchModel_; deferred = $q.defer(); createTicketModel = _createTicketModel_; systemAlertService = _systemAlertService_; events = _events_; }); }); beforeEach(function() { field = { "name": "impactedAreas", "label": "Impacted Areas", "value": { "impactedAreas": [{ "company": { "name": "Petramco" }, "site": { "name": "Irvine", "region": "Americas", "siteGroup": "Human Resources" }, "organization": "Engineering", "department": "Build / Release", "id": "CIA000000000168" }] } }; ticket = { "type": "change", "accessMappings": { "fieldMappings": { "impactedService": "write", "summary": "write" }, "company": { "name": "Petramco" } } }; scope.field = new FieldVO().build(field); scope.ticket = ticket; }); function loadCustomerCompanyData() { customerCompanyData = { "name": "Calbro Services", "site": { "name": "Boston Support Center", "address": { "street": "350 Seventh Avenue, 18th Floor", "city": "Boston", "state": "Massachusetts", "country": "United States", "zip": "02101", "address": "350 Seventh Avenue, 18th Floor\r\nBoston, Massachusetts 02101\r\nUnited States" }, "region": "Americas", "siteGroup": "United States", "siteId": "STE_SOLN0002845" } }; } function getCompiledElement(){ var element = angular.element(''); var compiledElement = compile(element)(scope); scope.$digest(); return compiledElement; } it('should compile', function(){ var directiveElem = getCompiledElement(); var divElem = directiveElem[0]; expect(divElem).toBeDefined(); }); it('should close on cancel press', function () { scope.change = new ChangeVO(); var directiveElem = getCompiledElement(); var isolatedScope = directiveElem.isolateScope(); spyOn(isolatedScope, '$emit'); rootScope.$broadcast('discardChanges'); }); it('should check tooManyCompanies is set to true on controller load', function() { var companies = [ { "name": "Calbro Services", "id": "AG00123F73CF5EDFYTSQRbxaAAFsUA", "attributeMap": {} }, { "name": "Invention, Inc.", "id": "AG00123F73CF5EDFYTSQdLxaAAGcUA", "attributeMap": {} } ]; var data = {companies: companies}; spyOn(searchModel, 'getOperatingCompanies').and.callFake(function () { deferred.resolve(data); return deferred.promise; }); data.exceedsChunkSize = true; var directiveElem = getCompiledElement(); var isolatedScope = directiveElem.isolateScope(); expect(searchModel.getOperatingCompanies).toHaveBeenCalled(); expect(isolatedScope.state.tooManyCompanies).toBeTruthy(); expect(isolatedScope.selections.companies[0].name).toBe('Calbro Services'); }); it('should check tooManyCompanies is set to false on controller load', function() { var companies = []; var data = {companies: companies}; spyOn(searchModel, 'getOperatingCompanies').and.callFake(function () { deferred.resolve(data); return deferred.promise; }); data.exceedsChunkSize = false; var directiveElem = getCompiledElement(); var isolatedScope = directiveElem.isolateScope(); expect(searchModel.getOperatingCompanies).toHaveBeenCalled(); expect(isolatedScope.state.tooManyCompanies).toBeFalsy(); }); it('should set scope.data.value to empty object if scope.data.value is not provided', function() { field = { "name": "impactedAreas", "label": "Impacted Areas" }; ticket = { "type": "change", "accessMappings": { "fieldMappings": { "impactedService": "write", "summary": "write" }, "company": { "name": "Petramco" } } }; scope.field = new FieldVO().build(field); scope.ticket = ticket; var directiveElem = getCompiledElement(); var isolatedScope = directiveElem.isolateScope(); expect(isolatedScope.data.value.impactedAreas.length).toBe(0); expect(isolatedScope.data.value.addedImpactedAreas.length).toBe(0); expect(isolatedScope.data.value.removedImpactedAreas.length).toBe(0); }); it('should successfully call getCompaniesByName in case of input text search', function() { var companies = [ { "name": "Calbro Services", "id": "AG00123F73CF5EDFYTSQRbxaAAFsUA", "attributeMap": {} }, { "name": "Invention, Inc.", "id": "AG00123F73CF5EDFYTSQdLxaAAGcUA", "attributeMap": {} } ]; var data = {companies: companies}; spyOn(searchModel, 'getCompaniesByText').and.callFake(function () { deferred.resolve(data); return deferred.promise; }); var directiveElem = getCompiledElement(); var isolatedScope = directiveElem.isolateScope(); isolatedScope.getCompaniesByName('Cal'); expect(searchModel.getCompaniesByText).toHaveBeenCalled(); }); it('should successfully watch the customer.company model and update the currentImpactedArea', function() { loadCustomerCompanyData(); var directiveElem = getCompiledElement(); var isolatedScope = directiveElem.isolateScope(); isolatedScope.customer = { company: customerCompanyData }; scope.$digest(); expect(isolatedScope.currentImpactedArea.company.name).toBe('Calbro Services'); }); describe('should successfully watch the currentImpactedArea.company model ', function () { var directiveElem, isolatedScope, resultList; beforeEach(function() { resultList = { objects: [ { "name": "Human Resources", "id": "AG00123F73CF5EDVYTSQN8FaAAZsUA", "attributeMap": { "companyName": "Calbro Services", "id": "POR000000000011" } } ], exceedsChunkSize: true }; loadCustomerCompanyData(); spyOn(createTicketModel, 'getList').and.callFake(function () { deferred.resolve(resultList); return deferred.promise; }); directiveElem = getCompiledElement(); isolatedScope = directiveElem.isolateScope(); isolatedScope.currentImpactedArea = { company: customerCompanyData }; }); it('and load organization and set the value of tooManyOrganizations to true ', function() { resultList.exceedsChunkSize = true; scope.$digest(); expect(isolatedScope.selections.organizations[0].name).toBe('Human Resources'); expect(isolatedScope.state.tooManyOrganizations).toBeTruthy(); }); it('and load organization and set the value of tooManyOrganizations to false ', function() { resultList.exceedsChunkSize = false; scope.$digest(); expect(isolatedScope.selections.organizations[0].name).toBe('Human Resources'); expect(isolatedScope.state.tooManyOrganizations).toBeFalsy(); }); it('and load regions and set the value of tooManyRegions to true ', function() { resultList.exceedsChunkSize = true; scope.$digest(); expect(isolatedScope.selections.regions[0].name).toBe('Human Resources'); expect(isolatedScope.state.tooManyRegions).toBeTruthy(); }); it('and load regions and set the value of tooManyRegions to false ', function() { resultList.exceedsChunkSize = false; scope.$digest(); expect(isolatedScope.selections.regions[0].name).toBe('Human Resources'); expect(isolatedScope.state.tooManyRegions).toBeFalsy(); }); it('and load siteGroups and set the value of tooManySiteGroups to true ', function() { resultList.exceedsChunkSize = true; scope.$digest(); isolatedScope.currentImpactedArea.region = { name: 'Region' } isolatedScope.loadSiteGroups(); scope.$digest(); expect(isolatedScope.selections.siteGroups[0].name).toBe('Human Resources'); expect(isolatedScope.state.tooManySiteGroups).toBeTruthy(); }); it('and load siteGroups and set the value of tooManySiteGroups to false ', function() { resultList.exceedsChunkSize = false; scope.$digest(); expect(isolatedScope.selections.siteGroups[0].name).toBe('Human Resources'); expect(isolatedScope.state.tooManySiteGroups).toBeFalsy(); }); it('and load sites and set the value of tooManySites to true ', function() { resultList.exceedsChunkSize = true; scope.$digest(); isolatedScope.currentImpactedArea.region = { name: 'Region' }; isolatedScope.currentImpactedArea.siteGroup = { name: 'Site Group', attributeMap: { regionName: 'Region Name' } }; isolatedScope.loadSites(); scope.$digest(); expect(isolatedScope.selections.sites[0].name).toBe('Human Resources'); expect(isolatedScope.state.tooManySites).toBeTruthy(); }); it('and load sites and set the value of tooManySites to false ', function() { resultList.exceedsChunkSize = false; scope.$digest(); expect(isolatedScope.selections.sites[0].name).toBe('Human Resources'); expect(isolatedScope.state.tooManySites).toBeFalsy(); }); }); describe('should successfully watch the currentImpactedArea.organization model ', function () { var directiveElem, isolatedScope, resultList; beforeEach(function () { resultList = { objects: [ { "name": "Human Resources", "id": "AG00123F73CF5EDVYTSQN8FaAAZsUA", "attributeMap": { "companyName": "Calbro Services", "id": "POR000000000011" } } ], exceedsChunkSize: true }; loadCustomerCompanyData(); directiveElem = getCompiledElement(); isolatedScope = directiveElem.isolateScope(); isolatedScope.currentImpactedArea = { company: customerCompanyData }; scope.$digest(); spyOn(createTicketModel, 'getList').and.callFake(function () { deferred.resolve(resultList); return deferred.promise; }); isolatedScope.currentImpactedArea.organization = resultList; }); it('and load department and set the value of tooManyDepartments to true ', function () { resultList.exceedsChunkSize = true; scope.$digest(); expect(isolatedScope.selections.departments[0].name).toBe('Human Resources'); expect(isolatedScope.state.tooManyDepartments).toBeTruthy(); }); it('and load department and set the value of tooManyDepartments to false ', function () { resultList.exceedsChunkSize = false; scope.$digest(); expect(isolatedScope.selections.departments[0].name).toBe('Human Resources'); expect(isolatedScope.state.tooManyDepartments).toBeFalsy(); }); }); it('should watch currentImpactedArea.siteGroup and update the value of currentImpactedArea.region and currentImpactedArea.site ', function () { var directiveElem = getCompiledElement(); var isolatedScope = directiveElem.isolateScope(); isolatedScope.currentImpactedArea = { company: customerCompanyData }; scope.$digest(); isolatedScope.currentImpactedArea.site = { attributeMap: { siteGroupName: 'Site Group Name' } }; isolatedScope.currentImpactedArea.siteGroup = { name: 'Hong Kong', attributeMap: { companyName: 'Calbro Services', regionName: 'Asia Pacific' } }; scope.$digest(); expect(isolatedScope.currentImpactedArea.region.name).toBe('Asia Pacific'); expect(isolatedScope.currentImpactedArea.site).toBeNull(); }); it('should watch currentImpactedArea.region and nullify the value of currentImpactedArea.siteGroup and currentImpactedArea.site ', function () { var directiveElem = getCompiledElement(); var isolatedScope = directiveElem.isolateScope(); isolatedScope.currentImpactedArea = { company: customerCompanyData }; scope.$digest(); isolatedScope.currentImpactedArea.site = { attributeMap: { regionName: 'Region Name' } }; isolatedScope.currentImpactedArea.siteGroup = { name: 'Hong Kong', attributeMap: { companyName: 'Calbro Services', regionName: 'Asia Pacific' } }; isolatedScope.currentImpactedArea.region = { name: 'Asia Pacific1' }; scope.$digest(); expect(isolatedScope.currentImpactedArea.siteGroup).toBeNull(); expect(isolatedScope.currentImpactedArea.site).toBeNull(); }); it('should watch currentImpactedArea.site and update the value of currentImpactedArea.siteGroup', function () { var directiveElem = getCompiledElement(); var isolatedScope = directiveElem.isolateScope(); isolatedScope.currentImpactedArea = { company: customerCompanyData }; scope.$digest(); isolatedScope.currentImpactedArea.site = { attributeMap: { siteGroupName: 'Site Group Name', regionName: 'Region Name' } }; scope.$digest(); expect(isolatedScope.currentImpactedArea.siteGroup.name).toBe('Site Group Name'); }); describe('should successfully call ', function () { var directiveElem, isolatedScope; loadCustomerCompanyData(); beforeEach(function () { directiveElem = getCompiledElement(); isolatedScope = directiveElem.isolateScope(); isolatedScope.currentImpactedArea = { company: customerCompanyData }; }); it('getListOfRegionsByName and return the list of regions', function () { var searchResult = { objects: [ { "name": "Americas", "attributeMap": { "companyName": "Calbro Services" } } ], exceedsChunkSize: true }, result; spyOn(createTicketModel, 'getListOfRegionsByName').and.callFake(function () { deferred.resolve(searchResult); return deferred.promise; }); result = isolatedScope.loadRegionsByName('Ame'); result.then(function(response) { console.log(response); expect(response.list[0].name).toBe('Americas'); }); scope.$digest(); expect(createTicketModel.getListOfRegionsByName).toHaveBeenCalled(); }); it('getListOfSiteGroupsByName and return the list of Site Groups', function () { var searchResult = { objects: [ { "name": "Amsterdam", "attributeMap": { "companyName": "Calbro Services", "regionName": "Europe" } } ], exceedsChunkSize: true }, result; isolatedScope.currentImpactedArea.region = { name: 'Asia Pacific1' }; spyOn(createTicketModel, 'getListOfSiteGroupsByName').and.callFake(function () { deferred.resolve(searchResult); return deferred.promise; }); result = isolatedScope.loadSiteGroupsByName('Ams'); result.then(function(response) { expect(response.list[0].name).toBe('Amsterdam'); }); scope.$digest(); expect(createTicketModel.getListOfSiteGroupsByName).toHaveBeenCalled(); }); it('loadSitesByName and return the list of Sites', function () { var searchResult = { objects: [ { "name": "Boston Support Center", "attributeMap": { "companyName": "Calbro Services", "regionName": "Europe" } } ], exceedsChunkSize: true }, result; isolatedScope.currentImpactedArea.region = { name: 'Asia Pacific1' }; isolatedScope.currentImpactedArea.siteGroup = { name: 'Hong Kong' }; spyOn(createTicketModel, 'getListOfSitesByName').and.callFake(function () { deferred.resolve(searchResult); return deferred.promise; }); result = isolatedScope.loadSitesByName('Boston'); result.then(function(response) { expect(response.list[0].name).toBe('Boston Support Center'); }); scope.$digest(); expect(createTicketModel.getListOfSitesByName).toHaveBeenCalled(); }); it('loadOrganizationsByName and return the list of organization', function () { var searchResult = { objects: [ { "name": "Human Resources", "attributeMap": { "companyName": "Calbro Services" } } ], exceedsChunkSize: true }, result; spyOn(createTicketModel, 'getListOfOrganizationsByName').and.callFake(function () { deferred.resolve(searchResult); return deferred.promise; }); result = isolatedScope.loadOrganizationsByName('Human'); result.then(function(response) { expect(response.list[0].name).toBe('Human Resources'); }); scope.$digest(); expect(createTicketModel.getListOfOrganizationsByName).toHaveBeenCalled(); }); it('loadDepartmentsByName and return the list of departments', function () { var searchResult = { objects: [ { "name": "Payroll", "attributeMap": { "companyName": "Calbro Services" } } ], exceedsChunkSize: true }, result; spyOn(createTicketModel, 'getListOfDepartmentsByName').and.callFake(function () { deferred.resolve(searchResult); return deferred.promise; }); isolatedScope.currentImpactedArea.organization = { name: 'Human Resource' }; result = isolatedScope.loadDepartmentsByName('Pay'); result.then(function(response) { expect(response.list[0].name).toBe('Payroll'); }); scope.$digest(); expect(createTicketModel.getListOfDepartmentsByName).toHaveBeenCalled(); }); }); it('should successfully add impacted area', function () { var directiveElem = getCompiledElement(); var isolatedScope = directiveElem.isolateScope(); isolatedScope.customer = { company: customerCompanyData }; scope.$digest(); isolatedScope.currentImpactedArea.site = { attributeMap: { regionName: 'Region Name' } }; isolatedScope.currentImpactedArea.siteGroup = { name: 'Hong Kong', attributeMap: { companyName: 'Calbro Services', regionName: 'Asia Pacific' } }; isolatedScope.currentImpactedArea.region = { name: 'Asia Pacific1' }; isolatedScope.currentImpactedArea.organization = { name: 'Human Resource' }; isolatedScope.currentImpactedArea.department = { name: 'Payroll' }; isolatedScope.addImpactedArea(); scope.$digest(); expect(isolatedScope.currentImpactedArea.company.name).toBe('Calbro Services'); expect(isolatedScope.addedImpactedAreas[0].organization).toBe('Human Resource'); }); it('should give an error if no input is given while adding impacted area', function () { spyOn(systemAlertService, 'error'); var directiveElem = getCompiledElement(); var isolatedScope = directiveElem.isolateScope(); isolatedScope.customer = { company: customerCompanyData }; scope.$digest(); isolatedScope.impactedAreas = [{ "company": { "name": "Calbro Services" } }]; isolatedScope.addImpactedArea(); scope.$digest(); expect(systemAlertService.error).toHaveBeenCalled(); }); describe('should successfully remove impacted area ', function () { var directiveElem, isolatedScope, data; beforeEach(function () { data = { "company": { "name": "Calbro Services" } }; directiveElem = getCompiledElement(); isolatedScope = directiveElem.isolateScope(); isolatedScope.impactedAreas = [data]; }); it('from addedImpactedAreas list', function () { isolatedScope.addedImpactedAreas = [data]; isolatedScope.removeImpactedArea(0); scope.$digest(); expect(isolatedScope.addedImpactedAreas.length).toBe(0); }); it('and added in removedImpactedAreas list', function () { isolatedScope.removedImpactedAreas = []; isolatedScope.removeImpactedArea(0); scope.$digest(); expect(isolatedScope.removedImpactedAreas[0].company.name).toBe('Calbro Services'); }); }); it('should successfully call AFTER_SAVED_CHANGES event', function () { var directiveElem = getCompiledElement(); var isolatedScope = directiveElem.isolateScope(); var data = { impactedAreas: [{ "company": { "name": "Calbro Services" } }] }; isolatedScope.impactedAreas = []; isolatedScope.$parent.$broadcast(events.AFTER_SAVED_CHANGES, data); scope.$digest(); expect(isolatedScope.impactedAreas[0].company.name).toBe('Calbro Services'); }); });