Skip to content

Commit 069d641

Browse files
committed
Merge pull request #200 from iamdustan/breaking-changes-test-id-incrementer
Modify test case ID generation for reducer changing
2 parents 72aa974 + 667d8f5 commit 069d641

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

test/Store.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ describe('Store', () => {
9595

9696
store.dispatch(addTodo('Perhaps'));
9797
expect(store.getState()).toEqual([{
98-
id: 2,
98+
id: 3,
9999
text: 'Perhaps'
100100
}, {
101101
id: 1,
@@ -108,7 +108,7 @@ describe('Store', () => {
108108
nextStore = new Store(todos);
109109
store.replaceReducer(nextStore.getReducer());
110110
expect(store.getState()).toEqual([{
111-
id: 2,
111+
id: 3,
112112
text: 'Perhaps'
113113
}, {
114114
id: 1,
@@ -120,7 +120,7 @@ describe('Store', () => {
120120

121121
store.dispatch(addTodo('Surely'));
122122
expect(store.getState()).toEqual([{
123-
id: 2,
123+
id: 3,
124124
text: 'Perhaps'
125125
}, {
126126
id: 1,
@@ -129,7 +129,7 @@ describe('Store', () => {
129129
id: 2,
130130
text: 'World'
131131
}, {
132-
id: 3,
132+
id: 4,
133133
text: 'Surely'
134134
}]);
135135
});

test/helpers/reducers.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import { ADD_TODO } from './actionTypes';
22

3+
4+
function id(state = []) {
5+
return state.reduce((result, item) => (
6+
item.id > result ? item.id : result
7+
), 0) + 1;
8+
}
9+
310
export function todos(state = [], action) {
411
switch (action.type) {
512
case ADD_TODO:
613
return [...state, {
7-
id: state.length ? state[state.length - 1].id + 1 : 1,
14+
id: id(state),
815
text: action.text
916
}];
1017
default:
@@ -16,7 +23,7 @@ export function todosReverse(state = [], action) {
1623
switch (action.type) {
1724
case ADD_TODO:
1825
return [{
19-
id: state.length ? state[0].id + 1 : 1,
26+
id: id(state),
2027
text: action.text
2128
}, ...state];
2229
default:

0 commit comments

Comments
 (0)