Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] createContext() not supported in enzyme. #1958

Closed
hyochan opened this issue Dec 31, 2018 · 4 comments · Fixed by #1966
Closed

[Feature Request] createContext() not supported in enzyme. #1958

hyochan opened this issue Dec 31, 2018 · 4 comments · Fixed by #1966
Projects

Comments

@hyochan
Copy link

hyochan commented Dec 31, 2018

I'd like to know how to test provider for context api. I've found some tutorials on testing consumer but no tutorial exists for testing provider itself.

import React, { createContext } from 'react';
import { Text, TouchableOpacity, View } from 'react-native';
import ProfileModal from '../components/shared/ProfileModal';

const ProfileModalContext = createContext();

export const ProfileModalConsumer = ProfileModalContext.Consumer;

export class ProfileModalProvider extends React.Component {
  static modal;

  state = {
    user: null,
  };

  actions = {
    setModal: (v) => {
      console.log('setModal', v);
      this.modal = v;
    },
    showModal: (user) => {
      console.log('showModal');
      this.setState({ user }, () => {
        if (this.modal) {
          console.log('openModal');
          this.modal.showAddBtn(true);
          this.modal.open();
        }
      });
    },
    onChatPressed: (navigation) => {
      if (this.modal) {
        this.modal.close();
      }
      navigation.navigate('Chat');
    }
  };

  render() {
    const { state, actions, modal } = this;
    const value = { state, actions, modal };
    return (
      <ProfileModalContext.Provider value={value}>
        {this.props.children}
        <ProfileModal
          id='profile_modal'
          ref={(v) => {
            console.log('v', v);
            this.modal = v;
          }}
          onChatPressed={ () => this.actions.onChatPressed(this.props.navigation) }
        />
      </ProfileModalContext.Provider>
    );
  }
}

I've tried making test code for above provder.

import 'react-native';
import * as React from 'react';
import { ProfileModalProvider } from '../ProfileModalProvider';

// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';
import { shallow, render } from 'enzyme';

describe('rendering test', () => {
  const wrapper = shallow(
    <ProfileModalProvider />,
  ).dive();

  it('renders as expected', () => {
    expect(wrapper).toMatchSnapshot();
  });
});

describe('interaction', () => {
  const context = {
    state: {
      user: null,
    },
    actions: {
      setModal: jest.fn(),
      showModal: jest.fn(),
      onChatPressed: jest.fn(),
    }
  };
  let props;
  let wrapper;
  beforeEach(() => {
    props = {
      navigation: {
        navigate: jest.fn(),
      },
      ...context,
    };
    wrapper = shallow(<ProfileModalProvider props={props}/>);
  });
  describe('clicking the button', () => {
    it('should call onChatPressed', () => {
      const btn = wrapper.find('#profile_modal');
      btn.props().onPress();
      expect(context.actions.onChatPressed).toBeCalled();
    });
  });
});

The result.
image

I am trying to work on open source project(talktalk-rn). The code is provided there if you need more information.

@ljharb
Copy link
Member

ljharb commented Dec 31, 2018

enzyme does not yet support the createContext API.

@hyochan
Copy link
Author

hyochan commented Feb 12, 2019

For those who are interested in this, we've tried to move createContext() separately somewhere like src/contexts and done the rest of the test. Here is the sourcecode and the project.

@hyochan hyochan closed this as completed Feb 12, 2019
@ljharb
Copy link
Member

ljharb commented Feb 12, 2019

I’m going to keep this open, pending support of createContext.

@ljharb ljharb reopened this Feb 12, 2019
@hyochan hyochan changed the title [Question] How to test context api provider with enzyme? [Feature Request] createContext() not supported in enzyme. Feb 20, 2019
@sol404
Copy link

sol404 commented Feb 21, 2019

Waiting this feature request, here's a workaround work for me.

const outer = shallow(<SimpleComp />);
const Children = outer.props().children({ /* context */ });
const wrapper = shallow(Children);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
React 16
  
Context
Development

Successfully merging a pull request may close this issue.

3 participants