Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing tests for Format API code #11562

Merged
merged 4 commits into from
Nov 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/rich-text/src/insert-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const OBJECT_REPLACEMENT_CHARACTER = '\ufffc';
* removed. Indices are retrieved from the selection if none are provided.
*
* @param {Object} value Value to modify.
* @param {string} formatToInsert Format to insert as object.
* @param {Object} formatToInsert Format to insert as object.
* @param {number} startIndex Start index.
* @param {number} endIndex End index.
*
Expand Down
30 changes: 30 additions & 0 deletions packages/rich-text/src/store/test/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Internal dependencies
*/
import { addFormatTypes, removeFormatTypes } from '../actions';

describe( 'actions', () => {
describe( 'addFormatTypes', () => {
it( 'should cast format types as an array', () => {
const formatTypes = { name: 'core/test-format' };
const expected = {
type: 'ADD_FORMAT_TYPES',
formatTypes: [ formatTypes ],
};

expect( addFormatTypes( formatTypes ) ).toEqual( expected );
} );
} );

describe( 'removeFormatTypes', () => {
it( 'should cast format types as an array', () => {
const names = 'core/test-format';
const expected = {
type: 'REMOVE_FORMAT_TYPES',
names: [ names ],
};

expect( removeFormatTypes( names ) ).toEqual( expected );
} );
} );
} );
38 changes: 38 additions & 0 deletions packages/rich-text/src/store/test/selectors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* External dependencies
*/
import deepFreeze from 'deep-freeze';

/**
* Internal dependencies
*/
import { getFormatTypes, getFormatType } from '../selectors';

describe( 'selectors', () => {
const defaultState = deepFreeze( {
formatTypes: {
'core/test-format': { name: 'core/test-format' },
'core/test-format-2': { name: 'core/test-format-2' },
},
} );

describe( 'getFormatTypes', () => {
it( 'should get format types', () => {
const expected = [
{ name: 'core/test-format' },
{ name: 'core/test-format-2' },
];

expect( getFormatTypes( defaultState ) ).toEqual( expected );
} );
} );

describe( 'getFormatType', () => {
it( 'should get a format type', () => {
const expected = { name: 'core/test-format' };
const result = getFormatType( defaultState, 'core/test-format' );

expect( result ).toEqual( expected );
} );
} );
} );
43 changes: 43 additions & 0 deletions packages/rich-text/src/test/get-format-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* External dependencies
*/
import { noop } from 'lodash';

/**
* Internal dependencies
*/
import { getFormatType } from '../get-format-type';
import { unregisterFormatType } from '../unregister-format-type';
import { registerFormatType } from '../register-format-type';
import { getFormatTypes } from '../get-format-types';

describe( 'getFormatType', () => {
beforeAll( () => {
// Initialize the rich-text store.
require( '../store' );
} );

afterEach( () => {
getFormatTypes().forEach( ( format ) => {
unregisterFormatType( format.name );
} );
} );

it( 'should return all format type elements', () => {
const formatType = {
edit: noop,
title: 'format title',
keywords: [ 'one', 'two', 'three' ],
formatTestSetting: 'settingTestValue',
tagName: 'test',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iseulde - should we add validation for tagName property? Values like testandtest 2` made me think about it.

className: null,
};

registerFormatType( 'core/test-format-with-settings', formatType );

expect( getFormatType( 'core/test-format-with-settings' ) ).toEqual( {
name: 'core/test-format-with-settings',
...formatType,
} );
} );
} );
57 changes: 57 additions & 0 deletions packages/rich-text/src/test/get-format-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* External dependencies
*/
import { noop } from 'lodash';

/**
* Internal dependencies
*/
import { getFormatTypes } from '../get-format-types';
import { unregisterFormatType } from '../unregister-format-type';
import { registerFormatType } from '../register-format-type';

describe( 'getFormatTypes', () => {
beforeAll( () => {
// Initialize the rich-text store.
require( '../store' );
} );

afterEach( () => {
getFormatTypes().forEach( ( format ) => {
unregisterFormatType( format.name );
} );
} );

it( 'should return an empty array at first', () => {
expect( getFormatTypes() ).toEqual( [] );
} );

it( 'should return all registered formats', () => {
const testFormat = {
edit: noop,
title: 'format title',
tagName: 'test',
className: null,
};
const testFormatWithSettings = {
edit: noop,
title: 'format title 2',
keywords: [ 'one', 'two', 'three' ],
tagName: 'test 2',
className: null,
formatTestSetting: 'settingTestValue',
};
registerFormatType( 'core/test-format', testFormat );
registerFormatType( 'core/test-format-with-settings', testFormatWithSettings );
expect( getFormatTypes() ).toEqual( [
{
name: 'core/test-format',
...testFormat,
},
{
name: 'core/test-format-with-settings',
...testFormatWithSettings,
},
] );
} );
} );
36 changes: 36 additions & 0 deletions packages/rich-text/src/test/insert-object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* External dependencies
*/
import deepFreeze from 'deep-freeze';

/**
* Internal dependencies
*/
import { insertObject } from '../insert-object';
import { getSparseArrayLength } from './helpers';
import { OBJECT_REPLACEMENT_CHARACTER } from '../special-characters';

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

it( 'should delete and insert', () => {
const record = {
formats: [ , , , , [ em ], [ em ], [ em ], , , , , , , ],
text: 'one two three',
start: 6,
end: 6,
};
const expected = {
formats: [ , , [ { ...obj, object: true } ], [ em ], , , , , , , ],
text: `on${ OBJECT_REPLACEMENT_CHARACTER }o three`,
start: 3,
end: 3,
};
const result = insertObject( deepFreeze( record ), obj, 2, 6 );

expect( result ).toEqual( expected );
expect( result ).not.toBe( record );
expect( getSparseArrayLength( result.formats ) ).toBe( 2 );
} );
} );
Loading