plugincollection.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals describe, it, expect, before, document */
  6. 'use strict';
  7. var modules = bender.amd.require( 'plugincollection', 'plugin', 'editor' );
  8. var editor;
  9. var PluginA, PluginB, PluginC;
  10. before( function() {
  11. var Editor = modules.editor;
  12. var Plugin = modules.plugin;
  13. PluginA = Plugin.extend();
  14. PluginB = Plugin.extend();
  15. PluginC = Plugin.extend();
  16. editor = new Editor( document.body.appendChild( document.createElement( 'div' ) ) );
  17. } );
  18. // Create 3 fake plugins that will be used on tests.
  19. CKEDITOR.define( 'plugin!A', [ 'plugin' ], function() {
  20. return PluginA;
  21. } );
  22. CKEDITOR.define( 'plugin!B', [ 'plugin' ], function() {
  23. return PluginB;
  24. } );
  25. CKEDITOR.define( 'plugin!C', [ 'plugin' ], function() {
  26. return PluginC;
  27. } );
  28. /////////////
  29. describe( 'load', function() {
  30. it( 'should add collection items for loaded plugins', function() {
  31. var PluginCollection = modules.plugincollection;
  32. var plugins = new PluginCollection( editor );
  33. return plugins.load( 'A,B' )
  34. .then( function() {
  35. expect( plugins.length ).to.equal( 2 );
  36. expect( plugins.get( 0 ) ).to.be.an.instanceof( PluginA );
  37. expect( plugins.get( 1 ) ).to.be.an.instanceof( PluginB );
  38. } );
  39. } );
  40. it( 'should set the `editor` property on loaded plugins', function() {
  41. var PluginCollection = modules.plugincollection;
  42. var plugins = new PluginCollection( editor );
  43. return plugins.load( 'A,B' )
  44. .then( function() {
  45. expect( plugins.get( 0 ).editor ).to.equal( editor );
  46. expect( plugins.get( 1 ).editor ).to.equal( editor );
  47. } );
  48. } );
  49. it( 'should throw error for invalid plugins', function() {
  50. var PluginCollection = modules.plugincollection;
  51. var plugins = new PluginCollection( editor );
  52. return plugins.load( 'A,BAD,B' )
  53. .then( function() {
  54. throw new Error( 'Test error: this promise should not be resolved successfully' );
  55. } )
  56. .catch( function( err ) {
  57. expect( err ).to.be.an.instanceof( Error );
  58. expect( err.name ).to.equal( 'CKEditor Error' );
  59. } );
  60. } );
  61. } );
  62. describe( 'add', function() {
  63. it( 'should add plugins to the collection', function() {
  64. var PluginCollection = modules.plugincollection;
  65. var plugins = new PluginCollection( editor );
  66. var pluginA = new PluginA();
  67. var pluginB = new PluginB();
  68. // `add()` requires the `name` property to the defined.
  69. pluginA.name = 'A';
  70. pluginB.name = 'B';
  71. plugins.add( pluginA );
  72. plugins.add( pluginB );
  73. expect( plugins.length ).to.equal( 2 );
  74. expect( plugins.get( 0 ) ).to.be.an.instanceof( PluginA );
  75. expect( plugins.get( 1 ) ).to.be.an.instanceof( PluginB );
  76. } );
  77. it( 'should do nothing if the plugin is already loaded', function() {
  78. var PluginCollection = modules.plugincollection;
  79. var plugins = new PluginCollection( editor );
  80. return plugins.load( 'A,B' )
  81. .then( function() {
  82. // Check length before `add()`.
  83. expect( plugins.length ).to.equal( 2 );
  84. var pluginA = new PluginA();
  85. pluginA.name = 'A';
  86. plugins.add( pluginA );
  87. // Length should not change after `add()`.
  88. expect( plugins.length ).to.equal( 2 );
  89. } );
  90. } );
  91. } );
  92. describe( 'get', function() {
  93. it( 'should get plugin by name', function() {
  94. var PluginCollection = modules.plugincollection;
  95. var plugins = new PluginCollection( editor );
  96. return plugins.load( 'A,B' )
  97. .then( function() {
  98. expect( plugins.get( 'A' ) ).to.be.an.instanceof( PluginA );
  99. expect( plugins.get( 'B' ) ).to.be.an.instanceof( PluginB );
  100. } );
  101. } );
  102. it( 'should return undefined for non existing plugin', function() {
  103. var PluginCollection = modules.plugincollection;
  104. var plugins = new PluginCollection( editor );
  105. return plugins.load( 'A,B' )
  106. .then( function() {
  107. expect( plugins.get( 'C' ) ).to.be.an( 'undefined' );
  108. } );
  109. } );
  110. } );