conversion.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Conversion from '../../src/conversion/conversion';
  6. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  7. describe( 'Conversion', () => {
  8. let conversion, dispA, dispB;
  9. beforeEach( () => {
  10. conversion = new Conversion();
  11. // Placeholders. Will be used only to see if their were given as attribute for a spy function.
  12. dispA = Symbol( 'dispA' );
  13. dispB = Symbol( 'dispB' );
  14. conversion.register( 'ab', [ dispA, dispB ] );
  15. conversion.register( 'a', [ dispA ] );
  16. conversion.register( 'b', [ dispB ] );
  17. } );
  18. describe( 'register()', () => {
  19. it( 'should throw when trying to use same group name twice', () => {
  20. expect( () => {
  21. conversion.register( 'ab' );
  22. } ).to.throw( CKEditorError, /conversion-register-group-exists/ );
  23. } );
  24. } );
  25. describe( 'for()', () => {
  26. it( 'should return object with .add() method', () => {
  27. const forResult = conversion.for( 'ab' );
  28. expect( forResult.add ).to.be.instanceof( Function );
  29. } );
  30. it( 'should throw if non-existing group name has been used', () => {
  31. expect( () => {
  32. conversion.for( 'foo' );
  33. } ).to.throw( CKEditorError, /conversion-for-unknown-group/ );
  34. } );
  35. } );
  36. describe( 'add()', () => {
  37. let helperA, helperB;
  38. beforeEach( () => {
  39. helperA = sinon.stub();
  40. helperB = sinon.stub();
  41. } );
  42. it( 'should be chainable', () => {
  43. const forResult = conversion.for( 'ab' );
  44. const addResult = forResult.add( () => {} );
  45. expect( addResult ).to.equal( addResult.add( () => {} ) );
  46. } );
  47. it( 'should fire given helper for every dispatcher in given group', () => {
  48. conversion.for( 'ab' ).add( helperA );
  49. expect( helperA.calledWithExactly( dispA ) ).to.be.true;
  50. expect( helperA.calledWithExactly( dispB ) ).to.be.true;
  51. conversion.for( 'b' ).add( helperB );
  52. expect( helperB.calledWithExactly( dispA ) ).to.be.false;
  53. expect( helperB.calledWithExactly( dispB ) ).to.be.true;
  54. } );
  55. } );
  56. } );