Closed AndrewOttavianoAbb closed 2 years ago
I am creating a wrapper around the
react-signature-canvas
and am working on writing jest tests for it withtesting-libarary/react
Wrapper components are definitely encouraged since this is an unopinionated and unstyled component! Everyone's got their own UI components, UX flows, and CSS styles, so I think composition for a more specific use-case is good separation of concerns (while this component handles the generic use-case).
When I originally built this component years ago, I also had an internal wrapper around it at my company, with buttons, a modal, react-bootstrap
integration, etc.
Also tested components great too 👍
Rendering in the browser seems to work. Any suggestions as to what I'm doing wrong or is this a bug?
Hard to tell without a reproduction as there's a number of moving pieces here. I've never seen this error before and a few pieces of your issue hint that this isn't a bug:
The stack trace shows that is being hit during unmounting actually. I'm not sure why that's happening, but componentWillUnmount
is being called. That calls the off
method to remove the event listeners.
In the current version of react-signature-canvas
, the underlying signature_pad
ref
starts as null
, so from the error message, it sounds like the internal ref
is null
during your mount.
The next version of react-signature-canvas
(the main
branch) is written in TS and because of the strict typing, actually has an error check if the ref
is null
. It's a rare case, but it usually means you're calling a method during a mount/unmount (sounds related, right?). Even in that version though, it would still error, but it would give a more specific (and hopefully more helpful) error from react-signature-canvas
itself.
You mentioned that rendering in browser works, so it may very well be your test set-up. Internally, the tests I wrote for this component use jsdom
and an additional window-resizeto
polyfill (for mocking the resize functionality). If there's no DOM, a DOM ref
wouldn't exist, so that could very well break a bunch of things like this.
So I'm not sure what your jest.config.js
looks like, but my suspicion would be that the test set-up is missing some configuration.
Looking at the stack trace a bit more, jsdom
does pop up so I'm assuming you are using jsdom
?
Also not sure which version of React this is on, as React 18 is currently untested (c.f. #76, which also discusses the need to migrate to RTL from Enzyme -- this may be related given that you're using RTL)
EDIT: Hmmm... it is possible you're running into the exact situation discussed in the TS rewrite review: https://github.com/agilgur5/react-signature-canvas/pull/42#discussion_r376819460 .
As I mentioned there, it's possible that doing nothing (i.e. catching the error and doing a no-op instead) when the ref
is null
is more appropriate than throwing an error; the crux there was if such a situation were proper user behavior or erroneous behavior (where throwing an error is indeed appropriate). That rewrite is non-breaking though, as it would error out regardless.
You're literally just mounting it, so err, that doesn't seem like erroneous behavior (😅 ), but as it only happens during testing, it still may be a testing set-up issue or a bug upstream in one of the testing libraries. Especially as this hasn't shown up in the many years this has existed until now
@agilgur5 thank you for the quick response! I am using jsdom
but it is the RTL wrapper around it import '@testing-library/jest-dom';
I am still on react 17, so that shouldn't be an issue.
I am using
jsdom
but it is the RTL wrapper around itimport '@testing-library/jest-dom';
Mmm @testing-library/jest-dom
isn't a wrapper for jsdom
, it's a set of DOM matchers for Jest. It doesn't depend on jsdom
either.
A minimal reproduction would help here. Especially your jest.config.js
, package.json
, and your component's render
code. The last part may be causing an error or something that's making it unmount in the testing environment.
I am working on getting a repro ready to go. Unfortunately, the project I'm working on is closed source. I am trying to get a codesandbox example but am running into with fillStyle
which I've read can be fixed w/jest-canvas-mock
but am not getting anywhere with that.
Here it is if you want to give it a try: https://codesandbox.io/s/react-17-forked-y43gl3?file=/src/tests/Signature.spec.js
I am trying to get a codesandbox example but am running into with
fillStyle
which I've read can be fixed w/jest-canvas-mock
but am not getting anywhere with that.
Yea this is probably the crux of the issue -- the canvas
implementation isn't working.
react-signature-canvas
's internal tests use node-canvas
, which jsdom
supports automatically if detected.
Here it is if you want to give it a try: https://codesandbox.io/s/react-17-forked-y43gl3?file=/src/tests/Signature.spec.js
You're using CRA for this CodeSandbox, so you may very well be running into https://github.com/hustcc/jest-canvas-mock/issues/72 .
I couldn't get it to work in CodeSandbox, and that might be due to requiring native binaries like node-canvas
etc, or due to CRA issues. Had some issues running in a node
template (couldn't get things to install), but that may make more sense than the CRA template since Jest runs via Node etc.
I did create a more minimal StackBlitz project for this instead, and the tests pass fine there: https://stackblitz.com/edit/rsc-issue-81?file=index.spec.js . So I think the issue is within some part of your environment, as a minimal example succeeds.
Switching to node-canvas
fixed that test. Found the same suggestion here.
@agilgur5 thank you for your responsiveness and willingness to help!
Fun fact, apparently I ran into the fillStyle
issue as well when I was first creating the test suite 3 years ago in https://github.com/agilgur5/react-signature-canvas/issues/33#issuecomment-515737745 .
Using compatible versions of node-canvas
and jsdom
resolved it there per that issue.
Hello and thank you so much for your work on this project! I am creating a wrapper around the
react-signature-canvas
and am working on writing jest tests for it withtesting-libarary/react
. I am simply trying to mount it to ensure it displays properly and am receiving the following error. I'm not passing anything to it besides aref
(created withuseRef
); I'm not even touchingcanvasProps
(at this point). I am rendering the parent<div />
with adata-testid
so that I can grab hold of it in testing-library.Test Code:
(
Signature
is my wrapper component, this is what I'm actually rendering<SignatureCanvas ref={canvasRef} {...canvasAttributes} />
butcanvasAttributes
isnull
in my test above.)Stack trace:
``` ● Signature › Component renders successfully TypeError: Cannot read properties of null (reading 'off') 5 | describe('Signature', () => { 6 | it('Component renders successfully', () => { > 7 | render(Rendering in the browser seems to work. Any suggestions as to what I'm doing wrong or is this a bug?