easyimage.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals window, setTimeout */
  6. import EasyImage from '../src/easyimage';
  7. import CloudServicesUploadAdapter from '../src/cloudservicesuploadadapter';
  8. import Image from '@ckeditor/ckeditor5-image/src/image';
  9. import ImageUpload from '@ckeditor/ckeditor5-upload/src/imageupload';
  10. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  11. import UploadGatewayMock from './_utils/uploadgatewaymock';
  12. import { createNativeFileMock } from '@ckeditor/ckeditor5-upload/tests/_utils/mocks';
  13. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  14. import CloudServicesMock from '@ckeditor/ckeditor5-cloudservices/tests/_utils/cloudservicesmock';
  15. const CloudServices = CloudServicesUploadAdapter._CloudServices;
  16. describe( 'EasyImage', () => {
  17. before( () => {
  18. CloudServicesUploadAdapter._CloudServices = CloudServicesMock;
  19. } );
  20. after( () => {
  21. CloudServicesUploadAdapter._CloudServices = CloudServices;
  22. } );
  23. it( 'should require other plugins', () => {
  24. const plugins = EasyImage.requires;
  25. expect( plugins ).to.include( CloudServicesUploadAdapter );
  26. expect( plugins ).to.include( Image );
  27. expect( plugins ).to.include( ImageUpload );
  28. } );
  29. it( 'should be able to initialize editor with itself', () => {
  30. const div = window.document.createElement( 'div' );
  31. window.document.body.appendChild( div );
  32. return ClassicTestEditor
  33. .create( div, {
  34. plugins: [ EasyImage ]
  35. } )
  36. .then( editor => {
  37. const easyImage = editor.plugins.get( EasyImage );
  38. expect( easyImage ).to.be.an.instanceOf( EasyImage );
  39. window.document.body.removeChild( div );
  40. return editor.destroy();
  41. } );
  42. } );
  43. describe( 'integration tests', () => {
  44. const CSUploader = CloudServicesUploadAdapter._UploadGateway;
  45. const sandbox = sinon.sandbox.create();
  46. let div;
  47. before( () => {
  48. // Mock uploader.
  49. CloudServicesUploadAdapter._UploadGateway = UploadGatewayMock;
  50. sandbox.stub( window, 'FileReader' ).callsFake( () => {
  51. const reader = {
  52. readAsDataURL: () => {
  53. reader.result = 'http://some-fake-url.jpg';
  54. reader.onload();
  55. }
  56. };
  57. return reader;
  58. } );
  59. } );
  60. after( () => {
  61. // Restore original uploader.
  62. CloudServicesUploadAdapter._UploadGateway = CSUploader;
  63. sandbox.restore();
  64. } );
  65. beforeEach( () => {
  66. div = window.document.createElement( 'div' );
  67. window.document.body.appendChild( div );
  68. } );
  69. afterEach( () => {
  70. window.document.body.removeChild( div );
  71. } );
  72. it( 'should enable easy image uploading', () => {
  73. return ClassicTestEditor
  74. .create( div, {
  75. plugins: [
  76. Paragraph, EasyImage
  77. ],
  78. cloudServices: {
  79. tokenUrl: 'abc',
  80. uploadUrl: 'http://upload.mock.url/'
  81. }
  82. } )
  83. .then( editor => {
  84. const notification = editor.plugins.get( 'Notification' );
  85. const upload = editor.plugins.get( CloudServicesUploadAdapter );
  86. return new Promise( ( resolve, reject ) => {
  87. notification.on( 'show:warning', ( evt, data ) => {
  88. reject( new Error( data.title ) );
  89. } );
  90. editor.document.on( 'change', () => {
  91. // Check whether the image is uploaded and the image's src is replaced correctly.
  92. if ( editor.getData() === '<figure class="image"><img src="http://image.mock.url/"></figure>' ) {
  93. editor.destroy().then( resolve );
  94. }
  95. } );
  96. editor.execute( 'imageUpload', { file: createNativeFileMock() } );
  97. setTimeout( () => {
  98. upload._uploadGateway.resolveLastUpload();
  99. } );
  100. } );
  101. } );
  102. } );
  103. } );
  104. } );