texttransformation-integration.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global document */
  6. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  7. import UndoEditing from '@ckeditor/ckeditor5-undo/src/undoediting';
  8. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  9. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  10. import TextTransformation from '../src/texttransformation';
  11. import Typing from '../src/typing';
  12. describe( 'Text transformation feature - integration', () => {
  13. let editorElement, editor, model, doc;
  14. testUtils.createSinonSandbox();
  15. beforeEach( () => {
  16. editorElement = document.createElement( 'div' );
  17. document.body.appendChild( editorElement );
  18. } );
  19. afterEach( () => {
  20. editorElement.remove();
  21. return editor.destroy();
  22. } );
  23. describe( 'with undo', () => {
  24. beforeEach( () => {
  25. return ClassicTestEditor
  26. .create( editorElement, { plugins: [ Typing, Paragraph, TextTransformation, UndoEditing ] } )
  27. .then( newEditor => {
  28. editor = newEditor;
  29. model = editor.model;
  30. doc = model.document;
  31. } );
  32. } );
  33. it( 'should undo text transformation', () => {
  34. editor.setData( '<p>foo</p>' );
  35. model.enqueueChange( model.createBatch(), writer => {
  36. writer.setSelection( doc.getRoot().getChild( 0 ), 'end' );
  37. writer.insertText( '(c', doc.selection.focus );
  38. } );
  39. model.enqueueChange( model.createBatch(), writer => {
  40. writer.setSelection( doc.getRoot().getChild( 0 ), 'end' );
  41. writer.insertText( ')', doc.selection.focus );
  42. } );
  43. expect( editor.getData(), 'inserted text' ).to.equal( '<p>foo©</p>' );
  44. editor.execute( 'undo' );
  45. expect( editor.getData(), 'after undo' ).to.equal( '<p>foo(c)</p>' );
  46. editor.execute( 'redo' );
  47. expect( editor.getData(), 'after redo' ).to.equal( '<p>foo©</p>' );
  48. } );
  49. it( 'should allow to undo-redo steps', () => {
  50. editor.setData( '<p></p>' );
  51. model.enqueueChange( model.createBatch(), writer => {
  52. writer.setSelection( doc.getRoot().getChild( 0 ), 'end' );
  53. writer.insertText( 'foo bar baz(c', doc.selection.focus );
  54. } );
  55. model.enqueueChange( model.createBatch(), writer => {
  56. writer.setSelection( doc.getRoot().getChild( 0 ), 'end' );
  57. writer.insertText( ')', doc.selection.focus );
  58. } );
  59. expect( editor.getData() ).to.equal( '<p>foo bar baz©</p>' );
  60. editor.execute( 'undo' );
  61. expect( editor.getData() ).to.equal( '<p>foo bar baz(c)</p>' );
  62. editor.execute( 'undo' );
  63. expect( editor.getData() ).to.equal( '<p>foo bar baz(c</p>' );
  64. editor.execute( 'redo' );
  65. expect( editor.getData() ).to.equal( '<p>foo bar baz(c)</p>' );
  66. editor.execute( 'redo' );
  67. expect( editor.getData() ).to.equal( '<p>foo bar baz©</p>' );
  68. } );
  69. } );
  70. } );