Canh Ngo pushed to branch feature/cmng-psp1-CHANNELMGR-469 at cms-community / hippo-addon-channel-manager
Commits: 41866afb by Canh Ngo at 2016-03-09T10:24:44+01:00 CHANNELMGR-469: added test cases for PageStructureService#removeComponent() - - - - - f94d29de by Canh Ngo at 2016-03-09T12:18:17+01:00 CHANNELMGR-469: created test-case for HippoIframeController#deleteComponent() - - - - - 3 changed files: - + frontend-ng/src/angularjs/channel/hippoIframe/hippoIframe.controller.spec.js - frontend-ng/src/angularjs/channel/page/pageStructure.service.js - frontend-ng/src/angularjs/channel/page/pageStructure.service.spec.js Changes: ===================================== frontend-ng/src/angularjs/channel/hippoIframe/hippoIframe.controller.spec.js ===================================== --- /dev/null +++ b/frontend-ng/src/angularjs/channel/hippoIframe/hippoIframe.controller.spec.js @@ -0,0 +1,79 @@ +/* + * + * * Copyright 2016 Hippo B.V. (http://www.onehippo.com) + * * + * * Licensed under the Apache License, Version 2.0 (the "License"); + * * you may not use this file except in compliance with the License. + * * You may obtain a copy of the License at + * * + * * http://www.apache.org/licenses/LICENSE-2.0 + * * + * * Unless required by applicable law or agreed to in writing, software + * * distributed under the License is distributed on an "AS IS" BASIS, + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * * See the License for the specific language governing permissions and + * * limitations under the License. + * + */ + +describe('hippoIframeCtrl', function () { + 'use strict'; + + var PageStructureService; + var $mdDialog; + var hippoIframeCtrl; + var $element; + var scope; + var $q; + + beforeEach(function () { + module('hippo-cm'); + + inject(function ($controller, _$rootScope_, _$q_, _$mdDialog_, _PageStructureService_) { + scope = _$rootScope_.$new(); + $q = _$q_; + + $mdDialog = _$mdDialog_; + PageStructureService = _PageStructureService_; + + spyOn(PageStructureService, 'removeComponent'); + spyOn(PageStructureService, 'showComponentProperties'); + + console.log('initiating controller'); + hippoIframeCtrl = $controller('hippoIframeCtrl', { + $scope: scope, + }); + }); + }); + + it('initializes mock services', function () { + expect(PageStructureService.removeComponent).toBeDefined(); + expect(PageStructureService.showComponentProperties).toBeDefined(); + expect($mdDialog.show).toBeDefined(); + expect($mdDialog.confirm).toBeDefined(); + expect(hippoIframeCtrl).toBeDefined(); + }); + + it('shows component properties dialog after rejecting the delete operation', function () { + spyOn($mdDialog, 'show').and.returnValue($q.reject()); + + hippoIframeCtrl.deleteComponent('1234'); + scope.$digest(); + + expect($mdDialog.confirm).toHaveBeenCalled(); + expect($mdDialog.show).toHaveBeenCalled(); + expect(PageStructureService.showComponentProperties).toHaveBeenCalledWith('1234'); + }); + + it('shows the confirmation dialog and deletes selected component', function () { + spyOn($mdDialog, 'show').and.returnValue($q.when([])); + + hippoIframeCtrl.deleteComponent('1234'); + scope.$digest(); + + expect($mdDialog.confirm).toHaveBeenCalled(); + expect($mdDialog.show).toHaveBeenCalled(); + expect(PageStructureService.removeComponent).toHaveBeenCalledWith('1234'); + }); + +}); ===================================== frontend-ng/src/angularjs/channel/page/pageStructure.service.js ===================================== --- a/frontend-ng/src/angularjs/channel/page/pageStructure.service.js +++ b/frontend-ng/src/angularjs/channel/page/pageStructure.service.js @@ -19,10 +19,11 @@ import { ComponentElement } from './element/componentElement'; export class PageStructureService { - constructor($log, HstConstants, hstCommentsProcessorService, HstService, ChannelService, CmsService, PageMetaDataService) { + constructor($q, $log, HstConstants, hstCommentsProcessorService, HstService, ChannelService, CmsService, PageMetaDataService) { 'ngInject'; // Injected + this.$q = $q; this.$log = $log; this.HST = HstConstants; this.HstService = HstService; @@ -90,7 +91,7 @@ export class PageStructureService { /** * Remove the component identified by given Id * @param componentId - * @returns {*} the removed component object + * @returns {*} a promise with removed successfully component */ removeComponent(componentId) { let component = null; @@ -100,13 +101,13 @@ export class PageStructureService { }); if (!foundContainer) { - console.log('Remove component ' + componentId + ' failed'); - return null; + return this.$q.reject(); } // request back-end to remove component return this._removeHstComponent(foundContainer.getId(), componentId) .then(() => { component.removeFromDOM(); + return component; }); } ===================================== frontend-ng/src/angularjs/channel/page/pageStructure.service.spec.js ===================================== --- a/frontend-ng/src/angularjs/channel/page/pageStructure.service.spec.js +++ b/frontend-ng/src/angularjs/channel/page/pageStructure.service.spec.js @@ -20,20 +20,26 @@ describe('PageStructureService', function () { var PageStructureService; var PageMetaDataService; var ChannelService; + var HstService; var $document; + var $q; var $log; var $window; + var $rootScope; beforeEach(function () { module('hippo-cm.channel.page'); - inject(function (_$log_, _$document_, _$window_, _PageStructureService_, _PageMetaDataService_, _ChannelService_) { + inject(function (_$q_, _$rootScope_, _$log_, _$document_, _$window_, _PageStructureService_, _PageMetaDataService_, _ChannelService_, _HstService_) { + $q = _$q_; + $rootScope = _$rootScope_; $log = _$log_; $document = _$document_; $window = _$window_; PageStructureService = _PageStructureService_; PageMetaDataService = _PageMetaDataService_; ChannelService = _ChannelService_; + HstService = _HstService_; }); }); @@ -280,6 +286,76 @@ describe('PageStructureService', function () { expect(PageStructureService.getComponent('no-such-component')).toBeNull(); }); + it('removes a valid component and calls HST successfully', function () { + var container = $j('#container1', $document); + var component = $j('#componentA', $document); + + PageStructureService.registerParsedElement(container[0].previousSibling, { + 'HST-Type': 'CONTAINER_COMPONENT', + uuid: 'container-123', + }); + PageStructureService.registerParsedElement(component[0].childNodes[0], { + 'HST-Type': 'CONTAINER_ITEM_COMPONENT', + 'HST-Label': 'Test Component', + uuid: 'component-1234', + }); + spyOn(HstService, 'doGet').and.returnValue($q.when([])); + + PageStructureService.removeComponent('component-1234').then(function (removedComponent) { + expect(removedComponent.getId()).toEqual('component-1234'); + }); + $rootScope.$digest(); + + expect(HstService.doGet).toHaveBeenCalledWith('container-123', 'delete', 'component-1234'); + }); + + it('removes a valid component but fails to call HST', function () { + var handler = jasmine.createSpy('success'); + var container = $j('#container1', $document); + var component = $j('#componentA', $document); + + PageStructureService.registerParsedElement(container[0].previousSibling, { + 'HST-Type': 'CONTAINER_COMPONENT', + uuid: 'container-123', + }); + PageStructureService.registerParsedElement(component[0].childNodes[0], { + 'HST-Type': 'CONTAINER_ITEM_COMPONENT', + 'HST-Label': 'Test Component', + uuid: 'component-1234', + }); + // mock the call to HST to be failed + spyOn(HstService, 'doGet').and.returnValue($q.reject()); + + PageStructureService.removeComponent('component-1234').then(handler); + $rootScope.$digest(); + + expect(HstService.doGet).toHaveBeenCalledWith('container-123', 'delete', 'component-1234'); + expect(handler).not.toHaveBeenCalled(); + }); + + it('removes an invalid component', function () { + var handler = jasmine.createSpy('success'); + var container = $j('#container1', $document); + var component = $j('#componentA', $document); + + PageStructureService.registerParsedElement(container[0].previousSibling, { + 'HST-Type': 'CONTAINER_COMPONENT', + uuid: 'container-123', + }); + PageStructureService.registerParsedElement(component[0].childNodes[0], { + 'HST-Type': 'CONTAINER_ITEM_COMPONENT', + 'HST-Label': 'Test Component', + uuid: 'component-1234', + }); + spyOn(HstService, 'doGet').and.returnValue($q.when([])); + + PageStructureService.removeComponent('component-123').then(handler); + $rootScope.$digest(); + + expect(handler).not.toHaveBeenCalled(); + expect(HstService.doGet).not.toHaveBeenCalled(); + }); + it('triggers an event to show the component properties dialog', function () { var componentElement = jasmine.createSpyObj(['getId', 'getLabel', 'getLastModified']); componentElement.getId.and.returnValue('testId'); View it on GitLab: https://code.onehippo.org/cms-community/hippo-addon-channel-manager/compare/30a1751a0eca88c6271c2dbbecc062e4e71b7e64...f94d29dee1735c276fccb7997807b69e4f451e88
_______________________________________________ Hippocms-svn mailing list Hippocms-svn@lists.onehippo.org https://lists.onehippo.org/mailman/listinfo/hippocms-svn