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']}, 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))
})

@@ -310,16 +310,57 @@
 lValue'%5D
+, stringSpace: null, immutableSpace: null
 %7D, actio
@@ -367,16 +367,16 @@
 n) =%3E %7B%0A
-
     acti
@@ -723,11 +723,9 @@
 tate
-, %7B
+.
 arra
@@ -730,25 +730,24 @@
 raySpace
-:
+,
  %5B1, 2%5D
-%7D
 )%0A%7D)%0A%0Ate
@@ -1269,24 +1269,24 @@
 .getState()%0A
-
   t.truthy(s
@@ -1327,12 +1327,250 @@
 leData))%0A%7D)%0A
+%0Atest('does not rehydrate unknown state keys', (t) =%3E %7B%0A  let store = finalCreateStore(createReducer())%0A  let someData = 1%0A  store.dispatch(rehydrate(%7BsomeData%7D))%0A  let state = store.getState()%0A  t.truthy(state.someData === undefined)%0A%7D)%0A

fix rehydrate tests, add unknown key test