Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Added tests for emoji input.
Browse files Browse the repository at this point in the history
  • Loading branch information
niegowski committed Mar 25, 2020
1 parent cd4fa3e commit c7b4f17
Showing 1 changed file with 92 additions and 1 deletion.
93 changes: 92 additions & 1 deletion tests/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
import ShiftEnter from '@ckeditor/ckeditor5-enter/src/shiftenter';
import Input from '../src/input';
import TextTransformation from '../src/texttransformation';

import Writer from '@ckeditor/ckeditor5-engine/src/model/writer';

Expand Down Expand Up @@ -39,7 +40,19 @@ describe( 'Input feature', () => {
domElement = document.createElement( 'div' );
document.body.appendChild( domElement );

return ClassicTestEditor.create( domElement, { plugins: [ Input, Paragraph, Bold, Italic, List, ShiftEnter, Link ] } )
const options = {
plugins: [ Input, Paragraph, Bold, Italic, List, ShiftEnter, Link, TextTransformation ],
typing: {
transformations: {
extra: [
{ from: ':)', to: '🙂' },
{ from: ':family:', to: '👨‍👩‍👧' }
]
}
}
};

return ClassicTestEditor.create( domElement, options )
.then( newEditor => {
// Mock image feature.
newEditor.model.schema.register( 'image', { allowWhere: '$text' } );
Expand Down Expand Up @@ -702,6 +715,84 @@ describe( 'Input feature', () => {

expect( getViewData( view ) ).to.equal( '<p>Foox{}<strong> </strong> Bar</p>' );
} );

describe( 'emoji', () => {
it( 'should properly handle input after multi-byte emoji transformation (in short paragraph)', () => {
testEmojiMutations( 'foo', ':)123456', '🙂123456' );
} );

it( 'should properly handle input after multi-byte emoji transformation (in long paragraph)', () => {
testEmojiMutations( createTestString( 200 ), ':)123456', '🙂123456' );
} );

it( 'should properly handle input in text with multi-byte emoji (in short paragraph)', () => {
testEmojiMutations( 'foo😱bar', '123456' );
} );

it( 'should properly handle input in text with multi-byte emoji (in long paragraph)', () => {
const textInParagraph = createTestString( 200, ( v, idx ) => idx == 60 ? '😱' : v );
testEmojiMutations( textInParagraph, '123456' );
} );

it( 'should properly handle input in middle of text with multi-byte emoji (in short paragraph)', () => {
testEmojiMutations( 'foo😱bar', '123456', 6 );
} );

it( 'should properly handle input in middle of text with multi-byte emoji (in long paragraph)', () => {
const textInParagraph = createTestString( 200, ( v, idx ) => idx == 60 ? '😱' : v );
testEmojiMutations( textInParagraph, '123456', 100 );
} );

it( 'should properly handle input after multi-byte emoji zwj sequence transformation (in short paragraph)', () => {
testEmojiMutations( 'foobar', ':family:123456', '👨‍👩‍👧123456' );
} );

it( 'should properly handle input after multi-byte emoji zwj sequence transformation (in long paragraph)', () => {
testEmojiMutations( createTestString( 200 ), ':family:123456', '👨‍👩‍👧123456' );
} );

function createTestString( length, callback = v => v ) {
const firstCharCode = 'a'.charCodeAt( 0 );
const lettersCount = 'z'.charCodeAt( 0 ) - firstCharCode + 1;
return new Array( length )
.fill( '' )
.map( ( value, idx ) => callback( String.fromCharCode( firstCharCode + idx % lettersCount ), idx ) )
.join( '' );
}

function testEmojiMutations( textInParagraph, typing, expectedOrIndex = typing, index = -1 ) {
let expected = expectedOrIndex;
if ( typeof expectedOrIndex == 'number' ) {
index = expectedOrIndex;
expected = typing;
}
setModelData( model, `<paragraph>${ textInParagraph }[]</paragraph>` );

typing.split( '' ).forEach( ( char, idx ) => simulateMutation( char, index < 0 ? index : index + idx ) );

const expectedResult = index < 0 ? textInParagraph + expected :
textInParagraph.substring( 0, index ) + expected + textInParagraph.substring( index );

expect( getModelData( model, { withoutSelection: true } ) )
.to.equal( `<paragraph>${ expectedResult }</paragraph>` );
}

function simulateMutation( text, index = -1 ) {
const placeOfMutation = viewRoot.getChild( 0 ).getChild( 0 );
const oldText = placeOfMutation.data;
const newText = index < 0 ? oldText + text :
oldText.substring( 0, index ) + text + oldText.substring( index );

viewDocument.fire( 'mutations', [
{
type: 'text',
oldText,
newText,
node: placeOfMutation
}
] );
}
} );
} );

describe( 'keystroke handling', () => {
Expand Down

0 comments on commit c7b4f17

Please sign in to comment.