浏览代码

Internal: Final adjustments to achieve 100% code coverage.

Marek Lewandowski 6 年之前
父节点
当前提交
a8e5655bde
共有 2 个文件被更改,包括 27 次插入23 次删除
  1. 3 1
      packages/ckeditor5-mention/src/mentionui.js
  2. 24 22
      packages/ckeditor5-mention/tests/mentionui.js

+ 3 - 1
packages/ckeditor5-mention/src/mentionui.js

@@ -536,10 +536,12 @@ function getBalloonPanelPositions( preferredPosition ) {
 
 // Creates a regex for marker.
 //
+// Function has to be exported to achieve 100% code coverage.
+//
 // @param {String} marker
 // @param {Number} minimumCharacters
 // @returns {String}
-function createRegExp( marker, minimumCharacters ) {
+export function createRegExp( marker, minimumCharacters ) {
 	const numberOfCharacters = minimumCharacters == 0 ? '*' : `{${ minimumCharacters },}`;
 	const patternBase = featureDetection.isPunctuationGroupSupported ? '\\p{Ps}\\p{Pi}"\'' : '\\(\\[{"\'';
 

+ 24 - 22
packages/ckeditor5-mention/tests/mentionui.js

@@ -3,7 +3,7 @@
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  */
 
-/* global document, setTimeout, Event */
+/* global window, document, setTimeout, Event */
 
 import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
@@ -18,7 +18,7 @@ import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 import ContextualBalloon from '@ckeditor/ckeditor5-ui/src/panel/balloon/contextualballoon';
 import env from '@ckeditor/ckeditor5-utils/src/env';
 
-import MentionUI from '../src/mentionui';
+import MentionUI, { createRegExp } from '../src/mentionui';
 import featureDetection from '../src/featuredetection';
 import MentionEditing from '../src/mentionediting';
 import MentionsView from '../src/ui/mentionsview';
@@ -444,38 +444,40 @@ describe( 'MentionUI', () => {
 		} );
 
 		describe( 'ES2018 RegExp Unicode property escapes fallback', () => {
-			// Cache the original RegExp to restore it after the tests.
-			const originalPunctationSupport = featureDetection.isPunctuationGroupSupported;
+			let regExpStub;
+
+			// Cache the original value to restore it after the tests.
+			const originalPunctuationSupport = featureDetection.isPunctuationGroupSupported;
 
 			before( () => {
 				featureDetection.isPunctuationGroupSupported = false;
 			} );
 
 			beforeEach( () => {
-				return createClassicTestEditor( staticConfig );
+				return createClassicTestEditor( staticConfig )
+					.then( editor => {
+						regExpStub = sinon.stub( window, 'RegExp' );
+
+						return editor;
+					} );
 			} );
 
 			after( () => {
-				// Restore the original RegExp.
-				featureDetection.isPunctuationGroupSupported = originalPunctationSupport;
+				featureDetection.isPunctuationGroupSupported = originalPunctuationSupport;
 			} );
 
-			it( 'should fallback to simpler RegExp if browser does not support unicode property escapes', () => {
-				setData( model, '<paragraph>[] foo</paragraph>' );
-
-				model.change( writer => {
-					writer.insertText( '〈', doc.selection.getFirstPosition() );
-				} );
-
-				model.change( writer => {
-					writer.insertText( '@', doc.selection.getFirstPosition() );
-				} );
+			it( 'returns a simplified RegExp for browsers not supporting Unicode punctuation groups', () => {
+				featureDetection.isPunctuationGroupSupported = false;
+				createRegExp( '@', 2 );
+				sinon.assert.calledOnce( regExpStub );
+				sinon.assert.calledWithExactly( regExpStub, '(^|[ \\(\\[{"\'])([@])([_a-zA-Z0-9À-ž]{2,}?)$', 'u' );
+			} );
 
-				return waitForDebounce()
-					.then( () => {
-						expect( panelView.isVisible ).to.be.false;
-						expect( editor.model.markers.has( 'mention' ) ).to.be.false;
-					} );
+			it( 'returns a ES2018 RegExp for browsers supporting Unicode punctuation groups', () => {
+				featureDetection.isPunctuationGroupSupported = true;
+				createRegExp( '@', 2 );
+				sinon.assert.calledOnce( regExpStub );
+				sinon.assert.calledWithExactly( regExpStub, '(^|[ \\p{Ps}\\p{Pi}"\'])([@])([_a-zA-Z0-9À-ž]{2,}?)$', 'u' );
 			} );
 		} );