-
-
Notifications
You must be signed in to change notification settings - Fork 221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Component Testing]: How to test a custom hook using useReactToPrint #665
Comments
The way I would do this: mock |
Thank you @MatthewHerbst for your reply, could you please add more hints or examples? I tried to resolve the issue today, but I failed to pass the tests. |
Hello, apologies for the very delayed response. Did you manage to solve this? I believe it's just a question of rendering/mocking the things correctly |
No, I told the team that I could not add the test but it works well without issues. |
So, I think you want something like the below. This is the pattern we use at my company for testing hooks. I haven't tried running this test with import { act, renderHook } from "@testing-library/react-hooks";
import { usePrint } from "./usePrint"
jest.mock("react-to-print", () => ({
useReactToPrint: jest.fn(),
}));
describe("usePrint", () => {
test("calls `react-to-print` correctly", () => {
const { result } = renderHook(() => usePrint());
expect(useReactToPrint).toHaveBeenCalled();
// If you also want to test that `react-to-print` properly calls `window.print` then you would need
// to not use a mock version since `jest.fn()` doesn't call `window.print`. I would personally argue
// that you shouldn't be testing 3rd party code though, so there's no reason for such a test
});
}); I've been meaning to add proper unit tests to this library for a long time. Might try and get around to that over the holidays. PRs always accepted 😄 |
I just spent a long time trying to figure out how to mock
having unit tests in this library would be extremely helpful for reference. |
I would generally advise against trying to mock and test 3rd party code. You should be testing that you pass the right props to You could also put unit tests on your
PR contributions are always welcome! 🙏😅 |
I have been adopting Behavior Driven Development where the Do you have a recommendation for how I should be doing this?
At the time, I am far too overemployed to dedicate time to study/contribute to the hundreds of open source solutions I use. However, I am willing to offer a humble amount of financial support to sponsorable maintainers to encourage more contributions! Thank you very much for developing this functionality. |
Ha, appreciate that, but totally unnecessary right now, very much in the same boat 😄 With the upcoming changes in React 19 I'm debating making this entire library just a function, no hook/component needed since we don't actually hold onto any of your application state anyways. Should make testing 1000% simpler. Will keep you updated, I hope to do some work on that this coming week/weekend. |
Hi @MatthewHerbst , thank you for your efforts. Do you have any future milestone in the future to add |
Hey, apologies for the delayed response here, have been out-of-town. Download gets tricky because it would likely require adding some sort of PDF generation library and those can be pretty heavy, especially when most users of Appreciate the sponsorship offers from you both. I'll think about what that might entail. |
Hey folks. I released I did also add that sponsorship button 😅 |
Hi @gregnb ,
This could be a discussion rather than an issue.
I created a custom hook called
usePrint
and inside it, I useduseReactToPrint
hook. I want to create a test with Jest and Testing Library. However, I failed to find the way to make sure that the print is called properly.My goal is to make sure the PDF preview will be opened when the
print
function is called.usePrint
hookusePrint.spec.tsx
Thank you.
The text was updated successfully, but these errors were encountered: