8
0

easyimage.js 3.6 KB

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