8
0

filerepository.js 11 KB

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