Document how to test a ReactN Component
See original GitHub issueHi @CharlesStover ,
I’m experimenting reactn using useGlobal hook. I’m excited with the simplicity brought by reactn against redux!
One thing I missed is a general path of how to test things up. Reactn is too fresh and there isn’t too much resources out there.
I started using the useGlobal hook, and I’m testing my components using mocks. I’ve wrote a mock file __mocks__/reactn.js
:
export const useGlobal = jest.fn()
Suppose I have a component that apply the useGlobal like this:
const [something, setSomething] = useGlobal('something')
And suppose a correponding test SomeComponent.test.js like:
const something = 'some-value'
const setSomething = jest.fn()
useGlobal.mockImplementationOnce([something, setSomething])
const wrapper = shallow(<SomeComponent />)
wrapper.simulate('click')
expect(setSomething).toBeCalledWith('foo-bar')
expect(useGlobal).toBeCalledWith(['something']);
expect(useGlobal.mock.calls.map(call => call[0])).toEqual(['something']);
This can work, but I’m not sure if it is the best approach.
My question is: what do you do for test the reactn usage? Do you have any plan for test helpers? Do you see them as part of the reactn core?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:25 (9 by maintainers)
Top Results From Across the Web
Testing Overview - React
You can test React components similar to testing other JavaScript code. ... This documentation section focuses on testing strategies for the first case....
Read more >How to Test React Components: the Complete Guide
Unlike your react components, your tests are not executed in the browser. Jest is the test runner and testing framework used by React....
Read more >Testing React Apps - Jest
At Facebook, we use Jest to test React applications. ... Let's create a snapshot test for a Link component that renders hyperlinks: Link.js....
Read more >Testing your React Components — Step by Step
How to test React.js components with Jest and Testing Library. Test events, async behavior, mock API calls, plus more.
Read more >React Testing Library
The React Testing Library is a very light-weight solution for testing React components. It provides light utility functions on top of react-dom ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks @CharlesStover, if you have a draft I could help contribute to it. I think a general idea of the direction can be inferred from this page
I don’t think I’ll need help with the docs. I’ll throw it into the README as I finish writing tests for 2.0. You are more than welcome to write Medium articles about your experiences. I definitely appreciate any publicity. 🙂
I think spying on the class components is intuitive and aligns with current React testing methods for non-ReactN Components, so I am happy with that.
I have never written a test for a functional Component with hooks. I cannot verify that it aligns with current React testing methods for non-ReactN Components. If it does not, let me know, because I want to make it so.
I am also open to feedback on new features unique to ReactN that make testing easier. I would be happy to reduce any boilerplate caused by ReactN. But I’m at a loss for what those features could possibly be or if there is even any boilerplate separate from what is required of testing React in the first place.