LouisBarranqueiro / reapop

:postbox: A simple and customizable React notifications system
https://louisbarranqueiro.github.io/reapop
MIT License
1.55k stars 78 forks source link

Problem with useNotifications() while testing #537

Closed pelotazos123 closed 1 year ago

pelotazos123 commented 1 year ago

Description

I'm using this package with react project with typescript, and notifications are working as intended so far, but every time I'm testing a component where the const { notify } = useNotifications(); I'm getting back the following error: TypeError: Cannot destructure property 'notify' of '((cov_27gs2bsvrf(...).s[3]++) , (0 , _reapop.useNotifications)(...))' as it is undefined.

Expected behaviour

Working as intended, just dont break jest testing.

Steps to reproduce the bug

I am not using Redux, so I followed the install instructions for React alone. I added the NotificationsProvider and NotificationsSystem to the App.tsx file. When running tests where the const { notify } = useNotifications(); (this one is the only hook giving me problems) appears, the error in testing shows up. I think I might be doing something wrong, so if there is any clarification I'll appreciate it.

LouisBarranqueiro commented 1 year ago

👋 @pelotazos123, sorry for the late reply, I was on vacation.

If the app works well except your tests, it probably means that the testing environment is not properly set up. useNotifications gets data from the ReapopNotificationsContext React Context. The error message states that the hook returns undefined. It might mean that in your tests, the components you test are not wrapped by the NotificationsProvider React component.

Here is an code example taken from this repo.

It would be easier for me to debug this problem if you could share more code or create a code sandbox so I can attempt to reproduce the problem. 🙂

LouisBarranqueiro commented 1 year ago

Closing this issue, feel free to re-open it if you still encounter this issue 🙂