From 8193f8eaff00d4f53a5d573c4de4c13b12db1107 Mon Sep 17 00:00:00 2001 From: Shane Friedman Date: Fri, 4 Aug 2023 18:48:58 -0400 Subject: [PATCH] Start pulling in tests --- package.json | 1 + src/components/__tests__/EditorView.test.tsx | 93 ++++++++++++++++++++ yarn.lock | 41 +++++++++ 3 files changed, 135 insertions(+) create mode 100644 src/components/__tests__/EditorView.test.tsx diff --git a/package.json b/package.json index 84e2f96d..46a9369b 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/components/__tests__/EditorView.test.tsx b/src/components/__tests__/EditorView.test.tsx new file mode 100644 index 00000000..defe0580 --- /dev/null +++ b/src/components/__tests__/EditorView.test.tsx @@ -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 ( + + + + ); + } + render(); + }); + + // 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( + { + scrolled++; + return false; + }} + /> + ); + + rerender( + { + 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 ( + + + + ); + } + render(); + }); +}); diff --git a/yarn.lock b/yarn.lock index b3d4d49d..3228215e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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 @@ -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" @@ -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"