8
0

imageuploadbutton.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 '@ckeditor/ckeditor5-image/src/image';
  9. import FileDialogButtonView from '../src/ui/filedialogbuttonview';
  10. import FileRepository from '../src/filerepository';
  11. import ImageUploadButton from '../src/imageuploadbutton';
  12. import ImageUploadEngine from '../src/imageuploadengine';
  13. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  14. import Notification from '@ckeditor/ckeditor5-ui/src/notification/notification';
  15. import { createNativeFileMock, AdapterMock } from './_utils/mocks';
  16. import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  17. describe( 'ImageUploadButton', () => {
  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, ImageUploadButton, 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 include ImageUploadEngine', () => {
  47. expect( editor.plugins.get( ImageUploadEngine ) ).to.be.instanceOf( ImageUploadEngine );
  48. } );
  49. it( 'should register insertImage button', () => {
  50. const button = editor.ui.componentFactory.create( 'insertImage' );
  51. expect( button ).to.be.instanceOf( FileDialogButtonView );
  52. } );
  53. it( 'should be disabled while ImageUploadCommand is disabled', () => {
  54. const button = editor.ui.componentFactory.create( 'insertImage' );
  55. const command = editor.commands.get( 'imageUpload' );
  56. command.isEnabled = true;
  57. expect( button.buttonView.isEnabled ).to.true;
  58. command.isEnabled = false;
  59. expect( button.buttonView.isEnabled ).to.false;
  60. } );
  61. // ckeditor5-upload/#77
  62. it( 'should be properly bound with ImageUploadCommand', () => {
  63. const button = editor.ui.componentFactory.create( 'insertImage' );
  64. const command = editor.commands.get( 'imageUpload' );
  65. const spy = sinon.spy();
  66. button.render();
  67. button.buttonView.on( 'execute', spy );
  68. command.isEnabled = false;
  69. button.buttonView.element.dispatchEvent( new Event( 'click' ) );
  70. sinon.assert.notCalled( spy );
  71. } );
  72. it( 'should execute imageUpload command', () => {
  73. const executeStub = sinon.stub( editor, 'execute' );
  74. const button = editor.ui.componentFactory.create( 'insertImage' );
  75. const files = [ createNativeFileMock() ];
  76. button.fire( 'done', files );
  77. sinon.assert.calledOnce( executeStub );
  78. expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
  79. expect( executeStub.firstCall.args[ 1 ].file ).to.equal( files[ 0 ] );
  80. } );
  81. it( 'should optimize the insertion position', () => {
  82. const button = editor.ui.componentFactory.create( 'insertImage' );
  83. const files = [ createNativeFileMock() ];
  84. setModelData( model, '<paragraph>f[]oo</paragraph>' );
  85. button.fire( 'done', files );
  86. const id = fileRepository.getLoader( files[ 0 ] ).id;
  87. expect( getModelData( model ) ).to.equal(
  88. `[<image uploadId="${ id }" uploadStatus="reading"></image>]` +
  89. '<paragraph>foo</paragraph>'
  90. );
  91. } );
  92. it( 'should correctly insert multiple files', () => {
  93. const button = editor.ui.componentFactory.create( 'insertImage' );
  94. const files = [ createNativeFileMock(), createNativeFileMock() ];
  95. setModelData( model, '<paragraph>foo[]</paragraph><paragraph>bar</paragraph>' );
  96. button.fire( 'done', files );
  97. const id1 = fileRepository.getLoader( files[ 0 ] ).id;
  98. const id2 = fileRepository.getLoader( files[ 1 ] ).id;
  99. expect( getModelData( model ) ).to.equal(
  100. '<paragraph>foo</paragraph>' +
  101. `<image uploadId="${ id1 }" uploadStatus="reading"></image>` +
  102. `[<image uploadId="${ id2 }" uploadStatus="reading"></image>]` +
  103. '<paragraph>bar</paragraph>'
  104. );
  105. } );
  106. it( 'should not execute imageUpload if the file is not an image', () => {
  107. const executeStub = sinon.stub( editor, 'execute' );
  108. const button = editor.ui.componentFactory.create( 'insertImage' );
  109. const file = {
  110. type: 'media/mp3',
  111. size: 1024
  112. };
  113. button.fire( 'done', [ file ] );
  114. sinon.assert.notCalled( executeStub );
  115. } );
  116. it( 'should work even if the FileList does not support iterators', () => {
  117. const executeStub = sinon.stub( editor, 'execute' );
  118. const button = editor.ui.componentFactory.create( 'insertImage' );
  119. const files = {
  120. 0: createNativeFileMock(),
  121. length: 1
  122. };
  123. button.fire( 'done', files );
  124. sinon.assert.calledOnce( executeStub );
  125. expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
  126. expect( executeStub.firstCall.args[ 1 ].file ).to.equal( files[ 0 ] );
  127. } );
  128. } );