watchdog.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. describe( 'Watchdog', () => {
  7. describe( 'simple scenarios', () => {
  8. it( 'watchdog should expose `create()` and `destroy()` methods', () => {
  9. const watchdog = new Watchdog();
  10. const FakeEditor = getFakeEditor();
  11. const editorCreateSpy = sinon.spy( FakeEditor, 'create' );
  12. const editorDestroySpy = sinon.spy( FakeEditor.prototype, 'destroy' );
  13. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  14. watchdog.setDestructor( editor => editor.destroy() );
  15. return watchdog.create()
  16. .then( () => {
  17. sinon.assert.calledOnce( editorCreateSpy );
  18. sinon.assert.notCalled( editorDestroySpy );
  19. return watchdog.destroy();
  20. } )
  21. .then( () => {
  22. sinon.assert.calledOnce( editorCreateSpy );
  23. sinon.assert.calledOnce( editorDestroySpy );
  24. } )
  25. } );
  26. } );
  27. describe( 'restart()', () => {
  28. it( 'should restart the editor', () => {
  29. const watchdog = new Watchdog();
  30. const FakeEditor = getFakeEditor();
  31. const editorCreateSpy = sinon.spy( FakeEditor, 'create' );
  32. const editorDestroySpy = sinon.spy( FakeEditor.prototype, 'destroy' );
  33. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  34. watchdog.setDestructor( editor => editor.destroy() );
  35. return watchdog.create()
  36. .then( () => {
  37. sinon.assert.calledOnce( editorCreateSpy );
  38. sinon.assert.notCalled( editorDestroySpy );
  39. return watchdog.restart();
  40. } )
  41. .then( () => {
  42. sinon.assert.calledTwice( editorCreateSpy );
  43. sinon.assert.calledOnce( editorDestroySpy );
  44. return watchdog.destroy();
  45. } );
  46. } );
  47. } );
  48. describe( 'editor', () => {
  49. it( 'should be the current editor used by the Watchdog', () => {
  50. const watchdog = new Watchdog();
  51. const FakeEditor = getFakeEditor();
  52. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  53. watchdog.setDestructor( editor => editor.destroy() );
  54. expect( watchdog.editor ).to.be.undefined;
  55. let oldEditor;
  56. return watchdog.create()
  57. .then( () => {
  58. oldEditor = watchdog.editor;
  59. expect( watchdog.editor ).to.be.instanceOf( FakeEditor );
  60. return watchdog.restart();
  61. } )
  62. .then( () => {
  63. expect( watchdog.editor ).to.be.instanceOf( FakeEditor );
  64. expect( watchdog.editor ).to.not.equal( oldEditor );
  65. return watchdog.destroy();
  66. } );
  67. } );
  68. } );
  69. } );
  70. function getFakeEditor() {
  71. return class FakeEditor {
  72. static create( el, config ) {
  73. this.el = el;
  74. this.config = config;
  75. return Promise.resolve( new this() );
  76. }
  77. constructor() {
  78. this.model = {
  79. document: {
  80. version: 0
  81. }
  82. };
  83. }
  84. getData() {
  85. return 'foo';
  86. }
  87. setData() {
  88. }
  89. destroy() {
  90. return Promise.resolve();
  91. }
  92. }
  93. }