easyimage.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. describe( 'EasyImage', () => {
  15. it( 'should require other plugins', () => {
  16. const plugins = EasyImage.requires;
  17. expect( plugins ).to.include( CloudServicesUploadAdapter );
  18. expect( plugins ).to.include( Image );
  19. expect( plugins ).to.include( ImageUpload );
  20. } );
  21. it( 'should be able to initialize editor with itself', () => {
  22. const div = window.document.createElement( 'div' );
  23. window.document.body.appendChild( div );
  24. return ClassicTestEditor
  25. .create( div, {
  26. plugins: [ EasyImage ]
  27. } )
  28. .then( editor => {
  29. const easyImage = editor.plugins.get( EasyImage );
  30. expect( easyImage ).to.be.an.instanceOf( EasyImage );
  31. window.document.body.removeChild( div );
  32. return editor.destroy();
  33. } );
  34. } );
  35. describe( 'integration tests', () => {
  36. const CSUploader = CloudServicesUploadAdapter._UploadGateway;
  37. const sandbox = sinon.sandbox.create();
  38. let div;
  39. before( () => {
  40. // Mock uploader.
  41. CloudServicesUploadAdapter._UploadGateway = UploadGatewayMock;
  42. sandbox.stub( window, 'FileReader' ).callsFake( () => {
  43. const reader = {
  44. readAsDataURL: () => {
  45. reader.result = 'http://some-fake-url.jpg';
  46. reader.onload();
  47. }
  48. };
  49. return reader;
  50. } );
  51. } );
  52. after( () => {
  53. // Restore original uploader.
  54. CloudServicesUploadAdapter._UploadGateway = CSUploader;
  55. sandbox.restore();
  56. } );
  57. beforeEach( () => {
  58. div = window.document.createElement( 'div' );
  59. window.document.body.appendChild( div );
  60. } );
  61. afterEach( () => {
  62. window.document.body.removeChild( div );
  63. } );
  64. it( 'should enable easy image uploading', () => {
  65. return ClassicTestEditor
  66. .create( div, {
  67. plugins: [
  68. Paragraph, EasyImage
  69. ],
  70. cloudServices: {
  71. token: 'abc',
  72. uploadUrl: 'http://upload.mock.url/'
  73. }
  74. } )
  75. .then( editor => {
  76. const notification = editor.plugins.get( 'Notification' );
  77. const upload = editor.plugins.get( CloudServicesUploadAdapter );
  78. return new Promise( ( resolve, reject ) => {
  79. notification.on( 'show:warning', ( evt, data ) => {
  80. reject( new Error( data.title ) );
  81. } );
  82. editor.document.on( 'change', () => {
  83. // Check whether the image is uploaded and the image's src is replaced correctly.
  84. if ( editor.getData() === '<figure class="image"><img src="http://image.mock.url/"></figure>' ) {
  85. editor.destroy().then( resolve );
  86. }
  87. } );
  88. editor.execute( 'imageUpload', { file: createNativeFileMock() } );
  89. setTimeout( () => {
  90. upload._uploadGateway.resolveLastUpload();
  91. } );
  92. } );
  93. } );
  94. } );
  95. } );
  96. } );