Skip to content

Commit

Permalink
Start pulling in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
smoores-dev committed Aug 4, 2023
1 parent 2c740b5 commit 8193f8e
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"prosemirror-keymap": "^1.2.1",
"prosemirror-model": "^1.18.3",
"prosemirror-state": "^1.4.2",
"prosemirror-test-builder": "^1.1.1",
"prosemirror-transform": "^1.7.3",
"prosemirror-view": "^1.29.1",
"react": "^18.2.0",
Expand Down
93 changes: 93 additions & 0 deletions src/components/__tests__/EditorView.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { render } from "@testing-library/react";
import { EditorState } from "prosemirror-state";
import { doc, li, p, schema, strong, ul } from "prosemirror-test-builder";
import React from "react";

import { useView } from "../../hooks/useView.js";
import { setupProseMirrorView } from "../../testing/setupProseMirrorView.js";
import { EditorView } from "../EditorView.js";

describe("EditorView", () => {
beforeAll(() => {
setupProseMirrorView();
});

it("reflects the current state in .props", () => {
const editorState = EditorState.create({ schema });

function Test() {
useView((view) => {
expect(view.state).toBe(view.props.state);
});

return null;
}

function TestEditor() {
return (
<EditorView defaultState={editorState}>
<Test></Test>
</EditorView>
);
}
render(<TestEditor />);
});

// This one doesn't work yet!
// eslint-disable-next-line jest/no-disabled-tests
it.skip("calls handleScrollToSelection when appropriate", () => {
const editorState = EditorState.create({ schema });
let scrolled = 0;

const { rerender } = render(
<EditorView
state={editorState}
handleScrollToSelection={() => {
scrolled++;
return false;
}}
/>
);

rerender(
<EditorView
state={editorState.apply(editorState.tr.scrollIntoView())}
handleScrollToSelection={() => {
scrolled++;
return false;
}}
/>
);

expect(scrolled).toBe(1);
});

it("can be queried for the DOM position at a doc position", () => {
const state = EditorState.create({ doc: doc(ul(li(p(strong("foo"))))) });

function Test() {
useView((view) => {
const inText = view.domAtPos(4);
expect(inText.offset).toBe(1);
expect(inText.node.nodeValue).toBe("foo");
const beforeLI = view.domAtPos(1);
expect(beforeLI.offset).toBe(0);
expect(beforeLI.node.nodeName).toBe("UL");
const afterP = view.domAtPos(7);
expect(afterP.offset).toBe(1);
expect(afterP.node.nodeName).toBe("LI");
});

return null;
}

function TestEditor() {
return (
<EditorView defaultState={state}>
<Test></Test>
</EditorView>
);
}
render(<TestEditor />);
});
});
41 changes: 41 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,7 @@ __metadata:
prosemirror-keymap: ^1.2.1
prosemirror-model: ^1.18.3
prosemirror-state: ^1.4.2
prosemirror-test-builder: ^1.1.1
prosemirror-transform: ^1.7.3
prosemirror-view: ^1.29.1
react: ^18.2.0
Expand Down Expand Up @@ -6895,6 +6896,35 @@ __metadata:
languageName: node
linkType: hard

"prosemirror-model@npm:^1.19.0":
version: 1.19.3
resolution: "prosemirror-model@npm:1.19.3"
dependencies:
orderedmap: ^2.0.0
checksum: a0c32127dfde5d5df7b990d85475533ed7c721fa424d5eb521f7b7ba6b2d65064da752013377f830b7d2961f3ca2507f5ed5a9117c3464c7551c1baff677dd5f
languageName: node
linkType: hard

"prosemirror-schema-basic@npm:^1.0.0":
version: 1.2.2
resolution: "prosemirror-schema-basic@npm:1.2.2"
dependencies:
prosemirror-model: ^1.19.0
checksum: 1f93f8678c797c53867da2473a3f69995edaeeea4a8cd631351f30bd75da372cd32203aa44c145d93ee607fa0c80b9888dcdfa1ce68b127513c02d3260441ffb
languageName: node
linkType: hard

"prosemirror-schema-list@npm:^1.0.0":
version: 1.3.0
resolution: "prosemirror-schema-list@npm:1.3.0"
dependencies:
prosemirror-model: ^1.0.0
prosemirror-state: ^1.0.0
prosemirror-transform: ^1.7.3
checksum: 03654e37c8e11598ade33a72cff20e5fdd4d87f129a2c83b378e8255e8003a37cdafc40fd7e7519ba6440f872127ef327d7063c7799325a40e9853ebfc83b544
languageName: node
linkType: hard

"prosemirror-state@npm:^1.0.0, prosemirror-state@npm:^1.4.2":
version: 1.4.2
resolution: "prosemirror-state@npm:1.4.2"
Expand All @@ -6906,6 +6936,17 @@ __metadata:
languageName: node
linkType: hard

"prosemirror-test-builder@npm:^1.1.1":
version: 1.1.1
resolution: "prosemirror-test-builder@npm:1.1.1"
dependencies:
prosemirror-model: ^1.0.0
prosemirror-schema-basic: ^1.0.0
prosemirror-schema-list: ^1.0.0
checksum: 48c34257093a2d3f85dc164a126e72245c773bbdcb0975460c38ea04dc9eae3386996ca02a14e7bc9013844ea7398e256cef894383fe9d56f14d102a16436f34
languageName: node
linkType: hard

"prosemirror-transform@npm:^1.0.0, prosemirror-transform@npm:^1.1.0":
version: 1.7.1
resolution: "prosemirror-transform@npm:1.7.1"
Expand Down

0 comments on commit 8193f8e

Please sign in to comment.