imageuploadengine.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. expect( data.title ).to.equal( 'Upload failed' );
  123. evt.stop();
  124. done();
  125. }, { priority: 'high' } );
  126. setModelData( document, '<paragraph>{}foo bar</paragraph>' );
  127. editor.execute( 'imageUpload', { file } );
  128. nativeReaderMock.mockError( 'Reading error.' );
  129. } );
  130. it( 'should not fire notification on abort', done => {
  131. const notification = editor.plugins.get( Notification );
  132. const file = createNativeFileMock();
  133. const spy = testUtils.sinon.spy();
  134. notification.on( 'show:warning', evt => {
  135. spy();
  136. evt.stop();
  137. }, { priority: 'high' } );
  138. setModelData( document, '<paragraph>{}foo bar</paragraph>' );
  139. editor.execute( 'imageUpload', { file } );
  140. nativeReaderMock.abort();
  141. setTimeout( () => {
  142. sinon.assert.notCalled( spy );
  143. done();
  144. }, 0 );
  145. } );
  146. it( 'should do nothing if image does not have uploadId', () => {
  147. setModelData( document, '<image src="image.png"></image>' );
  148. expect( getViewData( viewDocument ) ).to.equal(
  149. '[]<figure class="image ck-widget" contenteditable="false"><img src="image.png"></img></figure>'
  150. );
  151. } );
  152. it( 'should remove image in case of upload error', done => {
  153. const file = createNativeFileMock();
  154. const spy = testUtils.sinon.spy();
  155. const notification = editor.plugins.get( Notification );
  156. setModelData( document, '<paragraph>{}foo bar</paragraph>' );
  157. notification.on( 'show:warning', evt => {
  158. spy();
  159. evt.stop();
  160. }, { priority: 'high' } );
  161. editor.execute( 'imageUpload', { file } );
  162. document.once( 'changesDone', () => {
  163. document.once( 'changesDone', () => {
  164. expect( getModelData( document ) ).to.equal( '<paragraph>[]foo bar</paragraph>' );
  165. sinon.assert.calledOnce( spy );
  166. done();
  167. } );
  168. } );
  169. nativeReaderMock.mockError( 'Upload error.' );
  170. } );
  171. it( 'should abort upload if image is removed', () => {
  172. const file = createNativeFileMock();
  173. setModelData( document, '<paragraph>{}foo bar</paragraph>' );
  174. editor.execute( 'imageUpload', { file } );
  175. const abortSpy = testUtils.sinon.spy( loader, 'abort' );
  176. expect( loader.status ).to.equal( 'reading' );
  177. nativeReaderMock.mockSuccess( base64Sample );
  178. const image = document.getRoot().getChild( 0 );
  179. document.enqueueChanges( () => {
  180. const batch = document.batch();
  181. batch.remove( image );
  182. } );
  183. expect( loader.status ).to.equal( 'aborted' );
  184. sinon.assert.calledOnce( abortSpy );
  185. } );
  186. } );