import test from 'ava'

import { compose, createStore } from 'redux'
import Immutable from 'immutable'

import { REHYDRATE } from '../src/constants'
import { autoRehydrate } from '../src'

const someString = 'someString'

const createReducer = (actionCallback) => {
  return (state = {arraySpace: ['someInitialValue'], stringSpace: null, immutableSpace: null}, action) => {
    actionCallback && actionCallback(action)
    return state
  }
}

var finalCreateStore = compose(autoRehydrate())(createStore)

test('rehydrate: with array payload should overwrite substate', (t) => {
  let store = finalCreateStore(createReducer())
  store.dispatch(rehydrate({arraySpace: [1, 2]}))
  let state = store.getState()
  t.deepEqual(state.arraySpace, [1, 2])
})

test('rehydrate: rehydrates string state', (t) => {
  let store = finalCreateStore(createReducer())
  store.dispatch(rehydrate({stringSpace: someString}))
  let state = store.getState()
  t.deepEqual(state.stringSpace, someString)
})

const rehydrate = (payload) => ({type: REHYDRATE, payload})

test('can rehydrating immutable state', (t) => {
  let store = finalCreateStore(createReducer())
  let immutableData = Immutable.Map({a: 1})
  store.dispatch(rehydrate({immutableSpace: immutableData}))
  let state = store.getState()
  t.truthy(state.immutableSpace.equals(immutableData))
})

test('does not rehydrate unknown state keys', (t) => {
  let store = finalCreateStore(createReducer())
  let someData = 1
  store.dispatch(rehydrate({someData}))
  let state = store.getState()
  t.truthy(state.someData === undefined)
})

@@ -29,17 +29,8 @@
 rt %7B
- compose,
  cre
@@ -53,16 +53,16 @@
 'redux'%0A
+
 import I
@@ -435,35 +435,81 @@
 var 
-finalCreateStore = compose(
+configureStore = (reducer, initState) =%3E createStore(reducer, initState, 
 auto
@@ -524,21 +524,8 @@
 e())
-(createStore)
 %0A%0Ate
@@ -605,34 +605,32 @@
 let store = 
-finalCreat
+configur
 eStore(creat
@@ -762,24 +762,230 @@
 %5B1, 2%5D)%0A%7D)%0A%0A
+test('rehydrate: should work with initState', (t) =%3E %7B%0A  let store = configureStore(createReducer(), %7Ba: 1%7D)%0A  store.dispatch(rehydrate(%7Ba: 2%7D))%0A  let state = store.getState()%0A  t.deepEqual(state.a, 2)%0A%7D)%0A%0A
 test('rehydr
@@ -1034,34 +1034,32 @@
 let store = 
-finalCreat
+configur
 eStore(creat
@@ -1325,34 +1325,32 @@
 let store = 
-finalCreat
+configur
 eStore(creat
@@ -1629,18 +1629,16 @@
 e = 
-finalCreat
+configur
 eSto

add initialState test