8
0

imageuploadcommand.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * @license Copyright (c) 2003-2017, 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/imageuploadcommand';
  8. import FileRepository from '../src/filerepository';
  9. import { createNativeFileMock, AdapterMock } from './_utils/mocks';
  10. import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  11. import Image from '@ckeditor/ckeditor5-image/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, 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. command = new ImageUploadCommand( editor );
  36. doc = editor.document;
  37. const schema = doc.schema;
  38. schema.allow( { name: 'image', attributes: [ 'uploadId' ], inside: '$root' } );
  39. schema.requireAttributes( 'image', [ '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( doc, '<paragraph>f[o]o</paragraph>' );
  49. command.execute( { file } );
  50. const id = fileRepository.getLoader( file ).id;
  51. expect( getModelData( doc ) )
  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( doc, '<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( doc ) )
  61. .to.equal( `<paragraph>fo</paragraph>[<image uploadId="${ id }"></image>]<paragraph>o</paragraph>` );
  62. } );
  63. it( 'should allow to provide batch instance (options.batch)', () => {
  64. const batch = doc.batch();
  65. const file = createNativeFileMock();
  66. const spy = sinon.spy( batch, 'insert' );
  67. setModelData( doc, '<paragraph>[]foo</paragraph>' );
  68. command.execute( { batch, file } );
  69. const id = fileRepository.getLoader( file ).id;
  70. expect( getModelData( doc ) ).to.equal( `[<image uploadId="${ id }"></image>]<paragraph>foo</paragraph>` );
  71. sinon.assert.calledOnce( spy );
  72. } );
  73. it( 'should not insert image nor crash when image could not be inserted', () => {
  74. const file = createNativeFileMock();
  75. doc.schema.registerItem( 'other' );
  76. doc.schema.allow( { name: '$text', inside: 'other' } );
  77. doc.schema.allow( { name: 'other', inside: '$root' } );
  78. doc.schema.limits.add( 'other' );
  79. buildModelConverter().for( editor.editing.modelToView )
  80. .fromElement( 'other' )
  81. .toElement( 'p' );
  82. setModelData( doc, '<other>[]</other>' );
  83. command.execute( { file } );
  84. expect( getModelData( doc ) ).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( doc, '<paragraph>fo[]o</paragraph>' );
  91. expect( () => {
  92. command.execute( { file } );
  93. } ).to.not.throw();
  94. expect( getModelData( doc ) ).to.equal( '<paragraph>fo[]o</paragraph>' );
  95. expect( logStub.calledOnce ).to.be.true;
  96. } );
  97. } );
  98. } );