filerepository.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  7. import FileRepository from '../src/filerepository';
  8. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  9. import { createNativeFileMock, AdapterMock, NativeFileReaderMock } from './_utils/mocks';
  10. import log from '@ckeditor/ckeditor5-utils/src/log';
  11. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  12. import FileReader from '../src/filereader';
  13. describe( 'FileRepository', () => {
  14. let editor, fileRepository, adapterMock;
  15. testUtils.createSinonSandbox();
  16. beforeEach( () => {
  17. return VirtualTestEditor.create( {
  18. plugins: [ FileRepository ]
  19. } )
  20. .then( newEditor => {
  21. editor = newEditor;
  22. fileRepository = editor.plugins.get( 'fileRepository' );
  23. fileRepository.createAdapter = loader => {
  24. adapterMock = new AdapterMock( loader );
  25. return adapterMock;
  26. };
  27. } );
  28. } );
  29. it( 'should be initialized', () => {
  30. expect( fileRepository ).to.be.instanceOf( FileRepository );
  31. } );
  32. describe( 'init', () => {
  33. it( 'should create loaders collection', () => {
  34. expect( fileRepository.loaders ).to.be.instanceOf( Collection );
  35. } );
  36. it( 'should initialize uploaded observable', ( done ) => {
  37. expect( fileRepository.uploaded ).to.equal( 0 );
  38. fileRepository.on( 'change:uploaded', ( evt, name, value ) => {
  39. expect( value ).to.equal( 10 );
  40. done();
  41. } );
  42. fileRepository.uploaded = 10;
  43. } );
  44. it( 'should initialize uploadTotal', ( done ) => {
  45. expect( fileRepository.uploadTotal ).to.be.null;
  46. fileRepository.on( 'change:uploadTotal', ( evt, name, value ) => {
  47. expect( value ).to.equal( 10 );
  48. done();
  49. } );
  50. fileRepository.uploadTotal = 10;
  51. } );
  52. it( 'should initialize uploadedPercent', ( done ) => {
  53. expect( fileRepository.uploadedPercent ).to.equal( 0 );
  54. fileRepository.on( 'change:uploadedPercent', ( evt, name, value ) => {
  55. expect( value ).to.equal( 20 );
  56. done();
  57. } );
  58. fileRepository.uploadTotal = 200;
  59. fileRepository.uploaded = 40;
  60. } );
  61. } );
  62. describe( 'createLoader', () => {
  63. it( 'should show warning if adapter is not present', () => {
  64. const stub = testUtils.sinon.stub( log, 'warn' );
  65. fileRepository.createAdapter = undefined;
  66. fileRepository.createLoader( createNativeFileMock() );
  67. sinon.assert.calledOnce( stub );
  68. sinon.assert.calledWithExactly( stub, 'FileRepository: no createAdapter method found. Please define it before creating a loader.' );
  69. } );
  70. it( 'should setup listeners to update progress observables', () => {
  71. const loader1 = fileRepository.createLoader( createNativeFileMock() );
  72. const loader2 = fileRepository.createLoader( createNativeFileMock() );
  73. const loader3 = fileRepository.createLoader( createNativeFileMock() );
  74. loader1.uploaded = 10;
  75. loader1.uploadTotal = 100;
  76. loader2.uploaded = 50;
  77. loader2.uploadTotal = 200;
  78. loader3.uploaded = 40;
  79. loader3.uploadTotal = 200;
  80. expect( fileRepository.uploaded ).to.equal( 100 );
  81. expect( fileRepository.uploadTotal ).to.equal( 500 );
  82. expect( fileRepository.uploadedPercent ).to.equal( 20 );
  83. } );
  84. } );
  85. describe( 'getLoader', () => {
  86. it( 'should return null if loader does not exists', () => {
  87. const file1 = createNativeFileMock();
  88. const file2 = createNativeFileMock();
  89. fileRepository.createLoader( file2 );
  90. expect( fileRepository.getLoader( file1 ) ).to.be.null;
  91. } );
  92. it( 'should return loader by file instance', () => {
  93. const file = createNativeFileMock();
  94. const loader = fileRepository.createLoader( file );
  95. expect( fileRepository.getLoader( file ) ).to.equal( loader );
  96. } );
  97. } );
  98. describe( 'destroyLoader', () => {
  99. let file, loader, destroySpy;
  100. beforeEach( () => {
  101. file = createNativeFileMock();
  102. loader = fileRepository.createLoader( file );
  103. destroySpy = testUtils.sinon.spy( loader, '_destroy' );
  104. } );
  105. it( 'should destroy provided loader', () => {
  106. fileRepository.destroyLoader( loader );
  107. sinon.assert.calledOnce( destroySpy );
  108. expect( fileRepository.getLoader( file ) ).to.be.null;
  109. } );
  110. it( 'should destroy loader by provided file', () => {
  111. fileRepository.destroyLoader( file );
  112. sinon.assert.calledOnce( destroySpy );
  113. expect( fileRepository.getLoader( file ) ).to.be.null;
  114. } );
  115. } );
  116. describe( 'Loader', () => {
  117. let loader, file, nativeReaderMock;
  118. beforeEach( () => {
  119. testUtils.sinon.stub( window, 'FileReader', () => {
  120. nativeReaderMock = new NativeFileReaderMock();
  121. return nativeReaderMock;
  122. } );
  123. file = createNativeFileMock();
  124. loader = fileRepository.createLoader( file );
  125. } );
  126. describe( 'constructor', () => {
  127. it( 'should initialize id', () => {
  128. expect( loader.id ).to.be.number;
  129. } );
  130. it( 'should initialize file', () => {
  131. expect( loader.file ).to.equal( file );
  132. } );
  133. it( 'should initialize adapter', () => {
  134. expect( loader._adapter ).to.equal( adapterMock );
  135. } );
  136. it( 'should initialize reader', () => {
  137. expect( loader._reader ).to.be.instanceOf( FileReader );
  138. } );
  139. it( 'should initialize status observable', ( done ) => {
  140. expect( loader.status ).to.equal( 'idle' );
  141. loader.on( 'change:status', ( evt, name, value ) => {
  142. expect( value ).to.equal( 'uploading' );
  143. done();
  144. } );
  145. loader.status = 'uploading';
  146. } );
  147. it( 'should initialize uploaded observable', ( done ) => {
  148. expect( loader.uploaded ).to.equal( 0 );
  149. loader.on( 'change:uploaded', ( evt, name, value ) => {
  150. expect( value ).to.equal( 100 );
  151. done();
  152. } );
  153. loader.uploaded = 100;
  154. } );
  155. it( 'should initialize uploadTotal observable', ( done ) => {
  156. expect( loader.uploadTotal ).to.equal( null );
  157. loader.on( 'change:uploadTotal', ( evt, name, value ) => {
  158. expect( value ).to.equal( 100 );
  159. done();
  160. } );
  161. loader.uploadTotal = 100;
  162. } );
  163. it( 'should initialize uploadedPercent observable', ( done ) => {
  164. expect( loader.uploadedPercent ).to.equal( 0 );
  165. loader.on( 'change:uploadedPercent', ( evt, name, value ) => {
  166. expect( value ).to.equal( 23 );
  167. done();
  168. } );
  169. loader.uploaded = 23;
  170. loader.uploadTotal = 100;
  171. } );
  172. it( 'should initialize uploadResponse observable', ( done ) => {
  173. expect( loader.uploadResponse ).to.equal( null );
  174. loader.on( 'change:uploadResponse', ( evt, name, value ) => {
  175. expect( value ).to.equal( response );
  176. done();
  177. } );
  178. const response = {};
  179. loader.uploadResponse = response;
  180. } );
  181. } );
  182. describe( 'read', () => {
  183. it( 'should throw error when status is defferent than idle', () => {
  184. loader.status = 'uploading';
  185. expect( () => {
  186. loader.read();
  187. } ).to.throw( 'filerepository-read-wrong-status: You cannot call read if the status is different than idle.' );
  188. } );
  189. it( 'should return a promise', () => {
  190. expect( loader.read() ).to.be.instanceof( Promise );
  191. } );
  192. it( 'should set status to "reading"', () => {
  193. loader.read();
  194. expect( loader.status ).to.equal( 'reading' );
  195. } );
  196. it( 'should reject promise when reading is aborted', () => {
  197. const promise = loader.read().catch( e => {
  198. expect( e ).to.equal( 'aborted' );
  199. expect( loader.status ).to.equal( 'aborted' );
  200. } );
  201. loader.abort();
  202. return promise;
  203. } );
  204. it( 'should reject promise on reading error', () => {
  205. const promise = loader.read().catch( e => {
  206. expect( e ).to.equal( 'reading error' );
  207. expect( loader.status ).to.equal( 'error' );
  208. } );
  209. nativeReaderMock.mockError( 'reading error' );
  210. return promise;
  211. } );
  212. it( 'should resolve promise on reading complete', () => {
  213. const promise = loader.read()
  214. .then( data => {
  215. expect( data ).to.equal( 'result data' );
  216. expect( loader.status ).to.equal( 'idle' );
  217. } );
  218. nativeReaderMock.mockSuccess( 'result data' );
  219. return promise;
  220. } );
  221. } );
  222. describe( 'upload', () => {
  223. it( 'should throw error when status is defferent than idle', () => {
  224. loader.status = 'reading';
  225. expect( () => {
  226. loader.upload();
  227. } ).to.throw( 'filerepository-upload-wrong-status: You cannot call upload if the status is different than idle.' );
  228. } );
  229. it( 'should return a promise', () => {
  230. expect( loader.upload() ).to.be.instanceof( Promise );
  231. } );
  232. it( 'should set status to "uploading"', () => {
  233. loader.upload();
  234. expect( loader.status ).to.equal( 'uploading' );
  235. } );
  236. it( 'should reject promise when uploading is aborted', () => {
  237. const promise = loader.upload().catch( e => {
  238. expect( e ).to.equal( 'aborted' );
  239. expect( loader.status ).to.equal( 'aborted' );
  240. } );
  241. loader.abort();
  242. return promise;
  243. } );
  244. it( 'should reject promise on reading error', () => {
  245. const promise = loader.upload().catch( e => {
  246. expect( e ).to.equal( 'uploading error' );
  247. expect( loader.status ).to.equal( 'error' );
  248. } );
  249. adapterMock.mockError( 'uploading error' );
  250. return promise;
  251. } );
  252. it( 'should resolve promise on reading complete', () => {
  253. const promise = loader.upload()
  254. .then( data => {
  255. expect( data ).to.equal( 'result data' );
  256. expect( loader.status ).to.equal( 'idle' );
  257. } );
  258. adapterMock.mockSuccess( 'result data' );
  259. return promise;
  260. } );
  261. it( 'should monitor upload progress', () => {
  262. const promise = loader.upload()
  263. .then( data => {
  264. expect( data ).to.equal( 'result data' );
  265. expect( loader.status ).to.equal( 'idle' );
  266. } );
  267. expect( loader.uploaded ).to.equal( 0 );
  268. expect( loader.uploadTotal ).to.be.null;
  269. adapterMock.mockProgress( 1, 10 );
  270. expect( loader.uploaded ).to.equal( 1 );
  271. expect( loader.uploadTotal ).to.equal( 10 );
  272. adapterMock.mockProgress( 6, 10 );
  273. expect( loader.uploaded ).to.equal( 6 );
  274. expect( loader.uploadTotal ).to.equal( 10 );
  275. adapterMock.mockSuccess( 'result data' );
  276. return promise;
  277. } );
  278. } );
  279. } );
  280. } );