问题
I have recently wrapped my component in test with ThemeProvider. When I ran my tests it's throwing the following error
'Method “simulate” is meant to be run on 1 node. 0 found instead'
Before wrapping it was working fine. How can I resolve this issue? I found many issues similar to this in GitHub and I tried all those ways still I'm getting the same error.
The code before wrapping:
test('handleSelect function called on option select', () => {
const handleSelectSpy = sinon.spy();
wrapper = mount(
<Dropdown handleSelect={handleSelectSpy} options={options} />
);
dropdown = wrapper.find('Dropdown');
dropdown
.find('InputBase')
.find('[role="button"]')
.simulate('click');
expect(true).toBe(true);
});
});
code after wrapping:
test('handleSelect function called on option select', () => {
const handleSelectSpy = sinon.spy();
wrapper = mount(
<ThemeProvider>
<Dropdown handleSelect={handleSelectSpy} options={options} />
</ThemeProvider>,
);
dropdown = wrapper.find('Dropdown');
dropdown
.find('InputBase')
.find('[role="button"]')
.simulate('click');
expect(true).toBe(true);
});
});
Error: Dropdown - Full DOM rendering › handleSelect function called on option select Method “simulate” is meant to be run on 1 node. 0 found instead.
回答1:
.find('InputBase')
if this is a component classname/constructor function it should be without quotes:
.find(InputBase)
Only CSS selector is expected to be used inside quotes.
来源:https://stackoverflow.com/questions/56921107/method-simulate-is-meant-to-be-run-on-1-node-0-found-instead