@@ -54,12 +54,22 @@ describe('Combined entity slice', () => {
54
54
reducers : {
55
55
addOne : adapter . addOne ,
56
56
removeOne ( state , action : PayloadAction < string > ) {
57
- // TODO The nested `produce` calls don't mutate `state` here as I would have expected.
58
- // TODO (note that `state` here is actually an Immer Draft<S>, from `createReducer`)
59
- // TODO However, this works if we _return_ the new plain result value instead
60
- // TODO See https://github.com/immerjs/immer/issues/533
57
+ const sizeBefore = state . ids . length
58
+ // Originally, having nested `produce` calls don't mutate `state` here as I would have expected.
59
+ // (note that `state` here is actually an Immer Draft<S>, from `createReducer`)
60
+ // One woarkound was to return the new plain result value instead
61
+ // See https://github.com/immerjs/immer/issues/533
62
+ // However, after tweaking `createStateOperator` to check if the argument is a draft,
63
+ // we can just treat the operator as strictly mutating, without returning a result,
64
+ // and the result should be correct.
61
65
const result = adapter . removeOne ( state , action )
62
- return result
66
+
67
+ const sizeAfter = state . ids . length
68
+ if ( sizeBefore > 0 ) {
69
+ expect ( sizeAfter ) . toBe ( sizeBefore - 1 )
70
+ }
71
+
72
+ //Deliberately _don't_ return result
63
73
}
64
74
} ,
65
75
extraReducers : builder => {
@@ -75,11 +85,9 @@ describe('Combined entity slice', () => {
75
85
state . loading === 'pending' &&
76
86
action . meta . requestId === state . lastRequestId
77
87
) {
78
- return {
79
- ...adapter . setAll ( state , action . payload ) ,
80
- loading : 'finished' ,
81
- lastRequestId : null
82
- }
88
+ adapter . setAll ( state , action . payload )
89
+ state . loading = 'finished'
90
+ state . lastRequestId = null
83
91
}
84
92
} )
85
93
}
@@ -102,6 +110,9 @@ describe('Combined entity slice', () => {
102
110
expect ( booksAfterLoaded . lastRequestId ) . toBe ( null )
103
111
expect ( booksAfterLoaded . loading ) . toBe ( 'finished' )
104
112
113
+ store . dispatch ( addOne ( { id : 'd' , title : 'Remove Me' } ) )
114
+ store . dispatch ( removeOne ( 'd' ) )
115
+
105
116
store . dispatch ( addOne ( { id : 'c' , title : 'Middle' } ) )
106
117
107
118
const { books : booksAfterAddOne } = store . getState ( )
0 commit comments