utils.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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, createLabeledInputView } 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 "insertImageViaUrl" integration and throw warning' +
  70. 'for "image-upload-integrations-invalid-view" error', async () => {
  71. const editorElement = document.createElement( 'div' );
  72. document.body.appendChild( editorElement );
  73. const editor = await ClassicEditor
  74. .create( editorElement, {
  75. plugins: [
  76. Paragraph,
  77. Image,
  78. ImageUploadEditing,
  79. ImageUploadUI
  80. ],
  81. image: {
  82. upload: {
  83. panel: {
  84. items: [
  85. 'insertImageViaUrl',
  86. 'openCKFinder'
  87. ]
  88. }
  89. }
  90. }
  91. } );
  92. expect( Object.values( prepareIntegrations( editor ) ).length ).to.equal( 1 );
  93. editor.destroy();
  94. editorElement.remove();
  95. } );
  96. it( 'should return only "link" integration', async () => {
  97. const editorElement = document.createElement( 'div' );
  98. document.body.appendChild( editorElement );
  99. const editor = await ClassicEditor
  100. .create( editorElement, {
  101. plugins: [
  102. Paragraph,
  103. Link,
  104. Image,
  105. ImageUploadEditing,
  106. ImageUploadUI
  107. ],
  108. image: {
  109. upload: {
  110. panel: {
  111. items: [
  112. 'link'
  113. ]
  114. }
  115. }
  116. }
  117. } );
  118. expect( Object.values( prepareIntegrations( editor ) ).length ).to.equal( 1 );
  119. expect( Object.values( prepareIntegrations( editor ) )[ 0 ].label ).to.equal( 'Link' );
  120. expect( Object.values( prepareIntegrations( editor ) )[ 0 ] ).to.be.instanceOf( ButtonView );
  121. editor.destroy();
  122. editorElement.remove();
  123. } );
  124. it( 'should return "insertImageViaUrl" integration, when no integrations were configured', async () => {
  125. const editorElement = document.createElement( 'div' );
  126. document.body.appendChild( editorElement );
  127. const editor = await ClassicEditor
  128. .create( editorElement, {
  129. plugins: [
  130. Paragraph,
  131. Image,
  132. ImageUploadEditing,
  133. ImageUploadUI
  134. ]
  135. } );
  136. expect( Object.keys( prepareIntegrations( editor ) ).length ).to.equal( 1 );
  137. editor.destroy();
  138. editorElement.remove();
  139. } );
  140. } );
  141. describe( 'createLabeledInputView()', () => {
  142. describe( 'image URL input view', () => {
  143. it( 'should have placeholder', () => {
  144. const view = createLabeledInputView( { t: val => val } );
  145. expect( view.fieldView.placeholder ).to.equal( 'https://example.com/src/image.png' );
  146. } );
  147. it( 'should have info text', () => {
  148. const view = createLabeledInputView( { t: val => val } );
  149. expect( view.infoText ).to.match( /^Paste the image source URL/ );
  150. } );
  151. } );
  152. } );
  153. } );