insertmediacommand.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  7. import MediaEmbedEditing from '../src/mediaembedediting';
  8. import MediaEmbedCommand from '../src/mediaembedcommand';
  9. describe( 'MediaEmbedCommand', () => {
  10. let editor, model, command;
  11. beforeEach( () => {
  12. return ModelTestEditor
  13. .create( {
  14. plugins: [ MediaEmbedEditing ]
  15. } )
  16. .then( newEditor => {
  17. editor = newEditor;
  18. model = editor.model;
  19. command = new MediaEmbedCommand( editor );
  20. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  21. } );
  22. } );
  23. afterEach( () => {
  24. return editor.destroy();
  25. } );
  26. describe( 'isEnabled', () => {
  27. it( 'should be true if in a root', () => {
  28. setData( model, '[]' );
  29. expect( command.isEnabled ).to.be.true;
  30. } );
  31. it( 'should be true if in a paragraph (collapsed)', () => {
  32. setData( model, '<p>foo[]</p>' );
  33. expect( command.isEnabled ).to.be.true;
  34. } );
  35. it( 'should be true if in a paragraph (not collapsed)', () => {
  36. setData( model, '<p>[foo]</p>' );
  37. expect( command.isEnabled ).to.be.true;
  38. } );
  39. it( 'should be true if a media is selected', () => {
  40. setData( model, '[<media url="http://ckeditor.com"></media>]' );
  41. expect( command.isEnabled ).to.be.true;
  42. } );
  43. it( 'should be true if a media is selected in a table cell', () => {
  44. model.schema.register( 'table', { allowIn: '$root', isLimit: true, isObject: true, isBlock: true } );
  45. model.schema.register( 'tableRow', { allowIn: 'table', isLimit: true } );
  46. model.schema.register( 'tableCell', { allowIn: 'tableRow', isLimit: true, isSelectable: true } );
  47. model.schema.extend( 'media', { allowIn: 'tableCell' } );
  48. setData( model, '<table><tableRow><tableCell>[<media></media>]</tableCell></tableRow></table>' );
  49. expect( command.isEnabled ).to.be.true;
  50. } );
  51. it( 'should be true if in a table cell', () => {
  52. model.schema.register( 'table', { allowIn: '$root', isLimit: true, isObject: true, isBlock: true } );
  53. model.schema.register( 'tableRow', { allowIn: 'table', isLimit: true } );
  54. model.schema.register( 'tableCell', { allowIn: 'tableRow', isLimit: true, isSelectable: true } );
  55. model.schema.extend( '$block', { allowIn: 'tableCell' } );
  56. setData( model, '<table><tableRow><tableCell><p>foo[]</p></tableCell></tableRow></table>' );
  57. expect( command.isEnabled ).to.be.true;
  58. } );
  59. it( 'should be true when the selection directly in a block', () => {
  60. model.schema.register( 'block', { inheritAllFrom: '$block' } );
  61. model.schema.extend( '$text', { allowIn: 'block' } );
  62. setData( model, '<block>foo[]</block>' );
  63. expect( command.isEnabled ).to.be.true;
  64. } );
  65. it( 'should be false when the selection in a limit element', () => {
  66. model.schema.register( 'block', { inheritAllFrom: '$block' } );
  67. model.schema.register( 'limit', { allowIn: 'block', isLimit: true } );
  68. model.schema.extend( '$text', { allowIn: 'limit' } );
  69. setData( model, '<block><limit>foo[]</limit></block>' );
  70. expect( command.isEnabled ).to.be.false;
  71. } );
  72. } );
  73. describe( 'value', () => {
  74. it( 'should be null when no media is selected (paragraph)', () => {
  75. setData( model, '<p>foo[]</p>' );
  76. expect( command.value ).to.be.null;
  77. } );
  78. it( 'should equal the url of the selected media', () => {
  79. setData( model, '[<media url="http://ckeditor.com"></media>]' );
  80. expect( command.value ).to.equal( 'http://ckeditor.com' );
  81. } );
  82. } );
  83. describe( 'execute()', () => {
  84. it( 'should create a single batch', () => {
  85. setData( model, '<p>foo[]</p>' );
  86. const spy = sinon.spy();
  87. model.document.on( 'change', spy );
  88. command.execute( 'http://ckeditor.com' );
  89. sinon.assert.calledOnce( spy );
  90. } );
  91. it( 'should insert a media in an empty root and select it', () => {
  92. setData( model, '[]' );
  93. command.execute( 'http://ckeditor.com' );
  94. expect( getData( model ) ).to.equal( '[<media url="http://ckeditor.com"></media>]' );
  95. } );
  96. it( 'should update media url', () => {
  97. setData( model, '[<media url="http://ckeditor.com"></media>]' );
  98. command.execute( 'http://cksource.com' );
  99. expect( getData( model ) ).to.equal( '[<media url="http://cksource.com"></media>]' );
  100. } );
  101. } );
  102. } );