creator-utils.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: ui */
  6. 'use strict';
  7. import { createEditableUI, createEditorUI } from '/ckeditor5/ui/creator-utils.js';
  8. import ObservableMixin from '/ckeditor5/utils/observablemixin.js';
  9. import mix from '/ckeditor5/utils/mix.js';
  10. import Editor from '/ckeditor5/editor.js';
  11. import Controller from '/ckeditor5/ui/controller.js';
  12. import View from '/ckeditor5/ui/view.js';
  13. import Model from '/ckeditor5/ui/model.js';
  14. describe( 'creator-utils', () => {
  15. let editor;
  16. beforeEach( () => {
  17. editor = new Editor();
  18. } );
  19. describe( 'createEditableUI', () => {
  20. let editable;
  21. class Editable {}
  22. mix( Editable, ObservableMixin );
  23. class TestController extends Controller {
  24. constructor( editor, editable ) {
  25. super();
  26. this.editor = editor;
  27. this.editable = editable;
  28. this.viewModel = new Model();
  29. }
  30. }
  31. class TestView extends View {
  32. constructor( model, locale, editableElement ) {
  33. super( model, locale );
  34. this.editableElement = editableElement;
  35. }
  36. }
  37. beforeEach( () => {
  38. editable = new Editable();
  39. editable.bindTo = sinon.spy();
  40. } );
  41. it( 'creates an instance of editable UI', () => {
  42. const editableUI = createEditableUI( editor, editable, TestController, TestView );
  43. expect( editableUI ).to.be.instanceof( TestController );
  44. expect( editableUI ).to.have.property( 'editor', editor );
  45. expect( editableUI ).to.have.property( 'editable', editable );
  46. const view = editableUI.view;
  47. expect( view ).to.be.instanceof( TestView );
  48. expect( view.model ).to.equal( editableUI.viewModel );
  49. expect( view.editableElement ).to.be.undefined;
  50. expect( view.locale ).to.equal( editor.locale );
  51. } );
  52. it( 'passes the editable.domElement to the view and do not try to bind it again', () => {
  53. const editableElement = document.createElement( 'div' );
  54. editable.domElement = editableElement;
  55. editable.bindTo = sinon.spy();
  56. const editableUI = createEditableUI( editor, editable, TestController, TestView );
  57. expect( editableUI.view.editableElement ).to.equal( editableElement );
  58. expect( editable.bindTo.callCount ).to.equal( 0 );
  59. } );
  60. it( 'passes the editable.domElement to the view and do not try to bind it again', () => {
  61. const editableElement = document.createElement( 'div' );
  62. editable.domElement = editableElement;
  63. const editableUI = createEditableUI( editor, editable, TestController, TestView );
  64. expect( editableUI.view.editableElement ).to.equal( editableElement );
  65. expect( editable.bindTo.callCount ).to.equal( 0 );
  66. } );
  67. it( 'if editable.domElement is not yet defined, tries to bind the editable element', () => {
  68. const editableElement = document.createElement( 'div' );
  69. const editableUI = createEditableUI( editor, editable, TestController, TestView );
  70. editableUI.view.editableElement = editableElement;
  71. editableUI.fire( 'ready' );
  72. expect( editable.bindTo.calledWith( editableElement ) ).to.be.true;
  73. } );
  74. } );
  75. describe( 'createEditorUI', () => {
  76. class TestController extends Controller {
  77. constructor( editor ) {
  78. super();
  79. this.editor = editor;
  80. this.viewModel = new Model();
  81. }
  82. }
  83. class TestView extends View {}
  84. it( 'creates an instance of the EditorUI', () => {
  85. const editorUI = createEditorUI( editor, TestController, TestView );
  86. expect( editorUI ).to.be.instanceof( TestController );
  87. expect( editorUI.editor ).to.equal( editor );
  88. expect( editorUI.view ).to.be.instanceof( TestView );
  89. expect( editorUI.view.model ).to.equal( editorUI.viewModel );
  90. expect( editorUI.view.locale ).to.equal( editor.locale );
  91. } );
  92. } );
  93. } );