Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Testing React Components

Testing React Components

Andrei Picus

April 28, 2015
Tweet

Other Decks in Programming

Transcript

  1. What? 1. How to write good unit tests. 2. How

    to unit test a React component. 3. How to write the tests. 4. How to run the tests.
  2. What’s a unit test? A test, whose result (pass or

    fail) depends on the correctness of only one interesting piece of behaviour.
  3. Testing a client-server relation Do I ask the interface the

    right questions? Do I correctly handle all of the responses? Do I respond correctly to the questions? Do I try to respond to the questions? Contract C S INTERFACE Collaboration
  4. Inter-component communication Do I send the correct props? C Do

    I call the right callbacks? Do I react correctly to the new props? Do I implement the callback? API P
  5. Intra-component communication Do I compute the correct state? render() Component

    Do I call the right callbacks? Do I render the right things? Do I implement the callback? API
  6. Mocking component methods You need to do it on the

    component’s class before rendering.
  7. Mocking children • Mock the NodeJS module. • Have a

    mixin/lib that loads a component and mock that. • Shallow render.
  8. Setup a component Put it into a certain state and

    then assert stuff on it. Don’t do two actions that mutate state in a single test => create another test where you load the component in the 2nd state