imageuploadcommand.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  6. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  7. import ImageUploadCommand from '../../src/imageupload/imageuploadcommand';
  8. import FileRepository from '@ckeditor/ckeditor5-upload/src/filerepository';
  9. import { createNativeFileMock, UploadAdapterMock } from '@ckeditor/ckeditor5-upload/tests/_utils/mocks';
  10. import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  11. import Image from '../../src/image/imageediting';
  12. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  13. import { downcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
  14. import ModelPosition from '@ckeditor/ckeditor5-engine/src/model/position';
  15. import log from '@ckeditor/ckeditor5-utils/src/log';
  16. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  17. describe( 'ImageUploadCommand', () => {
  18. let editor, command, model, doc, fileRepository;
  19. testUtils.createSinonSandbox();
  20. class UploadAdapterPluginMock extends Plugin {
  21. init() {
  22. fileRepository = this.editor.plugins.get( FileRepository );
  23. fileRepository.createUploadAdapter = loader => {
  24. return new UploadAdapterMock( loader );
  25. };
  26. }
  27. }
  28. beforeEach( () => {
  29. return VirtualTestEditor
  30. .create( {
  31. plugins: [ FileRepository, Image, Paragraph, UploadAdapterPluginMock ]
  32. } )
  33. .then( newEditor => {
  34. editor = newEditor;
  35. model = editor.model;
  36. doc = model.document;
  37. command = new ImageUploadCommand( editor );
  38. const schema = model.schema;
  39. schema.extend( 'image', { allowAttributes: 'uploadId' } );
  40. } );
  41. } );
  42. afterEach( () => {
  43. return editor.destroy();
  44. } );
  45. describe( 'isEnabled', () => {
  46. it( 'should be true when the selection directly in the root', () => {
  47. setModelData( model, '[]' );
  48. expect( command.isEnabled ).to.be.true;
  49. } );
  50. it( 'should be true when the selection directly in a paragraph', () => {
  51. setModelData( model, '<paragraph>foo[]</paragraph>' );
  52. expect( command.isEnabled ).to.be.true;
  53. } );
  54. it( 'should be true when the selection directly in a block', () => {
  55. model.schema.register( 'block', { inheritAllFrom: '$block' } );
  56. model.schema.extend( '$text', { allowIn: 'block' } );
  57. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'block', view: 'block' } ) );
  58. setModelData( model, '<block>foo[]</block>' );
  59. expect( command.isEnabled ).to.be.true;
  60. } );
  61. it( 'should be false when the selection in a limit element', () => {
  62. model.schema.register( 'block', { inheritAllFrom: '$block' } );
  63. model.schema.register( 'limit', { allowIn: 'block', isLimit: true } );
  64. model.schema.extend( '$text', { allowIn: 'limit' } );
  65. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'block', view: 'block' } ) );
  66. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'limit', view: 'limit' } ) );
  67. setModelData( model, '<block><limit>foo[]</limit></block>' );
  68. expect( command.isEnabled ).to.be.false;
  69. } );
  70. } );
  71. describe( 'execute()', () => {
  72. it( 'should insert image at selection position (includes deleting selected content)', () => {
  73. const file = createNativeFileMock();
  74. setModelData( model, '<paragraph>f[o]o</paragraph>' );
  75. command.execute( { file } );
  76. const id = fileRepository.getLoader( file ).id;
  77. expect( getModelData( model ) )
  78. .to.equal( `<paragraph>f</paragraph>[<image uploadId="${ id }"></image>]<paragraph>o</paragraph>` );
  79. } );
  80. it( 'should insert directly at specified position (options.insertAt)', () => {
  81. const file = createNativeFileMock();
  82. setModelData( model, '<paragraph>f[]oo</paragraph>' );
  83. const insertAt = new ModelPosition( doc.getRoot(), [ 0, 2 ] ); // fo[]o
  84. command.execute( { file, insertAt } );
  85. const id = fileRepository.getLoader( file ).id;
  86. expect( getModelData( model ) )
  87. .to.equal( `<paragraph>fo</paragraph>[<image uploadId="${ id }"></image>]<paragraph>o</paragraph>` );
  88. } );
  89. it( 'should use parent batch', () => {
  90. const file = createNativeFileMock();
  91. setModelData( model, '<paragraph>[]foo</paragraph>' );
  92. model.change( writer => {
  93. expect( writer.batch.operations ).to.length( 0 );
  94. command.execute( { file } );
  95. expect( writer.batch.operations ).to.length.above( 0 );
  96. } );
  97. } );
  98. it( 'should not insert image nor crash when image could not be inserted', () => {
  99. const file = createNativeFileMock();
  100. model.schema.register( 'other', {
  101. allowIn: '$root',
  102. isLimit: true
  103. } );
  104. model.schema.extend( '$text', { allowIn: 'other' } );
  105. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'other', view: 'p' } ) );
  106. setModelData( model, '<other>[]</other>' );
  107. command.execute( { file } );
  108. expect( getModelData( model ) ).to.equal( '<other>[]</other>' );
  109. } );
  110. it( 'should not throw when upload adapter is not set (FileRepository will log an error anyway)', () => {
  111. const file = createNativeFileMock();
  112. fileRepository.createUploadAdapter = undefined;
  113. const logStub = testUtils.sinon.stub( log, 'error' );
  114. setModelData( model, '<paragraph>fo[]o</paragraph>' );
  115. expect( () => {
  116. command.execute( { file } );
  117. } ).to.not.throw();
  118. expect( getModelData( model ) ).to.equal( '<paragraph>fo[]o</paragraph>' );
  119. expect( logStub.calledOnce ).to.be.true;
  120. } );
  121. } );
  122. } );