Day 2, Segment 4

In notebook:
FrontEndMasters React Intro 2
Created at:
2016-12-28
Updated:
2016-12-29
Tags:
libraries React JavaScript
branch v2-21-2

If we run npm test everything is broken now because we've introduced Redux. All tests are failing. Redux is injected by the Router [which is not present in the tests]. We have to find a way to work around this.
01:00

Search.js
  // ****   Search.js   ****
...

// at the end:
const mapStateToProps = (state) => {
  return {
    searchTerm: state.searchTerm
  }
}

// 1. ++++ the typical solution
// this is for testing purposes
export const Unwrapped = Search

// connect expects Redux to be there
export default connect(mapStateToProps)(Search)
Now, import unwrapped into the spec and use instead of ​search​ (which relies on Redux). 
  // ****   Search.spec.js   ****

import React from 'react'
import { shallow } from 'enzyme'
import { shallowToJson } from 'enzyme-to-json'
// 1. ++++ do the import
import Search, { Unwrapped as UnwrappedSearch } from './Search'
import ShowCard from './ShowCard'
import preload from '../public/data.json'

test('Search snapshot test', () => {
  // 2. ++++ then use UnwrappedSearch everywhere
  // pass to this what Redux would have passed it
  // (shows={preload.shows} searchTerm='') ← replace Redux ↴
  const component = shallow(<UnwrappedSearch shows={preload.shows} searchTerm='' />)
  const tree = shallowToJson(component)
  expect(tree).toMatchSnapshot()
})

test('Search should render a ShowCard for each show', () => {
  // 2. ++++ then use UnwrappedSearch everywhere
  const component = shallow(<UnwrappedSearch searchTerm='' shows={preload.shows} />)
  expect(component.find(ShowCard).length).toEqual(preload.shows.length)
})

test('Search should render correct amount of shows based on search', () => {
  const searchWord = 'house'
  store.dispatch(setSearchTerm(searchWord))
  const component = render(<Provider store={store}><Search shows={preload.shows} /></Provider>)
  const showCount = preload.shows.filter((show) => `${show.title} ${show.description}`.toUpperCase().indexOf(searchWord.toUpperCase()) >= 0).length
  expect(component.find('.show-card').length).toEqual(showCount)
})
The test fails, but just because since the last snapshot, we've abstracted out the Header. We can create a new snapshot to accept the change. (At this point he uncommented the last two tests)

This test was easy to fix. To other components will be more complicated as they rely more in Redux.
We will have to fake out Redux:
  // ****   Search.spec.js   ****

import React from 'react'
// 1. ++++ import Provider
import { Provider } from 'react-redux'
// 3. ++++ also get render (to get the extra depth)
import { shallow, render } from 'enzyme'
import { shallowToJson } from 'enzyme-to-json'
// 2. ++++ import store and setSearchTerm
import store from './store'
import { setSearchTerm } from './actionCreators'
// 3. ++++ also get Search
import Search, { Unwrapped as UnwrappedSearch } from './Search'
import ShowCard from './ShowCard'
import preload from '../public/data.json'

test('Search snapshot test', () => {
  const component = shallow(<UnwrappedSearch shows={preload.shows} searchTerm='' />)
  const tree = shallowToJson(component)
  expect(tree).toMatchSnapshot()
})

// use UnwrappedSearch here too
test('Search should render a ShowCard for each show', () => {
  const component = shallow(<UnwrappedSearch searchTerm='' shows={preload.shows} />)
  expect(component.find(ShowCard).length).toEqual(preload.shows.length)
})

// 4. ++-- first uncomments this test
test('Search should render correct amount of shows based on search', () => {
  const searchWord = 'house'
  // use store ↴
  // basically we're dispaptching an action to the store
  // and testing the result of that
  store.dispatch(setSearchTerm(searchWord))
  const component = render(<Provider store={store}><Search shows={preload.shows} /></Provider>)
  const showCount = preload.shows.filter((show) => `${show.title} ${show.description}`.toUpperCase().indexOf(searchWord.toUpperCase()) >= 0).length
  // just use the CSS class as selector ↴
  expect(component.find('.show-card').length).toEqual(showCount)
})
branch v2-22

Testing the Reducers

Reducers are extremely easy to test. There's no state, just input and output. So easy in fact, that the dev tools can generate the tests for you. 
  1. Does one atomic action (pastes "house" in the search field)
  2. Selects the action in Redux Dev Tools 
  3. "Test" tab contains the test (can use Jest, Mocha, etc)
  4. Update the paths (module import)
  5. Clean the formatting to pass Standard linter rules
Grabs a few tests from the  dev tools:
  //  ****  reducers.spec.js    ****

import reducers from './reducers'

test('@@INIT', () => {
  let state
  state = reducers(undefined, {})
  expect(state).toEqual({searchTerm: '', omdbData: {}})
})

test('SET_SEARCH_TERM', () => {
  let state
  state = reducers({searchTerm: '', omdbData: {}}, {type: 'SET_SEARCH_TERM', searchTerm: 'house'})
  expect(state).toEqual({searchTerm: 'house', omdbData: {}})
})

test('ADD_OMDB_DATA', () => {
  let state
  state = reducers({searchTerm:'',omdbData:{}}, {type:'ADD_OMDB_DATA',imdbID:'tt3986586',omdbData:{Title:'Fuller House',Year:'2016–',Rated:'TV-G',Released:'26 Feb 2016',Runtime:'30 min',Genre:'Comedy, Family',Director:'N/A',Writer:'Jeff Franklin',Actors:'Candace Cameron Bure, Jodie Sweetin, Andrea Barber, Michael Campion',Plot:'In a continuation of Full House, D.J. Fuller is a mother of three young boys and is a recent widow. D.J.\'s sister Stephanie, her best friend Kimmy and Kimmy\'s teenage daughter all move in to help raise her sons. The house is now a lot fuller.',Language:'English',Country:'USA',Awards:'1 win.',Poster:'https://images-na.ssl-images-amazon.com/images/M/MV5BMTU2NzA0ODAyMF5BMl5BanBnXkFtZTgwNDE5MTIxMDI@._V1_SX300.jpg',Metascore:'N/A',imdbRating:'7.2',imdbVotes:'13,415',imdbID:'tt3986586',Type:'series',totalSeasons:'2',Response:'True'}})
  expect(state).toEqual({searchTerm:'',omdbData:{tt3986586:{Title:'Fuller House',Year:'2016–',Rated:'TV-G',Released:'26 Feb 2016',Runtime:'30 min',Genre:'Comedy, Family',Director:'N/A',Writer:'Jeff Franklin',Actors:'Candace Cameron Bure, Jodie Sweetin, Andrea Barber, Michael Campion',Plot:'In a continuation of Full House, D.J. Fuller is a mother of three young boys and is a recent widow. D.J.\'s sister Stephanie, her best friend Kimmy and Kimmy\'s teenage daughter all move in to help raise her sons. The house is now a lot fuller.',Language:'English',Country:'USA',Awards:'1 win.',Poster:'https://images-na.ssl-images-amazon.com/images/M/MV5BMTU2NzA0ODAyMF5BMl5BanBnXkFtZTgwNDE5MTIxMDI@._V1_SX300.jpg',Metascore:'N/A',imdbRating:'7.2',imdbVotes:'13,415',imdbID:'tt3986586',Type:'series',totalSeasons:'2',Response:'True'}}})
})
He usually doesn't test actionCreators and thunks. 

22:00