imageuploadcommand.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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, AdapterMock } 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/imageengine';
  12. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  13. import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
  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.createAdapter = loader => {
  24. return new AdapterMock( 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( 'execute()', () => {
  46. it( 'should insert image at selection position (includes deleting selected content)', () => {
  47. const file = createNativeFileMock();
  48. setModelData( model, '<paragraph>f[o]o</paragraph>' );
  49. command.execute( { file } );
  50. const id = fileRepository.getLoader( file ).id;
  51. expect( getModelData( model ) )
  52. .to.equal( `<paragraph>f</paragraph>[<image uploadId="${ id }"></image>]<paragraph>o</paragraph>` );
  53. } );
  54. it( 'should insert directly at specified position (options.insertAt)', () => {
  55. const file = createNativeFileMock();
  56. setModelData( model, '<paragraph>f[]oo</paragraph>' );
  57. const insertAt = new ModelPosition( doc.getRoot(), [ 0, 2 ] ); // fo[]o
  58. command.execute( { file, insertAt } );
  59. const id = fileRepository.getLoader( file ).id;
  60. expect( getModelData( model ) )
  61. .to.equal( `<paragraph>fo</paragraph>[<image uploadId="${ id }"></image>]<paragraph>o</paragraph>` );
  62. } );
  63. it( 'should use parent batch', () => {
  64. const file = createNativeFileMock();
  65. setModelData( model, '<paragraph>[]foo</paragraph>' );
  66. model.change( writer => {
  67. expect( writer.batch.deltas ).to.length( 0 );
  68. command.execute( { file } );
  69. expect( writer.batch.deltas ).to.length.above( 0 );
  70. } );
  71. } );
  72. it( 'should not insert image nor crash when image could not be inserted', () => {
  73. const file = createNativeFileMock();
  74. model.schema.register( 'other', {
  75. allowIn: '$root',
  76. isLimit: true
  77. } );
  78. model.schema.extend( '$text', { allowIn: 'other' } );
  79. buildModelConverter().for( editor.editing.modelToView )
  80. .fromElement( 'other' )
  81. .toElement( 'p' );
  82. setModelData( model, '<other>[]</other>' );
  83. command.execute( { file } );
  84. expect( getModelData( model ) ).to.equal( '<other>[]</other>' );
  85. } );
  86. it( 'should not throw when upload adapter is not set (FileRepository will log an error anyway)', () => {
  87. const file = createNativeFileMock();
  88. fileRepository.createAdapter = undefined;
  89. const logStub = testUtils.sinon.stub( log, 'error' );
  90. setModelData( model, '<paragraph>fo[]o</paragraph>' );
  91. expect( () => {
  92. command.execute( { file } );
  93. } ).to.not.throw();
  94. expect( getModelData( model ) ).to.equal( '<paragraph>fo[]o</paragraph>' );
  95. expect( logStub.calledOnce ).to.be.true;
  96. } );
  97. } );
  98. } );