8
0

mentionediting.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * @license Copyright (c) 2003-2019, 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 MentionEditing from '../src/mentionediting';
  7. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  8. import { getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. describe( 'MentionEditing', () => {
  11. testUtils.createSinonSandbox();
  12. it( 'should be named', () => {
  13. expect( MentionEditing.pluginName ).to.equal( 'MentionEditing' );
  14. } );
  15. describe( 'init()', () => {
  16. let editor, model, doc;
  17. it( 'should be loaded', () => {
  18. return createTestEditor()
  19. .then( newEditor => {
  20. expect( newEditor.plugins.get( MentionEditing ) ).to.be.instanceOf( MentionEditing );
  21. } );
  22. } );
  23. it( 'should set proper schema rules', () => {
  24. return createTestEditor()
  25. .then( newEditor => {
  26. model = newEditor.model;
  27. expect( model.schema.checkAttribute( [ '$root', '$text' ], 'mention' ) ).to.be.true;
  28. expect( model.schema.checkAttribute( [ '$block', '$text' ], 'mention' ) ).to.be.true;
  29. expect( model.schema.checkAttribute( [ '$clipboardHolder', '$text' ], 'mention' ) ).to.be.true;
  30. expect( model.schema.checkAttribute( [ '$block' ], 'mention' ) ).to.be.false;
  31. } );
  32. } );
  33. describe( 'conversion in the data pipeline', () => {
  34. beforeEach( () => {
  35. return createTestEditor()
  36. .then( newEditor => {
  37. editor = newEditor;
  38. model = editor.model;
  39. doc = model.document;
  40. } );
  41. } );
  42. it( 'should convert <span class="mention" data-mention="John"> to mention attribute', () => {
  43. editor.setData( '<p>foo <span class="mention" data-mention="John">John</span> bar</p>' );
  44. expect( getModelData( model, { withoutSelection: true } ) )
  45. .to.equal( '<paragraph>foo <$text mention="John">John</$text> bar</paragraph>' );
  46. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="John">John</span> bar</p>' );
  47. } );
  48. it( 'should remove mention on adding a text inside mention', () => {
  49. editor.setData( '<p>foo <span class="mention" data-mention="John">John</span> bar</p>' );
  50. model.change( writer => {
  51. const paragraph = doc.getRoot().getChild( 0 );
  52. writer.setSelection( paragraph, 5 );
  53. writer.insertText( 'a', doc.selection.getAttributes(), writer.createPositionAt( paragraph, 6 ) );
  54. } );
  55. expect( editor.getData() ).to.equal( '<p>foo Joahn bar</p>' );
  56. } );
  57. it( 'should remove mention on removing a text inside mention', () => {
  58. editor.setData( '<p>foo <span class="mention" data-mention="John">John</span> bar</p>' );
  59. model.change( writer => {
  60. const paragraph = doc.getRoot().getChild( 0 );
  61. writer.setSelection( paragraph, 5 );
  62. writer.remove( writer.createRange( writer.createPositionAt( paragraph, 5 ), writer.createPositionAt( paragraph, 6 ) ) );
  63. } );
  64. expect( editor.getData() ).to.equal( '<p>foo Jhn bar</p>' );
  65. } );
  66. } );
  67. } );
  68. function createTestEditor( mentionConfig ) {
  69. return VirtualTestEditor
  70. .create( {
  71. plugins: [ Paragraph, MentionEditing ],
  72. mention: mentionConfig
  73. } );
  74. }
  75. } );