const props = { disableTooltip: disableTooltipMock };
const wrapper = shallow(<PlannerTooltip {...props} />);
wrapper.props()
returns an object with all the props: {children: "Hello", className: "foo bar", includedProp="Success!"}
wrapper.prop('includedProp')
returns the value in that specific prop
Using a mock to spy on as a property to a component
const disableTooltipMock = jest.fn();
const wrapper = shallow(<PlannerTooltip onClick={disableTooltipMock} />);
it('should call disableTooltip', () => {
expect(disableTooltipMock).toHaveBeenCalled();
});
it('should call disableTooltip with parameter Wonder Woman', () => {
expect(disableTooltipMock).toHaveBeenCalledWith('Wonder Woman');
});
it('should call disableTooltip 2 times', () => {
expect(disableTooltipMock).toHaveBeenCalledTimes(2);
});
Finds everything on this
in the component.
const wrapper = shallow(<PlannerTooltip />);
const instance = wrapper.instance();
expect(instance.isOpen).toEqual(false);
const wrapper = shallow(<PlannerTooltip />);
const instance = wrapper.instance();
instance.openModal();
expect(instance.state.isOpen).toEqual(true);
wrapper.setProps({ activePlannerPage: 'LAYOUT' });
const wrapper = shallow(); wrapper.simulate('click');
Create the component, then set props
wrapper.setProps({ activePlannerPage: 'LAYOUT' });
You must do mount
instead of shallow to test componentDidUpdate. This will also run other lifecycle, for example componentWillReceiveProps
const wrapper = mount(<PlannerTooltip />);
wrapper.setProps({ activePlannerPage: 'LAYOUT' });
jest.useFakeTimers();
jest.runTimersToTime(5000);
jest.useRealTimers();
Det är något irriterande med att det inte går att mocka dependencies per test.
Om man gör mock på en dependency import, och sen gör "afterEach" reset all mocks, hur funkar det??