imageuploadui.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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, AdapterMock } 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.createAdapter = loader => {
  23. return new AdapterMock( 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 uploadImage button', () => {
  47. const button = editor.ui.componentFactory.create( 'uploadImage' );
  48. expect( button ).to.be.instanceOf( FileDialogButtonView );
  49. } );
  50. it( 'should be disabled while ImageUploadCommand is disabled', () => {
  51. const button = editor.ui.componentFactory.create( 'uploadImage' );
  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( 'uploadImage' );
  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( 'uploadImage' );
  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 ].file ).to.equal( files[ 0 ] );
  77. } );
  78. it( 'should optimize the insertion position', () => {
  79. const button = editor.ui.componentFactory.create( 'uploadImage' );
  80. const files = [ createNativeFileMock() ];
  81. setModelData( model, '<paragraph>f[]oo</paragraph>' );
  82. button.fire( 'done', files );
  83. const id = fileRepository.getLoader( files[ 0 ] ).id;
  84. expect( getModelData( model ) ).to.equal(
  85. `[<image uploadId="${ id }" uploadStatus="reading"></image>]` +
  86. '<paragraph>foo</paragraph>'
  87. );
  88. } );
  89. it( 'should correctly insert multiple files', () => {
  90. const button = editor.ui.componentFactory.create( 'uploadImage' );
  91. const files = [ createNativeFileMock(), createNativeFileMock() ];
  92. setModelData( model, '<paragraph>foo[]</paragraph><paragraph>bar</paragraph>' );
  93. button.fire( 'done', files );
  94. const id1 = fileRepository.getLoader( files[ 0 ] ).id;
  95. const id2 = fileRepository.getLoader( files[ 1 ] ).id;
  96. expect( getModelData( model ) ).to.equal(
  97. '<paragraph>foo</paragraph>' +
  98. `<image uploadId="${ id1 }" uploadStatus="reading"></image>` +
  99. `[<image uploadId="${ id2 }" uploadStatus="reading"></image>]` +
  100. '<paragraph>bar</paragraph>'
  101. );
  102. } );
  103. it( 'should not execute imageUpload if the file is not an image', () => {
  104. const executeStub = sinon.stub( editor, 'execute' );
  105. const button = editor.ui.componentFactory.create( 'uploadImage' );
  106. const file = {
  107. type: 'media/mp3',
  108. size: 1024
  109. };
  110. button.fire( 'done', [ file ] );
  111. sinon.assert.notCalled( executeStub );
  112. } );
  113. it( 'should work even if the FileList does not support iterators', () => {
  114. const executeStub = sinon.stub( editor, 'execute' );
  115. const button = editor.ui.componentFactory.create( 'uploadImage' );
  116. const files = {
  117. 0: createNativeFileMock(),
  118. length: 1
  119. };
  120. button.fire( 'done', files );
  121. sinon.assert.calledOnce( executeStub );
  122. expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
  123. expect( executeStub.firstCall.args[ 1 ].file ).to.equal( files[ 0 ] );
  124. } );
  125. } );