filerepository.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 ai 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. } );
  115. describe( 'createLoader()', () => {
  116. it( 'should return null if adapter is not present', () => {
  117. const stub = testUtils.sinon.stub( log, 'error' );
  118. fileRepository.createUploadAdapter = undefined;
  119. expect( fileRepository.createLoader( createNativeFileMock() ) ).to.be.null;
  120. sinon.assert.calledOnce( stub );
  121. sinon.assert.calledWithExactly(
  122. stub,
  123. 'filerepository-no-upload-adapter: Upload adapter is not defined.'
  124. );
  125. } );
  126. it( 'should setup listeners to update progress observables', () => {
  127. const loader1 = fileRepository.createLoader( createNativeFileMock() );
  128. const loader2 = fileRepository.createLoader( createNativeFileMock() );
  129. const loader3 = fileRepository.createLoader( createNativeFileMock() );
  130. loader1.uploaded = 10;
  131. loader1.uploadTotal = 100;
  132. loader2.uploaded = 50;
  133. loader2.uploadTotal = 200;
  134. loader3.uploaded = 40;
  135. loader3.uploadTotal = 200;
  136. expect( fileRepository.uploaded ).to.equal( 100 );
  137. expect( fileRepository.uploadTotal ).to.equal( 500 );
  138. expect( fileRepository.uploadedPercent ).to.equal( 20 );
  139. } );
  140. } );
  141. describe( 'getLoader()', () => {
  142. it( 'should return null if loader does not exists', () => {
  143. const file1 = createNativeFileMock();
  144. const file2 = createNativeFileMock();
  145. fileRepository.createLoader( file2 );
  146. expect( fileRepository.getLoader( file1 ) ).to.be.null;
  147. } );
  148. it( 'should return loader by file instance', () => {
  149. const file = createNativeFileMock();
  150. const loader = fileRepository.createLoader( file );
  151. expect( fileRepository.getLoader( file ) ).to.equal( loader );
  152. } );
  153. } );
  154. describe( 'destroyLoader()', () => {
  155. let file, loader, destroySpy;
  156. beforeEach( () => {
  157. file = createNativeFileMock();
  158. loader = fileRepository.createLoader( file );
  159. destroySpy = testUtils.sinon.spy( loader, '_destroy' );
  160. } );
  161. it( 'should destroy provided loader', () => {
  162. fileRepository.destroyLoader( loader );
  163. sinon.assert.calledOnce( destroySpy );
  164. expect( fileRepository.getLoader( file ) ).to.be.null;
  165. } );
  166. it( 'should destroy loader by provided file', () => {
  167. fileRepository.destroyLoader( file );
  168. sinon.assert.calledOnce( destroySpy );
  169. expect( fileRepository.getLoader( file ) ).to.be.null;
  170. } );
  171. } );
  172. describe( 'Loader', () => {
  173. let loader, file, nativeReaderMock;
  174. beforeEach( () => {
  175. testUtils.sinon.stub( window, 'FileReader' ).callsFake( () => {
  176. nativeReaderMock = new NativeFileReaderMock();
  177. return nativeReaderMock;
  178. } );
  179. file = createNativeFileMock();
  180. loader = fileRepository.createLoader( file );
  181. } );
  182. describe( 'constructor()', () => {
  183. it( 'should initialize id', () => {
  184. expect( loader.id ).to.be.a( 'string' );
  185. } );
  186. it( 'should initialize file', () => {
  187. expect( loader.file ).to.equal( file );
  188. } );
  189. it( 'should initialize adapter', () => {
  190. expect( loader._adapter ).to.equal( adapterMock );
  191. } );
  192. it( 'should initialize reader', () => {
  193. expect( loader._reader ).to.be.instanceOf( FileReader );
  194. } );
  195. it( 'should initialize status observable', done => {
  196. expect( loader.status ).to.equal( 'idle' );
  197. loader.on( 'change:status', ( evt, name, value ) => {
  198. expect( value ).to.equal( 'uploading' );
  199. done();
  200. } );
  201. loader.status = 'uploading';
  202. } );
  203. it( 'should initialize uploaded observable', done => {
  204. expect( loader.uploaded ).to.equal( 0 );
  205. loader.on( 'change:uploaded', ( evt, name, value ) => {
  206. expect( value ).to.equal( 100 );
  207. done();
  208. } );
  209. loader.uploaded = 100;
  210. } );
  211. it( 'should initialize uploadTotal observable', done => {
  212. expect( loader.uploadTotal ).to.equal( null );
  213. loader.on( 'change:uploadTotal', ( evt, name, value ) => {
  214. expect( value ).to.equal( 100 );
  215. done();
  216. } );
  217. loader.uploadTotal = 100;
  218. } );
  219. it( 'should initialize uploadedPercent observable', done => {
  220. expect( loader.uploadedPercent ).to.equal( 0 );
  221. loader.on( 'change:uploadedPercent', ( evt, name, value ) => {
  222. expect( value ).to.equal( 23 );
  223. done();
  224. } );
  225. loader.uploaded = 23;
  226. loader.uploadTotal = 100;
  227. } );
  228. it( 'should initialize uploadResponse observable', done => {
  229. const response = {};
  230. expect( loader.uploadResponse ).to.equal( null );
  231. loader.on( 'change:uploadResponse', ( evt, name, value ) => {
  232. expect( value ).to.equal( response );
  233. done();
  234. } );
  235. loader.uploadResponse = response;
  236. } );
  237. } );
  238. describe( 'read()', () => {
  239. it( 'should throw error when status is defferent than idle', () => {
  240. loader.status = 'uploading';
  241. expect( () => {
  242. loader.read();
  243. } ).to.throw( 'filerepository-read-wrong-status: You cannot call read if the status is different than idle.' );
  244. } );
  245. it( 'should return a promise', () => {
  246. expect( loader.read() ).to.be.instanceof( Promise );
  247. } );
  248. it( 'should set status to "reading"', () => {
  249. loader.read();
  250. expect( loader.status ).to.equal( 'reading' );
  251. } );
  252. it( 'should reject promise when reading is aborted', () => {
  253. const promise = loader.read().catch( e => {
  254. expect( e ).to.equal( 'aborted' );
  255. expect( loader.status ).to.equal( 'aborted' );
  256. } );
  257. loader.abort();
  258. return promise;
  259. } );
  260. it( 'should reject promise on reading error', () => {
  261. const promise = loader.read().catch( e => {
  262. expect( e ).to.equal( 'reading error' );
  263. expect( loader.status ).to.equal( 'error' );
  264. } );
  265. nativeReaderMock.mockError( 'reading error' );
  266. return promise;
  267. } );
  268. it( 'should resolve promise on reading complete', () => {
  269. const promise = loader.read()
  270. .then( data => {
  271. expect( data ).to.equal( 'result data' );
  272. expect( loader.status ).to.equal( 'idle' );
  273. } );
  274. nativeReaderMock.mockSuccess( 'result data' );
  275. return promise;
  276. } );
  277. } );
  278. describe( 'upload()', () => {
  279. it( 'should throw error when status is defferent than idle', () => {
  280. loader.status = 'reading';
  281. expect( () => {
  282. loader.upload();
  283. } ).to.throw( 'filerepository-upload-wrong-status: You cannot call upload if the status is different than idle.' );
  284. } );
  285. it( 'should return a promise', () => {
  286. expect( loader.upload() ).to.be.instanceof( Promise );
  287. } );
  288. it( 'should set status to "uploading"', () => {
  289. loader.upload();
  290. expect( loader.status ).to.equal( 'uploading' );
  291. } );
  292. it( 'should reject promise when uploading is aborted', () => {
  293. const promise = loader.upload().catch( e => {
  294. expect( e ).to.equal( 'aborted' );
  295. expect( loader.status ).to.equal( 'aborted' );
  296. } );
  297. loader.abort();
  298. return promise;
  299. } );
  300. it( 'should reject promise on reading error', () => {
  301. const promise = loader.upload().catch( e => {
  302. expect( e ).to.equal( 'uploading error' );
  303. expect( loader.status ).to.equal( 'error' );
  304. } );
  305. adapterMock.mockError( 'uploading error' );
  306. return promise;
  307. } );
  308. it( 'should resolve promise on reading complete', () => {
  309. const promise = loader.upload()
  310. .then( data => {
  311. expect( data ).to.equal( 'result data' );
  312. expect( loader.status ).to.equal( 'idle' );
  313. } );
  314. adapterMock.mockSuccess( 'result data' );
  315. return promise;
  316. } );
  317. it( 'should monitor upload progress', () => {
  318. const promise = loader.upload()
  319. .then( data => {
  320. expect( data ).to.equal( 'result data' );
  321. expect( loader.status ).to.equal( 'idle' );
  322. } );
  323. expect( loader.uploaded ).to.equal( 0 );
  324. expect( loader.uploadTotal ).to.be.null;
  325. adapterMock.mockProgress( 1, 10 );
  326. expect( loader.uploaded ).to.equal( 1 );
  327. expect( loader.uploadTotal ).to.equal( 10 );
  328. adapterMock.mockProgress( 6, 10 );
  329. expect( loader.uploaded ).to.equal( 6 );
  330. expect( loader.uploadTotal ).to.equal( 10 );
  331. adapterMock.mockSuccess( 'result data' );
  332. return promise;
  333. } );
  334. } );
  335. } );
  336. } );