bold.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import Bold from '../src/bold';
  8. import BoldEngine from '../src/boldengine';
  9. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  10. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  11. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  12. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  13. testUtils.createSinonSandbox();
  14. describe( 'Bold', () => {
  15. let editor, boldView;
  16. beforeEach( () => {
  17. const editorElement = document.createElement( 'div' );
  18. document.body.appendChild( editorElement );
  19. return ClassicTestEditor
  20. .create( editorElement, {
  21. plugins: [ Paragraph, Bold ]
  22. } )
  23. .then( newEditor => {
  24. editor = newEditor;
  25. boldView = editor.ui.componentFactory.create( 'bold' );
  26. } );
  27. } );
  28. afterEach( () => {
  29. return editor.destroy();
  30. } );
  31. it( 'should be loaded', () => {
  32. expect( editor.plugins.get( Bold ) ).to.be.instanceOf( Bold );
  33. } );
  34. it( 'should load BoldEngine', () => {
  35. expect( editor.plugins.get( BoldEngine ) ).to.be.instanceOf( BoldEngine );
  36. } );
  37. it( 'should register bold feature component', () => {
  38. expect( boldView ).to.be.instanceOf( ButtonView );
  39. expect( boldView.isOn ).to.be.false;
  40. expect( boldView.label ).to.equal( 'Bold' );
  41. expect( boldView.icon ).to.match( /<svg / );
  42. expect( boldView.keystroke ).to.equal( 'CTRL+B' );
  43. } );
  44. it( 'should execute bold command on model execute event', () => {
  45. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  46. boldView.fire( 'execute' );
  47. sinon.assert.calledOnce( executeSpy );
  48. sinon.assert.calledWithExactly( executeSpy, 'bold' );
  49. } );
  50. it( 'should bind model to bold command', () => {
  51. const command = editor.commands.get( 'bold' );
  52. expect( boldView.isOn ).to.be.false;
  53. expect( boldView.isEnabled ).to.be.true;
  54. command.value = true;
  55. expect( boldView.isOn ).to.be.true;
  56. command.isEnabled = false;
  57. expect( boldView.isEnabled ).to.be.false;
  58. } );
  59. it( 'should set keystroke in the model', () => {
  60. expect( boldView.keystroke ).to.equal( 'CTRL+B' );
  61. } );
  62. it( 'should set editor keystroke', () => {
  63. const spy = sinon.spy( editor, 'execute' );
  64. const keyEventData = {
  65. keyCode: keyCodes.b,
  66. ctrlKey: true,
  67. preventDefault: sinon.spy(),
  68. stopPropagation: sinon.spy()
  69. };
  70. const wasHandled = editor.keystrokes.press( keyEventData );
  71. expect( wasHandled ).to.be.true;
  72. expect( spy.calledOnce ).to.be.true;
  73. expect( keyEventData.preventDefault.calledOnce ).to.be.true;
  74. } );
  75. } );