8
0

rooteditableelement.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: view */
  6. 'use strict';
  7. import ContainerElement from '/ckeditor5/engine/view/containerelement.js';
  8. import EditableElement from '/ckeditor5/engine/view/editableelement.js';
  9. import RootEditableElement from '/ckeditor5/engine/view/rooteditableelement.js';
  10. import createDocumentMock from '/tests/engine/view/_utils/createdocumentmock.js';
  11. describe( 'RootEditableElement', () => {
  12. describe( 'constructor', () => {
  13. it( 'should create an element with default root name', () => {
  14. const root = new RootEditableElement( createDocumentMock(), 'div' );
  15. expect( root ).to.be.instanceof( EditableElement );
  16. expect( root ).to.be.instanceof( ContainerElement );
  17. expect( root.rootName ).to.equal( 'main' );
  18. expect( root.name ).to.equal( 'div' );
  19. expect( root.isFocused ).to.be.false;
  20. expect( root.isReadOnly ).to.be.false;
  21. } );
  22. it( 'should create an element with custom root name', () => {
  23. const root = new RootEditableElement( createDocumentMock(), 'h1', 'header' );
  24. expect( root.rootName ).to.equal( 'header' );
  25. expect( root.name ).to.equal( 'h1' );
  26. expect( root.isFocused ).to.be.false;
  27. expect( root.isReadOnly ).to.be.false;
  28. } );
  29. } );
  30. describe( 'isFocused', () => {
  31. let docMock, viewMain, viewHeader;
  32. beforeEach( () => {
  33. docMock = createDocumentMock();
  34. viewMain = new RootEditableElement( docMock, 'div' );
  35. viewHeader = new RootEditableElement( docMock, 'h1', 'header' );
  36. } );
  37. it( 'should be observable', () => {
  38. const root = new RootEditableElement( createDocumentMock(), 'div' );
  39. expect( root.isFocused ).to.be.false;
  40. const isFocusedSpy = sinon.spy();
  41. root.on( 'change:isFocused', isFocusedSpy );
  42. root.isFocused = true;
  43. expect( root.isFocused ).to.be.true;
  44. expect( isFocusedSpy.calledOnce ).to.be.true;
  45. } );
  46. it( 'should change isFocused when focusedEditable changes', () => {
  47. docMock.focusedEditable = viewMain;
  48. expect( viewMain.isFocused ).to.be.true;
  49. expect( viewHeader.isFocused ).to.be.false;
  50. docMock.focusedEditable = viewHeader;
  51. expect( viewMain.isFocused ).to.be.false;
  52. expect( viewHeader.isFocused ).to.be.true;
  53. docMock.focusedEditable = null;
  54. expect( viewMain.isFocused ).to.be.false;
  55. expect( viewHeader.isFocused ).to.be.false;
  56. } );
  57. } );
  58. describe( 'isReadOnly', () => {
  59. it( 'should be observable', () => {
  60. const root = new RootEditableElement( createDocumentMock(), 'div' );
  61. expect( root.isReadOnly ).to.be.false;
  62. const isReadOnlySpy = sinon.spy();
  63. root.on( 'change:isReadOnly', isReadOnlySpy );
  64. root.isReadOnly = true;
  65. expect( root.isReadOnly ).to.be.true;
  66. expect( isReadOnlySpy.calledOnce ).to.be.true;
  67. } );
  68. } );
  69. describe( 'getDocument', ()=> {
  70. it( 'should return document', () => {
  71. const docMock = createDocumentMock();
  72. const root = new RootEditableElement( docMock, 'div' );
  73. expect( root.getDocument() ).to.equal( docMock );
  74. } );
  75. } );
  76. } );