8
0

utils.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals document */
  6. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  7. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  8. import Image from '../../src/image';
  9. import ImageUploadUI from '../../src/imageupload/imageuploadui';
  10. import ImageUploadEditing from '../../src/imageupload/imageuploadediting';
  11. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  12. import Link from '@ckeditor/ckeditor5-link/src/link';
  13. import CKFinder from '@ckeditor/ckeditor5-ckfinder/src/ckfinder';
  14. import { createImageTypeRegExp, prepareIntegrations } from '../../src/imageupload/utils';
  15. describe( 'upload utils', () => {
  16. describe( 'createImageTypeRegExp()', () => {
  17. it( 'should return RegExp for testing regular mime type', () => {
  18. expect( createImageTypeRegExp( [ 'png' ] ).test( 'image/png' ) ).to.be.true;
  19. expect( createImageTypeRegExp( [ 'png' ] ).test( 'foo/png' ) ).to.be.false;
  20. expect( createImageTypeRegExp( [ 'png' ] ).test( 'png' ) ).to.be.false;
  21. } );
  22. it( 'should return RegExp for testing mime type with dot', () => {
  23. expect( createImageTypeRegExp( [ 'vnd.microsoft.icon' ] ).test( 'image/vnd.microsoft.icon' ) ).to.be.true;
  24. expect( createImageTypeRegExp( [ 'png' ] ).test( 'foo/vnd.microsoft.icon' ) ).to.be.false;
  25. expect( createImageTypeRegExp( [ 'png' ] ).test( 'vnd.microsoft.icon' ) ).to.be.false;
  26. } );
  27. it( 'should return RegExp for testing mime type with dash', () => {
  28. expect( createImageTypeRegExp( [ 'x-xbitmap' ] ).test( 'image/x-xbitmap' ) ).to.be.true;
  29. expect( createImageTypeRegExp( [ 'png' ] ).test( 'foo/x-xbitmap' ) ).to.be.false;
  30. expect( createImageTypeRegExp( [ 'png' ] ).test( 'x-xbitmap' ) ).to.be.false;
  31. } );
  32. it( 'should return RegExp for testing mime type with plus', () => {
  33. expect( createImageTypeRegExp( [ 'svg+xml' ] ).test( 'image/svg+xml' ) ).to.be.true;
  34. expect( createImageTypeRegExp( [ 'png' ] ).test( 'foo/svg+xml' ) ).to.be.false;
  35. expect( createImageTypeRegExp( [ 'png' ] ).test( 'svg+xml' ) ).to.be.false;
  36. } );
  37. } );
  38. describe( 'prepareIntegrations()', () => {
  39. it( 'should return "insetImageViaUrl" and "openCKFinder" integrations', async () => {
  40. const editorElement = document.createElement( 'div' );
  41. document.body.appendChild( editorElement );
  42. const editor = await ClassicEditor
  43. .create( editorElement, {
  44. plugins: [
  45. CKFinder,
  46. Paragraph,
  47. Image,
  48. ImageUploadEditing,
  49. ImageUploadUI
  50. ],
  51. image: {
  52. upload: {
  53. panel: {
  54. items: [
  55. 'insertImageViaUrl',
  56. 'openCKFinder'
  57. ]
  58. }
  59. }
  60. }
  61. } );
  62. const openCKFinderExtendedView = Object.values( prepareIntegrations( editor ) )[ 1 ];
  63. expect( openCKFinderExtendedView.class ).contains( 'ck-image-upload__ck-finder-button' );
  64. expect( openCKFinderExtendedView.label ).to.equal( 'Insert image or file' );
  65. expect( openCKFinderExtendedView.withText ).to.be.true;
  66. editor.destroy();
  67. editorElement.remove();
  68. } );
  69. it( 'should return only "inserImageViaUrl" integration', async () => {
  70. const editorElement = document.createElement( 'div' );
  71. document.body.appendChild( editorElement );
  72. const editor = await ClassicEditor
  73. .create( editorElement, {
  74. plugins: [
  75. Paragraph,
  76. Image,
  77. ImageUploadEditing,
  78. ImageUploadUI
  79. ],
  80. image: {
  81. upload: {
  82. panel: {
  83. items: [
  84. 'insertImageViaUrl',
  85. 'openCKFinder'
  86. ]
  87. }
  88. }
  89. }
  90. } );
  91. expect( Object.values( prepareIntegrations( editor ) ).length ).to.equal( 1 );
  92. editor.destroy();
  93. editorElement.remove();
  94. } );
  95. it( 'should return only "link" integration', async () => {
  96. const editorElement = document.createElement( 'div' );
  97. document.body.appendChild( editorElement );
  98. const editor = await ClassicEditor
  99. .create( editorElement, {
  100. plugins: [
  101. Paragraph,
  102. Link,
  103. Image,
  104. ImageUploadEditing,
  105. ImageUploadUI
  106. ],
  107. image: {
  108. upload: {
  109. panel: {
  110. items: [
  111. 'link'
  112. ]
  113. }
  114. }
  115. }
  116. } );
  117. expect( Object.values( prepareIntegrations( editor ) ).length ).to.equal( 1 );
  118. expect( Object.values( prepareIntegrations( editor ) )[ 0 ].label ).to.equal( 'Link' );
  119. expect( Object.values( prepareIntegrations( editor ) )[ 0 ] ).to.be.instanceOf( ButtonView );
  120. editor.destroy();
  121. editorElement.remove();
  122. } );
  123. it( 'should not return any integrations, should return "null" instead', async () => {
  124. const editorElement = document.createElement( 'div' );
  125. document.body.appendChild( editorElement );
  126. const editor = await ClassicEditor
  127. .create( editorElement, {
  128. plugins: [
  129. Paragraph,
  130. Image,
  131. ImageUploadEditing,
  132. ImageUploadUI
  133. ]
  134. } );
  135. expect( prepareIntegrations( editor ) ).to.equal( null );
  136. editor.destroy();
  137. editorElement.remove();
  138. } );
  139. } );
  140. } );