filerepository.js 9.9 KB

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