I’m newbie who have never used jest library (@testing-library). I just wonder that is it possible to update props in test code.
Here is my Sample tsx code :
const Dropdown : React.FC<IDropdownProps> = ({initialValue, items, selected, onSelect}) => {
return (
<div>
<div data-testid="test-dropdown">{selected || initialValue}</div>
<ul>
{items.map((el) => {
return (
<li
key={"a" + el.id}
onClick={onSelect}
role="list"
>
{el.name}
</li>
)
})}
</ul>
</div>
)
}
Here is my test code :
const items = [{id : 1, name : "1"},{id : 2, name : "2"}];
const initialValue = "Select"
test("Testing", () => {
const onSelectMock = jest.fn();
expect(screen.getByTestId('test-dropdown')).toHaveTextContent('Select');
const obj = render(<Dropdown items={items} initialValue={initialValue} onSelect={onSelectMock(items[1])} />);
fireEvent.click(el[1]);
expect(screen.getByTestId('test-dropdown')).toHaveTextContent('2');
});
After firing the event, I wanna update selected props, change it "Select" to "2", which is the property of Dropdown component.
If it is wrong, would you please recommend me to testing way?
2
Answers
You can use the
rerender
method exposed by yourrender
call:You can also check the testing library doc here
You can check bellow approach to test your call back function dynamically. and trigger rerender