@@ -15,7 +15,7 @@ import DraftUtils from "../api/DraftUtils";
15
15
import DividerBlock from "../blocks/DividerBlock" ;
16
16
import DraftailEditor from "./DraftailEditor" ;
17
17
import Toolbar from "./Toolbar" ;
18
- import { ENTITY_TYPE } from "../api/constants" ;
18
+ import { ENTITY_TYPE , INLINE_STYLE } from "../api/constants" ;
19
19
20
20
jest . mock ( "draft-js/lib/generateRandomKey" , ( ) => ( ) => "a" ) ;
21
21
@@ -401,7 +401,7 @@ describe("DraftailEditor", () => {
401
401
it ( "default" , ( ) => {
402
402
jest
403
403
. spyOn ( DraftUtils , "handleNewLine" )
404
- . mockImplementation ( ( editorState ) => editorState ) ;
404
+ . mockImplementation ( ( ) => EditorState . createEmpty ( ) ) ;
405
405
const wrapper = shallowNoLifecycle ( < DraftailEditor /> ) ;
406
406
407
407
expect (
@@ -412,6 +412,7 @@ describe("DraftailEditor", () => {
412
412
413
413
DraftUtils . handleNewLine . mockRestore ( ) ;
414
414
} ) ;
415
+
415
416
it ( "enabled br" , ( ) => {
416
417
const wrapper = shallowNoLifecycle ( < DraftailEditor enableLineBreak /> ) ;
417
418
@@ -421,6 +422,7 @@ describe("DraftailEditor", () => {
421
422
} ) ,
422
423
) . toBe ( "not-handled" ) ;
423
424
} ) ;
425
+
424
426
it ( "alt + enter on text" , ( ) => {
425
427
const wrapper = shallowNoLifecycle ( < DraftailEditor /> ) ;
426
428
@@ -430,39 +432,32 @@ describe("DraftailEditor", () => {
430
432
} ) ,
431
433
) . toBe ( "handled" ) ;
432
434
} ) ;
435
+
433
436
it ( "alt + enter on entity without url" , ( ) => {
434
437
const wrapper = shallowNoLifecycle (
435
438
< DraftailEditor
436
439
rawContentState = { {
437
440
entityMap : {
438
441
"1" : {
439
442
type : "LINK" ,
440
- mutability : "IMMUTABLE" ,
441
443
data : {
442
444
url : "test" ,
443
445
} ,
444
446
} ,
445
447
"2" : {
446
448
type : "LINK" ,
447
- mutability : "IMMUTABLE" ,
448
- data : { } ,
449
449
} ,
450
450
} ,
451
451
blocks : [
452
452
{
453
- key : "b3kdk" ,
454
453
text : "test" ,
455
- type : "unstyled" ,
456
- depth : 0 ,
457
- inlineStyleRanges : [ ] ,
458
454
entityRanges : [
459
455
{
460
456
offset : 0 ,
461
457
length : 4 ,
462
458
key : 2 ,
463
459
} ,
464
460
] ,
465
- data : { } ,
466
461
} ,
467
462
] ,
468
463
} }
@@ -484,27 +479,21 @@ describe("DraftailEditor", () => {
484
479
entityMap : {
485
480
"1" : {
486
481
type : "LINK" ,
487
- mutability : "IMMUTABLE" ,
488
482
data : {
489
483
url : "test" ,
490
484
} ,
491
485
} ,
492
486
} ,
493
487
blocks : [
494
488
{
495
- key : "b3kdk" ,
496
489
text : "test" ,
497
- type : "unstyled" ,
498
- depth : 0 ,
499
- inlineStyleRanges : [ ] ,
500
490
entityRanges : [
501
491
{
502
492
offset : 0 ,
503
493
length : 4 ,
504
494
key : 1 ,
505
495
} ,
506
496
] ,
507
- data : { } ,
508
497
} ,
509
498
] ,
510
499
} }
@@ -520,6 +509,54 @@ describe("DraftailEditor", () => {
520
509
521
510
window . open . mockRestore ( ) ;
522
511
} ) ;
512
+
513
+ it ( "style shortcut" , ( ) => {
514
+ jest . spyOn ( DraftUtils , "applyMarkdownStyle" ) ;
515
+
516
+ const wrapper = shallowNoLifecycle (
517
+ < DraftailEditor
518
+ rawContentState = { {
519
+ entityMap : { } ,
520
+ blocks : [ { text : "A *test*" } ] ,
521
+ } }
522
+ inlineStyles = { [ { type : INLINE_STYLE . ITALIC } ] }
523
+ /> ,
524
+ ) ;
525
+
526
+ expect ( wrapper . instance ( ) . handleReturn ( { } ) ) . toBe ( "handled" ) ;
527
+ expect ( DraftUtils . applyMarkdownStyle ) . toHaveBeenCalled ( ) ;
528
+
529
+ DraftUtils . applyMarkdownStyle . mockRestore ( ) ;
530
+ } ) ;
531
+
532
+ it ( "style shortcut but selection is not collapsed" , ( ) => {
533
+ jest . spyOn ( DraftUtils , "applyMarkdownStyle" ) ;
534
+
535
+ const wrapper = shallowNoLifecycle (
536
+ < DraftailEditor
537
+ rawContentState = { {
538
+ entityMap : { } ,
539
+ blocks : [ { key : "aaaa2" , text : "A *test*" } ] ,
540
+ } }
541
+ inlineStyles = { [ { type : INLINE_STYLE . ITALIC } ] }
542
+ /> ,
543
+ ) ;
544
+
545
+ // Monkey-patching the one method. A bit dirty.
546
+ const selection = new SelectionState ( ) . set ( "anchorKey" , "aaaa2" ) ;
547
+ selection . isCollapsed = ( ) => false ;
548
+ wrapper . setState ( {
549
+ editorState : Object . assign ( wrapper . state ( "editorState" ) , {
550
+ getSelection : ( ) => selection ,
551
+ getCurrentInlineStyle : ( ) => new OrderedSet ( ) ,
552
+ } ) ,
553
+ } ) ;
554
+
555
+ expect ( wrapper . instance ( ) . handleReturn ( { } ) ) . toBe ( "not-handled" ) ;
556
+ expect ( DraftUtils . applyMarkdownStyle ) . not . toHaveBeenCalled ( ) ;
557
+
558
+ DraftUtils . applyMarkdownStyle . mockRestore ( ) ;
559
+ } ) ;
523
560
} ) ;
524
561
525
562
it ( "onFocus, onBlur" , ( ) => {
@@ -680,8 +717,10 @@ describe("DraftailEditor", () => {
680
717
. spyOn ( DraftUtils , "getSelectedBlock" )
681
718
. mockImplementation ( ( ) => new ContentBlock ( ) ) ;
682
719
jest . spyOn ( DraftUtils , "addHorizontalRuleRemovingSelection" ) ;
720
+ jest . spyOn ( DraftUtils , "applyMarkdownStyle" ) ;
683
721
jest . spyOn ( behavior , "handleBeforeInputBlockType" ) ;
684
722
jest . spyOn ( behavior , "handleBeforeInputHR" ) ;
723
+ jest . spyOn ( behavior , "handleBeforeInputInlineStyle" ) ;
685
724
686
725
jest . spyOn ( wrapper . instance ( ) , "onChange" ) ;
687
726
} ) ;
@@ -730,6 +769,19 @@ describe("DraftailEditor", () => {
730
769
expect ( DraftUtils . addHorizontalRuleRemovingSelection ) . toHaveBeenCalled ( ) ;
731
770
DraftUtils . shouldHidePlaceholder . mockRestore ( ) ;
732
771
} ) ;
772
+
773
+ it ( "change style" , ( ) => {
774
+ wrapper . instance ( ) . render = ( ) => { } ;
775
+ behavior . handleBeforeInputInlineStyle = jest . fn ( ( ) => ( {
776
+ pattern : "**" ,
777
+ type : "BOLD" ,
778
+ start : 0 ,
779
+ end : 0 ,
780
+ } ) ) ;
781
+ expect ( wrapper . instance ( ) . handleBeforeInput ( "!" ) ) . toBe ( "handled" ) ;
782
+ expect ( wrapper . instance ( ) . onChange ) . toHaveBeenCalled ( ) ;
783
+ expect ( DraftUtils . applyMarkdownStyle ) . toHaveBeenCalled ( ) ;
784
+ } ) ;
733
785
} ) ;
734
786
735
787
describe ( "handlePastedText" , ( ) => {
0 commit comments