N
N
Ninja Mate2017-10-03 21:06:41
Software testing
Ninja Mate, 2017-10-03 21:06:41

How to Jest test such a reducer?

There is such a reducer

export const sdk = new NPROneSDK()

// Nearby
export const NPRONE_NEAR_BY = 'nprone/NPRONE_NEARBY'
export const getNearBy = () => dispatch => {
  console.info('getNearBy')

  return sdk.searchStations('94708')
    .then(nearBy => {
      dispatch(setNearBy(nearBy))
    })
    .catch(e => {
      onNearByError(e)
      throw (e)
    })
}
const setNearBy = (payload) => {
  console.info('setNearBy', payload)
  return {
    type: NPRONE_NEAR_BY,
    payload
  }
}
const onNearByError = (error) => {
  console.error('onNearByError', error)
}


// ------------------------------------
// Action Handlers
// ------------------------------------
const ACTION_HANDLERS = {
  [NPRONE_NEAR_BY]: (state, action) => Object.assign({}, state, {nearBy: action.payload})
}

const initialState = {
  nearBy: []
}
export default function nprOneReducer (state = initialState, action) {
  const handler = ACTION_HANDLERS[action.type]
  return handler ? handler(state, action) : state
}

Can't do the test.
import {NPRONE_NEAR_BY} from '../src/store/reducers/nprone'
import * as actions from '../src/store/reducers/nprone'

describe('actions', () => {
  it('should create an action to add a todo', () => {
    const expectedAction = {
      type: NPRONE_NEAR_BY
    }
    expect(actions.getNearBy()).toEqual(expectedAction)
  })
})

Gives this error

import { NPRONE_NEAR_BY } from '../src/store/reducers/nprone';
^^^^^^

What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Danakt Frost, 2017-10-03
@Danakt

To write tests for jest with es6 imports that the node does not accept, you need to transpile the code using babel-jest , which will turn es6 imports into commonjs.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question