How to unit test EditorContent #4008
Replies: 7 comments 3 replies
-
Hi, @jacobleetodd . I'm having the same issue. Are you using I tried to use |
Beta Was this translation helpful? Give feedback.
-
I'm having the same problem... even a simple change doesn't work.
|
Beta Was this translation helpful? Give feedback.
-
Came here for the same reason 🤔 |
Beta Was this translation helpful? Give feedback.
-
What I've found is that there seems to be an issue with happy-dom/jsdom. Prosemirror is relying on some layout APIs, which aren't/can't be mocked in a non-browser environment. I tried mocking those APIs manually but didn't find a solution, so in the end, we switched to Cypress component tests for testing the editor. |
Beta Was this translation helpful? Give feedback.
-
I was having the same issue and the snippet below met my needs. Credits to olpa on StackOverflow function getBoundingClientRect(): DOMRect {
const rec = {
x: 0,
y: 0,
bottom: 0,
height: 0,
left: 0,
right: 0,
top: 0,
width: 0,
};
return { ...rec, toJSON: () => rec };
}
class FakeDOMRectList extends Array<DOMRect> implements DOMRectList {
item(index: number): DOMRect | null {
return this[index];
}
}
document.elementFromPoint = (): null => null;
HTMLElement.prototype.getBoundingClientRect = getBoundingClientRect;
HTMLElement.prototype.getClientRects = (): DOMRectList =>
new FakeDOMRectList();
Range.prototype.getBoundingClientRect = getBoundingClientRect;
Range.prototype.getClientRects = (): DOMRectList =>
new FakeDOMRectList(); |
Beta Was this translation helpful? Give feedback.
-
For those wanting more context on how to use this with
It took me a lot of searching and testing different combinations to get this right so I hope this helps someone. Thanks to everyone else for all the useful info on this thread! |
Beta Was this translation helpful? Give feedback.
-
if you are using useEditor, you can do it the following way:
then, you can go inspect the HTML in localhost or in your test environment to validate the testid appears as an attribue. hope this helps! |
Beta Was this translation helpful? Give feedback.
-
Hi There,
I'm using
EditorContent
anduseEditor
from@tiptap/react
to build a basic rich text editor which is replacing a MUITextField
component. This results in the following default HTML rendered to the screen:We use React Testing Library (RTL) and Jest for our unit tests. Since I am replacing a
TextField
there are a number of existing test suites that need to be updated. In order to do so, I need to select the appropriate DOM element and type something in it. I am able to select the div:Or the p tag:
But when I try to type in either of them:
The tests fail and light up with errors:
The strange thing is that when I
screen.debug()
I can see that the text is in the DOM:These feel like internal errors or are perhaps related to rendering the component in jsdom. I haven't been able to find any documentation or really any mention of how to unit test around the
EditorContent
component. This seems like a common use case so I am very confused. Any help is greatly appreciated!Beta Was this translation helpful? Give feedback.
All reactions