Skip to content

Commit

Permalink
Merge pull request #190 from optimizely/jordan/fix-isCached-for-empty…
Browse files Browse the repository at this point in the history
…-store-states

Fix isCached function for empty store states
  • Loading branch information
jordangarcia committed Nov 5, 2015
2 parents 77ced5e + d44c8a5 commit 69f8087
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/reactor/fns.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,13 @@ function isCached(reactorState, keyPathOrGetter) {
return false
}

return entry.get('storeStates').every((stateId, storeId) => {
const storeStates = entry.get('storeStates')
if (storeStates.size === 0) {
// if there are no store states for this entry then it was never cached before
return false
}

return storeStates.every((stateId, storeId) => {
return reactorState.getIn(['storeStates', storeId]) === stateId
})
}
Expand Down
55 changes: 55 additions & 0 deletions tests/reactor-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,61 @@ describe('Reactor', () => {

expect(mockFn.calls.count()).toEqual(0)
})

it('should trigger an observer for a late registered store', () => {
var mockFn = jasmine.createSpy()
var reactor = new Reactor()
reactor.observe(['test'], mockFn)

expect(mockFn.calls.count()).toEqual(0)

reactor.registerStores({
test: Store({
getInitialState() {
return 1
}
})
})

expect(mockFn.calls.count()).toEqual(1)
expect(mockFn.calls.argsFor(0)).toEqual([1])
})

it('should trigger an observer for a late registered store for the identity getter', () => {
var mockFn = jasmine.createSpy()
var reactor = new Reactor()
reactor.observe([], mockFn)

expect(mockFn.calls.count()).toEqual(0)

reactor.registerStores({
test: Store({
getInitialState() {
return 1
},
initialize() {
this.on('increment', (state) => state + 1)
}
})
})

// it should call the observer after the store has been registered
expect(mockFn.calls.count()).toEqual(1)
var observedValue = mockFn.calls.argsFor(0)[0]
var expectedHandlerValue = Map({
test: 1
})
expect(is(observedValue, expectedHandlerValue)).toBe(true)

// it should call the observer again when the store handles an action
reactor.dispatch('increment')
expect(mockFn.calls.count()).toEqual(2)
var observedValue = mockFn.calls.argsFor(1)[0]
var expectedHandlerValue = Map({
test: 2
})
expect(is(observedValue, expectedHandlerValue)).toBe(true)
})
})

describe('#unobserve', () => {
Expand Down

0 comments on commit 69f8087

Please sign in to comment.