I just started exploring a unit testing framework for my react application. I locked my choice at @testing-library/react instead of Enzyme as it is recommended by React and helps writing more robust tests. I'm also planning to use Jest for completeness.
But before I could proceed with React Testing Library, I'm stuck at few questions. Can someone please help?
For instance,
- There is an
Editorcomponent which updates Redux store based on user input. - There is another component
Previewwhich reads value directly from store. - There is a third component
Containerwhich contains both of the above components passing some props.
Questions:
- How to test if user input in
Editorcomponent actually updates store? - How to test if
Previewcomponent can render value reading from store? - How to test if
Containercomponent passes props correctly to its children?
Container.jsx
import React from 'react';
import Editor from './Editor';
import Preview from './Preview';
const Container = ({editorType}) => {
return (
<div>
<Editor type={editorType} />
<Preview />
</div>
);
};
export default Container;
Editor.jsx
import React from 'react';
import { useSelector, useDispatch } from 'react-redux';
import {setName} from './actions';
const Editor = ({type}) => {
const name = useSelector(state => state.name);
const dispatch = useDispatch();
return (
<div>
<h1>Type: {type}</h1>
<input value={name} onChange={evt => dispatch(setName(evt.target.value))} />
</div>
);
};
export default Editor;
Preview.jsx
import React from 'react';
import { useSelector } from 'react-redux';
const Editor = ({type}) => {
const name = useSelector(state => state.name);
return (
<div>
<h1>Name: {name}</h1>
</div>
);
};
export default Preview;