File tree Expand file tree Collapse file tree 2 files changed +13
-6
lines changed Expand file tree Collapse file tree 2 files changed +13
-6
lines changed Original file line number Diff line number Diff 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 } ) ;
Original file line number Diff line number Diff line change 11import { 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+
310export 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 :
You can’t perform that action at this time.
0 commit comments