8
0

mentioncommand.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  7. import MentionCommand from '../src/mentioncommand';
  8. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  9. describe( 'MentionCommand', () => {
  10. let editor, command, model, doc, selection;
  11. beforeEach( () => {
  12. return ModelTestEditor
  13. .create()
  14. .then( newEditor => {
  15. editor = newEditor;
  16. model = editor.model;
  17. doc = model.document;
  18. selection = doc.selection;
  19. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  20. model.schema.register( 'x', { inheritAllFrom: '$block' } );
  21. model.schema.extend( '$text', { allowAttributes: [ 'mention' ] } );
  22. command = new MentionCommand( editor );
  23. } );
  24. } );
  25. afterEach( () => {
  26. command.destroy();
  27. return editor.destroy();
  28. } );
  29. describe( 'isEnabled', () => {
  30. it( 'should return true if characters with the attribute can be placed at caret position', () => {
  31. setData( model, '<paragraph>f[]oo</paragraph>' );
  32. expect( command.isEnabled ).to.be.true;
  33. } );
  34. it( 'should return false if characters with the attribute cannot be placed at caret position', () => {
  35. model.schema.addAttributeCheck( ( ctx, attributeName ) => {
  36. // Allow 'bold' on p>$text.
  37. if ( ctx.endsWith( 'x $text' ) && attributeName == 'mention' ) {
  38. return false;
  39. }
  40. } );
  41. setData( model, '<x>fo[]o</x>' );
  42. expect( command.isEnabled ).to.be.false;
  43. } );
  44. } );
  45. describe( 'execute()', () => {
  46. it( 'inserts mention object if mention was passed as string', () => {
  47. setData( model, '<paragraph>foo @Jo[]bar</paragraph>' );
  48. command.execute( {
  49. marker: '@',
  50. mention: '@John',
  51. range: model.createRange( selection.focus.getShiftedBy( -3 ), selection.focus )
  52. } );
  53. assertMention( doc.getRoot().getChild( 0 ).getChild( 1 ), '@John' );
  54. } );
  55. it( 'inserts mention object with data if mention was passed as object', () => {
  56. setData( model, '<paragraph>foo @Jo[]bar</paragraph>' );
  57. command.execute( {
  58. marker: '@',
  59. mention: { id: '@John', userId: '123456' },
  60. range: model.createRange( selection.focus.getShiftedBy( -3 ), selection.focus )
  61. } );
  62. const mentionNode = doc.getRoot().getChild( 0 ).getChild( 1 );
  63. assertMention( mentionNode, '@John' );
  64. expect( mentionNode.getAttribute( 'mention' ) ).to.have.property( 'userId', '123456' );
  65. } );
  66. it( 'inserts options.text as mention text', () => {
  67. setData( model, '<paragraph>foo @Jo[]bar</paragraph>' );
  68. command.execute( {
  69. marker: '@',
  70. mention: '@John',
  71. text: '@John Doe',
  72. range: model.createRange( selection.focus.getShiftedBy( -3 ), selection.focus )
  73. } );
  74. assertMention( doc.getRoot().getChild( 0 ).getChild( 1 ), '@John' );
  75. } );
  76. it( 'inserts mention attribute with passed marker for given range', () => {
  77. setData( model, '<paragraph>foo @Jo[]bar</paragraph>' );
  78. const end = model.createPositionAt( selection.focus );
  79. const start = end.getShiftedBy( -3 );
  80. command.execute( {
  81. marker: '#',
  82. mention: '#John',
  83. range: model.createRange( start, end )
  84. } );
  85. assertMention( doc.getRoot().getChild( 0 ).getChild( 1 ), '#John' );
  86. } );
  87. it( 'inserts mention attribute at current selection if no range was passed', () => {
  88. setData( model, '<paragraph>foo []bar</paragraph>' );
  89. command.execute( {
  90. marker: '@',
  91. mention: '@John'
  92. } );
  93. assertMention( doc.getRoot().getChild( 0 ).getChild( 1 ), '@John' );
  94. } );
  95. it( 'should set also other styles in inserted text', () => {
  96. model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
  97. setData( model, '<paragraph><$text bold="true">foo@John[]bar</$text></paragraph>' );
  98. command.execute( {
  99. marker: '@',
  100. mention: '@John',
  101. range: model.createRange( selection.focus.getShiftedBy( -5 ), selection.focus )
  102. } );
  103. const textNode = doc.getRoot().getChild( 0 ).getChild( 1 );
  104. assertMention( textNode, '@John' );
  105. expect( textNode.hasAttribute( 'bold' ) ).to.be.true;
  106. } );
  107. it( 'should throw if marker is not one character', () => {
  108. setData( model, '<paragraph>foo @Jo[]bar</paragraph>' );
  109. const testCases = [
  110. { marker: '##', mention: '##foo' },
  111. { marker: '', mention: '@foo' }
  112. ];
  113. for ( const options of testCases ) {
  114. expectToThrowCKEditorError( () => command.execute( options ), /mentioncommand-incorrect-marker/, editor );
  115. }
  116. } );
  117. it( 'should throw if marker does not match mention id', () => {
  118. setData( model, '<paragraph>foo @Jo[]bar</paragraph>' );
  119. const testCases = [
  120. { marker: '@', mention: 'foo' },
  121. { marker: '@', mention: { id: 'foo' } },
  122. { marker: '@', mention: { id: '#foo' } }
  123. ];
  124. for ( const options of testCases ) {
  125. expectToThrowCKEditorError( () => command.execute( options ), /mentioncommand-incorrect-id/, editor );
  126. }
  127. } );
  128. } );
  129. function assertMention( textNode, id ) {
  130. expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
  131. expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'uid' );
  132. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_text', textNode.data );
  133. expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'id', id );
  134. }
  135. } );