// tslint:disable:no-invalid-this import "jest"; import { Mark } from "prosemirror-model"; import { EditorState } from "prosemirror-state"; import { ExpectExtendSync } from "./expect"; declare global { namespace jest { interface Matchers { toMatchEditorState(expected: EditorState): R; } } } expect.extend({ toMatchEditorState: buildEditorStateMatcher({ matcherHint: { name: "toMatchEditorState" } }) }); export function buildEditorStateMatcher(options: { matcherHint: { name: string; received?: string; expected?: string; secondArgument?: string }; receivedExpr?: string; expectedExpr?: string; }): ExpectExtendSync { return function(received, expected) { const { matcherHint: { received: hintReceived = "received", expected: hintExpected = "expected", name: hintName, secondArgument: hintSecondArgument }, expectedExpr = "expected", receivedExpr = "received" } = options; const matcherHint = () => this.utils.matcherHint(`.${hintName}`, hintReceived, hintExpected, { secondArgument: hintSecondArgument }); const notMatcherHint = () => this.utils.matcherHint(`.not.${hintName}`, hintReceived, hintExpected, { secondArgument: hintSecondArgument }); // PR: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/24426 // tslint:disable:no-any const expectedColor = (text: string) => (this.utils.EXPECTED_COLOR as any)(text); const receivedColor = (text: string) => (this.utils.RECEIVED_COLOR as any)(text); // tslint:enable:no-any if (!(expected instanceof EditorState)) { return { message: () => `${matcherHint()}\n\n` + `Expected ${expectedColor(expectedExpr)} should be an EditorState (using instanceof):\n` + ` ${this.utils.printExpected(expected)}`, pass: false }; } if (!(received instanceof EditorState)) { return { message: () => `${matcherHint()}\n\n` + `Expected ${receivedColor(receivedExpr)} should be an EditorState (using instanceof):\n` + ` ${this.utils.printReceived(received)}`, pass: false }; } { const pass = received.doc.eq(expected.doc); if (!pass) { return { message: () => `${matcherHint()}\n\n` + `Expected ${receivedColor(`${receivedExpr}.doc`)} to be (using .eq):\n` + ` ${this.utils.printExpected(expected.doc)}\n` + `Received:\n` + ` ${this.utils.printReceived(received.doc)}`, pass }; } } { const pass = received.selection.eq(expected.selection); if (!pass) { return { message: () => `${matcherHint()}\n\n` + `Expected ${receivedColor(`${receivedExpr}.selection`)} to be (using .eq):\n` + ` ${this.utils.printExpected(expected.selection)}\n` + `Received:\n` + ` ${this.utils.printReceived(received.selection)}`, pass }; } } { const pass = expected.storedMarks == null || received.storedMarks == null ? expected.storedMarks === received.storedMarks : Mark.sameSet(expected.storedMarks, received.storedMarks); if (!pass) { return { message: () => `${matcherHint()}\n\n` + `Expected ${receivedColor(`${receivedExpr}.storedMarks`)} to be (using Mark.sameSet):\n` + ` ${this.utils.printExpected(expected.storedMarks)}\n` + `Received:\n` + ` ${this.utils.printReceived(received.storedMarks)}`, pass }; } } { const expectedPluginStates = expected.plugins.map(plugin => plugin.getState(expected)); const receivedPluginStates = received.plugins.map(plugin => plugin.getState(received)); const pass = JSON.stringify(expectedPluginStates) === JSON.stringify(receivedPluginStates); if (!pass) { return { message: () => `${matcherHint()}\n\n` + `Expected ${receivedColor( `${receivedExpr}.plugins` )}'s states to be (using JSON.stringify(a) === JSON.stringify(b)):\n` + ` ${this.utils.printExpected(expectedPluginStates)}\n` + `Received:\n` + ` ${this.utils.printReceived(receivedPluginStates)}`, pass }; } } return { message: () => `${notMatcherHint()}\n\n` + `Expected ${receivedColor(receivedExpr)} to not be (using .eq):\n` + ` ${this.utils.printExpected(expected)}\n` + `Received:\n` + ` ${this.utils.printReceived(received)}`, pass: true }; }; }