Tobias Jeger pushed to branch feature/cmng-psp1-CHANNELMGR-467 at cms-community / hippo-addon-channel-manager
Commits: 7a00e051 by Tobias Jeger at 2016-03-18T17:02:49+01:00 CHANNELMGR-467: fixed more pageStructure service tests. - - - - - 3f4a2345 by Tobias Jeger at 2016-03-18T20:45:10+01:00 CHANNELMGR-467: fixed and cleaned up remaining page structure service tests. - - - - - 2 changed files: - frontend-ng/src/angularjs/channel/page/pageStructure.service.fixture.html - frontend-ng/src/angularjs/channel/page/pageStructure.service.spec.js Changes: ===================================== frontend-ng/src/angularjs/channel/page/pageStructure.service.fixture.html ===================================== --- a/frontend-ng/src/angularjs/channel/page/pageStructure.service.fixture.html +++ b/frontend-ng/src/angularjs/channel/page/pageStructure.service.fixture.html @@ -14,50 +14,30 @@ limitations under the License. --> -<!-- placeholder comment --> -<div id="container1"></div> -<!-- placeholder comment --> -<div id="container2"></div> -<div id="container-transparent"> - <!-- placeholder comment --> -</div> -<div id="componentA"> - <!-- placeholder comment --> -</div> -<div id="componentB"> - <!-- placeholder comment --> -</div> -<!-- placeholder comment --> -<div id="component-transparent"></div> -<!-- { "HST-End": "true", "uuid": "test-uuid" } --> -<div id="component-transparent-empty"></div> -<!-- placeholder comment --> -<!-- { "HST-End": "true", "uuid": "test-uuid" } --> - -<!-- { "HST-Type": "CONTAINER_COMPONENT", "HST-Label": "container 1", "HST-XType": "HST.vBox", "uuid": "1111" } --> -<div id="container11"> - <div id="componentAA"> +<!-- { "HST-Type": "CONTAINER_COMPONENT", "HST-Label": "vBox container", "HST-XType": "HST.vBox", "uuid": "container-vbox" } --> +<div id="container-vbox"> + <div id="componentA"> <!-- { "HST-Type": "CONTAINER_ITEM_COMPONENT", "HST-Label": "component A", "uuid": "aaaa" } --> - <p>Some markup in component A</p> + <p id="test">Some markup in component A</p> <!-- { "HST-End": "true", "uuid": "aaaa" } --> </div> - <div id="componentBB"> + <div id="componentB"> <!-- { "HST-Type": "CONTAINER_ITEM_COMPONENT", "HST-Label": "component B", "uuid": "bbbb" } --> <p>Some markup in component B</p> <!-- { "HST-End": "true", "uuid": "bbbb" } --> </div> </div> -<!-- { "HST-End": "true", "uuid": "1111" } --> +<!-- { "HST-End": "true", "uuid": "container-vbox" } --> -<div id="container22"> - <!-- { "HST-Type": "CONTAINER_COMPONENT", "HST-Label": "container 2", "HST-XType": "HST.Transparent", "uuid": "2222" } --> - <!-- { "HST-Type": "CONTAINER_ITEM_COMPONENT", "HST-Label": "component C", "uuid": "cccc" } --> - <!-- Component C is empty --> - <!-- { "HST-End": "true", "uuid": "cccc" } --> - <!-- { "HST-Type": "CONTAINER_ITEM_COMPONENT", "HST-Label": "component D", "uuid": "dddd" } --> - <div id="componentDD"> +<div id="container-no-markup"> + <!-- { "HST-Type": "CONTAINER_COMPONENT", "HST-Label": "NoMarkup container", "HST-XType": "HST.Transparent", "uuid": "container-no-markup" } --> + <!-- { "HST-Type": "CONTAINER_ITEM_COMPONENT", "HST-Label": "Empty component in NoMarkup container", "uuid": "empty-component-no-markup" } --> + <!-- Component is empty --> + <!-- { "HST-End": "true", "uuid": "empty-component-no-markup" } --> + <!-- { "HST-Type": "CONTAINER_ITEM_COMPONENT", "HST-Label": "Component in NoMarkup container", "uuid": "component-no-markup" } --> + <div id="component-no-markup"> <p>Some markup in component D</p> </div> - <!-- { "HST-End": "true", "uuid": "dddd" } --> - <!-- { "HST-End": "true", "uuid": "2222" } --> + <!-- { "HST-End": "true", "uuid": "component-no-markup" } --> + <!-- { "HST-End": "true", "uuid": "container-no-markup" } --> </div> ===================================== 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 @@ -64,8 +64,6 @@ describe('PageStructureService', () => { expect($log.warn).toHaveBeenCalled(); }); - const jsonify = (element) => JSON.parse(element.data); - const childComment = (element) => { var children = element.childNodes; for (let i = 0; i < children.length; i++) { @@ -100,42 +98,71 @@ describe('PageStructureService', () => { return null; }; - it('registers containers in the correct order', () => { - const container1 = $j('#container11', $document)[0]; - const container2 = $j('#container22', $document)[0]; - const comment1 = previousComment(container1); - const comment2 = childComment(container2); + const registerParsedElement = (commentElement) => { + PageStructureService.registerParsedElement(commentElement, JSON.parse(commentElement.data)); + }; + + const registerVBoxContainer = () => { + const container = $j('#container-vbox', $document)[0]; + + registerParsedElement(previousComment(container)); + + return container; + }; + + const registerVBoxComponent = (id) => { + const component = $j(`#${id}`, $document)[0]; + + registerParsedElement(childComment(component)); + + return component; + }; + + const registerNoMarkupContainer = () => { + const container = $j('#container-no-markup', $document)[0]; - PageStructureService.registerParsedElement(comment1, jsonify(comment1)); - PageStructureService.registerParsedElement(comment2, jsonify(comment2)); + registerParsedElement(childComment(container)); + + return container; + }; + + const registerNoMarkupComponent = () => { + const component = $j('#component-no-markup', $document)[0]; + + registerParsedElement(previousComment(component)); + + return component; + }; + + const registerEmptyNoMarkupComponent = () => { + registerParsedElement(nextComment(childComment($j('#container-no-markup', $document)[0]))); + }; + + it('registers containers in the correct order', () => { + const container1 = registerVBoxContainer(); + const container2 = registerNoMarkupContainer(); expect(PageStructureService.containers.length).toEqual(2); expect(PageStructureService.containers[0].type).toEqual('container'); expect(PageStructureService.containers[0].isEmpty()).toEqual(true); expect(PageStructureService.containers[0].getComponents()).toEqual([]); - expect(PageStructureService.containers[0].getJQueryElement('iframeBoxElement')[0]).toEqual(container1); - expect(PageStructureService.containers[0].getLabel()).toEqual('container 1'); + expect(PageStructureService.containers[0].getBoxElement()[0]).toEqual(container1); + expect(PageStructureService.containers[0].getLabel()).toEqual('vBox container'); expect(PageStructureService.containers[1].type).toEqual('container'); expect(PageStructureService.containers[1].isEmpty()).toEqual(true); expect(PageStructureService.containers[1].getComponents()).toEqual([]); - expect(PageStructureService.containers[1].getJQueryElement('iframeBoxElement')[0]).toEqual(container2); - expect(PageStructureService.containers[1].getLabel()).toEqual('container 2'); + expect(PageStructureService.containers[1].getBoxElement()[0]).toEqual(container2); + expect(PageStructureService.containers[1].getLabel()).toEqual('NoMarkup container'); }); it('adds components to the most recently registered container', () => { - const container1 = $j('#container11', $document)[0]; - const componentA = $j('#componentAA', $document)[0]; - const componentB = $j('#componentBB', $document)[0]; - const comment1 = previousComment(container1); - const commentA = childComment(componentA); - const commentB = childComment(componentB); - - PageStructureService.registerParsedElement(comment1, jsonify(comment1)); - PageStructureService.registerParsedElement(comment1, jsonify(comment1)); - PageStructureService.registerParsedElement(commentA, jsonify(commentA)); - PageStructureService.registerParsedElement(commentB, jsonify(commentB)); + registerVBoxContainer(); + registerVBoxContainer(); + + const componentA = registerVBoxComponent('componentA'); + const componentB = registerVBoxComponent('componentB'); expect(PageStructureService.containers.length).toEqual(2); expect(PageStructureService.containers[0].isEmpty()).toEqual(true); @@ -143,21 +170,18 @@ describe('PageStructureService', () => { expect(PageStructureService.containers[1].getComponents().length).toEqual(2); expect(PageStructureService.containers[1].getComponents()[0].type).toEqual('component'); - expect(PageStructureService.containers[1].getComponents()[0].getJQueryElement('iframeBoxElement')[0]).toBe(componentA); + expect(PageStructureService.containers[1].getComponents()[0].getBoxElement()[0]).toBe(componentA); expect(PageStructureService.containers[1].getComponents()[0].getLabel()).toEqual('component A'); expect(PageStructureService.containers[1].getComponents()[0].container).toEqual(PageStructureService.containers[1]); expect(PageStructureService.containers[1].getComponents()[1].type).toEqual('component'); - expect(PageStructureService.containers[1].getComponents()[1].getJQueryElement('iframeBoxElement')[0]).toBe(componentB); + expect(PageStructureService.containers[1].getComponents()[1].getBoxElement()[0]).toBe(componentB); expect(PageStructureService.containers[1].getComponents()[1].getLabel()).toEqual('component B'); expect(PageStructureService.containers[1].getComponents()[1].container).toEqual(PageStructureService.containers[1]); }); it('clears the page structure', () => { - const container = $j('#container11', $document)[0]; - const comment = previousComment(container); - - PageStructureService.registerParsedElement(comment, jsonify(comment)); + registerVBoxContainer(); expect(PageStructureService.containers.length).toEqual(1); @@ -167,54 +191,41 @@ describe('PageStructureService', () => { }); it('registers additional elements', () => { - const container1 = $j('#container11', $document)[0]; - const container2 = $j('#container22', $document); - const comment1 = previousComment(container1); + registerVBoxContainer(); - PageStructureService.registerParsedElement(comment1, jsonify(comment1)); - PageStructureService.containers[0].setJQueryElement('test', container2); + const testElement = $j('#test', $document); + PageStructureService.containers[0].setJQueryElement('test', testElement); - expect(PageStructureService.containers[0].getJQueryElement('test')).toEqual(container2); + expect(PageStructureService.containers[0].getJQueryElement('test')).toEqual(testElement); }); it('finds the DOM element of a transparent container as parent of the comment', () => { - const container = $j('#container22', $document)[0]; - const comment = childComment(container); - - PageStructureService.registerParsedElement(comment, jsonify(comment)); + const container = registerNoMarkupContainer(); expect(PageStructureService.containers.length).toEqual(1); - expect(PageStructureService.containers[0].getJQueryElement('iframeBoxElement')[0]).toBe(container); + expect(PageStructureService.containers[0].getBoxElement()[0]).toBe(container); }); it('finds the DOM element of a component of a transparent container as next sibling of the comment', () => { - const container2 = $j('#container22', $document)[0]; - const componentD = $j('#componentDD', $document)[0]; - const comment2 = childComment(container2); - const commentD = previousComment(componentD); + registerNoMarkupContainer(); - PageStructureService.registerParsedElement(comment2, jsonify(comment2)); - PageStructureService.registerParsedElement(commentD, jsonify(commentD)); + const component = registerNoMarkupComponent(); expect(PageStructureService.containers.length).toEqual(1); expect(PageStructureService.containers[0].isEmpty()).toEqual(false); expect(PageStructureService.containers[0].getComponents().length).toEqual(1); - expect(PageStructureService.containers[0].getComponents()[0].getJQueryElement('iframeBoxElement')[0]).toBe(componentD); + expect(PageStructureService.containers[0].getComponents()[0].getBoxElement()[0]).toBe(component); expect(PageStructureService.containers[0].getComponents()[0].hasNoIFrameDomElement()).not.toEqual(true); }); it('registers no iframe box element in case of a transparent, empty component', () => { - const container2 = $j('#container22', $document)[0]; - const comment2 = childComment(container2); - const commentC = nextComment(comment2); - - PageStructureService.registerParsedElement(comment2, jsonify(comment2)); - PageStructureService.registerParsedElement(commentC, jsonify(commentC)); + registerNoMarkupContainer(); + registerEmptyNoMarkupComponent(); expect(PageStructureService.containers.length).toEqual(1); expect(PageStructureService.containers[0].isEmpty()).toEqual(false); expect(PageStructureService.containers[0].getComponents().length).toEqual(1); - expect(PageStructureService.containers[0].getComponents()[0].getJQueryElement('iframeBoxElement').length).toEqual(0); + expect(PageStructureService.containers[0].getComponents()[0].getBoxElement().length).toEqual(0); }); it('parses the page meta-data and adds it to the PageMetaDataService', () => { @@ -261,16 +272,12 @@ describe('PageStructureService', () => { }); it('returns a known component', () => { - const container1 = $j('#container11', $document)[0]; - const componentA = $j('#componentAA', $document)[0]; - const comment1 = previousComment(container1); - const commentA = childComment(componentA); - - PageStructureService.registerParsedElement(comment1, jsonify(comment1)); - PageStructureService.registerParsedElement(comment1, jsonify(comment1)); - PageStructureService.registerParsedElement(commentA, jsonify(commentA)); + registerVBoxContainer(); + registerVBoxContainer(); + registerVBoxComponent('componentA'); const pageComponent = PageStructureService.getComponentById('aaaa'); + expect(pageComponent).not.toBeNull(); expect(pageComponent.getId()).toEqual('aaaa'); expect(pageComponent.getLabel()).toEqual('component A'); @@ -281,13 +288,8 @@ describe('PageStructureService', () => { }); it('removes a valid component and calls HST successfully', () => { - const container1 = $j('#container11', $document)[0]; - const componentA = $j('#componentAA', $document)[0]; - const comment1 = previousComment(container1); - const commentA = childComment(componentA); - - PageStructureService.registerParsedElement(comment1, jsonify(comment1)); - PageStructureService.registerParsedElement(commentA, jsonify(commentA)); + registerVBoxContainer(); + registerVBoxComponent('componentA'); spyOn(HstService, 'removeHstComponent').and.returnValue($q.when([])); spyOn(RenderingService, 'fetchContainerMarkup').and.returnValue($q.when('')); @@ -296,69 +298,42 @@ describe('PageStructureService', () => { $rootScope.$digest(); - expect(HstService.removeHstComponent).toHaveBeenCalledWith('1111', 'aaaa'); + expect(HstService.removeHstComponent).toHaveBeenCalledWith('container-vbox', 'aaaa'); }); it('removes a valid component but fails to call HST', () => { - const container = $j('#container1', $document); - const 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', - }); + registerVBoxContainer(); + registerVBoxComponent('componentA'); // mock the call to HST to be failed - spyOn(HstService, 'doGet').and.returnValue($q.reject()); + spyOn(HstService, 'removeHstComponent').and.returnValue($q.reject()); - PageStructureService.removeComponentById('component-1234'); + PageStructureService.removeComponentById('aaaa'); $rootScope.$digest(); - expect(HstService.doGet).toHaveBeenCalledWith('container-123', 'delete', 'component-1234'); + expect(HstService.removeHstComponent).toHaveBeenCalledWith('container-vbox', 'aaaa'); }); it('removes an invalid component', () => { - const container = $j('#container1', $document); - const component = $j('#componentA', $document); + registerVBoxContainer(); + registerVBoxComponent('componentA'); - 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([])); + spyOn(HstService, 'removeHstComponent').and.returnValue($q.when([])); - PageStructureService.removeComponentById('component-123'); + PageStructureService.removeComponentById('unknown-component'); $rootScope.$digest(); - expect(HstService.doGet).not.toHaveBeenCalled(); + expect(HstService.removeHstComponent).not.toHaveBeenCalled(); }); it('returns a container by iframe element', () => { - const container1 = $j('#container1', $document); - const container2 = $j('#container2', $document); + registerVBoxContainer(); + const containerElement = registerNoMarkupContainer(); - PageStructureService.registerParsedElement(container1[0].previousSibling, { - 'HST-Type': 'CONTAINER_COMPONENT', - uuid: 'container-1', - }); - PageStructureService.registerParsedElement(container2[0].previousSibling, { - 'HST-Type': 'CONTAINER_COMPONENT', - uuid: 'container-2', - }); + const container = PageStructureService.getContainerByIframeElement(containerElement); - const container = PageStructureService.getContainerByIframeElement(container2[0]); expect(container).not.toBeNull(); - expect(container.getId()).toEqual('container-2'); + expect(container.getId()).toEqual('container-no-markup'); }); it('triggers an event to show the component properties dialog', () => { @@ -395,29 +370,14 @@ describe('PageStructureService', () => { }); it('prints parsed elements', () => { - const container = $j('#container1', $document); - const componentA = $j('#componentA', $document); - const componentB = $j('#componentB', $document); - - PageStructureService.registerParsedElement(container[0].previousSibling, { - 'HST-Type': 'CONTAINER_COMPONENT', - }); - PageStructureService.registerParsedElement(componentA[0].childNodes[0], { - 'HST-Type': 'CONTAINER_ITEM_COMPONENT', - 'HST-Label': 'component A', - }); - PageStructureService.registerParsedElement(container[0].previousSibling, { - 'HST-Type': 'CONTAINER_COMPONENT', - }); - PageStructureService.registerParsedElement(componentB[0].childNodes[0], { - 'HST-Type': 'CONTAINER_ITEM_COMPONENT', - 'HST-Label': 'component B', - }); + registerVBoxContainer(); + registerVBoxComponent('componentA'); + registerVBoxComponent('componentB'); spyOn($log, 'debug'); PageStructureService.printParsedElements(); - expect($log.debug.calls.count()).toEqual(4); + expect($log.debug.calls.count()).toEqual(3); }); }); View it on GitLab: https://code.onehippo.org/cms-community/hippo-addon-channel-manager/compare/96ddd5e57d394fa8b052715ab5fa49b604ba60cb...3f4a234524755a324fc02bd0808589309d98f051
_______________________________________________ Hippocms-svn mailing list Hippocms-svn@lists.onehippo.org https://lists.onehippo.org/mailman/listinfo/hippocms-svn