I want to test following code with Jest. I am simply trying to mock the MenuItem
component to see how many times it has been called, but instead I'm receiving an error.
Also I came across this question but Material-ui rendered component with weird names.
Then I ended up here but none of pre answered question work for me.
{
menuItems.map(menuItem => (
<MenuItem key={menuItem.value} value={menuItem.value}>
{menuItem.description}
</MenuItem>
))
}
test.js
import React from 'react';
import MenuItem from '@material-ui/core/MenuItem';
import { SimpleSelect } from './SimpleSelect';
import { shallowWithTheme, mountWithTheme } from '../helper';
describe('SimpleSelect component', () => {
jest.mock('@material-ui/core/MenuItem', () => jest.fn(() => {}));
let callback;
beforeEach(() => {
callback = jest.fn();
});
it('should render menu item', () => {
const menuItems = [
{
value: '12',
description: 'Description 123',
},
{
value: '456',
description: 'Description 456',
},
];
mountWithTheme(<SimpleSelect className="test" label="test" selected="default" onChange={callback} menuItems={menuItems} />);
expect(MenuItem).toHaveBeenCalled();
});
});
Edited
expect(wrapper.find(MenuItem)).toHaveLength(2);
expect(wrapper.find(MenuItem).length).toHaveLength(2);
expect(wrapper.find(MenuItem).length).toBe(2);
Error
Edited: 14 Dec, 19
export const shallowWithTheme = children => (
shallow(children, { theme })
);
export const mountWithTheme = (children, options) => (
mount(<ThemeProvider theme={theme}>{children}</ThemeProvider>, options)
);
styled-component version 4
"styled-components": "^4.1.1"
wrapper.debug() output
with shallowWrapper