8
0

creator.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. /* globals document */
  7. /* bender-include: ../_tools/tools.js */
  8. var modules = bender.amd.require( 'editor', 'plugin', 'promise', 'creator', 'ckeditorerror' );
  9. var editor, element;
  10. function initEditor( config ) {
  11. var Editor = modules.editor;
  12. element = document.createElement( 'div' );
  13. document.body.appendChild( element );
  14. editor = new Editor( element, config );
  15. return editor.init();
  16. }
  17. bender.tools.createSinonSandbox();
  18. before( function() {
  19. bender.tools.core.defineEditorCreatorMock( 'test1' );
  20. bender.tools.core.defineEditorCreatorMock( 'test-any1' );
  21. bender.tools.core.defineEditorCreatorMock( 'test-any2' );
  22. bender.tools.core.defineEditorCreatorMock( 'test-config1' );
  23. bender.tools.core.defineEditorCreatorMock( 'test-config2' );
  24. CKEDITOR.define( 'plugin!test3', [ 'plugin' ], function( Plugin ) {
  25. return class extends Plugin {};
  26. } );
  27. } );
  28. afterEach( function() {
  29. editor = null; // To make sure we're using the freshly inited editor.
  30. } );
  31. ///////////////////
  32. describe( 'init', function() {
  33. it( 'should instantiate the creator and call create()', function() {
  34. var Creator = modules.creator;
  35. return initEditor( {
  36. plugins: 'creator-test1'
  37. } )
  38. .then( function() {
  39. var creator = editor.plugins.get( 'creator-test1' );
  40. expect( creator ).to.be.instanceof( Creator );
  41. // The create method has been called.
  42. sinon.assert.calledOnce( creator.create );
  43. } );
  44. } );
  45. it( 'should instantiate any creator when more than one is available', function() {
  46. return initEditor( {
  47. plugins: 'creator-test-any1,creator-test-any2'
  48. } )
  49. .then( function() {
  50. var creator1 = editor.plugins.get( 'creator-test-any1' );
  51. var creator2 = editor.plugins.get( 'creator-test-any2' );
  52. expect( creator1.create.called + creator2.create.called ).to.be.equal( 1, 'only one of the creators should be used' );
  53. } );
  54. } );
  55. it( 'should use the creator specified in config.creator', function() {
  56. return initEditor( {
  57. creator: 'test-config2',
  58. plugins: 'creator-test-config1,creator-test-config2',
  59. } )
  60. .then( function() {
  61. var creator1 = editor.plugins.get( 'creator-test-config1' );
  62. var creator2 = editor.plugins.get( 'creator-test-config2' );
  63. sinon.assert.calledOnce( creator2.create );
  64. sinon.assert.notCalled( creator1.create );
  65. } );
  66. } );
  67. it( 'should throw an error if the creator doesn\'t exist', function() {
  68. var CKEditorError = modules.ckeditorerror;
  69. return initEditor( {
  70. creator: 'bad',
  71. plugins: 'creator-test1'
  72. } )
  73. .then( function() {
  74. throw new Error( 'This should not be executed.' );
  75. } )
  76. .catch( function( err ) {
  77. expect( err ).to.be.instanceof( CKEditorError );
  78. expect( err.message ).to.match( /^editor-creator-404:/ );
  79. } );
  80. } );
  81. it( 'should throw an error no creators are defined', function() {
  82. var CKEditorError = modules.ckeditorerror;
  83. return initEditor( {} )
  84. .then( function() {
  85. throw new Error( 'This should not be executed.' );
  86. } )
  87. .catch( function( err ) {
  88. expect( err ).to.be.instanceof( CKEditorError );
  89. expect( err.message ).to.match( /^editor-creator-404:/ );
  90. } );
  91. } );
  92. } );
  93. describe( 'destroy', function() {
  94. it( 'should call "destroy" on the creator', function() {
  95. var creator1;
  96. return initEditor( {
  97. plugins: 'creator-test1'
  98. } )
  99. .then( function() {
  100. creator1 = editor.plugins.get( 'creator-test1' );
  101. return editor.destroy();
  102. } )
  103. .then( function() {
  104. sinon.assert.calledOnce( creator1.destroy );
  105. } );
  106. } );
  107. } );