imageuploadcommand.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. describe( 'ImageUploadCommand', () => {
  16. let editor, command, doc, fileRepository;
  17. class UploadAdapterPluginMock extends Plugin {
  18. init() {
  19. fileRepository = this.editor.plugins.get( FileRepository );
  20. fileRepository.createAdapter = loader => {
  21. return new AdapterMock( loader );
  22. };
  23. }
  24. }
  25. beforeEach( () => {
  26. return VirtualTestEditor
  27. .create( {
  28. plugins: [ FileRepository, Image, Paragraph, UploadAdapterPluginMock ]
  29. } )
  30. .then( newEditor => {
  31. editor = newEditor;
  32. command = new ImageUploadCommand( editor );
  33. doc = editor.document;
  34. const schema = doc.schema;
  35. schema.allow( { name: 'image', attributes: [ 'uploadId' ], inside: '$root' } );
  36. schema.requireAttributes( 'image', [ 'uploadId' ] );
  37. } );
  38. } );
  39. afterEach( () => {
  40. return editor.destroy();
  41. } );
  42. describe( 'execute()', () => {
  43. it( 'should insert image at selection position (includes deleting selected content)', () => {
  44. const file = createNativeFileMock();
  45. setModelData( doc, '<paragraph>f[o]o</paragraph>' );
  46. command.execute( { file } );
  47. const id = fileRepository.getLoader( file ).id;
  48. expect( getModelData( doc ) )
  49. .to.equal( `<paragraph>f</paragraph>[<image uploadId="${ id }"></image>]<paragraph>o</paragraph>` );
  50. } );
  51. it( 'should insert directly at specified position (options.insertAt)', () => {
  52. const file = createNativeFileMock();
  53. setModelData( doc, '<paragraph>f[]oo</paragraph>' );
  54. const insertAt = new ModelPosition( doc.getRoot(), [ 0, 2 ] ); // fo[]o
  55. command.execute( { file, insertAt } );
  56. const id = fileRepository.getLoader( file ).id;
  57. expect( getModelData( doc ) )
  58. .to.equal( `<paragraph>fo</paragraph>[<image uploadId="${ id }"></image>]<paragraph>o</paragraph>` );
  59. } );
  60. it( 'should allow to provide batch instance (options.batch)', () => {
  61. const batch = doc.batch();
  62. const file = createNativeFileMock();
  63. const spy = sinon.spy( batch, 'insert' );
  64. setModelData( doc, '<paragraph>[]foo</paragraph>' );
  65. command.execute( { batch, file } );
  66. const id = fileRepository.getLoader( file ).id;
  67. expect( getModelData( doc ) ).to.equal( `[<image uploadId="${ id }"></image>]<paragraph>foo</paragraph>` );
  68. sinon.assert.calledOnce( spy );
  69. } );
  70. it( 'should not insert image nor crash when image could not be inserted', () => {
  71. const file = createNativeFileMock();
  72. doc.schema.registerItem( 'other' );
  73. doc.schema.allow( { name: '$text', inside: 'other' } );
  74. doc.schema.allow( { name: 'other', inside: '$root' } );
  75. doc.schema.limits.add( 'other' );
  76. buildModelConverter().for( editor.editing.modelToView )
  77. .fromElement( 'other' )
  78. .toElement( 'p' );
  79. setModelData( doc, '<other>[]</other>' );
  80. command.execute( { file } );
  81. expect( getModelData( doc ) ).to.equal( '<other>[]</other>' );
  82. } );
  83. } );
  84. } );