From aead7ef0e438ea1e600bbf8a22964a3db3a718c8 Mon Sep 17 00:00:00 2001 From: Thibaud Colas Date: Mon, 4 Jun 2018 01:28:46 +0300 Subject: [PATCH] chore(demo): add missing tests for demo editor copy-paste --- src/demo/components/DemoEditor.js | 6 +- src/demo/components/DemoEditor.test.js | 78 ++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 4 deletions(-) diff --git a/src/demo/components/DemoEditor.js b/src/demo/components/DemoEditor.js index c218a914..695df488 100644 --- a/src/demo/components/DemoEditor.js +++ b/src/demo/components/DemoEditor.js @@ -106,7 +106,7 @@ type State = { */ class DemoEditor extends Component { editorRef: ?Object; - copySource: ?Object; + copySource: Object; constructor(props: Props) { super(props); @@ -149,9 +149,7 @@ class DemoEditor extends Component { } componentWillUnmount() { - if (this.copySource) { - this.copySource.unregister(); - } + this.copySource.unregister(); } onChange(nextState: EditorState) { diff --git a/src/demo/components/DemoEditor.test.js b/src/demo/components/DemoEditor.test.js index cd2b6d6c..8a096491 100644 --- a/src/demo/components/DemoEditor.test.js +++ b/src/demo/components/DemoEditor.test.js @@ -31,6 +31,15 @@ describe("DemoEditor", () => { ).toMatchSnapshot(); }); + it("componentWillUnmount", () => { + const wrapper = mount(); + const copySource = wrapper.instance().copySource; + jest.spyOn(copySource, "unregister"); + expect(copySource).not.toBeNull(); + wrapper.unmount(); + expect(copySource.unregister).toHaveBeenCalled(); + }); + describe("#extended", () => { it("works", () => { expect( @@ -128,6 +137,28 @@ describe("DemoEditor", () => { ).toBe(null); }); + it("no entity", () => { + window.sessionStorage.getItem = jest.fn(() => + JSON.stringify({ + entityMap: {}, + blocks: [ + { + type: "atomic", + text: " ", + entityRanges: [], + }, + ], + }), + ); + const editable = mount() + .instance() + .blockRenderer({ + getType: () => "atomic", + getEntityAt: () => null, + }).editable; + expect(editable).toBe(false); + }); + it("HORIZONTAL_RULE", () => { window.sessionStorage.getItem = jest.fn(() => JSON.stringify({ @@ -198,6 +229,53 @@ describe("DemoEditor", () => { }); }); + describe("handlePastedText", () => { + it("handled by handleDraftEditorPastedText", () => { + const wrapper = mount(); + const content = { + blocks: [ + { + data: {}, + depth: 0, + entityRanges: [], + inlineStyleRanges: [], + key: "a", + text: "hello,\nworld!", + type: "unstyled", + }, + ], + entityMap: {}, + }; + const html = `

Hello, world!

`; + + expect( + wrapper + .instance() + .handlePastedText( + "hello,\nworld!", + html, + wrapper.state("editorState"), + ), + ).toBe(true); + }); + + it("default handling", () => { + const wrapper = mount(); + + expect( + wrapper + .instance() + .handlePastedText( + "this is plain text paste", + "this is plain text paste", + wrapper.state("editorState"), + ), + ).toBe(false); + }); + }); + describe("onTab", () => { it("works", () => { const wrapper = mount();