Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resetRecomputations method to assist unit testing #90

Merged
merged 2 commits into from
Mar 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,11 @@ suite('selector', () => {
})
```

Finally, each selector has a `resetRecomputations` method that sets
recomputations back to 0. The intended use is for a complex selector that may
have many independent tests and you don't want to manually manage the
computation count or create a "dummy" selector for each test.

### Q: How do I use Reselect with Immutable.js?

A: Selectors created with `createSelector` should work just fine with Immutable.js data structures.
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export function createSelectorCreator(memoize, ...memoizeOptions) {
}

selector.recomputations = () => recomputations
selector.resetRecomputations = () => recomputations = 0
return selector
}
}
Expand Down
20 changes: 20 additions & 0 deletions test/test_selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,24 @@ suite('selector', () => {
assert.strictEqual(selector({ a: 1, b: 2 }), firstResult)
assert.deepEqual(selector({ a: 2, b: 2 }), { x: 2, y: 2 })
})
test('resetRecomputations', () => {
const selector = createSelector(
state => state.a,
a => a
)
assert.equal(selector({ a: 1 }), 1)
assert.equal(selector({ a: 1 }), 1)
assert.equal(selector.recomputations(), 1)
assert.equal(selector({ a: 2 }), 2)
assert.equal(selector.recomputations(), 2)

selector.resetRecomputations()
assert.equal(selector.recomputations(), 0)

assert.equal(selector({ a: 1 }), 1)
assert.equal(selector({ a: 1 }), 1)
assert.equal(selector.recomputations(), 1)
assert.equal(selector({ a: 2 }), 2)
assert.equal(selector.recomputations(), 2)
})
})