imageuploadui.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document, Event */
  6. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  7. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  8. import Image from '../../src/image';
  9. import FileDialogButtonView from '@ckeditor/ckeditor5-upload/src/ui/filedialogbuttonview';
  10. import FileRepository from '@ckeditor/ckeditor5-upload/src/filerepository';
  11. import ImageUploadUI from '../../src/imageupload/imageuploadui';
  12. import ImageUploadEditing from '../../src/imageupload/imageuploadediting';
  13. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  14. import Notification from '@ckeditor/ckeditor5-ui/src/notification/notification';
  15. import { createNativeFileMock, UploadAdapterMock } from '@ckeditor/ckeditor5-upload/tests/_utils/mocks';
  16. import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  17. describe( 'ImageUploadUI', () => {
  18. let editor, model, editorElement, fileRepository;
  19. class UploadAdapterPluginMock extends Plugin {
  20. init() {
  21. fileRepository = this.editor.plugins.get( FileRepository );
  22. fileRepository.createUploadAdapter = loader => {
  23. return new UploadAdapterMock( loader );
  24. };
  25. }
  26. }
  27. beforeEach( () => {
  28. editorElement = document.createElement( 'div' );
  29. document.body.appendChild( editorElement );
  30. return ClassicEditor
  31. .create( editorElement, {
  32. plugins: [ Paragraph, Image, ImageUploadEditing, ImageUploadUI, FileRepository, UploadAdapterPluginMock ]
  33. } )
  34. .then( newEditor => {
  35. editor = newEditor;
  36. model = editor.model;
  37. // Hide all notifications (prevent alert() calls).
  38. const notification = editor.plugins.get( Notification );
  39. notification.on( 'show', evt => evt.stop() );
  40. } );
  41. } );
  42. afterEach( () => {
  43. editorElement.remove();
  44. return editor.destroy();
  45. } );
  46. it( 'should register imageUpload button', () => {
  47. const button = editor.ui.componentFactory.create( 'imageUpload' );
  48. expect( button ).to.be.instanceOf( FileDialogButtonView );
  49. } );
  50. it( 'should be disabled while ImageUploadCommand is disabled', () => {
  51. const button = editor.ui.componentFactory.create( 'imageUpload' );
  52. const command = editor.commands.get( 'imageUpload' );
  53. command.isEnabled = true;
  54. expect( button.buttonView.isEnabled ).to.true;
  55. command.isEnabled = false;
  56. expect( button.buttonView.isEnabled ).to.false;
  57. } );
  58. // ckeditor5-upload/#77
  59. it( 'should be properly bound with ImageUploadCommand', () => {
  60. const button = editor.ui.componentFactory.create( 'imageUpload' );
  61. const command = editor.commands.get( 'imageUpload' );
  62. const spy = sinon.spy();
  63. button.render();
  64. button.buttonView.on( 'execute', spy );
  65. command.isEnabled = false;
  66. button.buttonView.element.dispatchEvent( new Event( 'click' ) );
  67. sinon.assert.notCalled( spy );
  68. } );
  69. it( 'should execute imageUpload command', () => {
  70. const executeStub = sinon.stub( editor, 'execute' );
  71. const button = editor.ui.componentFactory.create( 'imageUpload' );
  72. const files = [ createNativeFileMock() ];
  73. button.fire( 'done', files );
  74. sinon.assert.calledOnce( executeStub );
  75. expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
  76. expect( executeStub.firstCall.args[ 1 ].files ).to.deep.equal( files );
  77. } );
  78. it( 'should execute imageUpload command with multiple files', () => {
  79. const executeStub = sinon.stub( editor, 'execute' );
  80. const button = editor.ui.componentFactory.create( 'imageUpload' );
  81. const files = [ createNativeFileMock(), createNativeFileMock(), createNativeFileMock() ];
  82. button.fire( 'done', files );
  83. sinon.assert.calledOnce( executeStub );
  84. expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
  85. expect( executeStub.firstCall.args[ 1 ].files ).to.deep.equal( files );
  86. } );
  87. it( 'should optimize the insertion position', () => {
  88. const button = editor.ui.componentFactory.create( 'imageUpload' );
  89. const files = [ createNativeFileMock() ];
  90. setModelData( model, '<paragraph>f[]oo</paragraph>' );
  91. button.fire( 'done', files );
  92. const id = fileRepository.getLoader( files[ 0 ] ).id;
  93. expect( getModelData( model ) ).to.equal(
  94. `[<image uploadId="${ id }" uploadStatus="reading"></image>]` +
  95. '<paragraph>foo</paragraph>'
  96. );
  97. } );
  98. it( 'should correctly insert multiple files', () => {
  99. const button = editor.ui.componentFactory.create( 'imageUpload' );
  100. const files = [ createNativeFileMock(), createNativeFileMock() ];
  101. setModelData( model, '<paragraph>foo[]</paragraph><paragraph>bar</paragraph>' );
  102. button.fire( 'done', files );
  103. const id1 = fileRepository.getLoader( files[ 0 ] ).id;
  104. const id2 = fileRepository.getLoader( files[ 1 ] ).id;
  105. expect( getModelData( model ) ).to.equal(
  106. '<paragraph>foo</paragraph>' +
  107. `<image uploadId="${ id1 }" uploadStatus="reading"></image>` +
  108. `[<image uploadId="${ id2 }" uploadStatus="reading"></image>]` +
  109. '<paragraph>bar</paragraph>'
  110. );
  111. } );
  112. it( 'should not execute imageUpload if the file is not an image', () => {
  113. const executeStub = sinon.stub( editor, 'execute' );
  114. const button = editor.ui.componentFactory.create( 'imageUpload' );
  115. const file = {
  116. type: 'media/mp3',
  117. size: 1024
  118. };
  119. button.fire( 'done', [ file ] );
  120. sinon.assert.notCalled( executeStub );
  121. } );
  122. it( 'should work even if the FileList does not support iterators', () => {
  123. const executeStub = sinon.stub( editor, 'execute' );
  124. const button = editor.ui.componentFactory.create( 'imageUpload' );
  125. const files = {
  126. 0: createNativeFileMock(),
  127. length: 1
  128. };
  129. button.fire( 'done', files );
  130. sinon.assert.calledOnce( executeStub );
  131. expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
  132. expect( executeStub.firstCall.args[ 1 ].files ).to.deep.equal( [ files[ 0 ] ] );
  133. } );
  134. } );