insertmediacommand.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 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( 'p', { allowIn: 'tableCell' } );
  48. model.schema.extend( 'media', { allowIn: 'p' } );
  49. setData( model, '<table><tableRow><tableCell><p>[<media></media>]</p></tableCell></tableRow></table>' );
  50. expect( command.isEnabled ).to.be.true;
  51. } );
  52. it( 'should be true if in a table cell', () => {
  53. model.schema.register( 'table', { allowIn: '$root', isLimit: true, isObject: true, isBlock: true } );
  54. model.schema.register( 'tableRow', { allowIn: 'table', isLimit: true } );
  55. model.schema.register( 'tableCell', { allowIn: 'tableRow', isLimit: true, isSelectable: true } );
  56. model.schema.extend( '$block', { allowIn: 'tableCell' } );
  57. setData( model, '<table><tableRow><tableCell><p>foo[]</p></tableCell></tableRow></table>' );
  58. expect( command.isEnabled ).to.be.true;
  59. } );
  60. it( 'should be true when the selection directly in a block', () => {
  61. model.schema.register( 'block', { inheritAllFrom: '$block' } );
  62. model.schema.extend( '$text', { allowIn: 'block' } );
  63. setData( model, '<block>foo[]</block>' );
  64. expect( command.isEnabled ).to.be.true;
  65. } );
  66. it( 'should be false when the selection in a limit element', () => {
  67. model.schema.register( 'block', { inheritAllFrom: '$block' } );
  68. model.schema.register( 'limit', { allowIn: 'block', isLimit: true } );
  69. model.schema.extend( '$text', { allowIn: 'limit' } );
  70. setData( model, '<block><limit>foo[]</limit></block>' );
  71. expect( command.isEnabled ).to.be.false;
  72. } );
  73. } );
  74. describe( 'value', () => {
  75. it( 'should be null when no media is selected (paragraph)', () => {
  76. setData( model, '<p>foo[]</p>' );
  77. expect( command.value ).to.be.null;
  78. } );
  79. it( 'should equal the url of the selected media', () => {
  80. setData( model, '[<media url="http://ckeditor.com"></media>]' );
  81. expect( command.value ).to.equal( 'http://ckeditor.com' );
  82. } );
  83. } );
  84. describe( 'execute()', () => {
  85. it( 'should create a single batch', () => {
  86. setData( model, '<p>foo[]</p>' );
  87. const spy = sinon.spy();
  88. model.document.on( 'change', spy );
  89. command.execute( 'http://ckeditor.com' );
  90. sinon.assert.calledOnce( spy );
  91. } );
  92. it( 'should insert a media in an empty root and select it', () => {
  93. setData( model, '[]' );
  94. command.execute( 'http://ckeditor.com' );
  95. expect( getData( model ) ).to.equal( '[<media url="http://ckeditor.com"></media>]' );
  96. } );
  97. it( 'should update media url', () => {
  98. setData( model, '[<media url="http://ckeditor.com"></media>]' );
  99. command.execute( 'http://cksource.com' );
  100. expect( getData( model ) ).to.equal( '[<media url="http://cksource.com"></media>]' );
  101. } );
  102. } );
  103. } );