watchdog.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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. /* globals setTimeout, window, console, document */
  6. import Watchdog from '../src/watchdog';
  7. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  8. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  11. describe( 'Watchdog', () => {
  12. let element;
  13. beforeEach( () => {
  14. element = document.createElement( 'div' );
  15. document.body.appendChild( element );
  16. } );
  17. afterEach( () => {
  18. element.remove();
  19. sinon.restore();
  20. } );
  21. describe( 'create()', () => {
  22. it( 'should create an editor instance', () => {
  23. const watchdog = new Watchdog();
  24. const editorCreateSpy = sinon.spy( ClassicTestEditor, 'create' );
  25. const editorDestroySpy = sinon.spy( ClassicTestEditor.prototype, 'destroy' );
  26. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  27. watchdog.setDestructor( editor => editor.destroy() );
  28. return watchdog.create( element, {} )
  29. .then( () => {
  30. sinon.assert.calledOnce( editorCreateSpy );
  31. sinon.assert.notCalled( editorDestroySpy );
  32. return watchdog.destroy();
  33. } )
  34. .then( () => {
  35. sinon.assert.calledOnce( editorCreateSpy );
  36. sinon.assert.calledOnce( editorDestroySpy );
  37. } );
  38. } );
  39. it( 'should throw an error when the creator is not defined', () => {
  40. const watchdog = new Watchdog();
  41. watchdog.setDestructor( editor => editor.destroy() );
  42. expectToThrowCKEditorError(
  43. () => watchdog.create(),
  44. /^watchdog-creator-not-defined/,
  45. null
  46. );
  47. } );
  48. it( 'should throw an error when the destructor is not defined', () => {
  49. const watchdog = new Watchdog();
  50. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  51. expectToThrowCKEditorError(
  52. () => watchdog.create(),
  53. /^watchdog-destructor-not-defined/,
  54. null
  55. );
  56. } );
  57. it( 'should properly copy the config', () => {
  58. const watchdog = new Watchdog();
  59. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  60. watchdog.setDestructor( editor => editor.destroy() );
  61. const config = {
  62. foo: [],
  63. bar: document.createElement( 'div' )
  64. };
  65. return watchdog.create( element, config ).then( () => {
  66. expect( watchdog.editor.config._config.foo ).to.not.equal( config.foo );
  67. expect( watchdog.editor.config._config.bar ).to.equal( config.bar );
  68. } );
  69. } );
  70. } );
  71. describe( 'restart()', () => {
  72. it( 'should restart the editor', () => {
  73. const watchdog = new Watchdog();
  74. const editorCreateSpy = sinon.spy( ClassicTestEditor, 'create' );
  75. const editorDestroySpy = sinon.spy( ClassicTestEditor.prototype, 'destroy' );
  76. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  77. watchdog.setDestructor( editor => editor.destroy() );
  78. return watchdog.create( element, {} )
  79. .then( () => {
  80. sinon.assert.calledOnce( editorCreateSpy );
  81. sinon.assert.notCalled( editorDestroySpy );
  82. return watchdog.restart();
  83. } )
  84. .then( () => {
  85. sinon.assert.calledTwice( editorCreateSpy );
  86. sinon.assert.calledOnce( editorDestroySpy );
  87. return watchdog.destroy();
  88. } );
  89. } );
  90. it( 'should restart the editor with the same data', () => {
  91. const watchdog = new Watchdog();
  92. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  93. watchdog.setDestructor( editor => editor.destroy() );
  94. return watchdog.create( element, {
  95. initialData: '<p>foo</p>',
  96. plugins: [ Paragraph ]
  97. } )
  98. .then( () => {
  99. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  100. return watchdog.restart();
  101. } )
  102. .then( () => {
  103. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  104. return watchdog.destroy();
  105. } );
  106. } );
  107. it( 'should support editor data passed as the `Editor.create()` as the first argument', () => {
  108. const watchdog = new Watchdog();
  109. watchdog.setCreator( ( data, config ) => ClassicTestEditor.create( data, config ) );
  110. watchdog.setDestructor( editor => editor.destroy() );
  111. return watchdog.create( '<p>foo</p>', { plugins: [ Paragraph ] } )
  112. .then( () => {
  113. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  114. return watchdog.restart();
  115. } )
  116. .then( () => {
  117. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  118. return watchdog.destroy();
  119. } );
  120. } );
  121. } );
  122. describe( 'editor', () => {
  123. it( 'should be the current editor instance', () => {
  124. const watchdog = new Watchdog();
  125. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  126. watchdog.setDestructor( editor => editor.destroy() );
  127. expect( watchdog.editor ).to.be.null;
  128. let oldEditor;
  129. return watchdog.create( element, {} )
  130. .then( () => {
  131. oldEditor = watchdog.editor;
  132. expect( watchdog.editor ).to.be.instanceOf( ClassicTestEditor );
  133. return watchdog.restart();
  134. } )
  135. .then( () => {
  136. expect( watchdog.editor ).to.be.instanceOf( ClassicTestEditor );
  137. expect( watchdog.editor ).to.not.equal( oldEditor );
  138. return watchdog.destroy();
  139. } )
  140. .then( () => {
  141. expect( watchdog.editor ).to.be.null;
  142. } );
  143. } );
  144. } );
  145. describe( 'error handling', () => {
  146. it( 'Watchdog should not restart editor during the initialization', () => {
  147. const watchdog = new Watchdog();
  148. watchdog.setCreator( el =>
  149. ClassicTestEditor.create( el )
  150. .then( () => Promise.reject( new Error( 'foo' ) ) )
  151. );
  152. watchdog.setDestructor( editor => editor.destroy() );
  153. return watchdog.create( element ).then(
  154. () => { throw new Error( '`watchdog.create()` should throw an error.' ); },
  155. err => {
  156. expect( err ).to.be.instanceOf( Error );
  157. expect( err.message ).to.equal( 'foo' );
  158. }
  159. );
  160. } );
  161. it( 'Watchdog should not restart editor during the destroy', () => {
  162. const watchdog = new Watchdog();
  163. watchdog.setCreator( el => ClassicTestEditor.create( el ) );
  164. watchdog.setDestructor( () => Promise.reject( new Error( 'foo' ) ) );
  165. return Promise.resolve()
  166. .then( () => watchdog.create( element ) )
  167. .then( () => watchdog.destroy() )
  168. .then(
  169. () => { throw new Error( '`watchdog.create()` should throw an error.' ); },
  170. err => {
  171. expect( err ).to.be.instanceOf( Error );
  172. expect( err.message ).to.equal( 'foo' );
  173. }
  174. );
  175. } );
  176. it( 'Watchdog should not hide intercepted errors', () => {
  177. const watchdog = new Watchdog();
  178. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  179. watchdog.setDestructor( editor => editor.destroy() );
  180. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  181. const originalErrorHandler = window.onerror;
  182. const windowErrorSpy = sinon.spy();
  183. window.onerror = windowErrorSpy;
  184. return watchdog.create( element ).then( () => {
  185. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  186. return new Promise( res => {
  187. watchdog.on( 'restart', () => {
  188. window.onerror = originalErrorHandler;
  189. sinon.assert.calledOnce( windowErrorSpy );
  190. expect( windowErrorSpy.getCall( 0 ).args[ 0 ] ).to.equal( 'Uncaught CKEditorError: foo' );
  191. watchdog.destroy().then( res );
  192. } );
  193. } );
  194. } );
  195. } );
  196. it( 'Watchdog should intercept editor errors and restart the editor during the runtime', () => {
  197. const watchdog = new Watchdog();
  198. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  199. watchdog.setDestructor( editor => editor.destroy() );
  200. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  201. const originalErrorHandler = window.onerror;
  202. window.onerror = undefined;
  203. return watchdog.create( element ).then( () => {
  204. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  205. return new Promise( res => {
  206. watchdog.on( 'restart', () => {
  207. window.onerror = originalErrorHandler;
  208. watchdog.destroy().then( res );
  209. } );
  210. } );
  211. } );
  212. } );
  213. it( 'Watchdog should not intercept non-editor errors', () => {
  214. const watchdog = new Watchdog();
  215. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  216. watchdog.setDestructor( editor => editor.destroy() );
  217. const editorErrorSpy = sinon.spy();
  218. watchdog.on( 'error', editorErrorSpy );
  219. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  220. const originalErrorHandler = window.onerror;
  221. window.onerror = undefined;
  222. return watchdog.create( element ).then( () => {
  223. setTimeout( () => {
  224. throw new Error( 'foo' );
  225. } );
  226. return new Promise( res => {
  227. setTimeout( () => {
  228. window.onerror = originalErrorHandler;
  229. sinon.assert.notCalled( editorErrorSpy );
  230. watchdog.destroy().then( res );
  231. } );
  232. } );
  233. } );
  234. } );
  235. it( 'Watchdog should not intercept other editor errors', () => {
  236. const watchdog1 = Watchdog.for( ClassicTestEditor );
  237. const watchdog2 = Watchdog.for( ClassicTestEditor );
  238. const config = {
  239. plugins: []
  240. };
  241. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  242. const originalErrorHandler = window.onerror;
  243. window.onerror = undefined;
  244. return Promise.all( [
  245. watchdog1.create( element, config ),
  246. watchdog2.create( element, config )
  247. ] ).then( () => {
  248. return new Promise( res => {
  249. const watchdog1ErrorSpy = sinon.spy();
  250. const watchdog2ErrorSpy = sinon.spy();
  251. watchdog1.on( 'restart', watchdog1ErrorSpy );
  252. watchdog2.on( 'restart', watchdog2ErrorSpy );
  253. setTimeout( () => throwCKEditorError( 'foo', watchdog2.editor ) );
  254. setTimeout( () => {
  255. window.onerror = originalErrorHandler;
  256. sinon.assert.notCalled( watchdog1ErrorSpy );
  257. sinon.assert.calledOnce( watchdog2ErrorSpy );
  258. Promise.all( [ watchdog1.destroy(), watchdog2.destroy() ] )
  259. .then( res );
  260. } );
  261. } );
  262. } );
  263. } );
  264. it( 'Watchdog should intercept editor errors and restart the editor if the editor can be found from the context', () => {
  265. const watchdog = new Watchdog();
  266. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  267. watchdog.setDestructor( editor => editor.destroy() );
  268. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  269. const originalErrorHandler = window.onerror;
  270. window.onerror = undefined;
  271. return watchdog.create( element ).then( () => {
  272. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor.model.document ) );
  273. return new Promise( res => {
  274. watchdog.on( 'restart', () => {
  275. window.onerror = originalErrorHandler;
  276. watchdog.destroy().then( res );
  277. } );
  278. } );
  279. } );
  280. } );
  281. it( 'Watchdog should intercept editor errors and restart the editor if the editor can be found from the context #2', () => {
  282. const watchdog = new Watchdog();
  283. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  284. watchdog.setDestructor( editor => editor.destroy() );
  285. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  286. const originalErrorHandler = window.onerror;
  287. window.onerror = undefined;
  288. return watchdog.create( element ).then( () => {
  289. setTimeout( () => throwCKEditorError( 'foo', {
  290. foo: [ 1, 2, 3, {
  291. bar: new Set( [
  292. new Map( [
  293. [ 'foo', 'bar' ],
  294. [ 0, watchdog.editor ]
  295. ] )
  296. ] )
  297. } ]
  298. } ) );
  299. return new Promise( res => {
  300. watchdog.on( 'restart', () => {
  301. window.onerror = originalErrorHandler;
  302. watchdog.destroy().then( res );
  303. } );
  304. } );
  305. } );
  306. } );
  307. it( 'editor should be restarted up to 3 times by default', () => {
  308. const watchdog = new Watchdog();
  309. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  310. watchdog.setDestructor( editor => editor.destroy() );
  311. const errorSpy = sinon.spy();
  312. watchdog.on( 'error', errorSpy );
  313. const restartSpy = sinon.spy();
  314. watchdog.on( 'restart', restartSpy );
  315. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  316. const originalErrorHandler = window.onerror;
  317. window.onerror = undefined;
  318. return watchdog.create( element ).then( () => {
  319. setTimeout( () => throwCKEditorError( 'foo1', watchdog.editor ) );
  320. setTimeout( () => throwCKEditorError( 'foo2', watchdog.editor ) );
  321. setTimeout( () => throwCKEditorError( 'foo3', watchdog.editor ) );
  322. setTimeout( () => throwCKEditorError( 'foo4', watchdog.editor ) );
  323. return new Promise( res => {
  324. setTimeout( () => {
  325. expect( errorSpy.callCount ).to.equal( 4 );
  326. expect( watchdog.crashes.length ).to.equal( 4 );
  327. expect( restartSpy.callCount ).to.equal( 3 );
  328. window.onerror = originalErrorHandler;
  329. watchdog.destroy().then( res );
  330. } );
  331. } );
  332. } );
  333. } );
  334. it( 'editor should be restarted up to `crashNumberLimit` times if the option is set', () => {
  335. const watchdog = new Watchdog( { crashNumberLimit: 2 } );
  336. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  337. watchdog.setDestructor( editor => editor.destroy() );
  338. const errorSpy = sinon.spy();
  339. watchdog.on( 'error', errorSpy );
  340. const restartSpy = sinon.spy();
  341. watchdog.on( 'restart', restartSpy );
  342. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  343. const originalErrorHandler = window.onerror;
  344. window.onerror = undefined;
  345. return watchdog.create( element ).then( () => {
  346. setTimeout( () => throwCKEditorError( 'foo1', watchdog.editor ) );
  347. setTimeout( () => throwCKEditorError( 'foo2', watchdog.editor ) );
  348. setTimeout( () => throwCKEditorError( 'foo3', watchdog.editor ) );
  349. setTimeout( () => throwCKEditorError( 'foo4', watchdog.editor ) );
  350. return new Promise( res => {
  351. setTimeout( () => {
  352. expect( errorSpy.callCount ).to.equal( 4 );
  353. expect( watchdog.crashes.length ).to.equal( 4 );
  354. expect( restartSpy.callCount ).to.equal( 2 );
  355. window.onerror = originalErrorHandler;
  356. watchdog.destroy().then( res );
  357. } );
  358. } );
  359. } );
  360. } );
  361. it( 'Watchdog should warn if the CKEditorError missing its context', () => {
  362. const watchdog = new Watchdog();
  363. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  364. watchdog.setDestructor( editor => editor.destroy() );
  365. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  366. const originalErrorHandler = window.onerror;
  367. window.onerror = undefined;
  368. sinon.stub( console, 'error' );
  369. return watchdog.create( element ).then( () => {
  370. setTimeout( () => throwCKEditorError( 'foo' ) );
  371. return new Promise( res => {
  372. setTimeout( () => {
  373. window.onerror = originalErrorHandler;
  374. expect( watchdog.crashes ).to.deep.equal( [] );
  375. sinon.assert.calledWithExactly(
  376. console.error,
  377. 'The error is missing its context and Watchdog cannot restart the proper editor.'
  378. );
  379. watchdog.destroy().then( res );
  380. } );
  381. } );
  382. } );
  383. } );
  384. it( 'Watchdog should omit error if the CKEditorError context is equal to null', () => {
  385. const watchdog = new Watchdog();
  386. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  387. watchdog.setDestructor( editor => editor.destroy() );
  388. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  389. const originalErrorHandler = window.onerror;
  390. window.onerror = undefined;
  391. sinon.stub( console, 'error' );
  392. return watchdog.create( element ).then( () => {
  393. setTimeout( () => throwCKEditorError( 'foo', null ) );
  394. return new Promise( res => {
  395. setTimeout( () => {
  396. window.onerror = originalErrorHandler;
  397. expect( watchdog.crashes ).to.deep.equal( [] );
  398. sinon.assert.notCalled( console.error );
  399. watchdog.destroy().then( res );
  400. } );
  401. } );
  402. } );
  403. } );
  404. it( 'editor should be restarted with the data before the crash #1', () => {
  405. const watchdog = new Watchdog();
  406. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  407. watchdog.setDestructor( editor => editor.destroy() );
  408. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  409. const originalErrorHandler = window.onerror;
  410. window.onerror = undefined;
  411. return watchdog.create( element, {
  412. initialData: '<p>foo</p>',
  413. plugins: [ Paragraph ]
  414. } ).then( () => {
  415. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  416. return new Promise( res => {
  417. watchdog.on( 'restart', () => {
  418. window.onerror = originalErrorHandler;
  419. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  420. watchdog.destroy().then( res );
  421. } );
  422. } );
  423. } );
  424. } );
  425. it( 'editor should be restarted with the data before the crash #2', () => {
  426. const watchdog = new Watchdog();
  427. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  428. watchdog.setDestructor( editor => editor.destroy() );
  429. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  430. const originalErrorHandler = window.onerror;
  431. window.onerror = undefined;
  432. return watchdog.create( element, {
  433. initialData: '<p>foo</p>',
  434. plugins: [ Paragraph ]
  435. } ).then( () => {
  436. const doc = watchdog.editor.model.document;
  437. watchdog.editor.model.change( writer => {
  438. writer.insertText( 'bar', writer.createPositionAt( doc.getRoot(), 1 ) );
  439. } );
  440. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  441. return new Promise( res => {
  442. watchdog.on( 'restart', () => {
  443. window.onerror = originalErrorHandler;
  444. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p><p>bar</p>' );
  445. watchdog.destroy().then( res );
  446. } );
  447. } );
  448. } );
  449. } );
  450. it( 'editor should be restarted with the data of the latest document version before the crash', () => {
  451. const watchdog = new Watchdog();
  452. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  453. watchdog.setDestructor( editor => editor.destroy() );
  454. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  455. const originalErrorHandler = window.onerror;
  456. window.onerror = undefined;
  457. return watchdog.create( element, {
  458. initialData: '<p>foo</p>',
  459. plugins: [ Paragraph ]
  460. } ).then( () => {
  461. const model = watchdog.editor.model;
  462. const doc = model.document;
  463. // Decrement the document version to simulate a situation when an operation
  464. // don't produce new document version.
  465. doc.version--;
  466. model.change( writer => {
  467. writer.insertText( 'bar', writer.createPositionAt( doc.getRoot(), 1 ) );
  468. } );
  469. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  470. return new Promise( res => {
  471. watchdog.on( 'restart', () => {
  472. window.onerror = originalErrorHandler;
  473. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  474. watchdog.destroy().then( res );
  475. } );
  476. } );
  477. } );
  478. } );
  479. it( 'editor should be restarted with the latest available data before the crash', () => {
  480. const watchdog = new Watchdog();
  481. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  482. watchdog.setDestructor( editor => editor.destroy() );
  483. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  484. const originalErrorHandler = window.onerror;
  485. window.onerror = undefined;
  486. sinon.stub( console, 'error' );
  487. return watchdog.create( element, {
  488. initialData: '<p>foo</p>',
  489. plugins: [ Paragraph ]
  490. } ).then( () => {
  491. const editorGetDataError = new Error( 'Some error' );
  492. const getDataStub = sinon.stub( watchdog.editor.data, 'get' )
  493. .throwsException( editorGetDataError );
  494. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  495. return new Promise( res => {
  496. const doc = watchdog.editor.model.document;
  497. watchdog.editor.model.change( writer => {
  498. writer.insertText( 'bar', writer.createPositionAt( doc.getRoot(), 1 ) );
  499. } );
  500. watchdog.on( 'restart', () => {
  501. window.onerror = originalErrorHandler;
  502. // It is called second time by during the default editor destruction
  503. // to update the source element.
  504. sinon.assert.calledTwice( getDataStub );
  505. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  506. sinon.assert.calledWith(
  507. console.error,
  508. editorGetDataError,
  509. 'An error happened during restoring editor data. Editor will be restored from the previously saved data.'
  510. );
  511. sinon.assert.calledWith(
  512. console.error,
  513. 'An error happened during the editor destructing.'
  514. );
  515. watchdog.destroy().then( res );
  516. } );
  517. } );
  518. } );
  519. } );
  520. } );
  521. describe( 'static for()', () => {
  522. it( 'should be a shortcut method for creating the watchdog', () => {
  523. const watchdog = Watchdog.for( ClassicTestEditor );
  524. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  525. const originalErrorHandler = window.onerror;
  526. window.onerror = undefined;
  527. return watchdog.create( element, {
  528. initialData: '<p>foo</p>',
  529. plugins: [ Paragraph ]
  530. } ).then( () => {
  531. const oldEditor = watchdog.editor;
  532. expect( watchdog.editor ).to.be.an.instanceOf( ClassicTestEditor );
  533. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  534. return new Promise( res => {
  535. watchdog.on( 'restart', () => {
  536. window.onerror = originalErrorHandler;
  537. expect( watchdog.editor ).to.be.an.instanceOf( ClassicTestEditor );
  538. expect( watchdog.editor ).to.not.equal( oldEditor );
  539. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  540. watchdog.destroy().then( res );
  541. } );
  542. } );
  543. } );
  544. } );
  545. } );
  546. describe( 'crashes', () => {
  547. it( 'should be an array of caught errors by the watchdog', () => {
  548. const watchdog = new Watchdog();
  549. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  550. watchdog.setDestructor( editor => editor.destroy() );
  551. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  552. const originalErrorHandler = window.onerror;
  553. window.onerror = undefined;
  554. return watchdog.create( element ).then( () => {
  555. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  556. setTimeout( () => throwCKEditorError( 'bar', watchdog.editor ) );
  557. return new Promise( res => {
  558. setTimeout( () => {
  559. window.onerror = originalErrorHandler;
  560. expect( watchdog.crashes[ 0 ].message ).to.equal( 'foo' );
  561. expect( watchdog.crashes[ 1 ].message ).to.equal( 'bar' );
  562. watchdog.destroy().then( res );
  563. } );
  564. } );
  565. } );
  566. } );
  567. } );
  568. } );
  569. function throwCKEditorError( name, context ) {
  570. throw new CKEditorError( name, context );
  571. }