I want to check my menu with nested menu items. I want to be able to simulate a click on a nested menu item and see if its handler is called. I already have tests for non-nested menu items.
Here is a simple version of the test I'm trying to build:
describe("menu", () => {
it("should click on nested nested menu items", () => {
const testOnClickSpy = Sinon.spy(() => {});
const component = mount(
<MuiThemeProvider><Menu>
<MenuItem
primaryText={<span id="test">test</span>} onTouchTap={testOnClickSpy}
menuItems={ <MenuItem primaryText={<span id="nested">nested</span>} /> }/>
</Menu></MuiThemeProvider>
);
simulateEvent(component.find("#test"), "touchTap");
expect(component.find("#test").exists()).toBe(true); // Works fine.
expect(testOnClickSpy.callCount).toBe(1); // Works fine.
component.update(); // <--- Does not seem to do anything?
expect(component.find("#nested").exists()).toBe(true); // <--- Because this item cannot be found?
})
})
I use this simulateEventto simulate the touch:
require("react-tap-event-plugin")();
function simulateEvent(wrappedTarget, eventType) {
const domNode = findDOMNode(wrappedTarget["node"]);
Simulate[eventType](domNode);
}
I am using React 15.6.1, material-ui 0.18.6, Enzyme 2.9.1 Jest 20.0.4
Maybe connected? React, Jest and Material-UI: how to test content displayed in modal or popover
source
share