@@ -177,6 +177,95 @@ it("can apply readonly patches", () => {
177177 expect ( applyPatches ( { } , patches ) ) . toEqual ( { x : 4 } )
178178} )
179179
180+ describe ( "assigning a value and then reverting it, generates no changes" , ( ) => {
181+ it ( "top-level" , ( ) => {
182+ type Item = { date : Date }
183+
184+ let initialDate = new Date ( )
185+ let data : Item = { date : initialDate }
186+
187+ const [ newData , patches ] = produceWithPatches ( data , draft => {
188+ const dateBefore = draft . date
189+ draft . date = new Date ( )
190+ draft . date = dateBefore
191+ } )
192+
193+ // Expect no patches and the returned value to be the same reference
194+ expect ( patches ) . toEqual ( [ ] )
195+ expect ( newData ) . toBe ( data )
196+ } )
197+
198+ it ( "top-level pathological case" , ( ) => {
199+ type Item = { number : Number }
200+
201+ let data : Item = { number : - 0 }
202+
203+ const [ newData , patches ] = produceWithPatches ( data , draft => {
204+ draft . number = 1
205+ draft . number = + 0
206+ } )
207+
208+ // Expect no patches and the returned value to be the same reference
209+ expect ( newData ) . not . toBe ( data )
210+ // Bug in patches, should be a patch but none generated
211+ expect ( patches ) . toEqual ( [ ] )
212+ } )
213+
214+ it ( "top-level with adjacent modified draft" , ( ) => {
215+ type Item = { date : Date ; other : { x : number } }
216+
217+ let initialDate = new Date ( )
218+ let data : Item = { date : initialDate , other : { x : 0 } }
219+
220+ const [ newData , patches ] = produceWithPatches ( data , draft => {
221+ const dateBefore = draft . date
222+ draft . date = new Date ( )
223+ draft . other . x ++
224+ draft . date = dateBefore
225+ } )
226+
227+ // Expect patches and the returned value not to be the same reference
228+ expect ( patches ) . not . toEqual ( [ ] )
229+ expect ( newData ) . not . toBe ( data )
230+ } )
231+
232+ it ( "nested in array" , ( ) => {
233+ type Item = { date : Date }
234+
235+ let initialDate = new Date ( )
236+ let data : Item [ ] = [ { date : initialDate } ]
237+
238+ const [ newData , patches ] = produceWithPatches ( data , draft => {
239+ const element = draft [ 0 ]
240+ const dateBefore = element . date
241+ element . date = new Date ( )
242+ element . date = dateBefore
243+ } )
244+
245+ // Expect no patches and the returned value to be the same reference
246+ expect ( patches ) . toEqual ( [ ] )
247+ expect ( newData ) . toBe ( data )
248+ } )
249+
250+ it ( "nested in map" , ( ) => {
251+ type Item = { date : Date }
252+
253+ let initialDate = new Date ( )
254+ let data : Map < number , Item > = new Map ( [ [ 0 , { date : initialDate } ] ] )
255+
256+ const [ newData , patches ] = produceWithPatches ( data , draft => {
257+ const element = draft . get ( 0 ) !
258+ const dateBefore = element . date
259+ element . date = new Date ( )
260+ element . date = dateBefore
261+ } )
262+
263+ // Expect no patches and the returned value to be the same reference
264+ expect ( patches ) . toEqual ( [ ] )
265+ expect ( newData ) . toBe ( data )
266+ } )
267+ } )
268+
180269describe ( "curried producer" , ( ) => {
181270 it ( "supports rest parameters" , ( ) => {
182271 type State = { readonly a : 1 }
0 commit comments