utils.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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/imageinsert/imageinsertui';
  10. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  11. import Link from '@ckeditor/ckeditor5-link/src/link';
  12. import CKFinder from '@ckeditor/ckeditor5-ckfinder/src/ckfinder';
  13. import { createImageTypeRegExp, prepareIntegrations, createLabeledInputView } from '../../src/imageinsert/utils';
  14. describe( 'Upload utils', () => {
  15. describe( 'createImageTypeRegExp()', () => {
  16. it( 'should return RegExp for testing regular mime type', () => {
  17. expect( createImageTypeRegExp( [ 'png' ] ).test( 'image/png' ) ).to.be.true;
  18. expect( createImageTypeRegExp( [ 'png' ] ).test( 'foo/png' ) ).to.be.false;
  19. expect( createImageTypeRegExp( [ 'png' ] ).test( 'png' ) ).to.be.false;
  20. } );
  21. it( 'should return RegExp for testing mime type with dot', () => {
  22. expect( createImageTypeRegExp( [ 'vnd.microsoft.icon' ] ).test( 'image/vnd.microsoft.icon' ) ).to.be.true;
  23. expect( createImageTypeRegExp( [ 'png' ] ).test( 'foo/vnd.microsoft.icon' ) ).to.be.false;
  24. expect( createImageTypeRegExp( [ 'png' ] ).test( 'vnd.microsoft.icon' ) ).to.be.false;
  25. } );
  26. it( 'should return RegExp for testing mime type with dash', () => {
  27. expect( createImageTypeRegExp( [ 'x-xbitmap' ] ).test( 'image/x-xbitmap' ) ).to.be.true;
  28. expect( createImageTypeRegExp( [ 'png' ] ).test( 'foo/x-xbitmap' ) ).to.be.false;
  29. expect( createImageTypeRegExp( [ 'png' ] ).test( 'x-xbitmap' ) ).to.be.false;
  30. } );
  31. it( 'should return RegExp for testing mime type with plus', () => {
  32. expect( createImageTypeRegExp( [ 'svg+xml' ] ).test( 'image/svg+xml' ) ).to.be.true;
  33. expect( createImageTypeRegExp( [ 'png' ] ).test( 'foo/svg+xml' ) ).to.be.false;
  34. expect( createImageTypeRegExp( [ 'png' ] ).test( 'svg+xml' ) ).to.be.false;
  35. } );
  36. } );
  37. describe( 'prepareIntegrations()', () => {
  38. it( 'should return "insetImageViaUrl" and "openCKFinder" integrations', async () => {
  39. const editorElement = document.createElement( 'div' );
  40. document.body.appendChild( editorElement );
  41. const editor = await ClassicEditor
  42. .create( editorElement, {
  43. plugins: [
  44. CKFinder,
  45. Paragraph,
  46. Image,
  47. ImageUploadUI
  48. ],
  49. image: {
  50. upload: {
  51. panel: {
  52. items: [
  53. 'insertImageViaUrl',
  54. 'openCKFinder'
  55. ]
  56. }
  57. }
  58. }
  59. } );
  60. const openCKFinderExtendedView = Object.values( prepareIntegrations( editor ) )[ 1 ];
  61. expect( openCKFinderExtendedView.class ).contains( 'ck-image-upload__ck-finder-button' );
  62. expect( openCKFinderExtendedView.label ).to.equal( 'Insert image or file' );
  63. expect( openCKFinderExtendedView.withText ).to.be.true;
  64. editor.destroy();
  65. editorElement.remove();
  66. } );
  67. it( 'should return only "insertImageViaUrl" integration and throw warning ' +
  68. 'for "image-upload-integrations-invalid-view" error', async () => {
  69. const editorElement = document.createElement( 'div' );
  70. document.body.appendChild( editorElement );
  71. const editor = await ClassicEditor
  72. .create( editorElement, {
  73. plugins: [
  74. Paragraph,
  75. Image,
  76. ImageUploadUI
  77. ],
  78. image: {
  79. upload: {
  80. panel: {
  81. items: [
  82. 'insertImageViaUrl',
  83. 'openCKFinder'
  84. ]
  85. }
  86. }
  87. }
  88. } );
  89. expect( Object.values( prepareIntegrations( editor ) ).length ).to.equal( 1 );
  90. editor.destroy();
  91. editorElement.remove();
  92. } );
  93. it( 'should return only "link" integration', async () => {
  94. const editorElement = document.createElement( 'div' );
  95. document.body.appendChild( editorElement );
  96. const editor = await ClassicEditor
  97. .create( editorElement, {
  98. plugins: [
  99. Paragraph,
  100. Link,
  101. Image,
  102. ImageUploadUI
  103. ],
  104. image: {
  105. upload: {
  106. panel: {
  107. items: [
  108. 'link'
  109. ]
  110. }
  111. }
  112. }
  113. } );
  114. expect( Object.values( prepareIntegrations( editor ) ).length ).to.equal( 1 );
  115. expect( Object.values( prepareIntegrations( editor ) )[ 0 ].label ).to.equal( 'Link' );
  116. expect( Object.values( prepareIntegrations( editor ) )[ 0 ] ).to.be.instanceOf( ButtonView );
  117. editor.destroy();
  118. editorElement.remove();
  119. } );
  120. it( 'should return "insertImageViaUrl" integration, when no integrations were configured', async () => {
  121. const editorElement = document.createElement( 'div' );
  122. document.body.appendChild( editorElement );
  123. const editor = await ClassicEditor
  124. .create( editorElement, {
  125. plugins: [
  126. Paragraph,
  127. Image,
  128. ImageUploadUI
  129. ]
  130. } );
  131. expect( Object.keys( prepareIntegrations( editor ) ).length ).to.equal( 1 );
  132. editor.destroy();
  133. editorElement.remove();
  134. } );
  135. } );
  136. describe( 'createLabeledInputView()', () => {
  137. describe( 'image URL input view', () => {
  138. it( 'should have placeholder', () => {
  139. const view = createLabeledInputView( { t: val => val } );
  140. expect( view.fieldView.placeholder ).to.equal( 'https://example.com/src/image.png' );
  141. } );
  142. it( 'should have info text', () => {
  143. const view = createLabeledInputView( { t: val => val } );
  144. expect( view.infoText ).to.match( /^Paste the image source URL/ );
  145. } );
  146. } );
  147. } );
  148. } );