editable.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import Editor from '/ckeditor5/editor.js';
  7. import Editable from '/ckeditor5/editable.js';
  8. import EditingController from '/ckeditor5/core/treecontroller/editingcontroller.js';
  9. import ViewElement from '/ckeditor5/core/treeview/element.js';
  10. describe( 'Editable', () => {
  11. const ELEMENT_NAME = 'h1';
  12. const EDITABLE_NAME = 'editableNaNaNaNa';
  13. let editable, editor;
  14. beforeEach( () => {
  15. editor = new Editor();
  16. editable = new Editable( editor, EDITABLE_NAME );
  17. } );
  18. describe( 'constructor', () => {
  19. it( 'sets the properties', () => {
  20. expect( editable ).to.have.property( 'editor', editor );
  21. expect( editable ).to.have.property( 'name', EDITABLE_NAME );
  22. expect( editable ).to.have.property( 'isEditable', true );
  23. expect( editable ).to.have.property( 'isFocused', false );
  24. } );
  25. } );
  26. describe( 'isEditable', () => {
  27. it( 'is observable', () => {
  28. const spy = sinon.spy();
  29. editable.on( 'change:isEditable', spy );
  30. editable.isEditable = false;
  31. expect( spy.calledOnce ).to.be.true;
  32. } );
  33. } );
  34. describe( 'isFocused', () => {
  35. it( 'is observable', () => {
  36. const spy = sinon.spy();
  37. editable.on( 'change:isFocused', spy );
  38. editable.isFocused = true;
  39. expect( spy.calledOnce ).to.be.true;
  40. } );
  41. } );
  42. describe( 'bindTo', () => {
  43. let domElement, editingView;
  44. beforeEach( () => {
  45. domElement = document.createElement( ELEMENT_NAME );
  46. editor.editing = new EditingController();
  47. editingView = editor.editing.view;
  48. editable.bindTo( domElement );
  49. } );
  50. it( 'creates view root element', () => {
  51. expect( editable.viewElement ).to.be.instanceof( ViewElement );
  52. expect( editable.viewElement ).to.have.property( 'name', ELEMENT_NAME );
  53. expect( editingView.viewRoots.get( EDITABLE_NAME ) ).to.equal( editable.viewElement );
  54. } );
  55. describe( 'isFocused binding', () => {
  56. it( 'reacts on editingView#focus', () => {
  57. editingView.fire( 'focus', {
  58. target: editable.viewElement
  59. } );
  60. expect( editable ).to.have.property( 'isFocused', true );
  61. } );
  62. it( 'reacts on editingView#blur', () => {
  63. editable.isFocused = true;
  64. editingView.fire( 'blur', {
  65. target: editable.viewElement
  66. } );
  67. expect( editable ).to.have.property( 'isFocused', false );
  68. } );
  69. it( 'reacts on editingView#focus only if target matches', () => {
  70. editingView.fire( 'focus', {
  71. target: new ViewElement( 'foo' )
  72. } );
  73. expect( editable ).to.have.property( 'isFocused', false );
  74. } );
  75. it( 'reacts on editingView#blur only if target matches', () => {
  76. editable.isFocused = true;
  77. editingView.fire( 'blur', {
  78. target: new ViewElement( 'foo' )
  79. } );
  80. expect( editable ).to.have.property( 'isFocused', true );
  81. } );
  82. } );
  83. } );
  84. describe( 'destroy', () => {
  85. it( 'offs everything', () => {
  86. const spy = sinon.spy( editable, 'stopListening' );
  87. editable.destroy();
  88. expect( spy.calledOnce ).to.be.true;
  89. expect( editable.viewElement ).to.be.null;
  90. expect( editable.domElement ).to.be.null;
  91. } );
  92. } );
  93. } );