Skip to content

Commit

Permalink
chore(demo): add missing tests for demo editor copy-paste
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaudcolas committed Jun 3, 2018
1 parent 6e3e6af commit aead7ef
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/demo/components/DemoEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ type State = {
*/
class DemoEditor extends Component<Props, State> {
editorRef: ?Object;
copySource: ?Object;
copySource: Object;

constructor(props: Props) {
super(props);
Expand Down Expand Up @@ -149,9 +149,7 @@ class DemoEditor extends Component<Props, State> {
}

componentWillUnmount() {
if (this.copySource) {
this.copySource.unregister();
}
this.copySource.unregister();
}

onChange(nextState: EditorState) {
Expand Down
78 changes: 78 additions & 0 deletions src/demo/components/DemoEditor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ describe("DemoEditor", () => {
).toMatchSnapshot();
});

it("componentWillUnmount", () => {
const wrapper = mount(<DemoEditor extended={false} />);
const copySource = wrapper.instance().copySource;
jest.spyOn(copySource, "unregister");
expect(copySource).not.toBeNull();
wrapper.unmount();
expect(copySource.unregister).toHaveBeenCalled();
});

describe("#extended", () => {
it("works", () => {
expect(
Expand Down Expand Up @@ -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(<DemoEditor extended={true} />)
.instance()
.blockRenderer({
getType: () => "atomic",
getEntityAt: () => null,
}).editable;
expect(editable).toBe(false);
});

it("HORIZONTAL_RULE", () => {
window.sessionStorage.getItem = jest.fn(() =>
JSON.stringify({
Expand Down Expand Up @@ -198,6 +229,53 @@ describe("DemoEditor", () => {
});
});

describe("handlePastedText", () => {
it("handled by handleDraftEditorPastedText", () => {
const wrapper = mount(<DemoEditor extended={false} />);
const content = {
blocks: [
{
data: {},
depth: 0,
entityRanges: [],
inlineStyleRanges: [],
key: "a",
text: "hello,\nworld!",
type: "unstyled",
},
],
entityMap: {},
};
const html = `<div data-draftjs-conductor-fragment='${JSON.stringify(
content,
)}'><p>Hello, world!</p></div>`;

expect(
wrapper
.instance()
.handlePastedText(
"hello,\nworld!",
html,
wrapper.state("editorState"),
),
).toBe(true);
});

it("default handling", () => {
const wrapper = mount(<DemoEditor extended={false} />);

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(<DemoEditor extended={false} />);
Expand Down

0 comments on commit aead7ef

Please sign in to comment.