imageuploadengine.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals window, setTimeout */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  7. import ImageEngine from '@ckeditor/ckeditor5-image/src/image/imageengine';
  8. import ImageUploadEngine from '../src/imageuploadengine';
  9. import ImageUploadCommand from '../src/imageuploadcommand';
  10. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  11. import DataTransfer from '@ckeditor/ckeditor5-clipboard/src/datatransfer';
  12. import FileRepository from '../src/filerepository';
  13. import { AdapterMock, createNativeFileMock, NativeFileReaderMock } from './_utils/mocks';
  14. import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  15. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  16. import { eventNameToConsumableType } from '@ckeditor/ckeditor5-engine/src/conversion/model-to-view-converters';
  17. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  18. import Notification from '@ckeditor/ckeditor5-ui/src/notification/notification';
  19. describe( 'ImageUploadEngine', () => {
  20. // eslint-disable-next-line max-len
  21. const base64Sample = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=';
  22. let editor, document, fileRepository, viewDocument, nativeReaderMock, loader, adapterMock;
  23. testUtils.createSinonSandbox();
  24. beforeEach( () => {
  25. testUtils.sinon.stub( window, 'FileReader', () => {
  26. nativeReaderMock = new NativeFileReaderMock();
  27. return nativeReaderMock;
  28. } );
  29. return ClassicTestEditor.create( {
  30. plugins: [ ImageEngine, ImageUploadEngine, Paragraph ]
  31. } )
  32. .then( newEditor => {
  33. editor = newEditor;
  34. document = editor.document;
  35. viewDocument = editor.editing.view;
  36. fileRepository = editor.plugins.get( FileRepository );
  37. fileRepository.createAdapter = newLoader => {
  38. loader = newLoader;
  39. adapterMock = new AdapterMock( loader );
  40. return adapterMock;
  41. };
  42. } );
  43. } );
  44. it( 'should register proper schema rules', () => {
  45. expect( document.schema.check( { name: 'image', attributes: [ 'uploadId' ], inside: '$root' } ) ).to.be.true;
  46. } );
  47. it( 'should register imageUpload command', () => {
  48. expect( editor.commands.get( 'imageUpload' ) ).to.be.instanceOf( ImageUploadCommand );
  49. } );
  50. it( 'should execute imageUpload command when image is pasted', () => {
  51. const spy = sinon.spy( editor, 'execute' );
  52. const fileMock = createNativeFileMock();
  53. const dataTransfer = new DataTransfer( { files: [ fileMock ] } );
  54. setModelData( document, '<paragraph>[]foo bar baz</paragraph>' );
  55. viewDocument.fire( 'clipboardInput', { dataTransfer } );
  56. sinon.assert.calledOnce( spy );
  57. sinon.assert.calledWith( spy, 'imageUpload' );
  58. const id = fileRepository.getLoader( fileMock ).id;
  59. expect( getModelData( document ) ).to.equal(
  60. `[<image uploadId="${ id }" uploadStatus="reading"></image>]<paragraph>foo bar baz</paragraph>`
  61. );
  62. } );
  63. it( 'should not execute imageUpload command when file is not an image', () => {
  64. const spy = sinon.spy( editor, 'execute' );
  65. const viewDocument = editor.editing.view;
  66. const fileMock = {
  67. type: 'media/mp3',
  68. size: 1024
  69. };
  70. const dataTransfer = new DataTransfer( { files: [ fileMock ] } );
  71. setModelData( document, '<paragraph>foo bar baz[]</paragraph>' );
  72. viewDocument.fire( 'clipboardInput', { dataTransfer } );
  73. sinon.assert.notCalled( spy );
  74. } );
  75. it( 'should not convert image\'s uploadId attribute if is consumed already', () => {
  76. editor.editing.modelToView.on( 'addAttribute:uploadId:image', ( evt, data, consumable ) => {
  77. consumable.consume( data.item, eventNameToConsumableType( evt.name ) );
  78. }, { priority: 'high' } );
  79. setModelData( document, '<image uploadId="1234"></image>' );
  80. expect( getViewData( viewDocument ) ).to.equal(
  81. '[]<figure class="image ck-widget" contenteditable="false">' +
  82. '<img></img>' +
  83. '</figure>' );
  84. } );
  85. it( 'should use read data once it is present', done => {
  86. const file = createNativeFileMock();
  87. setModelData( document, '<paragraph>{}foo bar</paragraph>' );
  88. editor.execute( 'imageUpload', { file } );
  89. document.once( 'changesDone', () => {
  90. expect( getViewData( viewDocument ) ).to.equal(
  91. '[<figure class="image ck-widget" contenteditable="false">' +
  92. `<img src="${ base64Sample }"></img>` +
  93. '</figure>]' +
  94. '<p>foo bar</p>' );
  95. expect( loader.status ).to.equal( 'uploading' );
  96. done();
  97. } );
  98. expect( loader.status ).to.equal( 'reading' );
  99. nativeReaderMock.mockSuccess( base64Sample );
  100. } );
  101. it( 'should replace read data with server response once it is present', done => {
  102. const file = createNativeFileMock();
  103. setModelData( document, '<paragraph>{}foo bar</paragraph>' );
  104. editor.execute( 'imageUpload', { file } );
  105. document.once( 'changesDone', () => {
  106. document.once( 'changesDone', () => {
  107. expect( getViewData( viewDocument ) ).to.equal(
  108. '[<figure class="image ck-widget" contenteditable="false"><img src="image.png"></img></figure>]<p>foo bar</p>'
  109. );
  110. expect( loader.status ).to.equal( 'idle' );
  111. done();
  112. } );
  113. adapterMock.mockSuccess( { original: 'image.png' } );
  114. } );
  115. nativeReaderMock.mockSuccess( base64Sample );
  116. } );
  117. it( 'should fire notification event in case of error', done => {
  118. const notification = editor.plugins.get( Notification );
  119. const file = createNativeFileMock();
  120. notification.on( 'show:warning', ( evt, data ) => {
  121. expect( data.message ).to.equal( 'Reading error.' );
  122. evt.stop();
  123. done();
  124. }, { priority: 'high' } );
  125. setModelData( document, '<paragraph>{}foo bar</paragraph>' );
  126. editor.execute( 'imageUpload', { file } );
  127. nativeReaderMock.mockError( 'Reading error.' );
  128. } );
  129. it( 'should not fire notification on abort', done => {
  130. const notification = editor.plugins.get( Notification );
  131. const file = createNativeFileMock();
  132. const spy = testUtils.sinon.spy();
  133. notification.on( 'show:warning', evt => {
  134. spy();
  135. evt.stop();
  136. }, { priority: 'high' } );
  137. setModelData( document, '<paragraph>{}foo bar</paragraph>' );
  138. editor.execute( 'imageUpload', { file } );
  139. nativeReaderMock.abort();
  140. setTimeout( () => {
  141. sinon.assert.notCalled( spy );
  142. done();
  143. }, 0 );
  144. } );
  145. it( 'should do nothing if image does not have uploadId', () => {
  146. setModelData( document, '<image src="image.png"></image>' );
  147. expect( getViewData( viewDocument ) ).to.equal(
  148. '[]<figure class="image ck-widget" contenteditable="false"><img src="image.png"></img></figure>'
  149. );
  150. } );
  151. it( 'should remove image in case of upload error', done => {
  152. const file = createNativeFileMock();
  153. const spy = testUtils.sinon.spy();
  154. const notification = editor.plugins.get( Notification );
  155. setModelData( document, '<paragraph>{}foo bar</paragraph>' );
  156. notification.on( 'show:warning', evt => {
  157. spy();
  158. evt.stop();
  159. }, { priority: 'high' } );
  160. editor.execute( 'imageUpload', { file } );
  161. document.once( 'changesDone', () => {
  162. document.once( 'changesDone', () => {
  163. expect( getModelData( document ) ).to.equal( '<paragraph>[]foo bar</paragraph>' );
  164. sinon.assert.calledOnce( spy );
  165. done();
  166. } );
  167. } );
  168. nativeReaderMock.mockError( 'Upload error.' );
  169. } );
  170. it( 'should abort upload if image is removed', () => {
  171. const file = createNativeFileMock();
  172. setModelData( document, '<paragraph>{}foo bar</paragraph>' );
  173. editor.execute( 'imageUpload', { file } );
  174. const abortSpy = testUtils.sinon.spy( loader, 'abort' );
  175. expect( loader.status ).to.equal( 'reading' );
  176. nativeReaderMock.mockSuccess( base64Sample );
  177. const image = document.getRoot().getChild( 0 );
  178. document.enqueueChanges( () => {
  179. const batch = document.batch();
  180. batch.remove( image );
  181. } );
  182. expect( loader.status ).to.equal( 'aborted' );
  183. sinon.assert.calledOnce( abortSpy );
  184. } );
  185. } );