filerepository.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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( 'createLoader()', () => {
  72. it( 'should return null if adapter is not present', () => {
  73. const stub = testUtils.sinon.stub( log, 'error' );
  74. fileRepository.createAdapter = undefined;
  75. expect( fileRepository.createLoader( createNativeFileMock() ) ).to.be.null;
  76. sinon.assert.calledOnce( stub );
  77. sinon.assert.calledWithExactly(
  78. stub,
  79. 'filerepository-no-adapter: Upload adapter is not defined.'
  80. );
  81. } );
  82. it( 'should setup listeners to update progress observables', () => {
  83. const loader1 = fileRepository.createLoader( createNativeFileMock() );
  84. const loader2 = fileRepository.createLoader( createNativeFileMock() );
  85. const loader3 = fileRepository.createLoader( createNativeFileMock() );
  86. loader1.uploaded = 10;
  87. loader1.uploadTotal = 100;
  88. loader2.uploaded = 50;
  89. loader2.uploadTotal = 200;
  90. loader3.uploaded = 40;
  91. loader3.uploadTotal = 200;
  92. expect( fileRepository.uploaded ).to.equal( 100 );
  93. expect( fileRepository.uploadTotal ).to.equal( 500 );
  94. expect( fileRepository.uploadedPercent ).to.equal( 20 );
  95. } );
  96. } );
  97. describe( 'getLoader()', () => {
  98. it( 'should return null if loader does not exists', () => {
  99. const file1 = createNativeFileMock();
  100. const file2 = createNativeFileMock();
  101. fileRepository.createLoader( file2 );
  102. expect( fileRepository.getLoader( file1 ) ).to.be.null;
  103. } );
  104. it( 'should return loader by file instance', () => {
  105. const file = createNativeFileMock();
  106. const loader = fileRepository.createLoader( file );
  107. expect( fileRepository.getLoader( file ) ).to.equal( loader );
  108. } );
  109. } );
  110. describe( 'destroyLoader()', () => {
  111. let file, loader, destroySpy;
  112. beforeEach( () => {
  113. file = createNativeFileMock();
  114. loader = fileRepository.createLoader( file );
  115. destroySpy = testUtils.sinon.spy( loader, '_destroy' );
  116. } );
  117. it( 'should destroy provided loader', () => {
  118. fileRepository.destroyLoader( loader );
  119. sinon.assert.calledOnce( destroySpy );
  120. expect( fileRepository.getLoader( file ) ).to.be.null;
  121. } );
  122. it( 'should destroy loader by provided file', () => {
  123. fileRepository.destroyLoader( file );
  124. sinon.assert.calledOnce( destroySpy );
  125. expect( fileRepository.getLoader( file ) ).to.be.null;
  126. } );
  127. } );
  128. describe( 'Loader', () => {
  129. let loader, file, nativeReaderMock;
  130. beforeEach( () => {
  131. testUtils.sinon.stub( window, 'FileReader' ).callsFake( () => {
  132. nativeReaderMock = new NativeFileReaderMock();
  133. return nativeReaderMock;
  134. } );
  135. file = createNativeFileMock();
  136. loader = fileRepository.createLoader( file );
  137. } );
  138. describe( 'constructor()', () => {
  139. it( 'should initialize id', () => {
  140. expect( loader.id ).to.be.a( 'string' );
  141. } );
  142. it( 'should initialize file', () => {
  143. expect( loader.file ).to.equal( file );
  144. } );
  145. it( 'should initialize adapter', () => {
  146. expect( loader._adapter ).to.equal( adapterMock );
  147. } );
  148. it( 'should initialize reader', () => {
  149. expect( loader._reader ).to.be.instanceOf( FileReader );
  150. } );
  151. it( 'should initialize status observable', done => {
  152. expect( loader.status ).to.equal( 'idle' );
  153. loader.on( 'change:status', ( evt, name, value ) => {
  154. expect( value ).to.equal( 'uploading' );
  155. done();
  156. } );
  157. loader.status = 'uploading';
  158. } );
  159. it( 'should initialize uploaded observable', done => {
  160. expect( loader.uploaded ).to.equal( 0 );
  161. loader.on( 'change:uploaded', ( evt, name, value ) => {
  162. expect( value ).to.equal( 100 );
  163. done();
  164. } );
  165. loader.uploaded = 100;
  166. } );
  167. it( 'should initialize uploadTotal observable', done => {
  168. expect( loader.uploadTotal ).to.equal( null );
  169. loader.on( 'change:uploadTotal', ( evt, name, value ) => {
  170. expect( value ).to.equal( 100 );
  171. done();
  172. } );
  173. loader.uploadTotal = 100;
  174. } );
  175. it( 'should initialize uploadedPercent observable', done => {
  176. expect( loader.uploadedPercent ).to.equal( 0 );
  177. loader.on( 'change:uploadedPercent', ( evt, name, value ) => {
  178. expect( value ).to.equal( 23 );
  179. done();
  180. } );
  181. loader.uploaded = 23;
  182. loader.uploadTotal = 100;
  183. } );
  184. it( 'should initialize uploadResponse observable', done => {
  185. const response = {};
  186. expect( loader.uploadResponse ).to.equal( null );
  187. loader.on( 'change:uploadResponse', ( evt, name, value ) => {
  188. expect( value ).to.equal( response );
  189. done();
  190. } );
  191. loader.uploadResponse = response;
  192. } );
  193. } );
  194. describe( 'read()', () => {
  195. it( 'should throw error when status is defferent than idle', () => {
  196. loader.status = 'uploading';
  197. expect( () => {
  198. loader.read();
  199. } ).to.throw( 'filerepository-read-wrong-status: You cannot call read if the status is different than idle.' );
  200. } );
  201. it( 'should return a promise', () => {
  202. expect( loader.read() ).to.be.instanceof( Promise );
  203. } );
  204. it( 'should set status to "reading"', () => {
  205. loader.read();
  206. expect( loader.status ).to.equal( 'reading' );
  207. } );
  208. it( 'should reject promise when reading is aborted', () => {
  209. const promise = loader.read().catch( e => {
  210. expect( e ).to.equal( 'aborted' );
  211. expect( loader.status ).to.equal( 'aborted' );
  212. } );
  213. loader.abort();
  214. return promise;
  215. } );
  216. it( 'should reject promise on reading error', () => {
  217. const promise = loader.read().catch( e => {
  218. expect( e ).to.equal( 'reading error' );
  219. expect( loader.status ).to.equal( 'error' );
  220. } );
  221. nativeReaderMock.mockError( 'reading error' );
  222. return promise;
  223. } );
  224. it( 'should resolve promise on reading complete', () => {
  225. const promise = loader.read()
  226. .then( data => {
  227. expect( data ).to.equal( 'result data' );
  228. expect( loader.status ).to.equal( 'idle' );
  229. } );
  230. nativeReaderMock.mockSuccess( 'result data' );
  231. return promise;
  232. } );
  233. } );
  234. describe( 'upload()', () => {
  235. it( 'should throw error when status is defferent than idle', () => {
  236. loader.status = 'reading';
  237. expect( () => {
  238. loader.upload();
  239. } ).to.throw( 'filerepository-upload-wrong-status: You cannot call upload if the status is different than idle.' );
  240. } );
  241. it( 'should return a promise', () => {
  242. expect( loader.upload() ).to.be.instanceof( Promise );
  243. } );
  244. it( 'should set status to "uploading"', () => {
  245. loader.upload();
  246. expect( loader.status ).to.equal( 'uploading' );
  247. } );
  248. it( 'should reject promise when uploading is aborted', () => {
  249. const promise = loader.upload().catch( e => {
  250. expect( e ).to.equal( 'aborted' );
  251. expect( loader.status ).to.equal( 'aborted' );
  252. } );
  253. loader.abort();
  254. return promise;
  255. } );
  256. it( 'should reject promise on reading error', () => {
  257. const promise = loader.upload().catch( e => {
  258. expect( e ).to.equal( 'uploading error' );
  259. expect( loader.status ).to.equal( 'error' );
  260. } );
  261. adapterMock.mockError( 'uploading error' );
  262. return promise;
  263. } );
  264. it( 'should resolve promise on reading complete', () => {
  265. const promise = loader.upload()
  266. .then( data => {
  267. expect( data ).to.equal( 'result data' );
  268. expect( loader.status ).to.equal( 'idle' );
  269. } );
  270. adapterMock.mockSuccess( 'result data' );
  271. return promise;
  272. } );
  273. it( 'should monitor upload progress', () => {
  274. const promise = loader.upload()
  275. .then( data => {
  276. expect( data ).to.equal( 'result data' );
  277. expect( loader.status ).to.equal( 'idle' );
  278. } );
  279. expect( loader.uploaded ).to.equal( 0 );
  280. expect( loader.uploadTotal ).to.be.null;
  281. adapterMock.mockProgress( 1, 10 );
  282. expect( loader.uploaded ).to.equal( 1 );
  283. expect( loader.uploadTotal ).to.equal( 10 );
  284. adapterMock.mockProgress( 6, 10 );
  285. expect( loader.uploaded ).to.equal( 6 );
  286. expect( loader.uploadTotal ).to.equal( 10 );
  287. adapterMock.mockSuccess( 'result data' );
  288. return promise;
  289. } );
  290. } );
  291. } );
  292. } );