mocks.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  6. /**
  7. * Returns object that mocks native File object.
  8. */
  9. export const createNativeFileMock = () => ( {
  10. type: 'image/jpeg',
  11. size: 1024
  12. } );
  13. /**
  14. * AdapterMock class.
  15. * Simulates adapter behaviour without any server-side communications.
  16. */
  17. export class UploadAdapterMock {
  18. constructor( loader ) {
  19. this.loader = loader;
  20. }
  21. /**
  22. * Starts mocked upload process.
  23. *
  24. * @returns {Promise}
  25. */
  26. upload() {
  27. return new Promise( ( resolve, reject ) => {
  28. this._resolveCallback = resolve;
  29. this._rejectCallback = reject;
  30. if ( this.uploadStartedCallback ) {
  31. this.uploadStartedCallback();
  32. }
  33. } );
  34. }
  35. /**
  36. * Aborts reading.
  37. */
  38. abort() {
  39. this._rejectCallback( 'aborted' );
  40. }
  41. /**
  42. * Allows to mock error during file upload.
  43. *
  44. * @param { Object } error
  45. */
  46. mockError( error ) {
  47. this._rejectCallback( error );
  48. }
  49. /**
  50. * Allows to mock file upload success.
  51. *
  52. * @param { Object } data Mock data returned from server passed to resolved promise.
  53. */
  54. mockSuccess( data ) {
  55. this._resolveCallback( data );
  56. }
  57. /**
  58. * Allows to mock file upload progress.
  59. *
  60. * @param {Number} uploaded Bytes uploaded.
  61. * @param {Number} total Total bytes to upload.
  62. */
  63. mockProgress( uploaded, total ) {
  64. this.loader.uploaded = uploaded;
  65. this.loader.uploadTotal = total;
  66. }
  67. }
  68. /**
  69. * NativeFileReaderMock class.
  70. * Simulates FileReader behaviour.
  71. */
  72. export class NativeFileReaderMock {
  73. /**
  74. * Mock method used to initialize reading.
  75. */
  76. readAsDataURL() {}
  77. /**
  78. * Aborts reading process.
  79. */
  80. abort() {
  81. this.onabort();
  82. }
  83. /**
  84. * Allows to mock file reading success.
  85. * @param {*} result File reading result.
  86. */
  87. mockSuccess( result ) {
  88. this.result = result;
  89. this.onload();
  90. }
  91. /**
  92. * Allows to mock error during file read.
  93. *
  94. * @param { Object } error
  95. */
  96. mockError( error ) {
  97. this.error = error;
  98. this.onerror();
  99. }
  100. /**
  101. * Allows to mock file upload progress.
  102. */
  103. mockProgress( progress ) {
  104. this.onprogress( { loaded: progress } );
  105. }
  106. }
  107. export class UploadAdapterPluginMock extends Plugin {
  108. init() {
  109. const fileRepository = this.editor.plugins.get( 'FileRepository' );
  110. fileRepository.createUploadAdapter = () => {};
  111. }
  112. }