8
0

watchdog.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import Watchdog from '../src/watchdog';
  6. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  7. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  8. describe( 'Watchdog', () => {
  9. testUtils.createSinonSandbox();
  10. describe( 'create()', () => {
  11. it( 'should create an editor instance', () => {
  12. const watchdog = new Watchdog();
  13. const editorCreateSpy = testUtils.sinon.spy( VirtualTestEditor, 'create' );
  14. const editorDestroySpy = testUtils.sinon.spy( VirtualTestEditor.prototype, 'destroy' );
  15. watchdog.setCreator( ( el, config ) => VirtualTestEditor.create( el, config ) );
  16. watchdog.setDestructor( editor => editor.destroy() );
  17. return watchdog.create( document.createElement( 'div' ), {} )
  18. .then( () => {
  19. sinon.assert.calledOnce( editorCreateSpy );
  20. sinon.assert.notCalled( editorDestroySpy );
  21. return watchdog.destroy();
  22. } )
  23. .then( () => {
  24. sinon.assert.calledOnce( editorCreateSpy );
  25. sinon.assert.calledOnce( editorDestroySpy );
  26. } )
  27. } );
  28. } );
  29. describe( 'restart()', () => {
  30. it( 'should restart the editor', () => {
  31. const watchdog = new Watchdog();
  32. const editorCreateSpy = testUtils.sinon.spy( VirtualTestEditor, 'create' );
  33. const editorDestroySpy = testUtils.sinon.spy( VirtualTestEditor.prototype, 'destroy' );
  34. watchdog.setCreator( ( el, config ) => VirtualTestEditor.create( el, config ) );
  35. watchdog.setDestructor( editor => editor.destroy() );
  36. return watchdog.create( document.createElement( 'div' ), {} )
  37. .then( () => {
  38. sinon.assert.calledOnce( editorCreateSpy );
  39. sinon.assert.notCalled( editorDestroySpy );
  40. return watchdog.restart();
  41. } )
  42. .then( () => {
  43. sinon.assert.calledTwice( editorCreateSpy );
  44. sinon.assert.calledOnce( editorDestroySpy );
  45. return watchdog.destroy();
  46. } );
  47. } );
  48. it( 'should restart the editor with the same data', () => {
  49. const watchdog = new Watchdog();
  50. // VirtualTestEditor doesn't handle the `config.initialData` properly.
  51. const FakeEditor = getFakeEditor();
  52. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  53. watchdog.setDestructor( editor => editor.destroy() );
  54. return watchdog.create( document.createElement( 'div' ), { initialData: 'foo' } )
  55. .then( () => {
  56. expect( watchdog.editor.getData() ).to.equal( 'foo' );
  57. return watchdog.restart();
  58. } )
  59. .then( () => {
  60. expect( watchdog.editor.getData() ).to.equal( 'foo' );
  61. return watchdog.destroy();
  62. } );
  63. } );
  64. } );
  65. describe( 'editor', () => {
  66. it( 'should be the current editor instance', () => {
  67. const watchdog = new Watchdog();
  68. watchdog.setCreator( ( el, config ) => VirtualTestEditor.create( el, config ) );
  69. watchdog.setDestructor( editor => editor.destroy() );
  70. expect( watchdog.editor ).to.be.null;
  71. let oldEditor;
  72. return watchdog.create( document.createElement( 'div' ), {} )
  73. .then( () => {
  74. oldEditor = watchdog.editor;
  75. expect( watchdog.editor ).to.be.instanceOf( VirtualTestEditor );
  76. return watchdog.restart();
  77. } )
  78. .then( () => {
  79. expect( watchdog.editor ).to.be.instanceOf( VirtualTestEditor );
  80. expect( watchdog.editor ).to.not.equal( oldEditor );
  81. return watchdog.destroy();
  82. } )
  83. .then( () => {
  84. expect( watchdog.editor ).to.be.null;
  85. } );
  86. } );
  87. } );
  88. describe( 'error handling', () => {
  89. it( 'Watchdog should not restart editor during the initialization', () => {
  90. const watchdog = new Watchdog();
  91. watchdog.setCreator( () => Promise.reject( new Error( 'foo' ) ) );
  92. watchdog.setDestructor( () => { } );
  93. return watchdog.create( document.createElement( 'div' ) ).then(
  94. () => { throw new Error( '`watchdog.create()` should throw an error.' ) },
  95. err => {
  96. expect( err ).to.be.instanceOf( Error );
  97. expect( err.message ).to.equal( 'foo' );
  98. }
  99. );
  100. } );
  101. it( 'Watchdog should not restart editor during the destroy', () => {
  102. const watchdog = new Watchdog();
  103. watchdog.setCreator( el => VirtualTestEditor.create( el ) );
  104. watchdog.setDestructor( () => Promise.reject( new Error( 'foo' ) ) );
  105. return Promise.resolve()
  106. .then( () => watchdog.create( document.createElement( 'div' ) ) )
  107. .then( () => watchdog.destroy() )
  108. .then(
  109. () => { throw new Error( '`watchdog.create()` should throw an error.' ) },
  110. err => {
  111. expect( err ).to.be.instanceOf( Error );
  112. expect( err.message ).to.equal( 'foo' );
  113. }
  114. );
  115. } );
  116. } );
  117. } );
  118. function getFakeEditor() {
  119. return class FakeEditor {
  120. static create( el, config ) {
  121. const editor = new this();
  122. return Promise.resolve()
  123. .then( () => editor.create( el, config ) );
  124. }
  125. create( el, config ) {
  126. this.el = el;
  127. this.config = config;
  128. this._data = config.initialData || '';
  129. return this;
  130. }
  131. constructor() {
  132. this.model = {
  133. document: {
  134. version: 0
  135. }
  136. };
  137. }
  138. getData() {
  139. return this._data;
  140. }
  141. destroy() {
  142. this.el.remove();
  143. return Promise.resolve();
  144. }
  145. }
  146. }