uploadadapter.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import Image from '@ckeditor/ckeditor5-image/src/image';
  8. import ImageUpload from '@ckeditor/ckeditor5-upload/src/imageupload';
  9. import CKFinderUploadAdapter from '../src/uploadadapter';
  10. import FileRepository from '@ckeditor/ckeditor5-upload/src/filerepository';
  11. import { createNativeFileMock } from '@ckeditor/ckeditor5-upload/tests/_utils/mocks';
  12. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  13. import log from '@ckeditor/ckeditor5-utils/src/log';
  14. describe( 'CKFinderUploadAdapter', () => {
  15. let editor, sinonXHR;
  16. testUtils.createSinonSandbox();
  17. beforeEach( () => {
  18. const editorElement = document.createElement( 'div' );
  19. document.body.appendChild( editorElement );
  20. sinonXHR = testUtils.sinon.useFakeXMLHttpRequest();
  21. return ClassicTestEditor.create( editorElement, {
  22. plugins: [ Image, ImageUpload, CKFinderUploadAdapter ],
  23. ckfinder: {
  24. uploadUrl: 'http://example.com'
  25. }
  26. } )
  27. .then( newEditor => {
  28. editor = newEditor;
  29. } );
  30. } );
  31. afterEach( () => {
  32. sinonXHR.restore();
  33. } );
  34. it( 'should be loaded', () => {
  35. expect( editor.plugins.get( CKFinderUploadAdapter ) ).to.be.instanceOf( CKFinderUploadAdapter );
  36. } );
  37. describe( 'Adapter', () => {
  38. let adapter, loaderMock;
  39. beforeEach( () => {
  40. const file = createNativeFileMock();
  41. file.name = 'image.jpeg';
  42. loaderMock = { file };
  43. adapter = editor.plugins.get( FileRepository ).createAdapter( loaderMock );
  44. } );
  45. it( 'crateAdapter method should be registered and have upload and abort methods', () => {
  46. expect( adapter ).to.be.defined;
  47. expect( adapter.upload ).to.be.function;
  48. expect( adapter.abort ).to.be.function;
  49. } );
  50. it( 'should log warning when there is no configuration', () => {
  51. const editorElement = document.createElement( 'div' );
  52. document.body.appendChild( editorElement );
  53. const warnSub = testUtils.sinon.stub( log, 'warn' );
  54. return ClassicTestEditor.create( editorElement, {
  55. plugins: [ Image, ImageUpload, CKFinderUploadAdapter ],
  56. } )
  57. .then( () => {
  58. sinon.assert.calledOnce( warnSub );
  59. sinon.assert.calledWithExactly(
  60. warnSub,
  61. 'ckfinder-upload-adapter-no-config: Please provide "ckfinder.uploadUrl" config option.'
  62. );
  63. } );
  64. } );
  65. describe( 'upload', () => {
  66. it( 'should return promise', () => {
  67. expect( adapter.upload() ).to.be.instanceof( Promise );
  68. } );
  69. it( 'should call url from config', () => {
  70. let request;
  71. const validResponse = {
  72. uploaded: 1
  73. };
  74. const promise = adapter.upload()
  75. .then( () => {
  76. expect( request.url ).to.equal( 'http://example.com' );
  77. } );
  78. request = sinonXHR.requests[ 0 ];
  79. request.respond( 200, { 'Content-Type': 'application/json' }, JSON.stringify( validResponse ) );
  80. return promise;
  81. } );
  82. it( 'should throw an error on generic request error', () => {
  83. const promise = adapter.upload()
  84. .then( () => {
  85. throw new Error( 'Promise should throw.' );
  86. } )
  87. .catch( msg => {
  88. expect( msg ).to.equal( 'Cannot upload file: image.jpeg.' );
  89. } );
  90. const request = sinonXHR.requests[ 0 ];
  91. request.error();
  92. return promise;
  93. } );
  94. it( 'should throw an error on error from server', () => {
  95. const responseError = {
  96. error: {
  97. message: 'Foo bar baz.'
  98. }
  99. };
  100. const promise = adapter.upload()
  101. .then( () => {
  102. throw new Error( 'Promise should throw.' );
  103. } )
  104. .catch( msg => {
  105. expect( msg ).to.equal( 'Foo bar baz.' );
  106. } );
  107. const request = sinonXHR.requests[ 0 ];
  108. request.respond( 200, { 'Content-Type': 'application/json' }, JSON.stringify( responseError ) );
  109. return promise;
  110. } );
  111. it( 'should throw a generic error on error from server without message', () => {
  112. const responseError = {
  113. error: {}
  114. };
  115. const promise = adapter.upload()
  116. .then( () => {
  117. throw new Error( 'Promise should throw.' );
  118. } )
  119. .catch( msg => {
  120. expect( msg ).to.equal( 'Cannot upload file: image.jpeg.' );
  121. } );
  122. const request = sinonXHR.requests[ 0 ];
  123. request.respond( 200, { 'Content-Type': 'application/json' }, JSON.stringify( responseError ) );
  124. return promise;
  125. } );
  126. it( 'should throw an error on abort', () => {
  127. let request;
  128. const promise = adapter.upload()
  129. .then( () => {
  130. throw new Error( 'Promise should throw.' );
  131. } )
  132. .catch( () => {
  133. expect( request.aborted ).to.be.true;
  134. } );
  135. request = sinonXHR.requests[ 0 ];
  136. adapter.abort();
  137. return promise;
  138. } );
  139. it( 'abort should not throw before upload', () => {
  140. expect( () => {
  141. adapter.abort();
  142. } ).to.not.throw();
  143. } );
  144. it( 'should update progress', () => {
  145. adapter.upload();
  146. const request = sinonXHR.requests[ 0 ];
  147. request.uploadProgress( { loaded: 4, total: 10 } );
  148. expect( loaderMock.uploadTotal ).to.equal( 10 );
  149. expect( loaderMock.uploaded ).to.equal( 4 );
  150. } );
  151. } );
  152. } );
  153. } );