| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- /**
- * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
- /**
- * Returns object that mocks native File object.
- */
- export const createNativeFileMock = () => ( {
- type: 'image/jpeg',
- size: 1024
- } );
- /**
- * AdapterMock class.
- * Simulates adapter behaviour without any server-side communications.
- */
- export class UploadAdapterMock {
- constructor( loader ) {
- this.loader = loader;
- }
- /**
- * Starts mocked upload process.
- *
- * @returns {Promise}
- */
- upload() {
- return new Promise( ( resolve, reject ) => {
- this._resolveCallback = resolve;
- this._rejectCallback = reject;
- if ( this.uploadStartedCallback ) {
- this.uploadStartedCallback();
- }
- } );
- }
- /**
- * Aborts reading.
- */
- abort() {
- this._rejectCallback( 'aborted' );
- }
- /**
- * Allows to mock error during file upload.
- *
- * @param { Object } error
- */
- mockError( error ) {
- this._rejectCallback( error );
- }
- /**
- * Allows to mock file upload success.
- *
- * @param { Object } data Mock data returned from server passed to resolved promise.
- */
- mockSuccess( data ) {
- this._resolveCallback( data );
- }
- /**
- * Allows to mock file upload progress.
- *
- * @param {Number} uploaded Bytes uploaded.
- * @param {Number} total Total bytes to upload.
- */
- mockProgress( uploaded, total ) {
- this.loader.uploaded = uploaded;
- this.loader.uploadTotal = total;
- }
- }
- /**
- * NativeFileReaderMock class.
- * Simulates FileReader behaviour.
- */
- export class NativeFileReaderMock {
- /**
- * Mock method used to initialize reading.
- */
- readAsDataURL() {}
- /**
- * Aborts reading process.
- */
- abort() {
- this.onabort();
- }
- /**
- * Allows to mock file reading success.
- * @param {*} result File reading result.
- */
- mockSuccess( result ) {
- this.result = result;
- this.onload();
- }
- /**
- * Allows to mock error during file read.
- *
- * @param { Object } error
- */
- mockError( error ) {
- this.error = error;
- this.onerror();
- }
- /**
- * Allows to mock file upload progress.
- */
- mockProgress( progress ) {
- this.onprogress( { loaded: progress } );
- }
- }
- export class UploadAdapterPluginMock extends Plugin {
- init() {
- const fileRepository = this.editor.plugins.get( 'FileRepository' );
- fileRepository.createUploadAdapter = () => {};
- }
- }
|