Container Component Testing - Tuong-Nguyen/JavaScript-Structure GitHub Wiki

Libraries

  • Mocha
  • Expect
  • Enzyme

What to test

  • Markup
  • Behavior

Steps

Export unconnected component

  • Container component has 2 parts: unconnected component and default exported connect
  • Need export the unconnected component to test
export class ManageCoursePage extends React.Component {}

Mount the unconnected component

const wrapper = mount(<ManageCoursePage {...props}/>);

Use React wrapper

Find the element

const saveButton = wrapper.find('input').last();

Simulate an action

.simulate(event[, mock]): returns itself, ReactWrapper

  • event: the event name to be simulated
  • mock: a mock event object that will be merged with the event object passed to the handlers.
saveButton.simulate('click');

Expect for assertion

expect(wrapper.state().errors.title).toBe('Title must be at least 5 characters');