@@ -254,32 +254,66 @@ describe("draft-js-markdown-plugin", () => {
254254 expect ( newEditorState . getCurrentInlineStyle ( ) . size ) . toBe ( 0 ) ;
255255 } ) ;
256256
257- const testInsertNewBlock = type => ( ) => {
258- createMarkdownPlugin . __Rewire__ ( "insertEmptyBlock" , modifierSpy ) ; // eslint-disable-line no-underscore-dangle
259- currentRawContentState = {
260- entityMap : { } ,
261- blocks : [
262- {
263- key : "item1" ,
264- text : "Hello" ,
265- type,
266- depth : 0 ,
267- inlineStyleRanges : [ ] ,
268- entityRanges : [ ] ,
269- data : { } ,
270- } ,
271- ] ,
272- } ;
273- expect ( subject ( ) ) . toBe ( "handled" ) ;
274- expect ( modifierSpy ) . toHaveBeenCalledTimes ( 1 ) ;
275- expect ( store . setEditorState ) . toHaveBeenCalledWith ( newEditorState ) ;
276- } ;
277- [ "one" , "two" , "three" , "four" , "five" , "six" ] . forEach ( level => {
278- describe ( `on header-${ level } ` , ( ) => {
279- it (
280- "inserts new empty block" ,
281- testInsertNewBlock ( `header-${ level } ` )
282- ) ;
257+ const emptyBlockTypes = [
258+ "blockquote" ,
259+ "header-one" ,
260+ "header-two" ,
261+ "header-three" ,
262+ "header-four" ,
263+ "header-five" ,
264+ "header-six" ,
265+ ] ;
266+
267+ emptyBlockTypes . forEach ( type => {
268+ describe ( `on ${ type } ` , ( ) => {
269+ const text = "Hello" ;
270+ beforeEach ( ( ) => {
271+ createMarkdownPlugin . __Rewire__ ( "insertEmptyBlock" , modifierSpy ) ; // eslint-disable-line no-underscore-dangle
272+ currentRawContentState = {
273+ entityMap : { } ,
274+ blocks : [
275+ {
276+ key : "item1" ,
277+ text,
278+ type,
279+ depth : 0 ,
280+ inlineStyleRanges : [ ] ,
281+ entityRanges : [ ] ,
282+ data : { } ,
283+ } ,
284+ ] ,
285+ } ;
286+ } ) ;
287+
288+ describe ( "at the end of line" , ( ) => {
289+ beforeEach ( ( ) => {
290+ currentSelectionState = currentEditorState
291+ . getSelection ( )
292+ . merge ( {
293+ focusOffset : text . length ,
294+ anchorOffset : text . length ,
295+ } ) ;
296+
297+ currentEditorState = createEditorState (
298+ currentRawContentState ,
299+ currentSelectionState
300+ ) ;
301+ } ) ;
302+ it ( "inserts new empty block" , ( ) => {
303+ expect ( subject ( ) ) . toBe ( "handled" ) ;
304+ expect ( modifierSpy ) . toHaveBeenCalledTimes ( 1 ) ;
305+ expect ( store . setEditorState ) . toHaveBeenCalledWith (
306+ newEditorState
307+ ) ;
308+ } ) ;
309+ } ) ;
310+ describe ( "when not at the end of the line" , ( ) => {
311+ it ( "does not handle" , ( ) => {
312+ expect ( subject ( ) ) . toBe ( "not-handled" ) ;
313+ expect ( modifierSpy ) . not . toHaveBeenCalled ( ) ;
314+ expect ( store . setEditorState ) . not . toHaveBeenCalled ( ) ;
315+ } ) ;
316+ } ) ;
283317 } ) ;
284318 } ) ;
285319 [ "ctrlKey" , "shiftKey" , "metaKey" , "altKey" ] . forEach ( key => {
@@ -289,7 +323,27 @@ describe("draft-js-markdown-plugin", () => {
289323 props [ key ] = true ;
290324 event = new window . KeyboardEvent ( "keydown" , props ) ;
291325 } ) ;
292- it ( "inserts new empty block" , testInsertNewBlock ( "blockquote" ) ) ;
326+ it ( "inserts new empty block" , ( ) => {
327+ createMarkdownPlugin . __Rewire__ ( "insertEmptyBlock" , modifierSpy ) ; // eslint-disable-line no-underscore-dangle
328+ const text = "Hello" ;
329+ currentRawContentState = {
330+ entityMap : { } ,
331+ blocks : [
332+ {
333+ key : "item1" ,
334+ text,
335+ type : "any type" ,
336+ depth : 0 ,
337+ inlineStyleRanges : [ ] ,
338+ entityRanges : [ ] ,
339+ data : { } ,
340+ } ,
341+ ] ,
342+ } ;
343+ expect ( subject ( ) ) . toBe ( "handled" ) ;
344+ expect ( modifierSpy ) . toHaveBeenCalledTimes ( 1 ) ;
345+ expect ( store . setEditorState ) . toHaveBeenCalledWith ( newEditorState ) ;
346+ } ) ;
293347 } ) ;
294348 } ) ;
295349 it ( "handles new code block" , ( ) => {
0 commit comments