8
0

imageuploadbutton.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  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, doc, 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. doc = editor.document;
  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.isEnabled ).to.true;
  58. command.isEnabled = false;
  59. expect( button.isEnabled ).to.false;
  60. } );
  61. it( 'should execute imageUpload command', () => {
  62. const executeStub = sinon.stub( editor, 'execute' );
  63. const button = editor.ui.componentFactory.create( 'insertImage' );
  64. const files = [ createNativeFileMock() ];
  65. button.fire( 'done', files );
  66. sinon.assert.calledOnce( executeStub );
  67. expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
  68. expect( executeStub.firstCall.args[ 1 ].file ).to.equal( files[ 0 ] );
  69. } );
  70. it( 'should optimize the insertion position', () => {
  71. const button = editor.ui.componentFactory.create( 'insertImage' );
  72. const files = [ createNativeFileMock() ];
  73. setModelData( doc, '<paragraph>f[]oo</paragraph>' );
  74. button.fire( 'done', files );
  75. const id = fileRepository.getLoader( files[ 0 ] ).id;
  76. expect( getModelData( doc ) ).to.equal(
  77. `[<image uploadId="${ id }" uploadStatus="reading"></image>]` +
  78. '<paragraph>foo</paragraph>'
  79. );
  80. } );
  81. it( 'should correctly insert multiple files', () => {
  82. const button = editor.ui.componentFactory.create( 'insertImage' );
  83. const files = [ createNativeFileMock(), createNativeFileMock() ];
  84. setModelData( doc, '<paragraph>foo[]</paragraph><paragraph>bar</paragraph>' );
  85. button.fire( 'done', files );
  86. const id1 = fileRepository.getLoader( files[ 0 ] ).id;
  87. const id2 = fileRepository.getLoader( files[ 1 ] ).id;
  88. expect( getModelData( doc ) ).to.equal(
  89. '<paragraph>foo</paragraph>' +
  90. `<image uploadId="${ id1 }" uploadStatus="reading"></image>` +
  91. `[<image uploadId="${ id2 }" uploadStatus="reading"></image>]` +
  92. '<paragraph>bar</paragraph>'
  93. );
  94. } );
  95. it( 'should not execute imageUpload if the file is not an image', () => {
  96. const executeStub = sinon.stub( editor, 'execute' );
  97. const button = editor.ui.componentFactory.create( 'insertImage' );
  98. const file = {
  99. type: 'media/mp3',
  100. size: 1024
  101. };
  102. button.fire( 'done', [ file ] );
  103. sinon.assert.notCalled( executeStub );
  104. } );
  105. it( 'should work even if the FileList does not support iterators', () => {
  106. const executeStub = sinon.stub( editor, 'execute' );
  107. const button = editor.ui.componentFactory.create( 'insertImage' );
  108. const files = {
  109. 0: createNativeFileMock(),
  110. length: 1
  111. };
  112. button.fire( 'done', files );
  113. sinon.assert.calledOnce( executeStub );
  114. expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
  115. expect( executeStub.firstCall.args[ 1 ].file ).to.equal( files[ 0 ] );
  116. } );
  117. } );