@@ -54,12 +54,22 @@ describe('Combined entity slice', () => {
5454 reducers : {
5555 addOne : adapter . addOne ,
5656 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.
6165 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
6373 }
6474 } ,
6575 extraReducers : builder => {
@@ -75,11 +85,9 @@ describe('Combined entity slice', () => {
7585 state . loading === 'pending' &&
7686 action . meta . requestId === state . lastRequestId
7787 ) {
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
8391 }
8492 } )
8593 }
@@ -102,6 +110,9 @@ describe('Combined entity slice', () => {
102110 expect ( booksAfterLoaded . lastRequestId ) . toBe ( null )
103111 expect ( booksAfterLoaded . loading ) . toBe ( 'finished' )
104112
113+ store . dispatch ( addOne ( { id : 'd' , title : 'Remove Me' } ) )
114+ store . dispatch ( removeOne ( 'd' ) )
115+
105116 store . dispatch ( addOne ( { id : 'c' , title : 'Middle' } ) )
106117
107118 const { books : booksAfterAddOne } = store . getState ( )
0 commit comments