Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Mar 19, 2019
1 parent be756c9 commit 41b30f8
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 6 deletions.
57 changes: 51 additions & 6 deletions packages/rich-text/src/test/get-active-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,40 @@ import { getActiveFormat } from '../get-active-format';

describe( 'getActiveFormat', () => {
const em = { type: 'em' };
const strong = { type: 'strong' };

it( 'should get format by selection', () => {
it( 'should return undefined if there is no selection', () => {
const record = {
formats: [ [ em ], , , ],
formats: [ [ em ], [ em ], [ em ] ],
text: 'one',
};

expect( getActiveFormat( record, 'em' ) ).toBe( undefined );
} );

it( 'should return format at first character for uncollapsed selection', () => {
const record = {
formats: [ [ em ], [ strong ], , ],
text: 'one',
start: 0,
end: 1,
end: 2,
};

expect( getActiveFormat( record, 'em' ) ).toEqual( em );
expect( getActiveFormat( record, 'em' ) ).toBe( em );
} );

it( 'should not get any format if at the boundary', () => {
it( 'should return undefined if at the boundary before', () => {
const record = {
formats: [ [ em ], , [ em ] ],
text: 'one',
start: 3,
end: 3,
};

expect( getActiveFormat( record, 'em' ) ).toBe( undefined );
} );

it( 'should return undefined if at the boundary after', () => {
const record = {
formats: [ [ em ], , [ em ] ],
text: 'one',
Expand All @@ -29,7 +50,7 @@ describe( 'getActiveFormat', () => {
expect( getActiveFormat( record, 'em' ) ).toBe( undefined );
} );

it( 'should get format if inside boundary position', () => {
it( 'should return format if inside format', () => {
const record = {
formats: [ [ em ], [ em ], [ em ] ],
text: 'one',
Expand All @@ -39,4 +60,28 @@ describe( 'getActiveFormat', () => {

expect( getActiveFormat( record, 'em' ) ).toBe( em );
} );

it( 'should return activeFormats', () => {
const record = {
formats: [ [ em ], , [ em ] ],
text: 'one',
start: 1,
end: 1,
activeFormats: [ em ],
};

expect( getActiveFormat( record, 'em' ) ).toBe( em );
} );

it( 'should not return activeFormats for uncollapsed selection', () => {
const record = {
formats: [ [ em ], , [ em ] ],
text: 'one',
start: 1,
end: 2,
activeFormats: [ em ],
};

expect( getActiveFormat( record, 'em' ) ).toBe( undefined );
} );
} );
55 changes: 55 additions & 0 deletions packages/rich-text/src/test/update-formats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Internal dependencies
*/

import { updateFormats } from '../update-formats';
import { getSparseArrayLength } from './helpers';

describe( 'updateFormats', () => {
const em = { type: 'em' };

it( 'should update formats with empty array', () => {
const value = {
formats: [ [ em ] ],
text: '1',
};
const expected = {
...value,
activeFormats: [],
formats: [ , ],
};
const result = updateFormats( {
value,
start: 0,
end: 1,
formats: [],
} );

expect( result ).toEqual( expected );
expect( result ).toBe( value );
expect( getSparseArrayLength( result.formats ) ).toBe( 0 );
} );

it( 'should update formats and update references', () => {
const value = {
formats: [ [ em ], , ],
text: '123',
};
const expected = {
...value,
activeFormats: [ em ],
formats: [ [ em ], [ em ] ],
};
const result = updateFormats( {
value,
start: 1,
end: 2,
formats: [ { ...em } ],
} );

expect( result ).toEqual( expected );
expect( result ).toBe( value );
expect( result.formats[ 1 ][ 0 ] ).toBe( em );
expect( getSparseArrayLength( result.formats ) ).toBe( 2 );
} );
} );

0 comments on commit 41b30f8

Please sign in to comment.