Skip to content

Commit

Permalink
fix(jest-remirror): correct diff message (#1747)
Browse files Browse the repository at this point in the history
  • Loading branch information
ocavue committed Jun 26, 2022
1 parent a38062b commit b500283
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-penguins-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'jest-remirror': patch
---

Correct diff message ouputed by `toEqualRemirrorState`.
38 changes: 28 additions & 10 deletions packages/jest-remirror/src/jest-remirror-matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { createSelectionFromTaggedDocument } from './jest-remirror-utils';

export const remirrorMatchers: jest.ExpectExtendMap = {
toContainRemirrorDocument(state: EditorState, value: TaggedProsemirrorNode) {
const expected = value?.toJSON?.();

if (!isProsemirrorNode(value)) {
return {
pass: false,
Expand All @@ -28,6 +26,8 @@ export const remirrorMatchers: jest.ExpectExtendMap = {
};
}

const expected = value?.toJSON?.();

const pass = this.equals(state.doc.content.child(0).toJSON(), expected);
const message = pass
? () =>
Expand Down Expand Up @@ -55,6 +55,27 @@ export const remirrorMatchers: jest.ExpectExtendMap = {
},

toEqualRemirrorState(state: EditorState, value: TaggedProsemirrorNode) {
if (!isProsemirrorNode(value)) {
return {
pass: false,
message: () => 'The expected value should be an instance of ProsemirrorNode.',
};
}

if (!isEditorState(state)) {
return {
pass: false,
message: () => 'Expected the value passed in to be an EditorState',
};
}

if (value.type.schema !== state.schema) {
return {
pass: false,
message: () => 'Expected both values to be using the same schema.',
};
}

const expectedDocument = value?.toJSON?.();
const expectedSelection = createSelectionFromTaggedDocument(
state.doc,
Expand Down Expand Up @@ -97,26 +118,22 @@ export const remirrorMatchers: jest.ExpectExtendMap = {
`Expected JSON value of document to not contain:\n ${this.utils.printExpected(
value,
)}\n` +
`Actual JSON value of document:\n ${this.utils.printReceived(state.doc)}\n` +
`Expected JSON value of selection to not contain:\n ${this.utils.printExpected(
expectedSelection,
)}\n` +
`Actual JSON value of document:\n ${this.utils.printReceived(
state.doc.content.child(0),
)}\n` +
`Actual JSON value of selection:\n ${this.utils.printReceived(state.selection)}\n`
: () => {
const diffString = this.utils.diff(value, state.doc.content.child(0), {
const diffString = this.utils.diff(value, state.doc, {
expand: this.expand,
});
return (
`${this.utils.matcherHint('.toContainRemirrorDocument')}\n\n` +
`Expected JSON value of document to contain:\n${this.utils.printExpected(value)}\n` +
`Expected JSON value of document to contain:\n${this.utils.printExpected(
`Actual JSON value of document:\n ${this.utils.printReceived(state.doc)}\n` +
`Expected JSON value of selection to contain:\n${this.utils.printExpected(
expectedSelection,
)}\n` +
`Actual JSON value of document:\n ${this.utils.printReceived(
state.doc.content.child(0),
)}\n` +
`Actual JSON value of selection:\n ${this.utils.printReceived(state.selection)}` +
`${diffString ? `\n\nDifference:\n\n${diffString}` : ''}`
);
Expand Down Expand Up @@ -202,6 +219,7 @@ declare global {
* expect(view.state).toEqualRemirrorState(doc(p(`This is <head>SPARTA<anchor>`)));
* expect(view.state).not.toEqualRemirrorState(doc(p(`This is <cursor>SPARTA`)));
* });
* ```
*/
toEqualRemirrorState: (builder: TaggedProsemirrorNode) => R;
}
Expand Down

0 comments on commit b500283

Please sign in to comment.