-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add missing tests for Format API code
- Loading branch information
1 parent
1ac70e1
commit fd7a191
Showing
9 changed files
with
370 additions
and
3 deletions.
There are no files selected for viewing
Submodule gutenberg-mobile
added at
c90c36
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getFormatTypes, getFormatType } from '../selectors'; | ||
|
||
describe( 'selectors', () => { | ||
const defaultState = { | ||
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 ); | ||
} ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* 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', () => { | ||
// Initialize format 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', | ||
}; | ||
|
||
registerFormatType( 'core/test-format-with-settings', formatType ); | ||
|
||
expect( getFormatType( 'core/test-format-with-settings' ) ).toEqual( { | ||
name: 'core/test-format-with-settings', | ||
...formatType, | ||
} ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* 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', () => { | ||
// Initialize format 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 formatType1 = { edit: noop, title: 'format title' }; | ||
const formatType2 = { | ||
edit: noop, | ||
title: 'format title 2', | ||
keywords: [ 'one', 'two', 'three' ], | ||
formatTestSetting: 'settingTestValue', | ||
}; | ||
registerFormatType( 'core/test-format', formatType1 ); | ||
registerFormatType( 'core/test-format-with-settings', formatType2 ); | ||
expect( getFormatTypes() ).toEqual( [ | ||
{ | ||
name: 'core/test-format', | ||
...formatType1, | ||
}, | ||
{ | ||
name: 'core/test-format-with-settings', | ||
...formatType2, | ||
}, | ||
] ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import deepFreeze from 'deep-freeze'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
|
||
import { insertObject } from '../insert-object'; | ||
import { getSparseArrayLength } from './helpers'; | ||
|
||
describe( 'insert', () => { | ||
const obj = { type: 'obj' }; | ||
const em = { type: 'em' }; | ||
|
||
const OBJECT_REPLACEMENT_CHARACTER = '\ufffc'; | ||
|
||
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 ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import deepFreeze from 'deep-freeze'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
|
||
import { toggleFormat } from '../toggle-format'; | ||
import { getSparseArrayLength } from './helpers'; | ||
|
||
describe( 'toggleFormat', () => { | ||
const strong = { type: 'strong' }; | ||
const em = { type: 'em' }; | ||
|
||
it( 'should remove format if it exists at start of selection', () => { | ||
const record = { | ||
formats: [ , , , [ strong ], [ em, strong ], [ em ], [ em ], , , , , , , ], | ||
text: 'one two three', | ||
start: 3, | ||
end: 6, | ||
}; | ||
const expected = { | ||
formats: [ , , , , [ em ], [ em ], [ em ], , , , , , , ], | ||
text: 'one two three', | ||
start: 3, | ||
end: 6, | ||
}; | ||
const result = toggleFormat( deepFreeze( record ), strong ); | ||
|
||
expect( result ).toEqual( expected ); | ||
expect( result ).not.toBe( record ); | ||
expect( getSparseArrayLength( result.formats ) ).toBe( 3 ); | ||
} ); | ||
|
||
it( 'should apply format if it doesn\'t exist at start of selection', () => { | ||
const record = { | ||
formats: [ , , , , [ em, strong ], [ em ], [ em ], , , , , , , ], | ||
text: 'one two three', | ||
start: 3, | ||
end: 6, | ||
}; | ||
const expected = { | ||
formats: [ , , , [ strong ], [ em, strong ], [ em, strong ], [ em ], , , , , , , ], | ||
text: 'one two three', | ||
start: 3, | ||
end: 6, | ||
}; | ||
const result = toggleFormat( deepFreeze( record ), strong ); | ||
|
||
expect( result ).toEqual( expected ); | ||
expect( result ).not.toBe( record ); | ||
expect( getSparseArrayLength( result.formats ) ).toBe( 4 ); | ||
} ); | ||
} ); |
Oops, something went wrong.