Skip to content
Myr

base.js

This file

Path

__tests__/base.js

4,415 lines · 123.5 KiB · JavaScript

Last changed by

landed by oscabriel8848a5b

History of this file

Take it

The walk stopped after 200 commits: lines older than 07f5cc9 point at it.

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 1why, one step back
1"use strict"

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 2why, one step back
2import {vi} from "vitest"

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 3–8why, one step back
3import {
4 Immer,5 nothing,6 original,7 isDraft,8 immerable,

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 9why, one step back
9 enablePatches,

feat: Override array methods to avoid proxy creation while iterating and updating (#1184)Mark Eriksonbefore adoptiond38a9d0

line 10why, one step back
10 enableMapSet,

fix: draft relocated base refs after reverse/sort in array-methods plugin (#1255)spokodevbefore adoptiona73672a

lines 11–12why, one step back
11 enableArrayMethods,
12 applyPatches

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 13why, one step back
13} from "../src/immer"

feat: Override array methods to avoid proxy creation while iterating and updating (#1184)Mark Eriksonbefore adoptiond38a9d0

lines 14–21why, one step back
14
15import {16 each,17 shallowCopy,18 DRAFT_STATE,19 clearPlugin,20 PluginArrayMethods21} from "../src/internal"

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 22–24why, one step back
22import deepFreeze from "deep-freeze"
23import * as lodash from "lodash"24

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 25–27why, one step back
25enablePatches()
26enableMapSet()27

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 28why, one step back
28vi.setConfig({

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 29–31why, one step back
29 testTimeout: 2000
30})31

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 32why, one step back
32const isProd = process.env.NODE_ENV === "production"

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

line 33why, one step back
33

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 34–35why, one step back
34test("immer should have no dependencies", () => {
35 expect(require("../package.json").dependencies).toBeUndefined()

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 36–37why, one step back
36})
37

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 38–44why, one step back
38for (const autoFreeze of [true, false]) {
39 for (const useStrictShallowCopy of [true, false]) {40 for (const useListener of [true, false]) {41 const name = `${autoFreeze ? "auto-freeze=true" : "auto-freeze=false"}:${42 useStrictShallowCopy ? "shallow-copy=true" : "shallow-copy=false"43 }:${useListener ? "use-listener=true" : "use-listener=false"}`44 runBaseTest(name, autoFreeze, useStrictShallowCopy, useListener)

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 45–46why, one step back
45 }
46 }

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 47why, one step back
47}

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

line 48why, one step back
48

feat: Override array methods to avoid proxy creation while iterating and updating (#1184)Mark Eriksonbefore adoptiond38a9d0

lines 49–53why, one step back
49// Run one additional test suite with the array methods plugin enabled,
50// as that should be a separate concern from the other settings51const testArrayMethodsName = `array-plugin=true:auto-freeze=true:shallow-copy=false:use-listener=false`52runBaseTest(testArrayMethodsName, true, false, false, true)53

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 54why, one step back
54class Foo {}

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

line 55why, one step back
55

feat: Override array methods to avoid proxy creation while iterating and updating (#1184)Mark Eriksonbefore adoptiond38a9d0

lines 56–62why, one step back
56function runBaseTest(
57 name,58 autoFreeze,59 useStrictShallowCopy,60 useListener,61 useArrayMethods = false62) {

Merge pull request #1289 from maximilliangrand/fix/array-methods-noop-structural-sharingMark Eriksonbefore adoption907395a

line 63why, one step back
63 const listener = useListener ? function() {} : undefined

feat: Override array methods to avoid proxy creation while iterating and updating (#1184)Mark Eriksonbefore adoptiond38a9d0

line 64why, one step back
64

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 65why, one step back
65 const {produce, produceWithPatches} = createPatchedImmer({

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 66–67why, one step back
66 autoFreeze,
67 useStrictShallowCopy

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 68why, one step back
68 })

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

line 69why, one step back
69

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 70–73why, one step back
70 // When `useListener` is true, append a function to the arguments of every
71 // uncurried `produce` call in every test. This makes tests easier to read.72 function createPatchedImmer(options) {73 const immer = new Immer(options)

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

line 74why, one step back
74

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 75why, one step back
75 const {produce} = immer

Merge pull request #1289 from maximilliangrand/fix/array-methods-noop-structural-sharingMark Eriksonbefore adoption907395a

line 76why, one step back
76 immer.produce = function(...args) {

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 77–79why, one step back
77 return typeof args[1] === "function" && args.length < 3
78 ? produce(...args, listener)79 : produce(...args)

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 80–81why, one step back
80 }
81

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 82why, one step back
82 return immer

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 83–84why, one step back
83 }
84

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 85–87why, one step back
85 describe(`base functionality - ${name}`, () => {
86 let baseState87 let origBaseState

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

line 88why, one step back
88

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 89–90why, one step back
89 beforeEach(() => {
90 origBaseState = baseState = createBaseState()

feat: Override array methods to avoid proxy creation while iterating and updating (#1184)Mark Eriksonbefore adoptiond38a9d0

lines 91–97why, one step back
91
92 // Allow running our tests with and without the array method plugin93 if (useArrayMethods) {94 enableArrayMethods()95 } else {96 clearPlugin(PluginArrayMethods)97 }

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 98–99why, one step back
98 })
99

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 100why, one step back
100 it("returns the original state when no changes are made", () => {

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

line 101why, one step back
101 const nextState = produce(baseState, s => {

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 102–103why, one step back
102 expect(s.aProp).toBe("hi")
103 expect(s.anObject.nested).toMatchObject({yummie: true})

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 104–107why, one step back
104 })
105 expect(nextState).toBe(baseState)106 })107

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 108–109why, one step back
108 it("does structural sharing", () => {
109 const random = Math.random()

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

line 110why, one step back
110 const nextState = produce(baseState, s => {

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 111why, one step back
111 s.aProp = random

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 112–113why, one step back
112 })
113 expect(nextState).not.toBe(baseState)

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 114–115why, one step back
114 expect(nextState.aProp).toBe(random)
115 expect(nextState.nested).toBe(baseState.nested)

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 116–117why, one step back
116 })
117

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 118why, one step back
118 it("deep change bubbles up", () => {

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

line 119why, one step back
119 const nextState = produce(baseState, s => {

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 120why, one step back
120 s.anObject.nested.yummie = false

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 121–123why, one step back
121 })
122 expect(nextState).not.toBe(baseState)123 expect(nextState.anObject).not.toBe(baseState.anObject)

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 124–126why, one step back
124 expect(baseState.anObject.nested.yummie).toBe(true)
125 expect(nextState.anObject.nested.yummie).toBe(false)126 expect(nextState.anArray).toBe(baseState.anArray)

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 127–128why, one step back
127 })
128

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 129why, one step back
129 it("can add props", () => {

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

line 130why, one step back
130 const nextState = produce(baseState, s => {

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 131why, one step back
131 s.anObject.cookie = {tasty: true}

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 132–134why, one step back
132 })
133 expect(nextState).not.toBe(baseState)134 expect(nextState.anObject).not.toBe(baseState.anObject)

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 135–136why, one step back
135 expect(nextState.anObject.nested).toBe(baseState.anObject.nested)
136 expect(nextState.anObject.cookie).toEqual({tasty: true})

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 137–138why, one step back
137 })
138

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 139why, one step back
139 it("can delete props", () => {

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

line 140why, one step back
140 const nextState = produce(baseState, s => {

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 141why, one step back
141 delete s.anObject.nested

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 142–145why, one step back
142 delete s.nonexisting
143 })144 expect(nextState).not.toBe(baseState)145 expect(nextState.anObject).not.toBe(baseState.anObject)

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 146why, one step back
146 expect(nextState.anObject.nested).toBe(undefined)

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 147–148why, one step back
147 })
148

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 149why, one step back
149 it("can delete props - 2", () => {

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 150–155why, one step back
150 const nextState = produce(baseState, s => {
151 delete s.nonexisting152 })153 expect(nextState).toBe(baseState)154 })155

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 156–157why, one step back
156 // Found by: https://github.com/mweststrate/immer/pull/267
157 it("can delete props added in the producer", () => {

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

line 158why, one step back
158 const nextState = produce(baseState, s => {

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 159–160why, one step back
159 s.anObject.test = true
160 delete s.anObject.test

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 161–165why, one step back
161 })
162 expect(nextState).not.toBe(baseState)163 expect(nextState).toEqual(baseState)164 })165

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 166–168why, one step back
166 // Found by: https://github.com/mweststrate/immer/issues/328
167 it("can set a property that was just deleted", () => {168 const baseState = {a: 1}

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 169–170why, one step back
169 const nextState = produce(baseState, s => {
170 delete s.a

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 171why, one step back
171 s.a = 2

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

line 172why, one step back
172 })

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 173why, one step back
173 expect(nextState.a).toBe(2)

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 174–175why, one step back
174 })
175

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 176–177why, one step back
176 it("can set a property to its original value after deleting it", () => {
177 const baseState = {a: {b: 1}}

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

line 178why, one step back
178 const nextState = produce(baseState, s => {

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 179why, one step back
179 const a = s.a

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

line 180why, one step back
180 delete s.a

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 181why, one step back
181 s.a = a

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 182–186why, one step back
182 })
183 expect(nextState).not.toBe(baseState)184 expect(nextState).toEqual(baseState)185 })186

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 187–194why, one step back
187 it("can get property descriptors", () => {
188 const getDescriptor = Object.getOwnPropertyDescriptor189 const baseState = deepFreeze([{a: 1}])190 produce(baseState, arr => {191 const obj = arr[0]192 const desc = {193 configurable: true,194 enumerable: true,

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 195why, one step back
195 writable: true

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 196–198why, one step back
196 }
197198 // Known property

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 199–200why, one step back
199 expect(getDescriptor(obj, "a")).toMatchObject(desc)
200 expect(getDescriptor(arr, 0)).toMatchObject(desc)

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

line 201why, one step back
201

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 202–206why, one step back
202 // Deleted property
203 delete obj.a204 arr.pop()205 expect(getDescriptor(obj, "a")).toBeUndefined()206 expect(getDescriptor(arr, 0)).toBeUndefined()

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 207–208why, one step back
207
208 // Unknown property

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 209–210why, one step back
209 expect(getDescriptor(obj, "b")).toBeUndefined()
210 expect(getDescriptor(arr, 100)).toBeUndefined()

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

line 211why, one step back
211

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 212–216why, one step back
212 // Added property
213 obj.b = 2214 arr[100] = 1215 expect(getDescriptor(obj, "b")).toBeDefined()216 expect(getDescriptor(arr, 100)).toBeDefined()

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 217–219why, one step back
217 })
218 })219

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 220–221why, one step back
220 describe("array drafts", () => {
221 it("supports Array.isArray()", () => {

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

line 222why, one step back
222 const nextState = produce(baseState, s => {

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 223–224why, one step back
223 expect(Array.isArray(s.anArray)).toBeTruthy()
224 s.anArray.push(1)

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

line 225why, one step back
225 })

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 226why, one step back
226 expect(Array.isArray(nextState.anArray)).toBeTruthy()

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 227–228why, one step back
227 })
228

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 229–230why, one step back
229 it("supports index access", () => {
230 const value = baseState.anArray[0]

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

line 231why, one step back
231 const nextState = produce(baseState, s => {

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 232why, one step back
232 expect(s.anArray[0]).toBe(value)

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 233–237why, one step back
233 })
234 expect(nextState).toBe(baseState)235 })236237 it("supports iteration", () => {

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 238why, one step back
238 const base = [

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 239–240why, one step back
239 {id: 1, a: 1},
240 {id: 2, a: 1}

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 241–243why, one step back
241 ]
242 const findById = (collection, id) => {243 for (const item of collection) {

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 244–253why, one step back
244 if (item.id === id) return item
245 }246 return null247 }248 const result = produce(base, draft => {249 const obj1 = findById(draft, 1)250 const obj2 = findById(draft, 2)251 obj1.a = 2252 obj2.a = 2253 })

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 254–255why, one step back
254 expect(result[0].a).toEqual(2)
255 expect(result[1].a).toEqual(2)

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 256–257why, one step back
256 })
257

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 258why, one step back
258 it("can assign an index via bracket notation", () => {

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

line 259why, one step back
259 const nextState = produce(baseState, s => {

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 260why, one step back
260 s.anArray[3] = true

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 261–263why, one step back
261 })
262 expect(nextState).not.toBe(baseState)263 expect(nextState.anArray).not.toBe(baseState.anArray)

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 264why, one step back
264 expect(nextState.anArray[3]).toEqual(true)

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 265–266why, one step back
265 })
266

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 267why, one step back
267 it("can use splice() to both add and remove items", () => {

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

line 268why, one step back
268 const nextState = produce(baseState, s => {

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 269why, one step back
269 s.anArray.splice(1, 1, "a", "b")

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 270–271why, one step back
270 })
271 expect(nextState.anArray).not.toBe(baseState.anArray)

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 272–273why, one step back
272 expect(nextState.anArray[1]).toBe("a")
273 expect(nextState.anArray[2]).toBe("b")

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 274–275why, one step back
274 })
275

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 276why, one step back
276 it("can truncate via the length property", () => {

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 277–278why, one step back
277 const baseLength = baseState.anArray.length
278 const nextState = produce(baseState, s => {

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 279why, one step back
279 s.anArray.length = baseLength - 1

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 280–281why, one step back
280 })
281 expect(nextState.anArray).not.toBe(baseState.anArray)

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 282why, one step back
282 expect(nextState.anArray.length).toBe(baseLength - 1)

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 283–284why, one step back
283 })
284

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 285why, one step back
285 it("can extend via the length property", () => {

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 286–287why, one step back
286 const baseLength = baseState.anArray.length
287 const nextState = produce(baseState, s => {

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 288why, one step back
288 s.anArray.length = baseLength + 1

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 289–290why, one step back
289 })
290 expect(nextState.anArray).not.toBe(baseState.anArray)

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 291why, one step back
291 expect(nextState.anArray.length).toBe(baseLength + 1)

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 292–294why, one step back
292 })
293294 describe("mutating array methods", () => {

Merge pull request #1289 from maximilliangrand/fix/array-methods-noop-structural-sharingMark Eriksonbefore adoption907395a

lines 295–369why, one step back
295 describe("no-op calls preserve structural sharing", () => {
296 it("push() with no arguments returns the base", () => {297 const base = {list: [1, 2, 3], keep: {x: 1}}298 const [next, patches] = produceWithPatches(base, d => {299 d.list.push(...[])300 })301 expect(next).toBe(base)302 expect(next.keep).toBe(base.keep)303 expect(patches).toHaveLength(0)304 })305306 it("unshift() with no arguments returns the base", () => {307 const base = {list: [1, 2, 3]}308 const next = produce(base, d => {309 d.list.unshift()310 })311 expect(next).toBe(base)312 })313314 it("splice() that removes and inserts nothing returns the base", () => {315 const base = {list: [1, 2, 3]}316 const [next, patches] = produceWithPatches(base, d => {317 d.list.splice(1, 0)318 })319 expect(next).toBe(base)320 expect(patches).toHaveLength(0)321 })322323 it("pop()/shift() on an empty array return the base", () => {324 const base = {popped: [], shifted: []}325 const next = produce(base, d => {326 d.popped.pop()327 d.shifted.shift()328 })329 expect(next).toBe(base)330 })331332 it("no-op push() on an array inside a Set preserves the array's identity", () => {333 const arr = [1, 2, 3]334 const base = new Set([arr, {x: 1}])335 const next = produce(base, d => {336 for (const v of d) {337 if (Array.isArray(v)) v.push(...[])338 else v.x = 2339 }340 })341 expect(next).not.toBe(base)342 expect([...next].find(Array.isArray)).toBe(arr)343 })344345 it("sort()/reverse() on arrays with fewer than two elements return the base", () => {346 const base = {empty: [], single: [1]}347 const next = produce(base, d => {348 d.empty.sort()349 d.empty.reverse()350 d.single.sort((a, b) => a - b)351 d.single.reverse()352 })353 expect(next).toBe(base)354 })355356 it("no-op splice() on an array inside a Set preserves the array's identity", () => {357 const arr = [1, 2, 3]358 const base = new Set([arr, {x: 1}])359 const next = produce(base, d => {360 for (const v of d) {361 if (Array.isArray(v)) v.splice(1, 0)362 else v.x = 2363 }364 })365 expect(next).not.toBe(base)366 expect([...next].find(Array.isArray)).toBe(arr)367 })368 })369

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 370–432why, one step back
370 // Reported here: https://github.com/mweststrate/immer/issues/116
371 it("can pop then push", () => {372 const nextState = produce([1, 2, 3], s => {373 s.pop()374 s.push(100)375 })376 expect(nextState).toEqual([1, 2, 100])377 })378379 it("can be sorted", () => {380 const baseState = [{value: 3}, {value: 1}, {value: 2}]381 const nextState = produce(baseState, s => {382 s.sort((a, b) => a.value - b.value)383 })384 expect(nextState).not.toBe(baseState)385 expect(nextState).toEqual([{value: 1}, {value: 2}, {value: 3}])386 })387388 it("can be reversed", () => {389 const baseState = [{value: 1}, {value: 2}, {value: 3}]390 const nextState = produce(baseState, s => {391 s.reverse()392 })393 expect(nextState).not.toBe(baseState)394 expect(nextState).toEqual([{value: 3}, {value: 2}, {value: 1}])395 })396397 it("can be sorted with existing proxies", () => {398 const baseState = [{value: 3}, {value: 1}, {value: 2}]399 const nextState = produce(baseState, s => {400 // First mutate a nested object to create a proxy401 s[0].value = 4402 // Then sort the array403 s.sort((a, b) => a.value - b.value)404 })405 expect(nextState).not.toBe(baseState)406 expect(nextState).toEqual([{value: 1}, {value: 2}, {value: 4}])407 })408409 it("can be reversed with existing proxies", () => {410 const baseState = [{value: 1}, {value: 2}, {value: 3}]411 const nextState = produce(baseState, s => {412 // First mutate a nested object to create a proxy413 s[1].value = 5414 // Then reverse the array415 s.reverse()416 })417 expect(nextState).not.toBe(baseState)418 expect(nextState).toEqual([{value: 3}, {value: 5}, {value: 1}])419 })420421 it("can be sorted with unmodified existing proxies", () => {422 const baseState = [{value: 3}, {value: 1}, {value: 2}]423 const nextState = produce(baseState, s => {424 // Access a nested object to create a proxy, but don't modify it425 const firstValue = s[0].value // This creates a proxy for s[0]426 expect(firstValue).toBe(3) // But we don't modify it427428 // Then sort the array429 s.sort((a, b) => a.value - b.value)430 })431 expect(nextState).not.toBe(baseState)432 expect(nextState).toEqual([{value: 1}, {value: 2}, {value: 3}])

fix: draft relocated base refs after reverse/sort in array-methods plugin (#1255)spokodevbefore adoptiona73672a

lines 433–463why, one step back
433 })
434435 it("mutating an element after reverse() does not mutate the base", () => {436 const reordered = {id: 3}437 const baseState = [{id: 1}, {id: 2}, reordered]438 const nextState = produce(baseState, s => {439 s.reverse()440 // After reverse, the element at index 0 is the relocated base441 // object. It must be drafted, otherwise writing to it mutates base.442 expect(isDraft(s[0])).toBe(true)443 s[0].id = 99444 })445 expect(baseState).toEqual([{id: 1}, {id: 2}, {id: 3}])446 expect(reordered).toEqual({id: 3})447 expect(nextState).toEqual([{id: 99}, {id: 2}, {id: 1}])448 })449450 it("mutating an element after sort() does not mutate the base", () => {451 const baseState = [{value: 3}, {value: 1}, {value: 2}]452 const [nextState, patches, inverse] = produceWithPatches(453 baseState,454 s => {455 s.sort((a, b) => a.value - b.value)456 expect(isDraft(s[0])).toBe(true)457 s[0].value = 99458 }459 )460 expect(baseState).toEqual([{value: 3}, {value: 1}, {value: 2}])461 expect(nextState).toEqual([{value: 99}, {value: 2}, {value: 3}])462 expect(applyPatches(baseState, patches)).toEqual(nextState)463 expect(applyPatches(nextState, inverse)).toEqual(baseState)

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 464–500why, one step back
464 })
465466 describe("push()", () => {467 test("push single item", () => {468 const base = {items: [{id: 1}, {id: 2}]}469 const result = produce(base, draft => {470 draft.items.push({id: 3})471 })472 expect(result.items).toHaveLength(3)473 expect(result.items[2].id).toBe(3)474 })475476 test("push multiple items", () => {477 const base = {items: [{id: 1}]}478 const result = produce(base, draft => {479 draft.items.push({id: 2}, {id: 3}, {id: 4})480 })481 expect(result.items).toHaveLength(4)482 expect(result.items[3].id).toBe(4)483 })484485 test("push then mutate pushed item", () => {486 const base = {items: [{id: 1}]}487 const result = produce(base, draft => {488 draft.items.push({id: 2, value: 10})489 draft.items[1].value = 20490 })491 expect(result.items[1].value).toBe(20)492 })493494 test("push returns new length", () => {495 const base = {items: [1, 2]}496 produce(base, draft => {497 const newLength = draft.items.push(3, 4)498 expect(newLength).toBe(4)499 })500 })

Merge pull request #1283 from Jaybhade/fix/array-methods-assigned-index-keyMark Eriksonbefore adoptiond2c158f

lines 501–517why, one step back
501
502 test("push onto an index freed by pop() is patched", () => {503 const base = {items: ["a", "b", "c"]}504 const [result, patches, inverse] = produceWithPatches(505 base,506 draft => {507 draft.items.pop()508 draft.items.push("z")509 }510 )511 expect(result.items).toEqual(["a", "b", "z"])512 expect(patches).toEqual([513 {op: "replace", path: ["items", 2], value: "z"}514 ])515 expect(applyPatches(base, patches)).toEqual(result)516 expect(applyPatches(result, inverse)).toEqual(base)517 })

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 518–854why, one step back
518 })
519520 describe("unshift()", () => {521 test("unshift single item", () => {522 const base = {items: [{id: 2}, {id: 3}]}523 const result = produce(base, draft => {524 draft.items.unshift({id: 1})525 })526 expect(result.items).toHaveLength(3)527 expect(result.items[0].id).toBe(1)528 expect(result.items[1].id).toBe(2)529 })530531 test("unshift multiple items", () => {532 const base = {items: [{id: 4}]}533 const result = produce(base, draft => {534 draft.items.unshift({id: 1}, {id: 2}, {id: 3})535 })536 expect(result.items).toHaveLength(4)537 expect(result.items[0].id).toBe(1)538 expect(result.items[3].id).toBe(4)539 })540541 test("unshift then mutate unshifted item", () => {542 const base = {items: [{id: 2}]}543 const result = produce(base, draft => {544 draft.items.unshift({id: 1, value: 10})545 draft.items[0].value = 20546 })547 expect(result.items[0].value).toBe(20)548 })549550 test("unshift returns new length", () => {551 const base = {items: [3, 4]}552 produce(base, draft => {553 const newLength = draft.items.unshift(1, 2)554 expect(newLength).toBe(4)555 })556 })557 })558559 describe("shift()", () => {560 test("shift removes first item", () => {561 const base = {items: [{id: 1}, {id: 2}, {id: 3}]}562 const result = produce(base, draft => {563 const removed = draft.items.shift()564 expect(removed.id).toBe(1)565 })566 expect(result.items).toHaveLength(2)567 expect(result.items[0].id).toBe(2)568 })569570 test("shift on empty array returns undefined", () => {571 const base = {items: []}572 produce(base, draft => {573 const removed = draft.items.shift()574 expect(removed).toBeUndefined()575 })576 })577578 test("shift then mutate remaining items", () => {579 const base = {items: [{id: 1}, {id: 2, value: 10}]}580 const result = produce(base, draft => {581 draft.items.shift()582 draft.items[0].value = 20583 })584 expect(result.items).toHaveLength(1)585 expect(result.items[0].value).toBe(20)586 })587588 test("multiple shifts", () => {589 const base = {items: [{id: 1}, {id: 2}, {id: 3}]}590 const result = produce(base, draft => {591 draft.items.shift()592 draft.items.shift()593 })594 expect(result.items).toHaveLength(1)595 expect(result.items[0].id).toBe(3)596 })597 })598599 describe("splice() edge cases", () => {600 test("splice with only deleteCount (no items to add)", () => {601 const base = {items: [{id: 1}, {id: 2}, {id: 3}, {id: 4}]}602 const result = produce(base, draft => {603 const removed = draft.items.splice(1, 2)604 expect(removed).toHaveLength(2)605 expect(removed[0].id).toBe(2)606 })607 expect(result.items).toHaveLength(2)608 expect(result.items[0].id).toBe(1)609 expect(result.items[1].id).toBe(4)610 })611612 test("splice with negative start index", () => {613 const base = {items: [{id: 1}, {id: 2}, {id: 3}]}614 const result = produce(base, draft => {615 draft.items.splice(-1, 1, {id: 99})616 })617 expect(result.items[2].id).toBe(99)618 })619620 test("splice at start (index 0)", () => {621 const base = {items: [{id: 1}, {id: 2}]}622 const result = produce(base, draft => {623 draft.items.splice(0, 0, {id: 0})624 })625 expect(result.items[0].id).toBe(0)626 expect(result.items).toHaveLength(3)627 })628629 test("splice at end", () => {630 const base = {items: [{id: 1}, {id: 2}]}631 const result = produce(base, draft => {632 draft.items.splice(2, 0, {id: 3})633 })634 expect(result.items[2].id).toBe(3)635 })636637 test("splice then mutate spliced-in items", () => {638 const base = {items: [{id: 1}, {id: 3}]}639 const result = produce(base, draft => {640 draft.items.splice(1, 0, {id: 2, value: 10})641 draft.items[1].value = 20642 })643 expect(result.items[1].value).toBe(20)644 })645 })646647 describe("combined operations", () => {648 test("sort then push", () => {649 const base = {items: [{value: 3}, {value: 1}]}650 const result = produce(base, draft => {651 draft.items.sort((a, b) => a.value - b.value)652 draft.items.push({value: 4})653 })654 expect(result.items.map(i => i.value)).toEqual([1, 3, 4])655 })656657 test("reverse then unshift", () => {658 const base = {items: [{id: 1}, {id: 2}, {id: 3}]}659 const result = produce(base, draft => {660 draft.items.reverse()661 draft.items.unshift({id: 0})662 })663 expect(result.items.map(i => i.id)).toEqual([0, 3, 2, 1])664 })665666 test("splice then sort", () => {667 const base = {items: [{value: 5}, {value: 2}, {value: 8}]}668 const result = produce(base, draft => {669 draft.items.splice(1, 1, {value: 1})670 draft.items.sort((a, b) => a.value - b.value)671 })672 expect(result.items.map(i => i.value)).toEqual([1, 5, 8])673 })674675 test("push, pop, push sequence", () => {676 const base = {items: [{id: 1}]}677 const result = produce(base, draft => {678 draft.items.push({id: 2})679 draft.items.pop()680 draft.items.push({id: 3})681 })682 expect(result.items.map(i => i.id)).toEqual([1, 3])683 })684 })685686 describe("bulk operations with pre-existing proxies", () => {687 test("access items before sort", () => {688 const base = {items: [{value: 3}, {value: 1}, {value: 2}]}689 const result = produce(base, draft => {690 // Access all items to create proxies691 draft.items.forEach(item => item.value)692 // Then sort693 draft.items.sort((a, b) => a.value - b.value)694 })695 expect(result.items.map(i => i.value)).toEqual([1, 2, 3])696 })697698 test("mutate items before reverse", () => {699 const base = {700 items: [701 {id: 1, value: 10},702 {id: 2, value: 20}703 ]704 }705 const result = produce(base, draft => {706 // Mutate to create modified proxies707 draft.items[0].value = 15708 draft.items[1].value = 25709 // Then reverse710 draft.items.reverse()711 })712 expect(result.items[0].id).toBe(2)713 expect(result.items[0].value).toBe(25)714 expect(result.items[1].id).toBe(1)715 expect(result.items[1].value).toBe(15)716 })717 })718719 describe("return values", () => {720 test("pop returns removed item", () => {721 const base = {items: [{id: 1}, {id: 2}]}722 produce(base, draft => {723 const removed = draft.items.pop()724 expect(removed.id).toBe(2)725 // Verify we can mutate the returned item726 removed.modified = true727 expect(draft.items[0].modified).toBeUndefined()728 })729 })730731 test("shift returns removed item", () => {732 const base = {items: [{id: 1}, {id: 2}]}733 produce(base, draft => {734 const removed = draft.items.shift()735 expect(removed.id).toBe(1)736 })737 })738739 test("splice returns array of removed items", () => {740 const base = {items: [{id: 1}, {id: 2}, {id: 3}]}741 produce(base, draft => {742 const removed = draft.items.splice(0, 2)743 expect(removed).toHaveLength(2)744 expect(removed[0].id).toBe(1)745 expect(removed[1].id).toBe(2)746 })747 })748 })749 })750751 describe("non-mutating array methods", () => {752 // Test data factory753 const createTestData = () => ({754 items: [755 {id: 1, value: 10, nested: {count: 1}},756 {id: 2, value: 20, nested: {count: 2}},757 {id: 3, value: 30, nested: {count: 3}},758 {id: 4, value: 40, nested: {count: 4}},759 {id: 5, value: 50, nested: {count: 5}}760 ],761 other: {data: "test"}762 })763764 describe("filter()", () => {765 test("returns new array with filtered items", () => {766 const base = createTestData()767 const result = produce(base, draft => {768 const filtered = draft.items.filter(item => item.value > 25)769 expect(filtered).toHaveLength(3)770 expect(filtered[0].id).toBe(3)771 })772 expect(result).toBe(base) // No modifications773 })774775 test("mutations to filtered items are reflected in result", () => {776 const base = createTestData()777 const result = produce(base, draft => {778 const filtered = draft.items.filter(item => item.value > 25)779 // Verify filtered items are drafts780 expect(isDraft(filtered[0])).toBe(true)781 filtered[0].value = 999782 })783 expect(result.items[2].value).toBe(999) // id: 3 is at index 2784 expect(result.items[0].value).toBe(10) // Unchanged785 // Verify base state unchanged786 expect(base.items[2].value).toBe(30)787 // Verify result is a copy788 expect(result.items[2]).not.toBe(base.items[2])789 })790791 test("mutations to nested properties work correctly", () => {792 const base = createTestData()793 const result = produce(base, draft => {794 const filtered = draft.items.filter(item => item.id > 2)795 // Verify filtered items are drafts796 expect(isDraft(filtered[0])).toBe(true)797 filtered[0].nested.count = 100798 })799 expect(result.items[2].nested.count).toBe(100)800 // Verify base state unchanged801 expect(base.items[2].nested.count).toBe(3)802 // Verify result is a copy803 expect(result.items[2].nested).not.toBe(base.items[2].nested)804 })805806 test("multiple mutations to different filtered items", () => {807 const base = createTestData()808 const result = produce(base, draft => {809 const filtered = draft.items.filter(item => item.value > 15)810 // Verify all filtered items are drafts811 filtered.forEach(item => expect(isDraft(item)).toBe(true))812 filtered[0].value = 200 // id: 2813 filtered[1].value = 300 // id: 3814 filtered[2].value = 400 // id: 4815 })816 expect(result.items[1].value).toBe(200)817 expect(result.items[2].value).toBe(300)818 expect(result.items[3].value).toBe(400)819 // Verify base state unchanged820 expect(base.items[1].value).toBe(20)821 expect(base.items[2].value).toBe(30)822 expect(base.items[3].value).toBe(40)823 })824825 test("filtered array can be assigned to another property", () => {826 const base = createTestData()827 const result = produce(base, draft => {828 const filtered = draft.items.filter(item => item.value > 25)829 draft.other.filtered = filtered830 })831 expect(result.other.filtered).toHaveLength(3)832 expect(result.other.filtered[0].id).toBe(3)833 })834835 test("cross-reference: mutating filtered item affects original location", () => {836 const base = createTestData()837 const result = produce(base, draft => {838 const filtered = draft.items.filter(item => item.id === 3)839 draft.other.ref = filtered[0]840 filtered[0].value = 999841 })842 expect(result.items[2].value).toBe(999)843 expect(result.other.ref.value).toBe(999)844 })845846 test("read-only access doesn't cause mutations", () => {847 const base = createTestData()848 const result = produce(base, draft => {849 const filtered = draft.items.filter(item => item.value > 25)850 const sum = filtered.reduce((acc, item) => acc + item.value, 0)851 expect(sum).toBe(120) // 30 + 40 + 50852 })853 expect(result).toBe(base) // No modifications854 })

feat: Override array methods to avoid proxy creation while iterating and updating (#1184)Mark Eriksonbefore adoptiond38a9d0

lines 855–874why, one step back
855
856 test("mutating item in filter result updates original value", () => {857 const initialState = {858 largeArray: Array.from({length: 10}).map((_, i) => ({859 id: i,860 value: i * 10861 }))862 }863864 const result = produce(initialState, draft => {865 const filtered = draft.largeArray.filter(item => item.id <= 5)866867 filtered[0].value = 999868 draft.filtered = filtered869 })870871 expect(result.largeArray[0].value).toBe(999)872 expect(result.filtered[0].value).toBe(999)873 expect(result.largeArray[0]).toBe(result.filtered[0])874 })

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 875–1310why, one step back
875 })
876877 describe("map()", () => {878 test("returns new array with mapped items", () => {879 const base = createTestData()880 const result = produce(base, draft => {881 const mapped = draft.items.map(item => item.nested)882 expect(mapped).toHaveLength(5)883 expect(mapped[0].count).toBe(1)884 })885 expect(result).toBe(base)886 })887888 test("mutations to mapped nested objects work", () => {889 const base = createTestData()890 const result = produce(base, draft => {891 const nested = draft.items.map(item => item.nested)892 // Verify mapped items are drafts893 expect(isDraft(nested[0])).toBe(true)894 nested[0].count = 100895 })896 expect(result.items[0].nested.count).toBe(100)897 // Verify base state unchanged898 expect(base.items[0].nested.count).toBe(1)899 // Verify result is a copy900 expect(result.items[0].nested).not.toBe(base.items[0].nested)901 })902903 test("map with transformation then mutate", () => {904 const base = createTestData()905 const result = produce(base, draft => {906 const mapped = draft.items.map(item => ({907 ...item,908 doubled: item.value * 2909 }))910 // This creates new objects, so mutations won't affect original911 mapped[0].value = 999912 })913 expect(result.items[0].value).toBe(10) // Unchanged914 })915916 test("map returning original items allows mutation", () => {917 const base = createTestData()918 const result = produce(base, draft => {919 const mapped = draft.items.map(item => item) // Identity map920 // Verify mapped items are drafts921 expect(isDraft(mapped[0])).toBe(true)922 mapped[0].value = 999923 })924 expect(result.items[0].value).toBe(999)925 // Verify base state unchanged926 expect(base.items[0].value).toBe(10)927 // Verify result is a copy928 expect(result.items[0]).not.toBe(base.items[0])929 })930 })931932 describe("find()", () => {933 test("returns found item", () => {934 const base = createTestData()935 const result = produce(base, draft => {936 const found = draft.items.find(item => item.id === 3)937 expect(found?.value).toBe(30)938 })939 expect(result).toBe(base)940 })941942 test("mutations to found item are reflected", () => {943 const base = createTestData()944 const result = produce(base, draft => {945 const found = draft.items.find(item => item.id === 3)946 // Verify found item is a draft947 expect(isDraft(found)).toBe(true)948 if (found) {949 found.value = 999950 }951 })952 expect(result.items[2].value).toBe(999)953 // Verify base state unchanged954 expect(base.items[2].value).toBe(30)955 // Verify result is a copy956 expect(result.items[2]).not.toBe(base.items[2])957 })958959 test("nested mutations on found item", () => {960 const base = createTestData()961 const result = produce(base, draft => {962 const found = draft.items.find(item => item.id === 2)963 // Verify found item is a draft964 expect(isDraft(found)).toBe(true)965 if (found) {966 found.nested.count = 200967 }968 })969 expect(result.items[1].nested.count).toBe(200)970 // Verify base state unchanged971 expect(base.items[1].nested.count).toBe(2)972 // Verify result is a copy973 expect(result.items[1].nested).not.toBe(base.items[1].nested)974 })975976 test("find returns undefined when not found", () => {977 const base = createTestData()978 const result = produce(base, draft => {979 const found = draft.items.find(item => item.id === 999)980 expect(found).toBeUndefined()981 })982 expect(result).toBe(base)983 })984 })985986 describe("findLast()", () => {987 test("returns last matching item", () => {988 const base = {989 items: [990 {id: 1, type: "A"},991 {id: 2, type: "B"},992 {id: 3, type: "A"}993 ]994 }995 const result = produce(base, draft => {996 const found = draft.items.findLast(item => item.type === "A")997 expect(found?.id).toBe(3)998 })999 expect(result).toBe(base)1000 })10011002 test("mutations to findLast result work", () => {1003 const base = {1004 items: [1005 {id: 1, type: "A", value: 10},1006 {id: 2, type: "B", value: 20},1007 {id: 3, type: "A", value: 30}1008 ]1009 }1010 const result = produce(base, draft => {1011 const found = draft.items.findLast(item => item.type === "A")1012 // Verify found item is a draft1013 expect(isDraft(found)).toBe(true)1014 if (found) {1015 found.value = 9991016 }1017 })1018 expect(result.items[2].value).toBe(999)1019 // Verify base state unchanged1020 expect(base.items[2].value).toBe(30)1021 // Verify result is a copy1022 expect(result.items[2]).not.toBe(base.items[2])1023 })1024 })10251026 describe("slice()", () => {1027 test("returns sliced array", () => {1028 const base = createTestData()1029 const result = produce(base, draft => {1030 const sliced = draft.items.slice(1, 3)1031 expect(sliced).toHaveLength(2)1032 expect(sliced[0].id).toBe(2)1033 })1034 expect(result).toBe(base)1035 })10361037 test("mutations to sliced items work", () => {1038 const base = createTestData()1039 const result = produce(base, draft => {1040 const sliced = draft.items.slice(1, 3)1041 // Verify sliced items are drafts1042 expect(isDraft(sliced[0])).toBe(true)1043 sliced[0].value = 9991044 })1045 expect(result.items[1].value).toBe(999)1046 // Verify base state unchanged1047 expect(base.items[1].value).toBe(20)1048 // Verify result is a copy1049 expect(result.items[1]).not.toBe(base.items[1])1050 })10511052 test("slice with negative indices", () => {1053 const base = createTestData()1054 const result = produce(base, draft => {1055 const sliced = draft.items.slice(-2)1056 // Verify sliced items are drafts1057 expect(isDraft(sliced[0])).toBe(true)1058 sliced[0].value = 9991059 })1060 expect(result.items[3].value).toBe(999)1061 // Verify base state unchanged1062 expect(base.items[3].value).toBe(40)1063 // Verify result is a copy1064 expect(result.items[3]).not.toBe(base.items[3])1065 })1066 })10671068 describe("flatMap()", () => {1069 test("returns flattened array", () => {1070 const base = {1071 groups: [{items: [{id: 1}, {id: 2}]}, {items: [{id: 3}, {id: 4}]}]1072 }1073 const result = produce(base, draft => {1074 const flat = draft.groups.flatMap(group => group.items)1075 expect(flat).toHaveLength(4)1076 expect(flat[0].id).toBe(1)1077 })1078 expect(result).toBe(base)1079 })10801081 test("mutations to flatMapped items work", () => {1082 const base = {1083 groups: [1084 {1085 items: [1086 {id: 1, value: 10},1087 {id: 2, value: 20}1088 ]1089 },1090 {items: [{id: 3, value: 30}]}1091 ]1092 }1093 const result = produce(base, draft => {1094 const flat = draft.groups.flatMap(group => group.items)1095 // Verify flatMapped items are drafts1096 expect(isDraft(flat[0])).toBe(true)1097 flat[0].value = 9991098 })1099 expect(result.groups[0].items[0].value).toBe(999)1100 // Verify base state unchanged1101 expect(base.groups[0].items[0].value).toBe(10)1102 // Verify result is a copy1103 expect(result.groups[0].items[0]).not.toBe(base.groups[0].items[0])1104 })1105 })11061107 describe("reduce()", () => {1108 test("returns accumulated value", () => {1109 const base = createTestData()1110 const result = produce(base, draft => {1111 const sum = draft.items.reduce((acc, item) => acc + item.value, 0)1112 expect(sum).toBe(150) // 10 + 20 + 30 + 40 + 501113 })1114 expect(result).toBe(base)1115 })11161117 test("mutations during reduce are reflected", () => {1118 const base = createTestData()1119 const result = produce(base, draft => {1120 const sum = draft.items.reduce((acc, item, index) => {1121 // Verify items in reduce are drafts1122 expect(isDraft(item)).toBe(true)1123 if (index === 2) {1124 item.value = 9991125 }1126 return acc + item.value1127 }, 0)1128 expect(sum).toBe(1119) // 10 + 20 + 999 + 40 + 501129 })1130 expect(result.items[2].value).toBe(999)1131 // Verify base state unchanged1132 expect(base.items[2].value).toBe(30)1133 // Verify result is a copy1134 expect(result.items[2]).not.toBe(base.items[2])1135 })11361137 test("reduce with object accumulator", () => {1138 const base = createTestData()1139 const result = produce(base, draft => {1140 const grouped = draft.items.reduce((acc, item) => {1141 const key = item.value > 25 ? "high" : "low"1142 if (!acc[key]) acc[key] = []1143 acc[key].push(item)1144 return acc1145 }, {})1146 expect(grouped.low).toHaveLength(2)1147 expect(grouped.high).toHaveLength(3)1148 })1149 expect(result).toBe(base)1150 })11511152 test("reduce then mutate accumulated items", () => {1153 const base = createTestData()1154 const result = produce(base, draft => {1155 const items = draft.items.reduce((acc, item) => {1156 // Verify items in reduce are drafts1157 expect(isDraft(item)).toBe(true)1158 if (item.value > 25) acc.push(item)1159 return acc1160 }, [])1161 // Verify accumulated items are drafts1162 expect(isDraft(items[0])).toBe(true)1163 items[0].value = 9991164 })1165 expect(result.items[2].value).toBe(999) // id: 3 is at index 21166 // Verify base state unchanged1167 expect(base.items[2].value).toBe(30)1168 // Verify result is a copy1169 expect(result.items[2]).not.toBe(base.items[2])1170 })1171 })11721173 describe("forEach()", () => {1174 test("iterates over all items", () => {1175 const base = createTestData()1176 const result = produce(base, draft => {1177 let count = 01178 draft.items.forEach(item => {1179 count++1180 expect(item.id).toBeDefined()1181 })1182 expect(count).toBe(5)1183 })1184 expect(result).toBe(base)1185 })11861187 test("mutations during forEach are reflected", () => {1188 const base = createTestData()1189 const result = produce(base, draft => {1190 draft.items.forEach((item, index) => {1191 // Verify items in forEach are drafts1192 expect(isDraft(item)).toBe(true)1193 if (index === 1) {1194 item.value = 9991195 }1196 })1197 })1198 expect(result.items[1].value).toBe(999)1199 // Verify base state unchanged1200 expect(base.items[1].value).toBe(20)1201 // Verify result is a copy1202 expect(result.items[1]).not.toBe(base.items[1])1203 })12041205 test("forEach with nested mutations", () => {1206 const base = createTestData()1207 const result = produce(base, draft => {1208 draft.items.forEach(item => {1209 // Verify items in forEach are drafts1210 expect(isDraft(item)).toBe(true)1211 item.nested.count *= 101212 })1213 })1214 expect(result.items[0].nested.count).toBe(10)1215 expect(result.items[4].nested.count).toBe(50)1216 // Verify base state unchanged1217 expect(base.items[0].nested.count).toBe(1)1218 // Verify result is a copy1219 expect(result.items[0].nested).not.toBe(base.items[0].nested)1220 })12211222 test("forEach returns undefined", () => {1223 const base = createTestData()1224 const result = produce(base, draft => {1225 const returnValue = draft.items.forEach(item => item.value)1226 expect(returnValue).toBeUndefined()1227 })1228 expect(result).toBe(base)1229 })1230 })12311232 describe("indexOf()", () => {1233 test("returns index of found item", () => {1234 const base = {items: [1, 2, 3, 4, 5]}1235 const result = produce(base, draft => {1236 const index = draft.items.indexOf(3)1237 expect(index).toBe(2)1238 })1239 expect(result).toBe(base)1240 })12411242 test("returns -1 when item not found", () => {1243 const base = {items: [1, 2, 3, 4, 5]}1244 const result = produce(base, draft => {1245 const index = draft.items.indexOf(99)1246 expect(index).toBe(-1)1247 })1248 expect(result).toBe(base)1249 })12501251 test("works with object references", () => {1252 const base = createTestData()1253 const result = produce(base, draft => {1254 const item = draft.items[2]1255 const index = draft.items.indexOf(item)1256 expect(index).toBe(2)1257 })1258 expect(result).toBe(base)1259 })12601261 test("indexOf with fromIndex parameter", () => {1262 const base = {items: [1, 2, 3, 2, 5]}1263 const result = produce(base, draft => {1264 const firstIndex = draft.items.indexOf(2)1265 const secondIndex = draft.items.indexOf(2, 2)1266 expect(firstIndex).toBe(1)1267 expect(secondIndex).toBe(3)1268 })1269 expect(result).toBe(base)1270 })1271 })12721273 describe("join()", () => {1274 test("returns joined string", () => {1275 const base = {items: [1, 2, 3, 4, 5]}1276 const result = produce(base, draft => {1277 const joined = draft.items.join(",")1278 expect(joined).toBe("1,2,3,4,5")1279 })1280 expect(result).toBe(base)1281 })12821283 test("join with custom separator", () => {1284 const base = {items: ["a", "b", "c"]}1285 const result = produce(base, draft => {1286 const joined = draft.items.join(" - ")1287 expect(joined).toBe("a - b - c")1288 })1289 expect(result).toBe(base)1290 })12911292 test("join with no separator uses comma", () => {1293 const base = {items: [1, 2, 3]}1294 const result = produce(base, draft => {1295 const joined = draft.items.join()1296 expect(joined).toBe("1,2,3")1297 })1298 expect(result).toBe(base)1299 })13001301 test("join with objects calls toString", () => {1302 const base = createTestData()1303 const result = produce(base, draft => {1304 const joined = draft.items.join(",")1305 expect(joined).toContain("[object Object]")1306 })1307 expect(result).toBe(base)1308 })1309 })1310

feat: Override array methods to avoid proxy creation while iterating and updating (#1184)Mark Eriksonbefore adoptiond38a9d0

lines 1311–1821why, one step back
1311 describe("concat()", () => {
1312 test("returns new array with concatenated items", () => {1313 const base = {items: [{id: 1}, {id: 2}]}1314 const result = produce(base, draft => {1315 const concatenated = draft.items.concat([{id: 3}])1316 expect(concatenated).toHaveLength(3)1317 expect(concatenated[2].id).toBe(3)1318 })1319 expect(result).toBe(base)1320 })13211322 test("concat with no arguments creates shallow copy", () => {1323 const base = {1324 items: [1325 {id: 1, value: 10},1326 {id: 2, value: 20}1327 ]1328 }1329 const result = produce(base, draft => {1330 const copy = draft.items.concat()1331 expect(copy).toHaveLength(2)1332 expect(copy[0].id).toBe(1)1333 })1334 expect(result).toBe(base)1335 })13361337 // Behavior differs based on array methods plugin1338 if (useArrayMethods) {1339 test("concat returns base values, not drafts (with plugin)", () => {1340 const base = {items: [{id: 1, value: 10}]}1341 const result = produce(base, draft => {1342 const concatenated = draft.items.concat()1343 // With plugin: concat returns base values, not drafts1344 expect(isDraft(concatenated[0])).toBe(false)1345 })1346 expect(result).toBe(base)1347 })1348 } else {1349 test("concat returns draft proxies (default behavior)", () => {1350 const base = {items: [{id: 1, value: 10}]}1351 const result = produce(base, draft => {1352 const concatenated = draft.items.concat()1353 // Without plugin: concat returns draft proxies via get trap1354 expect(isDraft(concatenated[0])).toBe(true)1355 })1356 expect(result).toBe(base)1357 })1358 }13591360 test("concat with multiple arrays", () => {1361 const base = {items: [{id: 1}]}1362 const result = produce(base, draft => {1363 const concatenated = draft.items.concat([{id: 2}], [{id: 3}])1364 expect(concatenated).toHaveLength(3)1365 expect(concatenated[1].id).toBe(2)1366 expect(concatenated[2].id).toBe(3)1367 })1368 expect(result).toBe(base)1369 })13701371 test("concat result assigned to draft works", () => {1372 const base = {items: [{id: 1}], combined: []}1373 const result = produce(base, draft => {1374 draft.combined = draft.items.concat([{id: 2}])1375 })1376 expect(result.combined).toHaveLength(2)1377 expect(result.combined[0].id).toBe(1)1378 expect(result.combined[1].id).toBe(2)1379 })13801381 test("concat with primitives", () => {1382 const base = {numbers: [1, 2, 3]}1383 const result = produce(base, draft => {1384 const concatenated = draft.numbers.concat([4, 5])1385 expect(concatenated).toEqual([1, 2, 3, 4, 5])1386 })1387 expect(result).toBe(base)1388 })1389 })13901391 describe("flat()", () => {1392 test("returns flattened array", () => {1393 const base = {1394 nested: [1395 [1, 2],1396 [3, 4]1397 ]1398 }1399 const result = produce(base, draft => {1400 const flattened = draft.nested.flat()1401 expect(flattened).toEqual([1, 2, 3, 4])1402 })1403 expect(result).toBe(base)1404 })14051406 test("flat with depth parameter", () => {1407 const base = {nested: [[[1, 2]], [[3, 4]]]}1408 const result = produce(base, draft => {1409 const shallow = draft.nested.flat(1)1410 expect(shallow).toEqual([1411 [1, 2],1412 [3, 4]1413 ])1414 const deep = draft.nested.flat(2)1415 expect(deep).toEqual([1, 2, 3, 4])1416 })1417 expect(result).toBe(base)1418 })14191420 // Behavior differs based on array methods plugin1421 if (useArrayMethods) {1422 test("flat returns base values, not drafts (with plugin)", () => {1423 const base = {nested: [[{id: 1}], [{id: 2}]]}1424 const result = produce(base, draft => {1425 const flattened = draft.nested.flat()1426 expect(flattened).toHaveLength(2)1427 // With plugin: flat returns base values, not drafts1428 expect(isDraft(flattened[0])).toBe(false)1429 })1430 expect(result).toBe(base)1431 })1432 } else {1433 test("flat returns draft proxies (default behavior)", () => {1434 const base = {nested: [[{id: 1}], [{id: 2}]]}1435 const result = produce(base, draft => {1436 const flattened = draft.nested.flat()1437 expect(flattened).toHaveLength(2)1438 // Without plugin: flat returns draft proxies via get trap1439 expect(isDraft(flattened[0])).toBe(true)1440 })1441 expect(result).toBe(base)1442 })1443 }14441445 test("flat with empty nested arrays", () => {1446 const base = {nested: [[], [1, 2], []]}1447 const result = produce(base, draft => {1448 const flattened = draft.nested.flat()1449 expect(flattened).toEqual([1, 2])1450 })1451 expect(result).toBe(base)1452 })14531454 test("flat result assigned to draft works", () => {1455 const base = {nested: [[1], [2]], result: []}1456 const result = produce(base, draft => {1457 draft.result = draft.nested.flat()1458 })1459 expect(result.result).toEqual([1, 2])1460 })1461 })14621463 describe("findIndex()", () => {1464 test("returns index of found item", () => {1465 const base = createTestData()1466 const result = produce(base, draft => {1467 const index = draft.items.findIndex(item => item.id === 3)1468 expect(index).toBe(2)1469 })1470 expect(result).toBe(base)1471 })14721473 test("returns -1 when not found", () => {1474 const base = createTestData()1475 const result = produce(base, draft => {1476 const index = draft.items.findIndex(item => item.id === 999)1477 expect(index).toBe(-1)1478 })1479 expect(result).toBe(base)1480 })14811482 test("predicate receives correct arguments", () => {1483 const base = createTestData()1484 const result = produce(base, draft => {1485 draft.items.findIndex((item, index, array) => {1486 expect(typeof index).toBe("number")1487 expect(Array.isArray(array)).toBe(true)1488 return false1489 })1490 })1491 expect(result).toBe(base)1492 })14931494 test("works with primitives", () => {1495 const base = {numbers: [10, 20, 30, 40, 50]}1496 const result = produce(base, draft => {1497 const index = draft.numbers.findIndex(n => n > 25)1498 expect(index).toBe(2)1499 })1500 expect(result).toBe(base)1501 })1502 })15031504 describe("findLastIndex()", () => {1505 test("returns last matching index", () => {1506 const base = {1507 items: [1508 {id: 1, type: "A"},1509 {id: 2, type: "B"},1510 {id: 3, type: "A"}1511 ]1512 }1513 const result = produce(base, draft => {1514 const index = draft.items.findLastIndex(item => item.type === "A")1515 expect(index).toBe(2)1516 })1517 expect(result).toBe(base)1518 })15191520 test("returns -1 when not found", () => {1521 const base = {items: [{id: 1}, {id: 2}]}1522 const result = produce(base, draft => {1523 const index = draft.items.findLastIndex(item => item.id === 999)1524 expect(index).toBe(-1)1525 })1526 expect(result).toBe(base)1527 })15281529 test("predicate receives correct arguments", () => {1530 const base = createTestData()1531 const result = produce(base, draft => {1532 draft.items.findLastIndex((item, index, array) => {1533 expect(typeof index).toBe("number")1534 expect(Array.isArray(array)).toBe(true)1535 return false1536 })1537 })1538 expect(result).toBe(base)1539 })1540 })15411542 describe("some()", () => {1543 test("returns true when condition met", () => {1544 const base = createTestData()1545 const result = produce(base, draft => {1546 const hasHighValue = draft.items.some(item => item.value > 40)1547 expect(hasHighValue).toBe(true)1548 })1549 expect(result).toBe(base)1550 })15511552 test("returns false when no items match", () => {1553 const base = createTestData()1554 const result = produce(base, draft => {1555 const hasHugeValue = draft.items.some(item => item.value > 1000)1556 expect(hasHugeValue).toBe(false)1557 })1558 expect(result).toBe(base)1559 })15601561 test("short-circuits on first match", () => {1562 const base = {items: [{id: 1}, {id: 2}, {id: 3}]}1563 let callCount = 01564 const result = produce(base, draft => {1565 const found = draft.items.some(item => {1566 callCount++1567 return item.id === 11568 })1569 expect(found).toBe(true)1570 })1571 expect(callCount).toBe(1)1572 expect(result).toBe(base)1573 })15741575 test("predicate receives correct arguments", () => {1576 const base = createTestData()1577 const result = produce(base, draft => {1578 draft.items.some((item, index, array) => {1579 expect(typeof index).toBe("number")1580 expect(Array.isArray(array)).toBe(true)1581 return false1582 })1583 })1584 expect(result).toBe(base)1585 })15861587 test("works with primitives", () => {1588 const base = {numbers: [1, 2, 3, 4, 5]}1589 const result = produce(base, draft => {1590 expect(draft.numbers.some(n => n > 3)).toBe(true)1591 expect(draft.numbers.some(n => n > 10)).toBe(false)1592 })1593 expect(result).toBe(base)1594 })1595 })15961597 describe("every()", () => {1598 test("returns true when all items match", () => {1599 const base = createTestData()1600 const result = produce(base, draft => {1601 const allPositive = draft.items.every(item => item.value > 0)1602 expect(allPositive).toBe(true)1603 })1604 expect(result).toBe(base)1605 })16061607 test("returns false when any item fails", () => {1608 const base = createTestData()1609 const result = produce(base, draft => {1610 const allHighValue = draft.items.every(item => item.value > 30)1611 expect(allHighValue).toBe(false)1612 })1613 expect(result).toBe(base)1614 })16151616 test("short-circuits on first failure", () => {1617 const base = {items: [{id: 1}, {id: 2}, {id: 3}]}1618 let callCount = 01619 const result = produce(base, draft => {1620 const allMatch = draft.items.every(item => {1621 callCount++1622 return item.id === 9991623 })1624 expect(allMatch).toBe(false)1625 })1626 expect(callCount).toBe(1)1627 expect(result).toBe(base)1628 })16291630 test("predicate receives correct arguments", () => {1631 const base = createTestData()1632 const result = produce(base, draft => {1633 draft.items.every((item, index, array) => {1634 expect(typeof index).toBe("number")1635 expect(Array.isArray(array)).toBe(true)1636 return true1637 })1638 })1639 expect(result).toBe(base)1640 })16411642 test("returns true for empty array", () => {1643 const base = {items: []}1644 const result = produce(base, draft => {1645 const allMatch = draft.items.every(() => false)1646 expect(allMatch).toBe(true)1647 })1648 expect(result).toBe(base)1649 })1650 })16511652 describe("lastIndexOf()", () => {1653 test("returns last index of item", () => {1654 const base = {items: [1, 2, 3, 2, 1]}1655 const result = produce(base, draft => {1656 const index = draft.items.lastIndexOf(2)1657 expect(index).toBe(3)1658 })1659 expect(result).toBe(base)1660 })16611662 test("returns -1 when not found", () => {1663 const base = {items: [1, 2, 3]}1664 const result = produce(base, draft => {1665 const index = draft.items.lastIndexOf(99)1666 expect(index).toBe(-1)1667 })1668 expect(result).toBe(base)1669 })16701671 test("works with fromIndex parameter", () => {1672 const base = {items: [1, 2, 3, 2, 1]}1673 const result = produce(base, draft => {1674 const index = draft.items.lastIndexOf(2, 2)1675 expect(index).toBe(1)1676 })1677 expect(result).toBe(base)1678 })16791680 test("works with negative fromIndex", () => {1681 const base = {items: [1, 2, 3, 2, 1]}1682 const result = produce(base, draft => {1683 const index = draft.items.lastIndexOf(2, -2)1684 expect(index).toBe(3)1685 })1686 expect(result).toBe(base)1687 })1688 })16891690 describe("includes()", () => {1691 test("returns true when item exists", () => {1692 const base = {items: [1, 2, 3, 4, 5]}1693 const result = produce(base, draft => {1694 expect(draft.items.includes(3)).toBe(true)1695 })1696 expect(result).toBe(base)1697 })16981699 test("returns false when item doesn't exist", () => {1700 const base = {items: [1, 2, 3, 4, 5]}1701 const result = produce(base, draft => {1702 expect(draft.items.includes(99)).toBe(false)1703 })1704 expect(result).toBe(base)1705 })17061707 test("works with fromIndex parameter", () => {1708 const base = {items: [1, 2, 3, 4, 5]}1709 const result = produce(base, draft => {1710 expect(draft.items.includes(2, 2)).toBe(false)1711 expect(draft.items.includes(3, 2)).toBe(true)1712 })1713 expect(result).toBe(base)1714 })17151716 test("works with NaN", () => {1717 const base = {items: [1, NaN, 3]}1718 const result = produce(base, draft => {1719 expect(draft.items.includes(NaN)).toBe(true)1720 })1721 expect(result).toBe(base)1722 })17231724 test("works with negative fromIndex", () => {1725 const base = {items: [1, 2, 3, 4, 5]}1726 const result = produce(base, draft => {1727 expect(draft.items.includes(4, -2)).toBe(true)1728 expect(draft.items.includes(2, -2)).toBe(false)1729 })1730 expect(result).toBe(base)1731 })1732 })17331734 describe("toString()", () => {1735 test("returns string representation", () => {1736 const base = {items: [1, 2, 3]}1737 const result = produce(base, draft => {1738 const str = draft.items.toString()1739 expect(str).toBe("1,2,3")1740 })1741 expect(result).toBe(base)1742 })17431744 test("works with objects", () => {1745 const base = {items: [{id: 1}, {id: 2}]}1746 const result = produce(base, draft => {1747 const str = draft.items.toString()1748 expect(str).toContain("[object Object]")1749 })1750 expect(result).toBe(base)1751 })17521753 test("works with empty array", () => {1754 const base = {items: []}1755 const result = produce(base, draft => {1756 const str = draft.items.toString()1757 expect(str).toBe("")1758 })1759 expect(result).toBe(base)1760 })1761 })17621763 describe("toLocaleString()", () => {1764 test("returns locale string representation", () => {1765 const base = {items: [1, 2, 3]}1766 const result = produce(base, draft => {1767 const str = draft.items.toLocaleString()1768 expect(typeof str).toBe("string")1769 })1770 expect(result).toBe(base)1771 })17721773 test("works with numbers", () => {1774 const base = {numbers: [1000, 2000]}1775 const result = produce(base, draft => {1776 const str = draft.numbers.toLocaleString()1777 expect(typeof str).toBe("string")1778 })1779 expect(result).toBe(base)1780 })1781 })17821783 describe("findLast() additional edge cases", () => {1784 test("returns undefined when not found", () => {1785 const base = {items: [{id: 1}, {id: 2}, {id: 3}]}1786 const result = produce(base, draft => {1787 const found = draft.items.findLast(item => item.id === 999)1788 expect(found).toBeUndefined()1789 })1790 expect(result).toBe(base)1791 })17921793 test("nested mutations on findLast result", () => {1794 const base = {1795 items: [1796 {id: 1, type: "A", nested: {value: 10}},1797 {id: 2, type: "B", nested: {value: 20}},1798 {id: 3, type: "A", nested: {value: 30}}1799 ]1800 }1801 const result = produce(base, draft => {1802 const found = draft.items.findLast(item => item.type === "A")1803 expect(isDraft(found)).toBe(true)1804 if (found) {1805 found.nested.value = 9991806 }1807 })1808 expect(result.items[2].nested.value).toBe(999)1809 expect(base.items[2].nested.value).toBe(30)1810 })18111812 test("findLast on empty array", () => {1813 const base = {items: []}1814 const result = produce(base, draft => {1815 const found = draft.items.findLast(() => true)1816 expect(found).toBeUndefined()1817 })1818 expect(result).toBe(base)1819 })1820 })1821

fix: forward thisArg in the array-methods plugin (#1292)Bao Nguyenbefore adoption1d88550

lines 1822–1912why, one step back
1822 // Regression: the plugin hand-rolls filter/find/findLast and used to
1823 // drop the optional thisArg argument (ECMA-262 23.1.3.8, 23.1.3.12.1).1824 describe("thisArg forwarding", () => {1825 test("filter() passes thisArg to the predicate", () => {1826 const base = createTestData()1827 const ctx = {threshold: 25}1828 const seen = []1829 const result = produce(base, draft => {1830 const filtered = draft.items.filter(function (item) {1831 seen.push(this)1832 return item.value > this.threshold1833 }, ctx)1834 expect(filtered.map(item => item.id)).toEqual([3, 4, 5])1835 })1836 expect(seen).toHaveLength(5)1837 seen.forEach(self => expect(self).toBe(ctx))1838 expect(result).toBe(base)1839 })18401841 test("find() passes thisArg to the predicate", () => {1842 const base = createTestData()1843 const ctx = {targetId: 3}1844 const result = produce(base, draft => {1845 const found = draft.items.find(function (item) {1846 return item.id === this.targetId1847 }, ctx)1848 expect(found.value).toBe(30)1849 })1850 expect(result).toBe(base)1851 })18521853 test("findLast() passes thisArg to the predicate", () => {1854 const base = createTestData()1855 const ctx = {threshold: 25}1856 const result = produce(base, draft => {1857 const found = draft.items.findLast(function (item) {1858 return item.value > this.threshold1859 }, ctx)1860 expect(found.id).toBe(5)1861 })1862 expect(result).toBe(base)1863 })18641865 test("thisArg still reaches predicates the plugin does not hand-roll", () => {1866 const base = createTestData()1867 const ctx = {threshold: 25}1868 const result = produce(base, draft => {1869 expect(1870 draft.items.findIndex(function (item) {1871 return item.value > this.threshold1872 }, ctx)1873 ).toBe(2)1874 expect(1875 draft.items.findLastIndex(function (item) {1876 return item.value > this.threshold1877 }, ctx)1878 ).toBe(4)1879 expect(1880 draft.items.some(function (item) {1881 return item.value > this.threshold1882 }, ctx)1883 ).toBe(true)1884 expect(1885 draft.items.every(function (item) {1886 return item.value > this.threshold1887 }, ctx)1888 ).toBe(false)1889 })1890 expect(result).toBe(base)1891 })18921893 test("omitting thisArg leaves the predicate's this undefined", () => {1894 const base = createTestData()1895 const result = produce(base, draft => {1896 draft.items.filter(function () {1897 expect(this).toBeUndefined()1898 return false1899 })1900 draft.items.find(function () {1901 expect(this).toBeUndefined()1902 return false1903 })1904 draft.items.findLast(function () {1905 expect(this).toBeUndefined()1906 return false1907 })1908 })1909 expect(result).toBe(base)1910 })1911 })1912

feat: Override array methods to avoid proxy creation while iterating and updating (#1184)Mark Eriksonbefore adoptiond38a9d0

lines 1913–1956why, one step back
1913 describe("comparison: filter vs concat behavior", () => {
1914 test("filter returns drafts that can affect original", () => {1915 const base = {1916 items: [1917 {id: 1, value: 10},1918 {id: 2, value: 20}1919 ]1920 }1921 const result = produce(base, draft => {1922 const filtered = draft.items.filter(item => item.id === 1)1923 expect(isDraft(filtered[0])).toBe(true)1924 filtered[0].value = 9991925 })1926 expect(result.items[0].value).toBe(999)1927 })19281929 // Behavior differs based on array methods plugin1930 if (useArrayMethods) {1931 test("concat returns base values that cannot affect original (with plugin)", () => {1932 const base = {items: [{id: 1, value: 10}]}1933 const result = produce(base, draft => {1934 const concatenated = draft.items.concat()1935 // With plugin: concat returns base values, not drafts1936 expect(isDraft(concatenated[0])).toBe(false)1937 // Attempting to mutate won't affect the draft1938 // (would throw if autoFreeze is on, or just not be tracked)1939 })1940 expect(result).toBe(base)1941 })1942 } else {1943 test("concat returns drafts that can affect original (default behavior)", () => {1944 const base = {items: [{id: 1, value: 10}]}1945 const result = produce(base, draft => {1946 const concatenated = draft.items.concat()1947 // Without plugin: concat returns draft proxies via get trap1948 expect(isDraft(concatenated[0])).toBe(true)1949 // Mutations to concatenated items ARE tracked1950 concatenated[0].value = 9991951 })1952 expect(result.items[0].value).toBe(999)1953 })1954 }1955 })1956

Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37

lines 1957–2128why, one step back
1957 describe("combined operations", () => {
1958 test("chain filter then map then mutate", () => {1959 const base = createTestData()1960 const result = produce(base, draft => {1961 const filtered = draft.items.filter(item => item.value > 20)1962 // Verify filtered items are drafts1963 filtered.forEach(item => expect(isDraft(item)).toBe(true))1964 const nested = filtered.map(item => item.nested)1965 // Verify mapped items are drafts1966 expect(isDraft(nested[0])).toBe(true)1967 nested[0].count = 9991968 })1969 expect(result.items[2].nested.count).toBe(999)1970 // Verify base state unchanged1971 expect(base.items[2].nested.count).toBe(3)1972 // Verify result is a copy1973 expect(result.items[2].nested).not.toBe(base.items[2].nested)1974 })19751976 test("filter, find, then mutate", () => {1977 const base = createTestData()1978 const result = produce(base, draft => {1979 const filtered = draft.items.filter(item => item.value > 20)1980 // Verify filtered items are drafts1981 filtered.forEach(item => expect(isDraft(item)).toBe(true))1982 const found = filtered.find(item => item.id === 4)1983 // Verify found item is a draft1984 expect(isDraft(found)).toBe(true)1985 if (found) {1986 found.value = 9991987 }1988 })1989 expect(result.items[3].value).toBe(999)1990 // Verify base state unchanged1991 expect(base.items[3].value).toBe(40)1992 // Verify result is a copy1993 expect(result.items[3]).not.toBe(base.items[3])1994 })19951996 test("multiple filters with mutations", () => {1997 const base = createTestData()1998 const result = produce(base, draft => {1999 const filtered1 = draft.items.filter(item => item.value > 15)2000 // Verify first filter items are drafts

The first 2,000 of 4,415 lines.