enter.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  6. import Enter from '../src/enter';
  7. import EnterCommand from '../src/entercommand';
  8. import DomEventData from '@ckeditor/ckeditor5-engine/src/view/observer/domeventdata';
  9. describe( 'Enter feature', () => {
  10. let editor, viewDocument;
  11. beforeEach( () => {
  12. return VirtualTestEditor
  13. .create( {
  14. plugins: [ Enter ]
  15. } )
  16. .then( newEditor => {
  17. editor = newEditor;
  18. viewDocument = editor.editing.view.document;
  19. } );
  20. } );
  21. it( 'creates the commands', () => {
  22. expect( editor.commands.get( 'enter' ) ).to.be.instanceof( EnterCommand );
  23. } );
  24. it( 'listens to the editing view enter event', () => {
  25. const spy = editor.execute = sinon.spy();
  26. const domEvt = getDomEvent();
  27. viewDocument.fire( 'enter', new DomEventData( viewDocument, domEvt, { isSoft: false } ) );
  28. expect( spy.calledOnce ).to.be.true;
  29. expect( spy.calledWithExactly( 'enter' ) ).to.be.true;
  30. expect( domEvt.preventDefault.calledOnce ).to.be.true;
  31. } );
  32. it( 'scrolls the editing document to the selection after executing the command', () => {
  33. const domEvt = getDomEvent();
  34. const executeSpy = editor.execute = sinon.spy();
  35. const scrollSpy = sinon.stub( editor.editing.view, 'scrollToTheSelection' );
  36. viewDocument.fire( 'enter', new DomEventData( viewDocument, domEvt ) );
  37. sinon.assert.calledOnce( scrollSpy );
  38. sinon.assert.callOrder( executeSpy, scrollSpy );
  39. } );
  40. it( 'does not execute the command if soft enter should be used', () => {
  41. const domEvt = getDomEvent();
  42. const commandExecuteSpy = sinon.stub( editor.commands.get( 'enter' ), 'execute' );
  43. viewDocument.fire( 'enter', new DomEventData( viewDocument, domEvt, { isSoft: true } ) );
  44. sinon.assert.notCalled( commandExecuteSpy );
  45. } );
  46. function getDomEvent() {
  47. return {
  48. preventDefault: sinon.spy()
  49. };
  50. }
  51. } );