2

I want to test if component renders inside render function. Component is wrapped inside Consumer:

const TestComponent = () => (
  <div>
<ItemsContext.Consumer>
            {(value) => (
              <Autos
                autoEnabled={value.autoEnabled}
              />
            )}
          </ItemsContext.Consumer>
  </div>
);

I set context in the upper component:

const AutosWrapper = () => (

      <ItemsContext.Provider value={{ autoEnabled: false, sourcesEnabled: true }}>
        <TestComponent/>
      </ItemsContext.Provider>

)

My test is as follows:

import ItemsContext from '../ItemsContext';

describe('<TestComponent /> Component render', () => {

  let wrapper;

  beforeEach(() => {
    wrapper = shallow(<TestComponent {...props} />);
  });

  describe('<TestComponent /> rendering', () => {


    test('should render child components in <TestComponent /> component', () => {
      const autoContextWrapper = wrapper.find(ItemsContext.Consumer).at(0)
        .renderProp('children')();

      expect(autoContextWrapper.find(Autos).length).toEqual(1);
    });

  });
});

but when I run this test, I have following error - TypeError: Cannot read property 'autoEnabled' of undefined I cannot understand how to pass default values to the context or mock them. I tried such a variant but without success:

ItemsContext.Consumer = jest.fn((props) => props.children({ autoEnabled: false }));

1 Answer 1

1

You will have to wrap your component with the provider so that the consumer can access the context. This is especially useful as this enables you to change the context and do assertions based on the given context value.

Example:

import ItemsContext from '../ItemsContext';

describe('<TestComponent /> Component render', () => {
  describe('<TestComponent /> rendering', () => {
    test('should render child components in <SearchResult /> component', () => {
      const wrapper = shallow((
        <ItemsContext.Provider value={{ autoEnabled: true, sourcesEnabled: true }}>
          <TestComponent />
        </ItemsContext.Provider>
      ));

      const autoContextWrapper = wrapper
        .find(ItemsContext.Consumer)
        .at(0)
        .renderProp('children')();

      expect(autoContextWrapper.find(Topics).length).toEqual(1);
    });
  });
});
Sign up to request clarification or add additional context in comments.

3 Comments

I have an error - Method “renderProp” is meant to be run on 1 node. 0 found instead. In the debug method I do not see that component renders at all
I added dive() to shallow render in order to fix error “renderProp” is meant to be run on 1 node. 0 found instead', but now I have initial error during tests 'TypeError: Cannot read property 'autoEnabled' of undefined'
Sorry for the late response, would it be possible to create a code sandbox where this error can be reproduced? I know from experience it can be tricky to get Enzyme working with wrapped components (e.g. providers). Material-UI is using the following function in their tests: github.com/mui-org/material-ui/blob/master/packages/material-ui/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.