Skip to content

Commit

Permalink
100% coverage, new tests, and fixes a bug with recycle
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed Aug 6, 2015
1 parent 6d44fd0 commit 44d5814
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/alt/utils/StateFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
1 change: 1 addition & 0 deletions src/utils/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions test/reducer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
)
Expand All @@ -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')
Expand Down
19 changes: 18 additions & 1 deletion test/value-stores-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -26,14 +36,21 @@ 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()
})

assert(JSON.parse(alt.takeSnapshot()).ValueStore === 21, 'snapshot ok')

actions.fire()
},

'store with array works too'() {
assert.deepEqual(store2.state, [1, 2, 3])
actions.fire()
assert.deepEqual(store2.state, [1, 2, 3, 4])
}
}
}

0 comments on commit 44d5814

Please sign in to comment.