mention.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  6. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  7. import Element from '@ckeditor/ckeditor5-engine/src/view/element';
  8. import Text from '@ckeditor/ckeditor5-engine/src/view/text';
  9. import Mention from '../src/mention';
  10. import MentionEditing from '../src/mentionediting';
  11. import MentionUI from '../src/mentionui';
  12. describe( 'Mention', () => {
  13. let editorElement, editor, viewDocument;
  14. beforeEach( () => {
  15. editorElement = global.document.createElement( 'div' );
  16. global.document.body.appendChild( editorElement );
  17. return ClassicTestEditor
  18. .create( editorElement, {
  19. plugins: [ Mention ]
  20. } )
  21. .then( newEditor => {
  22. editor = newEditor;
  23. viewDocument = editor.editing.view.document;
  24. } );
  25. } );
  26. afterEach( () => {
  27. editorElement.remove();
  28. return editor.destroy();
  29. } );
  30. it( 'should be loaded', () => {
  31. expect( editor.plugins.get( Mention ) ).to.instanceOf( Mention );
  32. } );
  33. it( 'has proper name', () => {
  34. expect( Mention.pluginName ).to.equal( 'Mention' );
  35. } );
  36. it( 'should load MentionEditing plugin', () => {
  37. expect( editor.plugins.get( MentionEditing ) ).to.instanceOf( MentionEditing );
  38. } );
  39. it( 'should load MentionUI plugin', () => {
  40. expect( editor.plugins.get( MentionUI ) ).to.instanceOf( MentionUI );
  41. } );
  42. describe( 'toMentionAttribute()', () => {
  43. it( 'should create mention attribute with default properties', () => {
  44. const text = new Text( viewDocument, 'John Doe' );
  45. const viewElement = new Element( viewDocument, 'span', {
  46. 'data-mention': '@John'
  47. }, text );
  48. const mentionAttribute = editor.plugins.get( 'Mention' ).toMentionAttribute( viewElement );
  49. expect( mentionAttribute ).to.have.property( 'id', '@John' );
  50. expect( mentionAttribute ).to.have.property( 'uid' );
  51. expect( mentionAttribute ).to.have.property( '_text', 'John Doe' );
  52. } );
  53. it( 'should create mention attribute with provided attributes', () => {
  54. const text = new Text( viewDocument, 'John Doe' );
  55. const viewElement = new Element( viewDocument, 'span', {
  56. 'data-mention': '@John'
  57. }, text );
  58. const mentionAttribute = editor.plugins.get( 'Mention' ).toMentionAttribute( viewElement, { foo: 'bar' } );
  59. expect( mentionAttribute ).to.have.property( 'id', '@John' );
  60. expect( mentionAttribute ).to.have.property( 'foo', 'bar' );
  61. expect( mentionAttribute ).to.have.property( 'uid' );
  62. expect( mentionAttribute ).to.have.property( '_text', 'John Doe' );
  63. } );
  64. it( 'should return undefined if Element has no text node', () => {
  65. const viewElement = new Element( viewDocument, 'span', {
  66. 'data-mention': '@John'
  67. } );
  68. const mentionAttribute = editor.plugins.get( 'Mention' ).toMentionAttribute( viewElement );
  69. expect( mentionAttribute ).to.be.undefined;
  70. } );
  71. } );
  72. } );