imageuploadcommand.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 ImageUploadCommand from '../src/imageuploadcommand';
  7. import FileRepository from '../src/filerepository';
  8. import { createNativeFileMock, AdapterMock } from './_utils/mocks';
  9. import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  10. import Image from '@ckeditor/ckeditor5-image/src/image/imageengine';
  11. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  12. import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
  13. describe( 'ImageUploadCommand', () => {
  14. let editor, command, adapterMock, document, fileRepository;
  15. beforeEach( () => {
  16. return VirtualTestEditor.create( {
  17. plugins: [ FileRepository, Image, Paragraph ]
  18. } )
  19. .then( newEditor => {
  20. editor = newEditor;
  21. command = new ImageUploadCommand( editor );
  22. fileRepository = editor.plugins.get( FileRepository );
  23. fileRepository.createAdapter = loader => {
  24. adapterMock = new AdapterMock( loader );
  25. return adapterMock;
  26. };
  27. document = editor.document;
  28. const schema = document.schema;
  29. schema.allow( { name: 'image', attributes: [ 'uploadId' ], inside: '$root' } );
  30. schema.requireAttributes( 'image', [ 'uploadId' ] );
  31. } );
  32. } );
  33. describe( '_doExecute', () => {
  34. it( 'should insert image', () => {
  35. const file = createNativeFileMock();
  36. setModelData( document, '<paragraph>foo[]</paragraph>' );
  37. command._doExecute( { file } );
  38. const id = fileRepository.getLoader( file ).id;
  39. expect( getModelData( document ) ).to.equal( `[<image uploadId="${ id }"></image>]<paragraph>foo</paragraph>` );
  40. } );
  41. it( 'should insert image after other image', () => {
  42. const file = createNativeFileMock();
  43. setModelData( document, '[<image src="image.png"></image>]' );
  44. command._doExecute( { file } );
  45. const id = fileRepository.getLoader( file ).id;
  46. expect( getModelData( document ) ).to.equal( `<image src="image.png"></image>[<image uploadId="${ id }"></image>]` );
  47. } );
  48. it( 'should not insert image when proper insert position cannot be found', () => {
  49. const file = createNativeFileMock();
  50. document.schema.registerItem( 'other' );
  51. document.schema.allow( { name: 'other', inside: '$root' } );
  52. buildModelConverter().for( editor.editing.modelToView )
  53. .fromElement( 'other' )
  54. .toElement( 'span' );
  55. setModelData( document, '<other>[]</other>' );
  56. command._doExecute( { file } );
  57. expect( getModelData( document ) ).to.equal( '<other>[]</other>' );
  58. } );
  59. it( 'should not insert non-image', () => {
  60. const file = createNativeFileMock();
  61. file.type = 'audio/mpeg3';
  62. setModelData( document, '<paragraph>foo[]</paragraph>' );
  63. command._doExecute( { file } );
  64. expect( getModelData( document ) ).to.equal( '<paragraph>foo[]</paragraph>' );
  65. } );
  66. it( 'should allow to provide batch instance', () => {
  67. const batch = document.batch();
  68. const file = createNativeFileMock();
  69. const spy = sinon.spy( batch, 'insert' );
  70. setModelData( document, '<paragraph>foo[]</paragraph>' );
  71. command._doExecute( { batch, file } );
  72. const id = fileRepository.getLoader( file ).id;
  73. expect( getModelData( document ) ).to.equal( `[<image uploadId="${ id }"></image>]<paragraph>foo</paragraph>` );
  74. sinon.assert.calledOnce( spy );
  75. } );
  76. } );
  77. } );