8
0

editableelement.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import createDocumentMock from 'ckeditor5-engine/tests/view/_utils/createdocumentmock';
  6. import RootEditableElement from 'ckeditor5-engine/src/view/rooteditableelement';
  7. import Range from 'ckeditor5-engine/src/view/range';
  8. describe( 'EditableElement', () => {
  9. describe( 'isFocused', () => {
  10. let docMock, viewMain, viewHeader;
  11. beforeEach( () => {
  12. docMock = createDocumentMock();
  13. viewMain = new RootEditableElement( docMock, 'div' );
  14. viewHeader = new RootEditableElement( docMock, 'h1', 'header' );
  15. } );
  16. it( 'should be observable', () => {
  17. const root = new RootEditableElement( createDocumentMock(), 'div' );
  18. expect( root.isFocused ).to.be.false;
  19. const isFocusedSpy = sinon.spy();
  20. root.on( 'change:isFocused', isFocusedSpy );
  21. root.isFocused = true;
  22. expect( root.isFocused ).to.be.true;
  23. expect( isFocusedSpy.calledOnce ).to.be.true;
  24. } );
  25. it( 'should change isFocused on document render event', () => {
  26. const rangeMain = Range.createFromParentsAndOffsets( viewMain, 0, viewMain, 0 );
  27. const rangeHeader = Range.createFromParentsAndOffsets( viewHeader, 0, viewHeader, 0 );
  28. docMock.selection.addRange( rangeMain );
  29. docMock.isFocused = true;
  30. expect( viewMain.isFocused ).to.be.true;
  31. expect( viewHeader.isFocused ).to.be.false;
  32. docMock.selection.setRanges( [ rangeHeader ] );
  33. docMock.fire( 'render' );
  34. expect( viewMain.isFocused ).to.be.false;
  35. expect( viewHeader.isFocused ).to.be.true;
  36. } );
  37. it( 'should change isFocused when document.isFocus changes', () => {
  38. const rangeMain = Range.createFromParentsAndOffsets( viewMain, 0, viewMain, 0 );
  39. const rangeHeader = Range.createFromParentsAndOffsets( viewHeader, 0, viewHeader, 0 );
  40. docMock.selection.addRange( rangeMain );
  41. docMock.isFocused = true;
  42. expect( viewMain.isFocused ).to.be.true;
  43. expect( viewHeader.isFocused ).to.be.false;
  44. docMock.isFocused = false;
  45. expect( viewMain.isFocused ).to.be.false;
  46. expect( viewHeader.isFocused ).to.be.false;
  47. docMock.selection.setRanges( [ rangeHeader ] );
  48. expect( viewMain.isFocused ).to.be.false;
  49. expect( viewHeader.isFocused ).to.be.false;
  50. } );
  51. } );
  52. describe( 'isReadOnly', () => {
  53. it( 'should be observable', () => {
  54. const root = new RootEditableElement( createDocumentMock(), 'div' );
  55. expect( root.isReadOnly ).to.be.false;
  56. const isReadOnlySpy = sinon.spy();
  57. root.on( 'change:isReadOnly', isReadOnlySpy );
  58. root.isReadOnly = true;
  59. expect( root.isReadOnly ).to.be.true;
  60. expect( isReadOnlySpy.calledOnce ).to.be.true;
  61. } );
  62. } );
  63. describe( 'getDocument', ()=> {
  64. it( 'should return document', () => {
  65. const docMock = createDocumentMock();
  66. const root = new RootEditableElement( docMock, 'div' );
  67. expect( root.document ).to.equal( docMock );
  68. } );
  69. } );
  70. } );