/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/* globals document, Event */
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 ImageInsertUI from '../../src/imageinsert/imageinsertui';
import ImageUploadEditing from '../../src/imageinsert/imageinsertediting';
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';
describe( 'ImageInsertUI', () => {
let editor, model, editorElement, fileRepository;
class UploadAdapterPluginMock extends Plugin {
init() {
fileRepository = this.editor.plugins.get( FileRepository );
fileRepository.createUploadAdapter = loader => {
return new UploadAdapterMock( loader );
};
}
}
describe( 'dropdown', () => {
beforeEach( () => {
editorElement = document.createElement( 'div' );
document.body.appendChild( editorElement );
return ClassicEditor
.create( editorElement, {
plugins: [ Paragraph, Image, ImageUploadEditing, ImageInsertUI, FileRepository, UploadAdapterPluginMock, Clipboard ],
image: {
upload: {
panel: {
items: [
'insertImageViaUrl'
]
}
}
}
} )
.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 dropdown', () => {
const button = editor.ui.componentFactory.create( 'imageUpload' );
expect( button ).to.be.instanceOf( DropdownView );
} );
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( 'ImageInsertUI' );
const fileDialogButton = plugin._createFileDialogButtonView( editor.locale );
expect( fileDialogButton.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 dropdown = editor.ui.componentFactory.create( 'imageUpload' );
const command = editor.commands.get( 'imageUpload' );
const spy = sinon.spy();
dropdown.render();
dropdown.buttonView.on( 'execute', spy );
command.isEnabled = false;
dropdown.buttonView.element.dispatchEvent( new Event( 'click' ) );
sinon.assert.notCalled( spy );
} );
it( 'should execute imageUpload command', () => {
const executeStub = sinon.stub( editor, 'execute' );
const plugin = editor.plugins.get( 'ImageInsertUI' );
const fileDialogButton = plugin._createFileDialogButtonView( editor.locale );
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 );
} );
it( 'should execute imageUpload command with multiple files', () => {
const executeStub = sinon.stub( editor, 'execute' );
const plugin = editor.plugins.get( 'ImageInsertUI' );
const fileDialogButton = plugin._createFileDialogButtonView( editor.locale );
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 );
} );
it( 'should optimize the insertion position', () => {
const plugin = editor.plugins.get( 'ImageInsertUI' );
const fileDialogButton = plugin._createFileDialogButtonView( editor.locale );
const files = [ createNativeFileMock() ];
setModelData( model, '
test
' ); 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( 'test
' ); 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( '
