mocks.js 2.1 KB

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