insertmediacommand.js 3.3 KB

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