-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add default value for blockRenderMap
Summary: **Summary** PR for issue 1211 facebookarchive/draft-js#1211 **Test Plan** <s>no need</s> **Edit by Flarnie:** added unit test Closes facebookarchive/draft-js#1302 Reviewed By: zpao Differential Revision: D5881334 Pulled By: zpao fbshipit-source-id: b8d57b23a1654fe9cdcf84b74b25617413ef6248
- Loading branch information
1 parent
1cdee6a
commit d62a578
Showing
3 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
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
86 changes: 86 additions & 0 deletions
86
src/component/contents/__tests__/DraftEditorContent.react-test.js
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,86 @@ | ||
/** | ||
* Copyright (c) 2013-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @emails oncall+ui_infra | ||
* @typechecks | ||
*/ | ||
|
||
'use strict'; | ||
|
||
jest.disableAutomock(); | ||
|
||
const Draft = require('Draft'); | ||
const {Editor, EditorState, RichUtils} = Draft; | ||
const {mount} = require('enzyme'); | ||
const React = require('react'); | ||
|
||
describe('DraftEditor.react', () => { | ||
describe('with a custom block type and the default blockRenderMap', () => { | ||
it('defaults to "unstyled" block type for unknown block types', () => { | ||
const CUSTOM_BLOCK_TYPE = 'CUSTOM_BLOCK_TYPE'; | ||
|
||
function CustomText(props) { | ||
// contrived example | ||
return <p><b>{props.children}</b></p>; | ||
} | ||
|
||
class Container extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
editorState: EditorState.createEmpty(), | ||
}; | ||
this.toggleCustomBlock = this.toggleCustomBlock.bind(this); | ||
} | ||
toggleCustomBlock() { | ||
this.setState({ | ||
editorState: RichUtils.toggleBlockType( | ||
this.state.editorState, | ||
CUSTOM_BLOCK_TYPE, | ||
), | ||
}, () => { | ||
setTimeout(() => this.focus(), 0); | ||
}); | ||
} | ||
blockRenderFn(block) { | ||
if (block.getType() === CUSTOM_BLOCK_TYPE) { | ||
return { | ||
component: CustomText, | ||
editable: true, | ||
}; | ||
} | ||
return null; | ||
} | ||
render() { | ||
return ( | ||
<div className="container-root"> | ||
<div> | ||
<button onClick={this.toggleCustomBlock}>CenterAlign</button> | ||
</div> | ||
<Editor | ||
placeholder="Type away :)" | ||
editorState={this.state.editorState} | ||
blockRendererFn={this.blockRenderFn} | ||
onChange={this._handleChange} | ||
/> | ||
</div> | ||
); | ||
} | ||
_handleChange = (editorState) => { | ||
this.setState({editorState}); | ||
} | ||
} | ||
|
||
const mountedEditorContainer = mount(<Container />); | ||
const triggerCustomBlockRender = () => { | ||
mountedEditorContainer.find('button').simulate('click'); | ||
}; | ||
expect(triggerCustomBlockRender).not.toThrow(); | ||
}); | ||
}); | ||
}); |