diff --git a/src/alt/utils/StateFunctions.js b/src/alt/utils/StateFunctions.js index 4c54538a..32ecf23e 100644 --- a/src/alt/utils/StateFunctions.js +++ b/src/alt/utils/StateFunctions.js @@ -8,8 +8,12 @@ export function setAppState(instance, data, onStore) { const { config } = store.StoreModel const state = store.state if (config.onDeserialize) obj[key] = config.onDeserialize(value) || value - fn.eachObject(k => delete state[k], [state]) - fn.assign(state, obj[key]) + if (Object.prototype.toString.call(state) === '[object Object]') { + fn.eachObject(k => delete state[k], [state]) + fn.assign(state, obj[key]) + } else { + store.state = obj[key] + } onStore(store) } }, [obj]) diff --git a/src/utils/reducers.js b/src/utils/reducers.js index 10724ce7..1fe814a1 100644 --- a/src/utils/reducers.js +++ b/src/utils/reducers.js @@ -4,6 +4,7 @@ function getId(x) { return x.id || x } +/* istanbul ignore next */ function shallowEqual(a, b) { if (typeof a !== 'object' || typeof b !== 'object') return a === b if (a === b) return true diff --git a/test/reducer-test.js b/test/reducer-test.js index 12787e06..e67cff4d 100644 --- a/test/reducer-test.js +++ b/test/reducer-test.js @@ -13,7 +13,7 @@ const store = alt.createStore({ displayName: 'ValueStore', reduce: combine( - reduceWith([actions.fire], (state, payload) => { + reduceWith([actions.fire, actions.BAR], (state, payload) => { return state + 1 }) ) @@ -32,7 +32,6 @@ export default { actions.fire() actions.foo() actions.bar() - actions.fire() assert(store.getState() === 23, 'state is correct') assert.ok(spy.calledTwice, 'spy was only called twice') diff --git a/test/value-stores-test.js b/test/value-stores-test.js index e1588d19..78f3d098 100644 --- a/test/value-stores-test.js +++ b/test/value-stores-test.js @@ -16,6 +16,16 @@ const store = alt.createStore({ } }) +const store2 = alt.createStore({ + state: [1, 2, 3], + + displayName: 'Value2Store', + + reduce(state, payload) { + return state.concat(state[state.length - 1] + 1) + } +}) + export default { 'value stores': { beforeEach() { @@ -26,8 +36,9 @@ export default { assert(store.state === 21, 'store state is value') assert(store.getState() === 21, 'getState returns value too') - store.listen((state) => { + const unlisten = store.listen((state) => { assert(state === 22, 'incremented store state') + unlisten() done() }) @@ -35,5 +46,11 @@ export default { actions.fire() }, + + 'store with array works too'() { + assert.deepEqual(store2.state, [1, 2, 3]) + actions.fire() + assert.deepEqual(store2.state, [1, 2, 3, 4]) + } } }