|
|
@@ -9,7 +9,6 @@ import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor'
|
|
|
|
|
|
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
|
|
|
import Image from '../../src/image';
|
|
|
-import DropdownView from '@ckeditor/ckeditor5-ui/src/dropdown/dropdownview';
|
|
|
import FileDialogButtonView from '@ckeditor/ckeditor5-upload/src/ui/filedialogbuttonview';
|
|
|
import FileRepository from '@ckeditor/ckeditor5-upload/src/filerepository';
|
|
|
import ImageUploadUI from '../../src/imageupload/imageuploadui';
|
|
|
@@ -17,11 +16,6 @@ import ImageUploadEditing from '../../src/imageupload/imageuploadediting';
|
|
|
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
|
|
|
import Notification from '@ckeditor/ckeditor5-ui/src/notification/notification';
|
|
|
import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
|
|
|
-import EventInfo from '@ckeditor/ckeditor5-utils/src/eventinfo';
|
|
|
-import DomEventData from '@ckeditor/ckeditor5-engine/src/view/observer/domeventdata';
|
|
|
-import CKFinder from '@ckeditor/ckeditor5-ckfinder/src/ckfinder';
|
|
|
-import LabeledFieldView from '@ckeditor/ckeditor5-ui/src/labeledfield/labeledfieldview';
|
|
|
-import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
|
|
|
|
|
|
import { createNativeFileMock, UploadAdapterMock } from '@ckeditor/ckeditor5-upload/tests/_utils/mocks';
|
|
|
import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
|
|
|
@@ -38,578 +32,154 @@ describe( 'ImageUploadUI', () => {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- describe( 'file dialog button', () => {
|
|
|
- beforeEach( () => {
|
|
|
- editorElement = document.createElement( 'div' );
|
|
|
- document.body.appendChild( editorElement );
|
|
|
-
|
|
|
- return ClassicEditor
|
|
|
- .create( editorElement, {
|
|
|
- plugins: [ Paragraph, Image, ImageUploadEditing, ImageUploadUI, FileRepository, UploadAdapterPluginMock, Clipboard ]
|
|
|
- } )
|
|
|
- .then( newEditor => {
|
|
|
- editor = newEditor;
|
|
|
- model = editor.model;
|
|
|
-
|
|
|
- // Hide all notifications (prevent alert() calls).
|
|
|
- const notification = editor.plugins.get( Notification );
|
|
|
- notification.on( 'show', evt => evt.stop() );
|
|
|
- } );
|
|
|
- } );
|
|
|
-
|
|
|
- afterEach( () => {
|
|
|
- editorElement.remove();
|
|
|
-
|
|
|
- return editor.destroy();
|
|
|
- } );
|
|
|
- it( 'should register imageUpload file dialog button', () => {
|
|
|
- const button = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
-
|
|
|
- expect( button ).to.be.instanceOf( FileDialogButtonView );
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'should set proper accepted mime-types for imageUpload button as defined in configuration', () => {
|
|
|
- editor.config.set( 'image.upload.types', [ 'svg+xml', 'jpeg', 'vnd.microsoft.icon', 'x-xbitmap' ] );
|
|
|
-
|
|
|
- const button = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
-
|
|
|
- expect( button.acceptedType ).to.equal( 'image/svg+xml,image/jpeg,image/vnd.microsoft.icon,image/x-xbitmap' );
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'should be disabled while ImageUploadCommand is disabled', () => {
|
|
|
- const button = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
- const command = editor.commands.get( 'imageUpload' );
|
|
|
-
|
|
|
- command.isEnabled = true;
|
|
|
-
|
|
|
- expect( button.buttonView.isEnabled ).to.true;
|
|
|
-
|
|
|
- command.isEnabled = false;
|
|
|
-
|
|
|
- expect( button.buttonView.isEnabled ).to.false;
|
|
|
- } );
|
|
|
-
|
|
|
- // ckeditor5-upload/#77
|
|
|
- it( 'should be properly bound with ImageUploadCommand', () => {
|
|
|
- const button = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
- const command = editor.commands.get( 'imageUpload' );
|
|
|
- const spy = sinon.spy();
|
|
|
-
|
|
|
- button.render();
|
|
|
-
|
|
|
- button.buttonView.on( 'execute', spy );
|
|
|
-
|
|
|
- command.isEnabled = false;
|
|
|
-
|
|
|
- button.buttonView.element.dispatchEvent( new Event( 'click' ) );
|
|
|
-
|
|
|
- sinon.assert.notCalled( spy );
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'should execute imageUpload command', () => {
|
|
|
- const executeStub = sinon.stub( editor, 'execute' );
|
|
|
- const button = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
- const files = [ createNativeFileMock() ];
|
|
|
-
|
|
|
- button.fire( 'done', files );
|
|
|
- sinon.assert.calledOnce( executeStub );
|
|
|
- expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
|
|
|
- expect( executeStub.firstCall.args[ 1 ].file ).to.deep.equal( files );
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'should execute imageUpload command with multiple files', () => {
|
|
|
- const executeStub = sinon.stub( editor, 'execute' );
|
|
|
- const button = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
- const files = [ createNativeFileMock(), createNativeFileMock(), createNativeFileMock() ];
|
|
|
-
|
|
|
- button.fire( 'done', files );
|
|
|
- sinon.assert.calledOnce( executeStub );
|
|
|
- expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
|
|
|
- expect( executeStub.firstCall.args[ 1 ].file ).to.deep.equal( files );
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'should optimize the insertion position', () => {
|
|
|
- const button = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
- const files = [ createNativeFileMock() ];
|
|
|
-
|
|
|
- setModelData( model, '<paragraph>f[]oo</paragraph>' );
|
|
|
-
|
|
|
- button.fire( 'done', files );
|
|
|
-
|
|
|
- const id = fileRepository.getLoader( files[ 0 ] ).id;
|
|
|
-
|
|
|
- expect( getModelData( model ) ).to.equal(
|
|
|
- `[<image uploadId="${ id }" uploadStatus="reading"></image>]` +
|
|
|
- '<paragraph>foo</paragraph>'
|
|
|
- );
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'should correctly insert multiple files', () => {
|
|
|
- const button = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
- const files = [ createNativeFileMock(), createNativeFileMock() ];
|
|
|
-
|
|
|
- setModelData( model, '<paragraph>foo[]</paragraph><paragraph>bar</paragraph>' );
|
|
|
-
|
|
|
- button.fire( 'done', files );
|
|
|
-
|
|
|
- const id1 = fileRepository.getLoader( files[ 0 ] ).id;
|
|
|
- const id2 = fileRepository.getLoader( files[ 1 ] ).id;
|
|
|
-
|
|
|
- expect( getModelData( model ) ).to.equal(
|
|
|
- '<paragraph>foo</paragraph>' +
|
|
|
- `<image uploadId="${ id1 }" uploadStatus="reading"></image>` +
|
|
|
- `[<image uploadId="${ id2 }" uploadStatus="reading"></image>]` +
|
|
|
- '<paragraph>bar</paragraph>'
|
|
|
- );
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'should not execute imageUpload if the file is not an image', () => {
|
|
|
- const executeStub = sinon.stub( editor, 'execute' );
|
|
|
- const button = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
- const file = {
|
|
|
- type: 'media/mp3',
|
|
|
- size: 1024
|
|
|
- };
|
|
|
-
|
|
|
- button.fire( 'done', [ file ] );
|
|
|
- sinon.assert.notCalled( executeStub );
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'should work even if the FileList does not support iterators', () => {
|
|
|
- const executeStub = sinon.stub( editor, 'execute' );
|
|
|
- const button = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
- const files = {
|
|
|
- 0: createNativeFileMock(),
|
|
|
- length: 1
|
|
|
- };
|
|
|
-
|
|
|
- button.fire( 'done', files );
|
|
|
- sinon.assert.calledOnce( executeStub );
|
|
|
- expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
|
|
|
- expect( executeStub.firstCall.args[ 1 ].file ).to.deep.equal( [ files[ 0 ] ] );
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'should add the new image after the selected one, without replacing the selected image', () => {
|
|
|
- const button = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
- const files = [ createNativeFileMock() ];
|
|
|
-
|
|
|
- setModelData( model, '[<image src="/assets/sample.png"></image>]<paragraph>bar</paragraph>' );
|
|
|
-
|
|
|
- button.fire( 'done', files );
|
|
|
-
|
|
|
- const id1 = fileRepository.getLoader( files[ 0 ] ).id;
|
|
|
-
|
|
|
- expect( getModelData( model ) ).to.equal(
|
|
|
- '<image src="/assets/sample.png"></image>' +
|
|
|
- `[<image uploadId="${ id1 }" uploadStatus="reading"></image>]` +
|
|
|
- '<paragraph>bar</paragraph>'
|
|
|
- );
|
|
|
- } );
|
|
|
+ beforeEach( () => {
|
|
|
+ editorElement = document.createElement( 'div' );
|
|
|
+ document.body.appendChild( editorElement );
|
|
|
+
|
|
|
+ return ClassicEditor
|
|
|
+ .create( editorElement, {
|
|
|
+ plugins: [ Paragraph, Image, ImageUploadEditing, ImageUploadUI, FileRepository, UploadAdapterPluginMock, Clipboard ]
|
|
|
+ } )
|
|
|
+ .then( newEditor => {
|
|
|
+ editor = newEditor;
|
|
|
+ model = editor.model;
|
|
|
+
|
|
|
+ // Hide all notifications (prevent alert() calls).
|
|
|
+ const notification = editor.plugins.get( Notification );
|
|
|
+ notification.on( 'show', evt => evt.stop() );
|
|
|
+ } );
|
|
|
} );
|
|
|
|
|
|
- describe( 'dropdown', () => {
|
|
|
- beforeEach( () => {
|
|
|
- editorElement = document.createElement( 'div' );
|
|
|
- document.body.appendChild( editorElement );
|
|
|
-
|
|
|
- return ClassicEditor
|
|
|
- .create( editorElement, {
|
|
|
- plugins: [ Paragraph, Image, ImageUploadEditing, ImageUploadUI, FileRepository, UploadAdapterPluginMock, Clipboard ],
|
|
|
- image: {
|
|
|
- upload: {
|
|
|
- panel: {
|
|
|
- items: [
|
|
|
- 'insertImageViaUrl'
|
|
|
- ]
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- } )
|
|
|
- .then( newEditor => {
|
|
|
- editor = newEditor;
|
|
|
- model = editor.model;
|
|
|
+ afterEach( () => {
|
|
|
+ editorElement.remove();
|
|
|
|
|
|
- // Hide all notifications (prevent alert() calls).
|
|
|
- const notification = editor.plugins.get( Notification );
|
|
|
- notification.on( 'show', evt => evt.stop() );
|
|
|
- } );
|
|
|
- } );
|
|
|
-
|
|
|
- afterEach( () => {
|
|
|
- editorElement.remove();
|
|
|
+ return editor.destroy();
|
|
|
+ } );
|
|
|
|
|
|
- return editor.destroy();
|
|
|
- } );
|
|
|
- it( 'should register imageUpload dropdown', () => {
|
|
|
- const button = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
+ it( 'should register imageUpload button', () => {
|
|
|
+ const button = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
|
|
|
- expect( button ).to.be.instanceOf( DropdownView );
|
|
|
- } );
|
|
|
+ expect( button ).to.be.instanceOf( FileDialogButtonView );
|
|
|
+ } );
|
|
|
|
|
|
- it( 'should set proper accepted mime-types for imageUpload button as defined in configuration', () => {
|
|
|
- editor.config.set( 'image.upload.types', [ 'svg+xml', 'jpeg', 'vnd.microsoft.icon', 'x-xbitmap' ] );
|
|
|
+ it( 'should set proper accepted mime-types for imageUpload button as defined in configuration', () => {
|
|
|
+ editor.config.set( 'image.upload.types', [ 'svg+xml', 'jpeg', 'vnd.microsoft.icon', 'x-xbitmap' ] );
|
|
|
|
|
|
- const plugin = editor.plugins.get( 'ImageUploadUI' );
|
|
|
- const fileDialogButton = plugin._createFileDialogButtonView( editor.locale );
|
|
|
+ const button = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
|
|
|
- expect( fileDialogButton.acceptedType ).to.equal( 'image/svg+xml,image/jpeg,image/vnd.microsoft.icon,image/x-xbitmap' );
|
|
|
- } );
|
|
|
+ expect( button.acceptedType ).to.equal( 'image/svg+xml,image/jpeg,image/vnd.microsoft.icon,image/x-xbitmap' );
|
|
|
+ } );
|
|
|
|
|
|
- it( 'should be disabled while ImageUploadCommand is disabled', () => {
|
|
|
- const button = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
- const command = editor.commands.get( 'imageUpload' );
|
|
|
+ it( 'should be disabled while ImageUploadCommand is disabled', () => {
|
|
|
+ const button = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
+ const command = editor.commands.get( 'imageUpload' );
|
|
|
|
|
|
- command.isEnabled = true;
|
|
|
+ command.isEnabled = true;
|
|
|
|
|
|
- expect( button.buttonView.isEnabled ).to.true;
|
|
|
+ expect( button.buttonView.isEnabled ).to.true;
|
|
|
|
|
|
- command.isEnabled = false;
|
|
|
+ command.isEnabled = false;
|
|
|
|
|
|
- expect( button.buttonView.isEnabled ).to.false;
|
|
|
- } );
|
|
|
+ expect( button.buttonView.isEnabled ).to.false;
|
|
|
+ } );
|
|
|
|
|
|
- // ckeditor5-upload/#77
|
|
|
- it( 'should be properly bound with ImageUploadCommand', () => {
|
|
|
- const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
- const command = editor.commands.get( 'imageUpload' );
|
|
|
- const spy = sinon.spy();
|
|
|
+ // ckeditor5-upload/#77
|
|
|
+ it( 'should be properly bound with ImageUploadCommand', () => {
|
|
|
+ const button = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
+ const command = editor.commands.get( 'imageUpload' );
|
|
|
+ const spy = sinon.spy();
|
|
|
|
|
|
- dropdown.render();
|
|
|
+ button.render();
|
|
|
|
|
|
- dropdown.buttonView.on( 'execute', spy );
|
|
|
+ button.buttonView.on( 'execute', spy );
|
|
|
|
|
|
- command.isEnabled = false;
|
|
|
+ command.isEnabled = false;
|
|
|
|
|
|
- dropdown.buttonView.element.dispatchEvent( new Event( 'click' ) );
|
|
|
+ button.buttonView.element.dispatchEvent( new Event( 'click' ) );
|
|
|
|
|
|
- sinon.assert.notCalled( spy );
|
|
|
- } );
|
|
|
+ sinon.assert.notCalled( spy );
|
|
|
+ } );
|
|
|
|
|
|
- it( 'should execute imageUpload command', () => {
|
|
|
- const executeStub = sinon.stub( editor, 'execute' );
|
|
|
- const plugin = editor.plugins.get( 'ImageUploadUI' );
|
|
|
- const fileDialogButton = plugin._createFileDialogButtonView( editor.locale );
|
|
|
- const files = [ createNativeFileMock() ];
|
|
|
+ it( 'should execute imageUpload command', () => {
|
|
|
+ const executeStub = sinon.stub( editor, 'execute' );
|
|
|
+ const button = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
+ const files = [ createNativeFileMock() ];
|
|
|
|
|
|
- fileDialogButton.fire( 'done', files );
|
|
|
- sinon.assert.calledOnce( executeStub );
|
|
|
- expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
|
|
|
- expect( executeStub.firstCall.args[ 1 ].file ).to.deep.equal( files );
|
|
|
- } );
|
|
|
+ button.fire( 'done', files );
|
|
|
+ sinon.assert.calledOnce( executeStub );
|
|
|
+ expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
|
|
|
+ expect( executeStub.firstCall.args[ 1 ].file ).to.deep.equal( files );
|
|
|
+ } );
|
|
|
|
|
|
- it( 'should execute imageUpload command with multiple files', () => {
|
|
|
- const executeStub = sinon.stub( editor, 'execute' );
|
|
|
- const plugin = editor.plugins.get( 'ImageUploadUI' );
|
|
|
- const fileDialogButton = plugin._createFileDialogButtonView( editor.locale );
|
|
|
- const files = [ createNativeFileMock(), createNativeFileMock(), createNativeFileMock() ];
|
|
|
+ it( 'should execute imageUpload command with multiple files', () => {
|
|
|
+ const executeStub = sinon.stub( editor, 'execute' );
|
|
|
+ const button = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
+ const files = [ createNativeFileMock(), createNativeFileMock(), createNativeFileMock() ];
|
|
|
|
|
|
- fileDialogButton.fire( 'done', files );
|
|
|
- sinon.assert.calledOnce( executeStub );
|
|
|
- expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
|
|
|
- expect( executeStub.firstCall.args[ 1 ].file ).to.deep.equal( files );
|
|
|
- } );
|
|
|
+ button.fire( 'done', files );
|
|
|
+ sinon.assert.calledOnce( executeStub );
|
|
|
+ expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
|
|
|
+ expect( executeStub.firstCall.args[ 1 ].file ).to.deep.equal( files );
|
|
|
+ } );
|
|
|
|
|
|
- it( 'should optimize the insertion position', () => {
|
|
|
- const plugin = editor.plugins.get( 'ImageUploadUI' );
|
|
|
- const fileDialogButton = plugin._createFileDialogButtonView( editor.locale );
|
|
|
- const files = [ createNativeFileMock() ];
|
|
|
+ it( 'should optimize the insertion position', () => {
|
|
|
+ const button = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
+ const files = [ createNativeFileMock() ];
|
|
|
|
|
|
- setModelData( model, '<paragraph>f[]oo</paragraph>' );
|
|
|
+ setModelData( model, '<paragraph>f[]oo</paragraph>' );
|
|
|
|
|
|
- fileDialogButton.fire( 'done', files );
|
|
|
+ button.fire( 'done', files );
|
|
|
|
|
|
- const id = fileRepository.getLoader( files[ 0 ] ).id;
|
|
|
+ const id = fileRepository.getLoader( files[ 0 ] ).id;
|
|
|
|
|
|
- expect( getModelData( model ) ).to.equal(
|
|
|
- `[<image uploadId="${ id }" uploadStatus="reading"></image>]` +
|
|
|
+ expect( getModelData( model ) ).to.equal(
|
|
|
+ `[<image uploadId="${ id }" uploadStatus="reading"></image>]` +
|
|
|
'<paragraph>foo</paragraph>'
|
|
|
- );
|
|
|
- } );
|
|
|
+ );
|
|
|
+ } );
|
|
|
|
|
|
- it( 'should correctly insert multiple files', () => {
|
|
|
- const plugin = editor.plugins.get( 'ImageUploadUI' );
|
|
|
- const fileDialogButton = plugin._createFileDialogButtonView( editor.locale );
|
|
|
- const files = [ createNativeFileMock(), createNativeFileMock() ];
|
|
|
+ it( 'should correctly insert multiple files', () => {
|
|
|
+ const button = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
+ const files = [ createNativeFileMock(), createNativeFileMock() ];
|
|
|
|
|
|
- setModelData( model, '<paragraph>foo[]</paragraph><paragraph>bar</paragraph>' );
|
|
|
+ setModelData( model, '<paragraph>foo[]</paragraph><paragraph>bar</paragraph>' );
|
|
|
|
|
|
- fileDialogButton.fire( 'done', files );
|
|
|
+ button.fire( 'done', files );
|
|
|
|
|
|
- const id1 = fileRepository.getLoader( files[ 0 ] ).id;
|
|
|
- const id2 = fileRepository.getLoader( files[ 1 ] ).id;
|
|
|
+ const id1 = fileRepository.getLoader( files[ 0 ] ).id;
|
|
|
+ const id2 = fileRepository.getLoader( files[ 1 ] ).id;
|
|
|
|
|
|
- expect( getModelData( model ) ).to.equal(
|
|
|
- '<paragraph>foo</paragraph>' +
|
|
|
+ expect( getModelData( model ) ).to.equal(
|
|
|
+ '<paragraph>foo</paragraph>' +
|
|
|
`<image uploadId="${ id1 }" uploadStatus="reading"></image>` +
|
|
|
`[<image uploadId="${ id2 }" uploadStatus="reading"></image>]` +
|
|
|
'<paragraph>bar</paragraph>'
|
|
|
- );
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'should not execute imageUpload if the file is not an image', () => {
|
|
|
- const executeStub = sinon.stub( editor, 'execute' );
|
|
|
- const plugin = editor.plugins.get( 'ImageUploadUI' );
|
|
|
- const fileDialogButton = plugin._createFileDialogButtonView( editor.locale );
|
|
|
- const file = {
|
|
|
- type: 'media/mp3',
|
|
|
- size: 1024
|
|
|
- };
|
|
|
-
|
|
|
- fileDialogButton.fire( 'done', [ file ] );
|
|
|
- sinon.assert.notCalled( executeStub );
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'should work even if the FileList does not support iterators', () => {
|
|
|
- const executeStub = sinon.stub( editor, 'execute' );
|
|
|
- const plugin = editor.plugins.get( 'ImageUploadUI' );
|
|
|
- const fileDialogButton = plugin._createFileDialogButtonView( editor.locale );
|
|
|
- const files = {
|
|
|
- 0: createNativeFileMock(),
|
|
|
- length: 1
|
|
|
- };
|
|
|
-
|
|
|
- fileDialogButton.fire( 'done', files );
|
|
|
- sinon.assert.calledOnce( executeStub );
|
|
|
- expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
|
|
|
- expect( executeStub.firstCall.args[ 1 ].file ).to.deep.equal( [ files[ 0 ] ] );
|
|
|
- } );
|
|
|
-
|
|
|
- describe( 'dropdown action button', () => {
|
|
|
- it( 'should be an instance of FileDialogButtonView', () => {
|
|
|
- const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
-
|
|
|
- expect( dropdown.buttonView.actionView ).to.be.instanceOf( FileDialogButtonView );
|
|
|
- } );
|
|
|
- } );
|
|
|
-
|
|
|
- describe( 'dropdown panel buttons', () => {
|
|
|
- it( 'should have "Update" label on submit button when URL input is already filled', () => {
|
|
|
- const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
- const viewDocument = editor.editing.view.document;
|
|
|
-
|
|
|
- editor.setData( '<figure><img src="/assets/sample.png" /></figure>' );
|
|
|
-
|
|
|
- editor.editing.view.change( writer => {
|
|
|
- writer.setSelection( viewDocument.getRoot().getChild( 0 ), 'on' );
|
|
|
- } );
|
|
|
-
|
|
|
- const img = viewDocument.selection.getSelectedElement();
|
|
|
-
|
|
|
- const data = fakeEventData();
|
|
|
- const eventInfo = new EventInfo( img, 'click' );
|
|
|
- const domEventDataMock = new DomEventData( viewDocument, eventInfo, data );
|
|
|
-
|
|
|
- viewDocument.fire( 'click', domEventDataMock );
|
|
|
-
|
|
|
- dropdown.isOpen = true;
|
|
|
-
|
|
|
- const inputValue = dropdown.panelView.children.first.imageURLInputValue;
|
|
|
-
|
|
|
- expect( dropdown.isOpen ).to.be.true;
|
|
|
- expect( inputValue ).to.equal( '/assets/sample.png' );
|
|
|
- expect( dropdown.panelView.children.first.insertButtonView.label ).to.equal( 'Update' );
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'should have "Insert" label on submit button on uploading a new image', () => {
|
|
|
- const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
- const viewDocument = editor.editing.view.document;
|
|
|
-
|
|
|
- editor.setData( '<p>test</p>' );
|
|
|
-
|
|
|
- editor.editing.view.change( writer => {
|
|
|
- writer.setSelection( viewDocument.getRoot().getChild( 0 ), 'end' );
|
|
|
- } );
|
|
|
-
|
|
|
- const el = viewDocument.selection.getSelectedElement();
|
|
|
-
|
|
|
- const data = fakeEventData();
|
|
|
- const eventInfo = new EventInfo( el, 'click' );
|
|
|
- const domEventDataMock = new DomEventData( viewDocument, eventInfo, data );
|
|
|
-
|
|
|
- viewDocument.fire( 'click', domEventDataMock );
|
|
|
-
|
|
|
- dropdown.isOpen = true;
|
|
|
-
|
|
|
- const inputValue = dropdown.panelView.children.first.imageURLInputValue;
|
|
|
-
|
|
|
- expect( dropdown.isOpen ).to.be.true;
|
|
|
- expect( inputValue ).to.equal( '' );
|
|
|
- expect( dropdown.panelView.children.first.insertButtonView.label ).to.equal( 'Insert' );
|
|
|
- } );
|
|
|
- } );
|
|
|
-
|
|
|
- describe( 'dropdown panel integrations', () => {
|
|
|
- describe( 'insert image via URL form', () => {
|
|
|
- it( 'should have "Insert image via URL" label on inserting new image', () => {
|
|
|
- const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
- const viewDocument = editor.editing.view.document;
|
|
|
-
|
|
|
- editor.setData( '<p>test</p>' );
|
|
|
-
|
|
|
- editor.editing.view.change( writer => {
|
|
|
- writer.setSelection( viewDocument.getRoot().getChild( 0 ), 'end' );
|
|
|
- } );
|
|
|
-
|
|
|
- const el = viewDocument.selection.getSelectedElement();
|
|
|
-
|
|
|
- const data = fakeEventData();
|
|
|
- const eventInfo = new EventInfo( el, 'click' );
|
|
|
- const domEventDataMock = new DomEventData( viewDocument, eventInfo, data );
|
|
|
-
|
|
|
- viewDocument.fire( 'click', domEventDataMock );
|
|
|
-
|
|
|
- dropdown.isOpen = true;
|
|
|
-
|
|
|
- const inputValue = dropdown.panelView.children.first.imageURLInputValue;
|
|
|
-
|
|
|
- const insertImageViaUrlForm = dropdown.panelView.children.first.getIntegration( 'insertImageViaUrl' );
|
|
|
-
|
|
|
- expect( dropdown.isOpen ).to.be.true;
|
|
|
- expect( inputValue ).to.equal( '' );
|
|
|
- expect( insertImageViaUrlForm.label ).to.equal( 'Insert image via URL' );
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'should have "Update image URL" label on updating the image source URL', () => {
|
|
|
- const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
- const viewDocument = editor.editing.view.document;
|
|
|
-
|
|
|
- editor.setData( '<figure><img src="/assets/sample.png" /></figure>' );
|
|
|
-
|
|
|
- editor.editing.view.change( writer => {
|
|
|
- writer.setSelection( viewDocument.getRoot().getChild( 0 ), 'on' );
|
|
|
- } );
|
|
|
-
|
|
|
- const el = viewDocument.selection.getSelectedElement();
|
|
|
-
|
|
|
- const data = fakeEventData();
|
|
|
- const eventInfo = new EventInfo( el, 'click' );
|
|
|
- const domEventDataMock = new DomEventData( viewDocument, eventInfo, data );
|
|
|
-
|
|
|
- viewDocument.fire( 'click', domEventDataMock );
|
|
|
-
|
|
|
- dropdown.isOpen = true;
|
|
|
-
|
|
|
- const inputValue = dropdown.panelView.children.first.imageURLInputValue;
|
|
|
- const insertImageViaUrlForm = dropdown.panelView.children.first.getIntegration( 'insertImageViaUrl' );
|
|
|
-
|
|
|
- expect( dropdown.isOpen ).to.be.true;
|
|
|
- expect( inputValue ).to.equal( '/assets/sample.png' );
|
|
|
- expect( insertImageViaUrlForm.label ).to.equal( 'Update image URL' );
|
|
|
- } );
|
|
|
- } );
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'should remove all attributes from model except "src" when updating the image source URL', () => {
|
|
|
- const viewDocument = editor.editing.view.document;
|
|
|
- const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
- const insertButtonView = dropdown.panelView.children.first.insertButtonView;
|
|
|
- const commandSpy = sinon.spy( editor.commands.get( 'imageInsert' ), 'execute' );
|
|
|
- const submitSpy = sinon.spy();
|
|
|
-
|
|
|
- dropdown.isOpen = true;
|
|
|
-
|
|
|
- editor.setData( '<figure><img src="image-url-800w.jpg"' +
|
|
|
- 'srcset="image-url-480w.jpg 480w,image-url-800w.jpg 800w"' +
|
|
|
- 'sizes="(max-width: 600px) 480px,800px"' +
|
|
|
- 'alt="test-image"></figure>' );
|
|
|
-
|
|
|
- editor.editing.view.change( writer => {
|
|
|
- writer.setSelection( viewDocument.getRoot().getChild( 0 ), 'on' );
|
|
|
- } );
|
|
|
-
|
|
|
- const selectedElement = editor.model.document.selection.getSelectedElement();
|
|
|
-
|
|
|
- expect( selectedElement.getAttribute( 'src' ) ).to.equal( 'image-url-800w.jpg' );
|
|
|
- expect( selectedElement.hasAttribute( 'srcset' ) ).to.be.true;
|
|
|
-
|
|
|
- dropdown.panelView.children.first.imageURLInputValue = '/assets/sample3.png';
|
|
|
-
|
|
|
- dropdown.on( 'submit', submitSpy );
|
|
|
-
|
|
|
- insertButtonView.fire( 'execute' );
|
|
|
-
|
|
|
- sinon.assert.notCalled( commandSpy );
|
|
|
- sinon.assert.calledOnce( submitSpy );
|
|
|
- expect( dropdown.isOpen ).to.be.false;
|
|
|
- expect( selectedElement.getAttribute( 'src' ) ).to.equal( '/assets/sample3.png' );
|
|
|
- expect( selectedElement.hasAttribute( 'srcset' ) ).to.be.false;
|
|
|
- expect( selectedElement.hasAttribute( 'sizes' ) ).to.be.false;
|
|
|
- } );
|
|
|
-
|
|
|
- describe( 'events', () => {
|
|
|
- it( 'should emit "submit" event when clicking on submit button', () => {
|
|
|
- const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
- const insertButtonView = dropdown.panelView.children.first.insertButtonView;
|
|
|
- const commandSpy = sinon.spy( editor.commands.get( 'imageInsert' ), 'execute' );
|
|
|
- const submitSpy = sinon.spy();
|
|
|
-
|
|
|
- dropdown.isOpen = true;
|
|
|
-
|
|
|
- dropdown.on( 'submit', submitSpy );
|
|
|
-
|
|
|
- insertButtonView.fire( 'execute' );
|
|
|
-
|
|
|
- expect( dropdown.isOpen ).to.be.false;
|
|
|
- sinon.assert.calledOnce( commandSpy );
|
|
|
- sinon.assert.calledOnce( submitSpy );
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'should emit "cancel" event when clicking on cancel button', () => {
|
|
|
- const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
- const cancelButtonView = dropdown.panelView.children.first.cancelButtonView;
|
|
|
- const commandSpy = sinon.spy( editor.commands.get( 'imageInsert' ), 'execute' );
|
|
|
- const cancelSpy = sinon.spy();
|
|
|
-
|
|
|
- dropdown.isOpen = true;
|
|
|
+ );
|
|
|
+ } );
|
|
|
|
|
|
- dropdown.on( 'cancel', cancelSpy );
|
|
|
+ it( 'should not execute imageUpload if the file is not an image', () => {
|
|
|
+ const executeStub = sinon.stub( editor, 'execute' );
|
|
|
+ const button = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
+ const file = {
|
|
|
+ type: 'media/mp3',
|
|
|
+ size: 1024
|
|
|
+ };
|
|
|
|
|
|
- cancelButtonView.fire( 'execute' );
|
|
|
+ button.fire( 'done', [ file ] );
|
|
|
+ sinon.assert.notCalled( executeStub );
|
|
|
+ } );
|
|
|
|
|
|
- expect( dropdown.isOpen ).to.be.false;
|
|
|
- sinon.assert.notCalled( commandSpy );
|
|
|
- sinon.assert.calledOnce( cancelSpy );
|
|
|
- } );
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'should inject integrations to the dropdown panel view from the config', async () => {
|
|
|
- const editor = await ClassicEditor
|
|
|
- .create( editorElement, {
|
|
|
- plugins: [
|
|
|
- CKFinder,
|
|
|
- Paragraph,
|
|
|
- Image,
|
|
|
- ImageUploadEditing,
|
|
|
- ImageUploadUI,
|
|
|
- FileRepository,
|
|
|
- UploadAdapterPluginMock,
|
|
|
- Clipboard
|
|
|
- ],
|
|
|
- image: {
|
|
|
- upload: {
|
|
|
- panel: {
|
|
|
- items: [
|
|
|
- 'insertImageViaUrl',
|
|
|
- 'openCKFinder'
|
|
|
- ]
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- } );
|
|
|
-
|
|
|
- const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
-
|
|
|
- expect( dropdown.panelView.children.first._integrations.length ).to.equal( 2 );
|
|
|
- expect( dropdown.panelView.children.first._integrations.first ).to.be.instanceOf( LabeledFieldView );
|
|
|
- expect( dropdown.panelView.children.first._integrations.last ).to.be.instanceOf( ButtonView );
|
|
|
-
|
|
|
- editor.destroy();
|
|
|
- } );
|
|
|
+ it( 'should work even if the FileList does not support iterators', () => {
|
|
|
+ const executeStub = sinon.stub( editor, 'execute' );
|
|
|
+ const button = editor.ui.componentFactory.create( 'imageUpload' );
|
|
|
+ const files = {
|
|
|
+ 0: createNativeFileMock(),
|
|
|
+ length: 1
|
|
|
+ };
|
|
|
+
|
|
|
+ button.fire( 'done', files );
|
|
|
+ sinon.assert.calledOnce( executeStub );
|
|
|
+ expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
|
|
|
+ expect( executeStub.firstCall.args[ 1 ].file ).to.deep.equal( [ files[ 0 ] ] );
|
|
|
} );
|
|
|
} );
|
|
|
-
|
|
|
-function fakeEventData() {
|
|
|
- return {
|
|
|
- preventDefault: sinon.spy()
|
|
|
- };
|
|
|
-}
|