Skip to content

Commit 5821079

Browse files
committed
Add missing tests for Markdown style shortcuts on return
1 parent fdd8a6d commit 5821079

File tree

1 file changed

+68
-16
lines changed

1 file changed

+68
-16
lines changed

Diff for: lib/components/DraftailEditor.test.js

+68-16
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import DraftUtils from "../api/DraftUtils";
1515
import DividerBlock from "../blocks/DividerBlock";
1616
import DraftailEditor from "./DraftailEditor";
1717
import Toolbar from "./Toolbar";
18-
import { ENTITY_TYPE } from "../api/constants";
18+
import { ENTITY_TYPE, INLINE_STYLE } from "../api/constants";
1919

2020
jest.mock("draft-js/lib/generateRandomKey", () => () => "a");
2121

@@ -401,7 +401,7 @@ describe("DraftailEditor", () => {
401401
it("default", () => {
402402
jest
403403
.spyOn(DraftUtils, "handleNewLine")
404-
.mockImplementation((editorState) => editorState);
404+
.mockImplementation(() => EditorState.createEmpty());
405405
const wrapper = shallowNoLifecycle(<DraftailEditor />);
406406

407407
expect(
@@ -412,6 +412,7 @@ describe("DraftailEditor", () => {
412412

413413
DraftUtils.handleNewLine.mockRestore();
414414
});
415+
415416
it("enabled br", () => {
416417
const wrapper = shallowNoLifecycle(<DraftailEditor enableLineBreak />);
417418

@@ -421,6 +422,7 @@ describe("DraftailEditor", () => {
421422
}),
422423
).toBe("not-handled");
423424
});
425+
424426
it("alt + enter on text", () => {
425427
const wrapper = shallowNoLifecycle(<DraftailEditor />);
426428

@@ -430,39 +432,32 @@ describe("DraftailEditor", () => {
430432
}),
431433
).toBe("handled");
432434
});
435+
433436
it("alt + enter on entity without url", () => {
434437
const wrapper = shallowNoLifecycle(
435438
<DraftailEditor
436439
rawContentState={{
437440
entityMap: {
438441
"1": {
439442
type: "LINK",
440-
mutability: "IMMUTABLE",
441443
data: {
442444
url: "test",
443445
},
444446
},
445447
"2": {
446448
type: "LINK",
447-
mutability: "IMMUTABLE",
448-
data: {},
449449
},
450450
},
451451
blocks: [
452452
{
453-
key: "b3kdk",
454453
text: "test",
455-
type: "unstyled",
456-
depth: 0,
457-
inlineStyleRanges: [],
458454
entityRanges: [
459455
{
460456
offset: 0,
461457
length: 4,
462458
key: 2,
463459
},
464460
],
465-
data: {},
466461
},
467462
],
468463
}}
@@ -484,27 +479,21 @@ describe("DraftailEditor", () => {
484479
entityMap: {
485480
"1": {
486481
type: "LINK",
487-
mutability: "IMMUTABLE",
488482
data: {
489483
url: "test",
490484
},
491485
},
492486
},
493487
blocks: [
494488
{
495-
key: "b3kdk",
496489
text: "test",
497-
type: "unstyled",
498-
depth: 0,
499-
inlineStyleRanges: [],
500490
entityRanges: [
501491
{
502492
offset: 0,
503493
length: 4,
504494
key: 1,
505495
},
506496
],
507-
data: {},
508497
},
509498
],
510499
}}
@@ -520,6 +509,54 @@ describe("DraftailEditor", () => {
520509

521510
window.open.mockRestore();
522511
});
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+
});
523560
});
524561

525562
it("onFocus, onBlur", () => {
@@ -680,8 +717,10 @@ describe("DraftailEditor", () => {
680717
.spyOn(DraftUtils, "getSelectedBlock")
681718
.mockImplementation(() => new ContentBlock());
682719
jest.spyOn(DraftUtils, "addHorizontalRuleRemovingSelection");
720+
jest.spyOn(DraftUtils, "applyMarkdownStyle");
683721
jest.spyOn(behavior, "handleBeforeInputBlockType");
684722
jest.spyOn(behavior, "handleBeforeInputHR");
723+
jest.spyOn(behavior, "handleBeforeInputInlineStyle");
685724

686725
jest.spyOn(wrapper.instance(), "onChange");
687726
});
@@ -730,6 +769,19 @@ describe("DraftailEditor", () => {
730769
expect(DraftUtils.addHorizontalRuleRemovingSelection).toHaveBeenCalled();
731770
DraftUtils.shouldHidePlaceholder.mockRestore();
732771
});
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+
});
733785
});
734786

735787
describe("handlePastedText", () => {

0 commit comments

Comments
 (0)