filereader.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals window */
  6. import FileReader from '../src/filereader';
  7. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  8. describe( 'FileReader', () => {
  9. let reader, fileMock, nativeReaderMock;
  10. testUtils.createSinonSandbox();
  11. beforeEach( () => {
  12. testUtils.sinon.stub( window, 'FileReader', () => {
  13. nativeReaderMock = new NativeReaderMock();
  14. return nativeReaderMock;
  15. } );
  16. fileMock = {
  17. size: 1024
  18. };
  19. reader = new FileReader();
  20. } );
  21. it( 'should initialize loaded property', () => {
  22. expect( reader.loaded ).to.equal( 0 );
  23. } );
  24. it( 'should update loaded property', () => {
  25. nativeReaderMock._mockProgress( 10 );
  26. expect( reader.loaded ).to.equal( 10 );
  27. nativeReaderMock._mockProgress( 20 );
  28. expect( reader.loaded ).to.equal( 20 );
  29. nativeReaderMock._mockProgress( 55 );
  30. expect( reader.loaded ).to.equal( 55 );
  31. } );
  32. describe( 'read', () => {
  33. it( 'should return a promise', () => {
  34. expect( reader.read( fileMock ) ).to.be.instanceOf( Promise );
  35. } );
  36. it( 'should resolve on loading complete', () => {
  37. const promise = reader.read( fileMock )
  38. .then( result => {
  39. expect( result ).to.equal( 'File contents.' );
  40. } );
  41. nativeReaderMock._mockLoading( 'File contents.' );
  42. return promise;
  43. } );
  44. it( 'should reject on loading error', () => {
  45. const promise = reader.read( fileMock )
  46. .then( () => {
  47. throw new Error( 'Reader should not resolve.' );
  48. }, ( status ) => {
  49. expect( status ).to.equal( 'error' );
  50. expect( reader.error ).to.equal( 'Error during file reading.' );
  51. } );
  52. nativeReaderMock._mockError( 'Error during file reading.' );
  53. return promise;
  54. } );
  55. it( 'should reject on loading abort', () => {
  56. const promise = reader.read( fileMock )
  57. .then( () => {
  58. throw new Error( 'Reader should not resolve.' );
  59. }, ( status ) => {
  60. expect( status ).to.equal( 'abort' );
  61. } );
  62. nativeReaderMock._mockAbort();
  63. return promise;
  64. } );
  65. } );
  66. describe( 'abort', () => {
  67. it( 'should allow to abort reading', () => {
  68. const promise = reader.read( fileMock )
  69. .then( () => {
  70. throw new Error( 'Reader should not resolve.' );
  71. }, ( status ) => {
  72. expect( status ).to.equal( 'abort' );
  73. } );
  74. reader.abort();
  75. return promise;
  76. } );
  77. } );
  78. function NativeReaderMock() {
  79. this.readAsDataURL = () => {};
  80. this.abort = () => {
  81. this._mockAbort();
  82. };
  83. this._mockLoading = result => {
  84. this.result = result;
  85. this.onload();
  86. };
  87. this._mockError = error => {
  88. this.error = error;
  89. this.onerror();
  90. };
  91. this._mockAbort = () => {
  92. this.onabort();
  93. };
  94. this._mockProgress = progress => {
  95. this.onprogress( { loaded: progress } );
  96. };
  97. }
  98. } );