Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework some editor tests to use visual cursor and snapshots #186

Merged
merged 24 commits into from
Jun 23, 2023
Merged

Conversation

danvk
Copy link
Collaborator

@danvk danvk commented Jun 22, 2023

This reworks some of the new editor tests from #154 to be more legible by making them more "visual". So rather than:

editor
  .chain()	
  .setContent(defaultParagraph + defaultParagraph)	
  // 18 is the beginning of the second paragraph	
  .setTextSelection(18)	
  .run();

We have:

setUpEditorWithSelection(editor, `<p>Some content</p><p>|Some content</p>`);

The | here indicates where the cursor should be. With two | characters you get a selection. This is somewhat similar in spirit to how TypeScript tests editor interactions (google "typescript fourslash").

This has a few advantages:

  • More legible than a magic number
  • Editing the editor content in a test doesn't require updating the selection positions by hand

On the assertion side, I set up snapshot testing. This tends to make it much clearer what the resulting state of the document after an interaction is than a series of assertions.

Closes #169

@codecov
Copy link

codecov bot commented Jun 22, 2023

Codecov Report

Merging #186 (1ffb84d) into main (540c5cb) will increase coverage by 0.70%.
The diff coverage is 98.93%.

@@            Coverage Diff             @@
##             main     #186      +/-   ##
==========================================
+ Coverage   65.83%   66.53%   +0.70%     
==========================================
  Files          87       87              
  Lines        4338     4429      +91     
  Branches      322      331       +9     
==========================================
+ Hits         2856     2947      +91     
  Misses       1465     1465              
  Partials       17       17              
Impacted Files Coverage Δ
src/views/ReferenceView.tsx 0.00% <0.00%> (ø)
src/editor/test-utils.ts 100.00% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more


const positions = [];
for (let i = minPos; i <= maxPos; i++) {
const text = editor.view.state.doc.textBetween(i, i + 1);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably isn't the most efficient way to do this, but it works!

expect(getText(content.child(0))).toEqual('der');
expect(getText(content.child(1))).toEqual('Content Line 1');
expect(getText(content.child(2))).toEqual('Content Line 2');
expect(getPrettyHTML(editor)).toMatchInlineSnapshot(`
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an example of a test that reads much more clearly after this change.

@danvk danvk marked this pull request as ready for review June 22, 2023 21:25
@danvk danvk requested a review from sehyod June 22, 2023 21:25
@danvk danvk mentioned this pull request Jun 23, 2023
expect(getText(content.child(1))).toEqual('Content Line 1');
expect(getText(content.child(2))).toEqual('Content Line 2');
expect(getPrettyHTML(editor)).toMatchInlineSnapshot(`
"<collapsible-block folded=\\"false\\">
Copy link
Collaborator

@cguedes cguedes Jun 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danvk @sehyod wondering if we can fix/improve these ' vs '' in the snapshot (would read better without the \\") and also, would be great to match the setUpEditorWithSelection.

Not sure we can change this: vitest-dev/vitest#856

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I 100% agree. I have pushed a change to use single quotes in getPrettyHtml

cguedes
cguedes previously approved these changes Jun 23, 2023
* @returns The text positions of the one or two "|" characters in the editor.
*/
export function setUpEditorWithSelection(editor: Editor, contentHTML: string) {
editor.chain().setContent(contentHTML).run();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: No need to chain to run only one command. You can write editor.commands.setContent(contentHTML);

`<collapsible-block folded='false'>
<collapsible-summary></collapsible-summary>
<collapsible-content>
<p></p>|
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be <p>|</p> to correspond to a real situation: users can only place the cursor in valid positions, ie in Text nodes.
What happens here is that setContent command will surround | with a paragraph and a draggableBlock, because that is the only way, given the schema, to have text after the paragraph, this could lead to unexpected behaviours in the tests. In this case it does work because when setUpEditorWithSelection deletes the | character, it does remove the blocks surrounding it too because these blocks cannot be empty

@cguedes cguedes merged commit 26e7617 into main Jun 23, 2023
11 checks passed
@cguedes cguedes deleted the test-cursor branch June 23, 2023 09:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Testing node manipulation
3 participants