indexiatech / redux-immutablejs

Redux Immutable facilities.
BSD 3-Clause "New" or "Revised" License
684 stars 36 forks source link

Having trouble providing initial state to reducers #10

Closed kwhitaker closed 9 years ago

kwhitaker commented 9 years ago

I'm trying to write a reducer, but my tests keep failing.

Here's the reducer:

import Immutable from 'immutable';
import { createReducer } from 'redux-immutablejs';
import * as types from '../constants/CharacterActionTypes';

const initialState = Immutable.fromJS(localStorage.characters || []);

export const characters = createReducer(initialState, {
  [types.GET_CHARACTERS]: (state) => state
});

And my test:

import Immutable from 'immutable';
import { combineReducers } from 'redux-immutablejs';
import * as types from '../../constants/CharacterActionTypes';
import * as characters from '../CharacterReducers';

describe('Character reducers', () => {
  const testReducer = combineReducers(characters);
  const defaultState = Immutable.fromJS([
    {
      id: 1,
      name: 'Test Character'
    }
  ]);

  it(`should handle ${types.GET_CHARACTERS}`, () => {
    expect(testReducer(defaultState, {
      type: types.GET_CHARACTERS
    }).toJS()).toEqual(defaultState.toJS());
  });
});

That test fails with the following message:

Expected Object({ characters: [  ] }) to equal [ Object({ id: 1, name: 'Test Character' }) ].

I'm sure I'm just missing something obvious. Can you give me some idea of how to structure my reducer so this functions as expected?

Thanks

kwhitaker commented 9 years ago

Found the fix - I had forgotten to specify a domain in my test state:

  const defaultState = Immutable.fromJS({
    characters: [
      {
        id: 1,
        name: 'Test Character'
      }
    ]
  });
asaf commented 9 years ago

:+1: