Skip to content

Commit

Permalink
state preservation when replacing a reducer: continue incrementing th…
Browse files Browse the repository at this point in the history
…e id
  • Loading branch information
iamdustan committed Jul 1, 2015
1 parent 34d90b9 commit 4c3101c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
8 changes: 4 additions & 4 deletions test/Store.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ describe('Store', () => {

store.dispatch(addTodo('Perhaps'));
expect(store.getState()).toEqual([{
id: 2,
id: 3,
text: 'Perhaps'
}, {
id: 1,
Expand All @@ -108,7 +108,7 @@ describe('Store', () => {
nextStore = new Store(todos);
store.replaceReducer(nextStore.getReducer());
expect(store.getState()).toEqual([{
id: 2,
id: 3,
text: 'Perhaps'
}, {
id: 1,
Expand All @@ -120,7 +120,7 @@ describe('Store', () => {

store.dispatch(addTodo('Surely'));
expect(store.getState()).toEqual([{
id: 2,
id: 3,
text: 'Perhaps'
}, {
id: 1,
Expand All @@ -129,7 +129,7 @@ describe('Store', () => {
id: 2,
text: 'World'
}, {
id: 3,
id: 4,
text: 'Surely'
}]);
});
Expand Down
10 changes: 8 additions & 2 deletions test/helpers/reducers.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { ADD_TODO } from './actionTypes';


var id = (state = []) =>
state.reduce((id, item) => (
item.id > id ? item.id : id
), 0) + 1;

export function todos(state = [], action) {
switch (action.type) {
case ADD_TODO:
return [...state, {
id: state.length ? state[state.length - 1].id + 1 : 1,
id: id(state),
text: action.text
}];
default:
Expand All @@ -16,7 +22,7 @@ export function todosReverse(state = [], action) {
switch (action.type) {
case ADD_TODO:
return [{
id: state.length ? state[0].id + 1 : 1,
id: id(state),
text: action.text
}, ...state];
default:
Expand Down

0 comments on commit 4c3101c

Please sign in to comment.