8
0

context.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 Context from '../src/context';
  6. import Plugin from '../src/plugin';
  7. import VirtualTestEditor from './_utils/virtualtesteditor';
  8. describe( 'Context', () => {
  9. it( 'should share the same instance of plugin within editors using the same context', async () => {
  10. class ContextPluginA extends Plugin {}
  11. class ContextPluginB extends Plugin {}
  12. class EditorPluginA extends Plugin {}
  13. const context = await Context.create( { plugins: [ ContextPluginA, ContextPluginB ] } );
  14. const editorA = await VirtualTestEditor.create( { context, plugins: [ ContextPluginA, EditorPluginA ] } );
  15. const editorB = await VirtualTestEditor.create( { context, plugins: [ ContextPluginB, EditorPluginA ] } );
  16. expect( editorA.plugins.get( ContextPluginA ) ).to.equal( context.plugins.get( ContextPluginA ) );
  17. expect( editorA.plugins.has( ContextPluginB ) ).to.equal( false );
  18. expect( editorB.plugins.get( ContextPluginB ) ).to.equal( context.plugins.get( ContextPluginB ) );
  19. expect( editorB.plugins.has( ContextPluginA ) ).to.equal( false );
  20. expect( context.plugins.has( EditorPluginA ) ).to.equal( false );
  21. expect( editorA.plugins.get( EditorPluginA ) ).to.not.equal( editorB.plugins.get( EditorPluginA ) );
  22. await context.destroy();
  23. } );
  24. it( 'should share the same instance of plugin (dependencies) within editors using the same context', async () => {
  25. class ContextPluginA extends Plugin {}
  26. class ContextPluginB extends Plugin {}
  27. class EditorPluginA extends Plugin {
  28. static get requires() {
  29. return [ ContextPluginA ];
  30. }
  31. }
  32. class EditorPluginB extends Plugin {
  33. static get requires() {
  34. return [ ContextPluginB ];
  35. }
  36. }
  37. const context = await Context.create( { plugins: [ ContextPluginA, ContextPluginB ] } );
  38. const editorA = await VirtualTestEditor.create( { context, plugins: [ EditorPluginA ] } );
  39. const editorB = await VirtualTestEditor.create( { context, plugins: [ EditorPluginB ] } );
  40. expect( context.plugins.get( ContextPluginA ) ).to.equal( editorA.plugins.get( ContextPluginA ) );
  41. expect( context.plugins.get( ContextPluginB ) ).to.equal( editorB.plugins.get( ContextPluginB ) );
  42. await context.destroy();
  43. } );
  44. it( 'should not initialize twice plugin added to the context and the editor', async () => {
  45. const initSpy = sinon.spy();
  46. const afterInitSpy = sinon.spy();
  47. class ContextPluginA extends Plugin {
  48. init() {
  49. initSpy();
  50. }
  51. afterInit() {
  52. afterInitSpy();
  53. }
  54. }
  55. const context = await Context.create( { plugins: [ ContextPluginA ] } );
  56. const editor = await VirtualTestEditor.create( { context, plugins: [ ContextPluginA ] } );
  57. expect( context.plugins.get( ContextPluginA ) ).to.equal( editor.plugins.get( ContextPluginA ) );
  58. sinon.assert.calledOnce( initSpy );
  59. sinon.assert.calledOnce( afterInitSpy );
  60. await context.destroy();
  61. } );
  62. it( 'should not destroy context along with the editor when context is injected to the editor', async () => {
  63. const contextPluginDestroySpy = sinon.spy();
  64. const editorPluginDestroySpy = sinon.spy();
  65. class ContextPlugin extends Plugin {
  66. destroy() {
  67. contextPluginDestroySpy();
  68. }
  69. }
  70. class EditorPlugin extends Plugin {
  71. destroy() {
  72. editorPluginDestroySpy();
  73. }
  74. }
  75. const context = await Context.create( { plugins: [ ContextPlugin ] } );
  76. const editor = await VirtualTestEditor.create( { context, plugins: [ ContextPlugin, EditorPlugin ] } );
  77. await editor.destroy();
  78. sinon.assert.calledOnce( editorPluginDestroySpy );
  79. sinon.assert.notCalled( contextPluginDestroySpy );
  80. } );
  81. it( 'should destroy all editors with injected context when context is destroyed', async () => {
  82. const context = await Context.create();
  83. const editorA = await VirtualTestEditor.create( { context } );
  84. const editorB = await VirtualTestEditor.create( { context } );
  85. const editorC = await VirtualTestEditor.create();
  86. sinon.spy( editorA, 'destroy' );
  87. sinon.spy( editorB, 'destroy' );
  88. sinon.spy( editorC, 'destroy' );
  89. await context.destroy();
  90. sinon.assert.calledOnce( editorA.destroy );
  91. sinon.assert.calledOnce( editorB.destroy );
  92. sinon.assert.notCalled( editorC.destroy );
  93. } );
  94. it( 'should be able to add context plugin to the editor using pluginName property', async () => {
  95. class ContextPluginA extends Plugin {
  96. static get pluginName() {
  97. return 'ContextPluginA';
  98. }
  99. }
  100. class ContextPluginB extends Plugin {
  101. static get pluginName() {
  102. return 'ContextPluginB';
  103. }
  104. static get requires() {
  105. return [ ContextPluginA ];
  106. }
  107. }
  108. const context = await Context.create( { plugins: [ ContextPluginB ] } );
  109. const editor = await VirtualTestEditor.create( { context, plugins: [ 'ContextPluginA' ] } );
  110. expect( editor.plugins.has( ContextPluginA ) ).to.equal( true );
  111. expect( editor.plugins.has( ContextPluginB ) ).to.equal( false );
  112. } );
  113. } );