diff --git a/src/dom_components/view/ComponentTextView.js b/src/dom_components/view/ComponentTextView.js index f5223e49d..0c6535443 100644 --- a/src/dom_components/view/ComponentTextView.js +++ b/src/dom_components/view/ComponentTextView.js @@ -78,17 +78,13 @@ export default ComponentView.extend({ * @return string */ getContent() { - const { rte } = this; - const { activeRte } = rte || {}; - let content = ''; + const { activeRte } = this; + const canGetRteContent = + activeRte && typeof activeRte.getContent === 'function'; - if (activeRte && typeof activeRte.getContent === 'function') { - content = activeRte.getContent(); - } else { - content = this.getChildrenContainer().innerHTML; - } - - return content; + return canGetRteContent + ? activeRte.getContent() + : this.getChildrenContainer().innerHTML; }, /** diff --git a/test/specs/dom_components/view/ComponentTextView.js b/test/specs/dom_components/view/ComponentTextView.js index 72f36f0a9..4a35fb63f 100644 --- a/test/specs/dom_components/view/ComponentTextView.js +++ b/test/specs/dom_components/view/ComponentTextView.js @@ -39,4 +39,38 @@ describe('ComponentTextView', () => { fixtures.appendChild(view.render().el); expect(view.el.innerHTML).toEqual('test'); }); + + describe('.getContent', () => { + let fakeRte, fakeRteContent, fakeChildContainer; + + beforeEach(() => { + fakeRteContent = 'fakeRteContent'; + + fakeRte = { + getContent: jest.fn(() => fakeRteContent) + }; + + fakeChildContainer = { + innerHTML: 'fakeChildInnerHTML' + }; + + spyOn(view, 'getChildrenContainer').and.returnValue(fakeChildContainer); + }); + + it('should get content from active RTE if available', () => { + view.activeRte = fakeRte; + expect(view.getContent()).toEqual(fakeRteContent); + expect(fakeRte.getContent).toHaveBeenCalled(); + }); + + it("should get child container's `innerHTML` if active RTE is not available or if it has no `getContent` function", () => { + expect(view.getContent()).toEqual(fakeChildContainer.innerHTML); + + fakeRte.getContent = null; + view.activeRte = fakeRte; + expect(view.getContent()).toEqual(fakeChildContainer.innerHTML); + + expect(view.getChildrenContainer).toHaveBeenCalledTimes(2); + }); + }); });