filerepository.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /**
  2. * @license Copyright (c) 2003-2018, 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 Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  8. import PendingActions from '@ckeditor/ckeditor5-core/src/pendingactions';
  9. import FileRepository from '../src/filerepository';
  10. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  11. import { createNativeFileMock, UploadAdapterMock, NativeFileReaderMock } from './_utils/mocks';
  12. import log from '@ckeditor/ckeditor5-utils/src/log';
  13. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  14. import FileReader from '../src/filereader';
  15. describe( 'FileRepository', () => {
  16. let editor, fileRepository, adapterMock;
  17. testUtils.createSinonSandbox();
  18. class UploadAdapterPluginMock extends Plugin {
  19. init() {
  20. fileRepository = this.editor.plugins.get( 'FileRepository' );
  21. fileRepository.createUploadAdapter = loader => {
  22. adapterMock = new UploadAdapterMock( loader );
  23. return adapterMock;
  24. };
  25. }
  26. }
  27. beforeEach( () => {
  28. return VirtualTestEditor
  29. .create( {
  30. plugins: [ FileRepository, UploadAdapterPluginMock ]
  31. } )
  32. .then( newEditor => {
  33. editor = newEditor;
  34. } );
  35. } );
  36. afterEach( () => {
  37. return editor.destroy();
  38. } );
  39. it( 'should be initialized', () => {
  40. expect( fileRepository ).to.be.instanceOf( FileRepository );
  41. } );
  42. describe( 'init()', () => {
  43. it( 'should create loaders collection', () => {
  44. expect( fileRepository.loaders ).to.be.instanceOf( Collection );
  45. } );
  46. it( 'should initialize uploaded observable', done => {
  47. expect( fileRepository.uploaded ).to.equal( 0 );
  48. fileRepository.on( 'change:uploaded', ( evt, name, value ) => {
  49. expect( value ).to.equal( 10 );
  50. done();
  51. } );
  52. fileRepository.uploaded = 10;
  53. } );
  54. it( 'should initialize uploadTotal', done => {
  55. expect( fileRepository.uploadTotal ).to.be.null;
  56. fileRepository.on( 'change:uploadTotal', ( evt, name, value ) => {
  57. expect( value ).to.equal( 10 );
  58. done();
  59. } );
  60. fileRepository.uploadTotal = 10;
  61. } );
  62. it( 'should initialize uploadedPercent', done => {
  63. expect( fileRepository.uploadedPercent ).to.equal( 0 );
  64. fileRepository.on( 'change:uploadedPercent', ( evt, name, value ) => {
  65. expect( value ).to.equal( 20 );
  66. done();
  67. } );
  68. fileRepository.uploadTotal = 200;
  69. fileRepository.uploaded = 40;
  70. } );
  71. } );
  72. describe( 'pending actions', () => {
  73. let pendingActions;
  74. beforeEach( () => {
  75. pendingActions = editor.plugins.get( PendingActions );
  76. } );
  77. it( 'should requires PendingActions plugin', () => {
  78. expect( editor.plugins.get( PendingActions ) ).to.instanceof( PendingActions );
  79. } );
  80. it( 'should add pending action when upload is in progress', () => {
  81. expect( pendingActions ).to.have.property( 'isPending', false );
  82. expect( Array.from( pendingActions ) ).to.length( 0 );
  83. fileRepository.createLoader( createNativeFileMock() );
  84. expect( pendingActions ).to.have.property( 'isPending', true );
  85. expect( Array.from( pendingActions, action => action.message ) ).to.have.members( [ 'Upload in progress 0%.' ] );
  86. } );
  87. it( 'should add only one pending action in a case of multiple load', () => {
  88. fileRepository.createLoader( createNativeFileMock() );
  89. fileRepository.createLoader( createNativeFileMock() );
  90. fileRepository.createLoader( createNativeFileMock() );
  91. expect( Array.from( pendingActions ) ).to.length( 1 );
  92. } );
  93. it( 'should remove pending action when all uploads are finished', () => {
  94. expect( pendingActions ).to.have.property( 'isPending', false );
  95. expect( Array.from( pendingActions ) ).to.length( 0 );
  96. const loader1 = fileRepository.createLoader( createNativeFileMock() );
  97. const loader2 = fileRepository.createLoader( createNativeFileMock() );
  98. expect( pendingActions ).to.have.property( 'isPending', true );
  99. expect( Array.from( pendingActions, action => action.message ) ).to.have.members( [ 'Upload in progress 0%.' ] );
  100. fileRepository.destroyLoader( loader1 );
  101. expect( pendingActions ).to.have.property( 'isPending', true );
  102. expect( Array.from( pendingActions, action => action.message ) ).to.have.members( [ 'Upload in progress 0%.' ] );
  103. fileRepository.destroyLoader( loader2 );
  104. expect( pendingActions ).to.have.property( 'isPending', false );
  105. expect( Array.from( pendingActions ) ).to.length( 0 );
  106. } );
  107. it( 'should bind pending action message to upload percentage value', () => {
  108. fileRepository.createLoader( createNativeFileMock() );
  109. fileRepository.uploadedPercent = 10.345;
  110. expect( Array.from( pendingActions )[ 0 ] ).to.have.property( 'message', 'Upload in progress 10%.' );
  111. fileRepository.uploadedPercent = 30.235;
  112. expect( Array.from( pendingActions )[ 0 ] ).to.have.property( 'message', 'Upload in progress 30%.' );
  113. } );
  114. it( 'should add pending action correctly when one upload is after another', () => {
  115. const loader1 = fileRepository.createLoader( createNativeFileMock() );
  116. expect( pendingActions.first ).to.have.property( 'message', 'Upload in progress 0%.' );
  117. fileRepository.destroyLoader( loader1 );
  118. expect( pendingActions.first ).to.null;
  119. fileRepository.createLoader( createNativeFileMock() );
  120. expect( pendingActions.first ).to.have.property( 'message', 'Upload in progress 0%.' );
  121. } );
  122. } );
  123. describe( 'createLoader()', () => {
  124. it( 'should return null if adapter is not present', () => {
  125. const stub = testUtils.sinon.stub( log, 'error' );
  126. fileRepository.createUploadAdapter = undefined;
  127. expect( fileRepository.createLoader( createNativeFileMock() ) ).to.be.null;
  128. sinon.assert.calledOnce( stub );
  129. sinon.assert.calledWithExactly(
  130. stub,
  131. 'filerepository-no-upload-adapter: Upload adapter is not defined.'
  132. );
  133. } );
  134. it( 'should setup listeners to update progress observables', () => {
  135. const loader1 = fileRepository.createLoader( createNativeFileMock() );
  136. const loader2 = fileRepository.createLoader( createNativeFileMock() );
  137. const loader3 = fileRepository.createLoader( createNativeFileMock() );
  138. loader1.uploaded = 10;
  139. loader1.uploadTotal = 100;
  140. loader2.uploaded = 50;
  141. loader2.uploadTotal = 200;
  142. loader3.uploaded = 40;
  143. loader3.uploadTotal = 200;
  144. expect( fileRepository.uploaded ).to.equal( 100 );
  145. expect( fileRepository.uploadTotal ).to.equal( 500 );
  146. expect( fileRepository.uploadedPercent ).to.equal( 20 );
  147. } );
  148. } );
  149. describe( 'getLoader()', () => {
  150. it( 'should return null if loader does not exists', () => {
  151. const file1 = createNativeFileMock();
  152. const file2 = createNativeFileMock();
  153. fileRepository.createLoader( file2 );
  154. expect( fileRepository.getLoader( file1 ) ).to.be.null;
  155. } );
  156. it( 'should return loader by file instance', () => {
  157. const file = createNativeFileMock();
  158. const loader = fileRepository.createLoader( file );
  159. expect( fileRepository.getLoader( file ) ).to.equal( loader );
  160. } );
  161. } );
  162. describe( 'destroyLoader()', () => {
  163. let file, loader, destroySpy;
  164. beforeEach( () => {
  165. file = createNativeFileMock();
  166. loader = fileRepository.createLoader( file );
  167. destroySpy = testUtils.sinon.spy( loader, '_destroy' );
  168. } );
  169. it( 'should destroy provided loader', () => {
  170. fileRepository.destroyLoader( loader );
  171. sinon.assert.calledOnce( destroySpy );
  172. expect( fileRepository.getLoader( file ) ).to.be.null;
  173. } );
  174. it( 'should destroy loader by provided file', () => {
  175. fileRepository.destroyLoader( file );
  176. sinon.assert.calledOnce( destroySpy );
  177. expect( fileRepository.getLoader( file ) ).to.be.null;
  178. } );
  179. } );
  180. describe( 'Loader', () => {
  181. let loader, file, nativeReaderMock;
  182. beforeEach( () => {
  183. testUtils.sinon.stub( window, 'FileReader' ).callsFake( () => {
  184. nativeReaderMock = new NativeFileReaderMock();
  185. return nativeReaderMock;
  186. } );
  187. file = createNativeFileMock();
  188. loader = fileRepository.createLoader( file );
  189. } );
  190. describe( 'constructor()', () => {
  191. it( 'should initialize id', () => {
  192. expect( loader.id ).to.be.a( 'string' );
  193. } );
  194. it( 'should initialize file', () => {
  195. expect( loader.file ).to.equal( file );
  196. } );
  197. it( 'should initialize adapter', () => {
  198. expect( loader._adapter ).to.equal( adapterMock );
  199. } );
  200. it( 'should initialize reader', () => {
  201. expect( loader._reader ).to.be.instanceOf( FileReader );
  202. } );
  203. it( 'should initialize status observable', done => {
  204. expect( loader.status ).to.equal( 'idle' );
  205. loader.on( 'change:status', ( evt, name, value ) => {
  206. expect( value ).to.equal( 'uploading' );
  207. done();
  208. } );
  209. loader.status = 'uploading';
  210. } );
  211. it( 'should initialize uploaded observable', done => {
  212. expect( loader.uploaded ).to.equal( 0 );
  213. loader.on( 'change:uploaded', ( evt, name, value ) => {
  214. expect( value ).to.equal( 100 );
  215. done();
  216. } );
  217. loader.uploaded = 100;
  218. } );
  219. it( 'should initialize uploadTotal observable', done => {
  220. expect( loader.uploadTotal ).to.equal( null );
  221. loader.on( 'change:uploadTotal', ( evt, name, value ) => {
  222. expect( value ).to.equal( 100 );
  223. done();
  224. } );
  225. loader.uploadTotal = 100;
  226. } );
  227. it( 'should initialize uploadedPercent observable', done => {
  228. expect( loader.uploadedPercent ).to.equal( 0 );
  229. loader.on( 'change:uploadedPercent', ( evt, name, value ) => {
  230. expect( value ).to.equal( 23 );
  231. done();
  232. } );
  233. loader.uploaded = 23;
  234. loader.uploadTotal = 100;
  235. } );
  236. it( 'should initialize uploadResponse observable', done => {
  237. const response = {};
  238. expect( loader.uploadResponse ).to.equal( null );
  239. loader.on( 'change:uploadResponse', ( evt, name, value ) => {
  240. expect( value ).to.equal( response );
  241. done();
  242. } );
  243. loader.uploadResponse = response;
  244. } );
  245. } );
  246. describe( 'read()', () => {
  247. it( 'should throw error when status is defferent than idle', () => {
  248. loader.status = 'uploading';
  249. expect( () => {
  250. loader.read();
  251. } ).to.throw( 'filerepository-read-wrong-status: You cannot call read if the status is different than idle.' );
  252. } );
  253. it( 'should return a promise', () => {
  254. expect( loader.read() ).to.be.instanceof( Promise );
  255. } );
  256. it( 'should set status to "reading"', () => {
  257. loader.read();
  258. expect( loader.status ).to.equal( 'reading' );
  259. } );
  260. it( 'should reject promise when reading is aborted', () => {
  261. const promise = loader.read().catch( e => {
  262. expect( e ).to.equal( 'aborted' );
  263. expect( loader.status ).to.equal( 'aborted' );
  264. } );
  265. loader.abort();
  266. return promise;
  267. } );
  268. it( 'should reject promise on reading error', () => {
  269. const promise = loader.read().catch( e => {
  270. expect( e ).to.equal( 'reading error' );
  271. expect( loader.status ).to.equal( 'error' );
  272. } );
  273. nativeReaderMock.mockError( 'reading error' );
  274. return promise;
  275. } );
  276. it( 'should resolve promise on reading complete', () => {
  277. const promise = loader.read()
  278. .then( data => {
  279. expect( data ).to.equal( 'result data' );
  280. expect( loader.status ).to.equal( 'idle' );
  281. } );
  282. nativeReaderMock.mockSuccess( 'result data' );
  283. return promise;
  284. } );
  285. } );
  286. describe( 'upload()', () => {
  287. it( 'should throw error when status is defferent than idle', () => {
  288. loader.status = 'reading';
  289. expect( () => {
  290. loader.upload();
  291. } ).to.throw( 'filerepository-upload-wrong-status: You cannot call upload if the status is different than idle.' );
  292. } );
  293. it( 'should return a promise', () => {
  294. expect( loader.upload() ).to.be.instanceof( Promise );
  295. } );
  296. it( 'should set status to "uploading"', () => {
  297. loader.upload();
  298. expect( loader.status ).to.equal( 'uploading' );
  299. } );
  300. it( 'should reject promise when uploading is aborted', () => {
  301. const promise = loader.upload().catch( e => {
  302. expect( e ).to.equal( 'aborted' );
  303. expect( loader.status ).to.equal( 'aborted' );
  304. } );
  305. loader.abort();
  306. return promise;
  307. } );
  308. it( 'should reject promise on reading error', () => {
  309. const promise = loader.upload().catch( e => {
  310. expect( e ).to.equal( 'uploading error' );
  311. expect( loader.status ).to.equal( 'error' );
  312. } );
  313. adapterMock.mockError( 'uploading error' );
  314. return promise;
  315. } );
  316. it( 'should resolve promise on reading complete', () => {
  317. const promise = loader.upload()
  318. .then( data => {
  319. expect( data ).to.equal( 'result data' );
  320. expect( loader.status ).to.equal( 'idle' );
  321. } );
  322. adapterMock.mockSuccess( 'result data' );
  323. return promise;
  324. } );
  325. it( 'should monitor upload progress', () => {
  326. const promise = loader.upload()
  327. .then( data => {
  328. expect( data ).to.equal( 'result data' );
  329. expect( loader.status ).to.equal( 'idle' );
  330. } );
  331. expect( loader.uploaded ).to.equal( 0 );
  332. expect( loader.uploadTotal ).to.be.null;
  333. adapterMock.mockProgress( 1, 10 );
  334. expect( loader.uploaded ).to.equal( 1 );
  335. expect( loader.uploadTotal ).to.equal( 10 );
  336. adapterMock.mockProgress( 6, 10 );
  337. expect( loader.uploaded ).to.equal( 6 );
  338. expect( loader.uploadTotal ).to.equal( 10 );
  339. adapterMock.mockSuccess( 'result data' );
  340. return promise;
  341. } );
  342. } );
  343. } );
  344. } );