easyimage.js 3.2 KB

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