8
0

watchdog.js 21 KB

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