imageuploadbutton.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 Image from '@ckeditor/ckeditor5-image/src/image';
  8. import FileDialogButtonView from '../src/ui/filedialogbuttonview';
  9. import FileRepository from '../src/filerepository';
  10. import ImageUploadButton from '../src/imageuploadbutton';
  11. import ImageUploadEngine from '../src/imageuploadengine';
  12. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  13. import Notification from '@ckeditor/ckeditor5-ui/src/notification/notification';
  14. import { createNativeFileMock, AdapterMock } from './_utils/mocks';
  15. import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  16. describe( 'ImageUploadButton', () => {
  17. let editor, doc, editorElement, fileRepository;
  18. beforeEach( () => {
  19. editorElement = document.createElement( 'div' );
  20. document.body.appendChild( editorElement );
  21. return ClassicEditor
  22. .create( editorElement, {
  23. plugins: [ Paragraph, Image, ImageUploadButton, FileRepository ]
  24. } )
  25. .then( newEditor => {
  26. editor = newEditor;
  27. doc = editor.document;
  28. fileRepository = editor.plugins.get( FileRepository );
  29. fileRepository.createAdapter = loader => {
  30. return new AdapterMock( loader );
  31. };
  32. // Hide all notifications (prevent alert() calls).
  33. const notification = editor.plugins.get( Notification );
  34. notification.on( 'show', evt => evt.stop() );
  35. } );
  36. } );
  37. afterEach( () => {
  38. editorElement.remove();
  39. return editor.destroy();
  40. } );
  41. it( 'should include ImageUploadEngine', () => {
  42. expect( editor.plugins.get( ImageUploadEngine ) ).to.be.instanceOf( ImageUploadEngine );
  43. } );
  44. it( 'should register insertImage button', () => {
  45. const button = editor.ui.componentFactory.create( 'insertImage' );
  46. expect( button ).to.be.instanceOf( FileDialogButtonView );
  47. } );
  48. it( 'should be disabled while ImageUploadCommand is disabled', () => {
  49. const button = editor.ui.componentFactory.create( 'insertImage' );
  50. const command = editor.commands.get( 'imageUpload' );
  51. command.isEnabled = true;
  52. expect( button.isEnabled ).to.true;
  53. command.isEnabled = false;
  54. expect( button.isEnabled ).to.false;
  55. } );
  56. it( 'should execute imageUpload command', () => {
  57. const executeStub = sinon.stub( editor, 'execute' );
  58. const button = editor.ui.componentFactory.create( 'insertImage' );
  59. const files = [ createNativeFileMock() ];
  60. button.fire( 'done', files );
  61. sinon.assert.calledOnce( executeStub );
  62. expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
  63. expect( executeStub.firstCall.args[ 1 ].file ).to.equal( files[ 0 ] );
  64. } );
  65. it( 'should optimize the insertion position', () => {
  66. const button = editor.ui.componentFactory.create( 'insertImage' );
  67. const files = [ createNativeFileMock() ];
  68. setModelData( doc, '<paragraph>f[]oo</paragraph>' );
  69. button.fire( 'done', files );
  70. const id = fileRepository.getLoader( files[ 0 ] ).id;
  71. expect( getModelData( doc ) ).to.equal(
  72. `[<image uploadId="${ id }" uploadStatus="reading"></image>]` +
  73. '<paragraph>foo</paragraph>'
  74. );
  75. } );
  76. it( 'should correctly insert multiple files', () => {
  77. const button = editor.ui.componentFactory.create( 'insertImage' );
  78. const files = [ createNativeFileMock(), createNativeFileMock() ];
  79. setModelData( doc, '<paragraph>foo[]</paragraph><paragraph>bar</paragraph>' );
  80. button.fire( 'done', files );
  81. const id1 = fileRepository.getLoader( files[ 0 ] ).id;
  82. const id2 = fileRepository.getLoader( files[ 1 ] ).id;
  83. expect( getModelData( doc ) ).to.equal(
  84. '<paragraph>foo</paragraph>' +
  85. `<image uploadId="${ id1 }" uploadStatus="reading"></image>` +
  86. `[<image uploadId="${ id2 }" uploadStatus="reading"></image>]` +
  87. '<paragraph>bar</paragraph>'
  88. );
  89. } );
  90. it( 'should not execute imageUpload if the file is not an image', () => {
  91. const executeStub = sinon.stub( editor, 'execute' );
  92. const button = editor.ui.componentFactory.create( 'insertImage' );
  93. const file = {
  94. type: 'media/mp3',
  95. size: 1024
  96. };
  97. button.fire( 'done', [ file ] );
  98. sinon.assert.notCalled( executeStub );
  99. } );
  100. it( 'should work even if the FileList does not support iterators', () => {
  101. const executeStub = sinon.stub( editor, 'execute' );
  102. const button = editor.ui.componentFactory.create( 'insertImage' );
  103. const files = {
  104. 0: createNativeFileMock(),
  105. length: 1
  106. };
  107. button.fire( 'done', files );
  108. sinon.assert.calledOnce( executeStub );
  109. expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
  110. expect( executeStub.firstCall.args[ 1 ].file ).to.equal( files[ 0 ] );
  111. } );
  112. } );