Skip to content
Myr

base.js

This file

Path

__tests__/base.js

4,267 lines · 119.0 KiB · JavaScript

Last changed by

fix: preserve structural sharing for no-op array-methods callsmaximilliangrandbefore adoption0c3efdd

History of this file

Take it

The walk stopped after 200 commits: lines older than 220d61d point at it.

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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"

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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"

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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) {

chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f

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

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

line 68why, one step back
68 })

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

line 69why, one step back
69

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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

chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f

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

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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 => {

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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 => {

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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)

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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 => {

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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)

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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 => {

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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)

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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 => {

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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)

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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 => {

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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 })

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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 => {

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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 => {

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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 })

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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 => {

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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", () => {

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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}

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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 })

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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 => {

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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)

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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 => {

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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)

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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 => {

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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)

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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 => {

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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)

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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", () => {

fix: preserve structural sharing for no-op array-methods callsmaximilliangrandbefore adoption0c3efdd

lines 295–332why, 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 })331 })332

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

lines 333–395why, one step back
333 // Reported here: https://github.com/mweststrate/immer/issues/116
334 it("can pop then push", () => {335 const nextState = produce([1, 2, 3], s => {336 s.pop()337 s.push(100)338 })339 expect(nextState).toEqual([1, 2, 100])340 })341342 it("can be sorted", () => {343 const baseState = [{value: 3}, {value: 1}, {value: 2}]344 const nextState = produce(baseState, s => {345 s.sort((a, b) => a.value - b.value)346 })347 expect(nextState).not.toBe(baseState)348 expect(nextState).toEqual([{value: 1}, {value: 2}, {value: 3}])349 })350351 it("can be reversed", () => {352 const baseState = [{value: 1}, {value: 2}, {value: 3}]353 const nextState = produce(baseState, s => {354 s.reverse()355 })356 expect(nextState).not.toBe(baseState)357 expect(nextState).toEqual([{value: 3}, {value: 2}, {value: 1}])358 })359360 it("can be sorted with existing proxies", () => {361 const baseState = [{value: 3}, {value: 1}, {value: 2}]362 const nextState = produce(baseState, s => {363 // First mutate a nested object to create a proxy364 s[0].value = 4365 // Then sort the array366 s.sort((a, b) => a.value - b.value)367 })368 expect(nextState).not.toBe(baseState)369 expect(nextState).toEqual([{value: 1}, {value: 2}, {value: 4}])370 })371372 it("can be reversed with existing proxies", () => {373 const baseState = [{value: 1}, {value: 2}, {value: 3}]374 const nextState = produce(baseState, s => {375 // First mutate a nested object to create a proxy376 s[1].value = 5377 // Then reverse the array378 s.reverse()379 })380 expect(nextState).not.toBe(baseState)381 expect(nextState).toEqual([{value: 3}, {value: 5}, {value: 1}])382 })383384 it("can be sorted with unmodified existing proxies", () => {385 const baseState = [{value: 3}, {value: 1}, {value: 2}]386 const nextState = produce(baseState, s => {387 // Access a nested object to create a proxy, but don't modify it388 const firstValue = s[0].value // This creates a proxy for s[0]389 expect(firstValue).toBe(3) // But we don't modify it390391 // Then sort the array392 s.sort((a, b) => a.value - b.value)393 })394 expect(nextState).not.toBe(baseState)395 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 396–426why, one step back
396 })
397398 it("mutating an element after reverse() does not mutate the base", () => {399 const reordered = {id: 3}400 const baseState = [{id: 1}, {id: 2}, reordered]401 const nextState = produce(baseState, s => {402 s.reverse()403 // After reverse, the element at index 0 is the relocated base404 // object. It must be drafted, otherwise writing to it mutates base.405 expect(isDraft(s[0])).toBe(true)406 s[0].id = 99407 })408 expect(baseState).toEqual([{id: 1}, {id: 2}, {id: 3}])409 expect(reordered).toEqual({id: 3})410 expect(nextState).toEqual([{id: 99}, {id: 2}, {id: 1}])411 })412413 it("mutating an element after sort() does not mutate the base", () => {414 const baseState = [{value: 3}, {value: 1}, {value: 2}]415 const [nextState, patches, inverse] = produceWithPatches(416 baseState,417 s => {418 s.sort((a, b) => a.value - b.value)419 expect(isDraft(s[0])).toBe(true)420 s[0].value = 99421 }422 )423 expect(baseState).toEqual([{value: 3}, {value: 1}, {value: 2}])424 expect(nextState).toEqual([{value: 99}, {value: 2}, {value: 3}])425 expect(applyPatches(baseState, patches)).toEqual(nextState)426 expect(applyPatches(nextState, inverse)).toEqual(baseState)

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

lines 427–463why, one step back
427 })
428429 describe("push()", () => {430 test("push single item", () => {431 const base = {items: [{id: 1}, {id: 2}]}432 const result = produce(base, draft => {433 draft.items.push({id: 3})434 })435 expect(result.items).toHaveLength(3)436 expect(result.items[2].id).toBe(3)437 })438439 test("push multiple items", () => {440 const base = {items: [{id: 1}]}441 const result = produce(base, draft => {442 draft.items.push({id: 2}, {id: 3}, {id: 4})443 })444 expect(result.items).toHaveLength(4)445 expect(result.items[3].id).toBe(4)446 })447448 test("push then mutate pushed item", () => {449 const base = {items: [{id: 1}]}450 const result = produce(base, draft => {451 draft.items.push({id: 2, value: 10})452 draft.items[1].value = 20453 })454 expect(result.items[1].value).toBe(20)455 })456457 test("push returns new length", () => {458 const base = {items: [1, 2]}459 produce(base, draft => {460 const newLength = draft.items.push(3, 4)461 expect(newLength).toBe(4)462 })463 })

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

lines 464–480why, one step back
464
465 test("push onto an index freed by pop() is patched", () => {466 const base = {items: ["a", "b", "c"]}467 const [result, patches, inverse] = produceWithPatches(468 base,469 draft => {470 draft.items.pop()471 draft.items.push("z")472 }473 )474 expect(result.items).toEqual(["a", "b", "z"])475 expect(patches).toEqual([476 {op: "replace", path: ["items", 2], value: "z"}477 ])478 expect(applyPatches(base, patches)).toEqual(result)479 expect(applyPatches(result, inverse)).toEqual(base)480 })

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

lines 481–817why, one step back
481 })
482483 describe("unshift()", () => {484 test("unshift single item", () => {485 const base = {items: [{id: 2}, {id: 3}]}486 const result = produce(base, draft => {487 draft.items.unshift({id: 1})488 })489 expect(result.items).toHaveLength(3)490 expect(result.items[0].id).toBe(1)491 expect(result.items[1].id).toBe(2)492 })493494 test("unshift multiple items", () => {495 const base = {items: [{id: 4}]}496 const result = produce(base, draft => {497 draft.items.unshift({id: 1}, {id: 2}, {id: 3})498 })499 expect(result.items).toHaveLength(4)500 expect(result.items[0].id).toBe(1)501 expect(result.items[3].id).toBe(4)502 })503504 test("unshift then mutate unshifted item", () => {505 const base = {items: [{id: 2}]}506 const result = produce(base, draft => {507 draft.items.unshift({id: 1, value: 10})508 draft.items[0].value = 20509 })510 expect(result.items[0].value).toBe(20)511 })512513 test("unshift returns new length", () => {514 const base = {items: [3, 4]}515 produce(base, draft => {516 const newLength = draft.items.unshift(1, 2)517 expect(newLength).toBe(4)518 })519 })520 })521522 describe("shift()", () => {523 test("shift removes first item", () => {524 const base = {items: [{id: 1}, {id: 2}, {id: 3}]}525 const result = produce(base, draft => {526 const removed = draft.items.shift()527 expect(removed.id).toBe(1)528 })529 expect(result.items).toHaveLength(2)530 expect(result.items[0].id).toBe(2)531 })532533 test("shift on empty array returns undefined", () => {534 const base = {items: []}535 produce(base, draft => {536 const removed = draft.items.shift()537 expect(removed).toBeUndefined()538 })539 })540541 test("shift then mutate remaining items", () => {542 const base = {items: [{id: 1}, {id: 2, value: 10}]}543 const result = produce(base, draft => {544 draft.items.shift()545 draft.items[0].value = 20546 })547 expect(result.items).toHaveLength(1)548 expect(result.items[0].value).toBe(20)549 })550551 test("multiple shifts", () => {552 const base = {items: [{id: 1}, {id: 2}, {id: 3}]}553 const result = produce(base, draft => {554 draft.items.shift()555 draft.items.shift()556 })557 expect(result.items).toHaveLength(1)558 expect(result.items[0].id).toBe(3)559 })560 })561562 describe("splice() edge cases", () => {563 test("splice with only deleteCount (no items to add)", () => {564 const base = {items: [{id: 1}, {id: 2}, {id: 3}, {id: 4}]}565 const result = produce(base, draft => {566 const removed = draft.items.splice(1, 2)567 expect(removed).toHaveLength(2)568 expect(removed[0].id).toBe(2)569 })570 expect(result.items).toHaveLength(2)571 expect(result.items[0].id).toBe(1)572 expect(result.items[1].id).toBe(4)573 })574575 test("splice with negative start index", () => {576 const base = {items: [{id: 1}, {id: 2}, {id: 3}]}577 const result = produce(base, draft => {578 draft.items.splice(-1, 1, {id: 99})579 })580 expect(result.items[2].id).toBe(99)581 })582583 test("splice at start (index 0)", () => {584 const base = {items: [{id: 1}, {id: 2}]}585 const result = produce(base, draft => {586 draft.items.splice(0, 0, {id: 0})587 })588 expect(result.items[0].id).toBe(0)589 expect(result.items).toHaveLength(3)590 })591592 test("splice at end", () => {593 const base = {items: [{id: 1}, {id: 2}]}594 const result = produce(base, draft => {595 draft.items.splice(2, 0, {id: 3})596 })597 expect(result.items[2].id).toBe(3)598 })599600 test("splice then mutate spliced-in items", () => {601 const base = {items: [{id: 1}, {id: 3}]}602 const result = produce(base, draft => {603 draft.items.splice(1, 0, {id: 2, value: 10})604 draft.items[1].value = 20605 })606 expect(result.items[1].value).toBe(20)607 })608 })609610 describe("combined operations", () => {611 test("sort then push", () => {612 const base = {items: [{value: 3}, {value: 1}]}613 const result = produce(base, draft => {614 draft.items.sort((a, b) => a.value - b.value)615 draft.items.push({value: 4})616 })617 expect(result.items.map(i => i.value)).toEqual([1, 3, 4])618 })619620 test("reverse then unshift", () => {621 const base = {items: [{id: 1}, {id: 2}, {id: 3}]}622 const result = produce(base, draft => {623 draft.items.reverse()624 draft.items.unshift({id: 0})625 })626 expect(result.items.map(i => i.id)).toEqual([0, 3, 2, 1])627 })628629 test("splice then sort", () => {630 const base = {items: [{value: 5}, {value: 2}, {value: 8}]}631 const result = produce(base, draft => {632 draft.items.splice(1, 1, {value: 1})633 draft.items.sort((a, b) => a.value - b.value)634 })635 expect(result.items.map(i => i.value)).toEqual([1, 5, 8])636 })637638 test("push, pop, push sequence", () => {639 const base = {items: [{id: 1}]}640 const result = produce(base, draft => {641 draft.items.push({id: 2})642 draft.items.pop()643 draft.items.push({id: 3})644 })645 expect(result.items.map(i => i.id)).toEqual([1, 3])646 })647 })648649 describe("bulk operations with pre-existing proxies", () => {650 test("access items before sort", () => {651 const base = {items: [{value: 3}, {value: 1}, {value: 2}]}652 const result = produce(base, draft => {653 // Access all items to create proxies654 draft.items.forEach(item => item.value)655 // Then sort656 draft.items.sort((a, b) => a.value - b.value)657 })658 expect(result.items.map(i => i.value)).toEqual([1, 2, 3])659 })660661 test("mutate items before reverse", () => {662 const base = {663 items: [664 {id: 1, value: 10},665 {id: 2, value: 20}666 ]667 }668 const result = produce(base, draft => {669 // Mutate to create modified proxies670 draft.items[0].value = 15671 draft.items[1].value = 25672 // Then reverse673 draft.items.reverse()674 })675 expect(result.items[0].id).toBe(2)676 expect(result.items[0].value).toBe(25)677 expect(result.items[1].id).toBe(1)678 expect(result.items[1].value).toBe(15)679 })680 })681682 describe("return values", () => {683 test("pop returns removed item", () => {684 const base = {items: [{id: 1}, {id: 2}]}685 produce(base, draft => {686 const removed = draft.items.pop()687 expect(removed.id).toBe(2)688 // Verify we can mutate the returned item689 removed.modified = true690 expect(draft.items[0].modified).toBeUndefined()691 })692 })693694 test("shift returns removed item", () => {695 const base = {items: [{id: 1}, {id: 2}]}696 produce(base, draft => {697 const removed = draft.items.shift()698 expect(removed.id).toBe(1)699 })700 })701702 test("splice returns array of removed items", () => {703 const base = {items: [{id: 1}, {id: 2}, {id: 3}]}704 produce(base, draft => {705 const removed = draft.items.splice(0, 2)706 expect(removed).toHaveLength(2)707 expect(removed[0].id).toBe(1)708 expect(removed[1].id).toBe(2)709 })710 })711 })712 })713714 describe("non-mutating array methods", () => {715 // Test data factory716 const createTestData = () => ({717 items: [718 {id: 1, value: 10, nested: {count: 1}},719 {id: 2, value: 20, nested: {count: 2}},720 {id: 3, value: 30, nested: {count: 3}},721 {id: 4, value: 40, nested: {count: 4}},722 {id: 5, value: 50, nested: {count: 5}}723 ],724 other: {data: "test"}725 })726727 describe("filter()", () => {728 test("returns new array with filtered items", () => {729 const base = createTestData()730 const result = produce(base, draft => {731 const filtered = draft.items.filter(item => item.value > 25)732 expect(filtered).toHaveLength(3)733 expect(filtered[0].id).toBe(3)734 })735 expect(result).toBe(base) // No modifications736 })737738 test("mutations to filtered items are reflected in result", () => {739 const base = createTestData()740 const result = produce(base, draft => {741 const filtered = draft.items.filter(item => item.value > 25)742 // Verify filtered items are drafts743 expect(isDraft(filtered[0])).toBe(true)744 filtered[0].value = 999745 })746 expect(result.items[2].value).toBe(999) // id: 3 is at index 2747 expect(result.items[0].value).toBe(10) // Unchanged748 // Verify base state unchanged749 expect(base.items[2].value).toBe(30)750 // Verify result is a copy751 expect(result.items[2]).not.toBe(base.items[2])752 })753754 test("mutations to nested properties work correctly", () => {755 const base = createTestData()756 const result = produce(base, draft => {757 const filtered = draft.items.filter(item => item.id > 2)758 // Verify filtered items are drafts759 expect(isDraft(filtered[0])).toBe(true)760 filtered[0].nested.count = 100761 })762 expect(result.items[2].nested.count).toBe(100)763 // Verify base state unchanged764 expect(base.items[2].nested.count).toBe(3)765 // Verify result is a copy766 expect(result.items[2].nested).not.toBe(base.items[2].nested)767 })768769 test("multiple mutations to different filtered items", () => {770 const base = createTestData()771 const result = produce(base, draft => {772 const filtered = draft.items.filter(item => item.value > 15)773 // Verify all filtered items are drafts774 filtered.forEach(item => expect(isDraft(item)).toBe(true))775 filtered[0].value = 200 // id: 2776 filtered[1].value = 300 // id: 3777 filtered[2].value = 400 // id: 4778 })779 expect(result.items[1].value).toBe(200)780 expect(result.items[2].value).toBe(300)781 expect(result.items[3].value).toBe(400)782 // Verify base state unchanged783 expect(base.items[1].value).toBe(20)784 expect(base.items[2].value).toBe(30)785 expect(base.items[3].value).toBe(40)786 })787788 test("filtered array can be assigned to another property", () => {789 const base = createTestData()790 const result = produce(base, draft => {791 const filtered = draft.items.filter(item => item.value > 25)792 draft.other.filtered = filtered793 })794 expect(result.other.filtered).toHaveLength(3)795 expect(result.other.filtered[0].id).toBe(3)796 })797798 test("cross-reference: mutating filtered item affects original location", () => {799 const base = createTestData()800 const result = produce(base, draft => {801 const filtered = draft.items.filter(item => item.id === 3)802 draft.other.ref = filtered[0]803 filtered[0].value = 999804 })805 expect(result.items[2].value).toBe(999)806 expect(result.other.ref.value).toBe(999)807 })808809 test("read-only access doesn't cause mutations", () => {810 const base = createTestData()811 const result = produce(base, draft => {812 const filtered = draft.items.filter(item => item.value > 25)813 const sum = filtered.reduce((acc, item) => acc + item.value, 0)814 expect(sum).toBe(120) // 30 + 40 + 50815 })816 expect(result).toBe(base) // No modifications817 })

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

lines 818–837why, one step back
818
819 test("mutating item in filter result updates original value", () => {820 const initialState = {821 largeArray: Array.from({length: 10}).map((_, i) => ({822 id: i,823 value: i * 10824 }))825 }826827 const result = produce(initialState, draft => {828 const filtered = draft.largeArray.filter(item => item.id <= 5)829830 filtered[0].value = 999831 draft.filtered = filtered832 })833834 expect(result.largeArray[0].value).toBe(999)835 expect(result.filtered[0].value).toBe(999)836 expect(result.largeArray[0]).toBe(result.filtered[0])837 })

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

lines 838–1273why, one step back
838 })
839840 describe("map()", () => {841 test("returns new array with mapped items", () => {842 const base = createTestData()843 const result = produce(base, draft => {844 const mapped = draft.items.map(item => item.nested)845 expect(mapped).toHaveLength(5)846 expect(mapped[0].count).toBe(1)847 })848 expect(result).toBe(base)849 })850851 test("mutations to mapped nested objects work", () => {852 const base = createTestData()853 const result = produce(base, draft => {854 const nested = draft.items.map(item => item.nested)855 // Verify mapped items are drafts856 expect(isDraft(nested[0])).toBe(true)857 nested[0].count = 100858 })859 expect(result.items[0].nested.count).toBe(100)860 // Verify base state unchanged861 expect(base.items[0].nested.count).toBe(1)862 // Verify result is a copy863 expect(result.items[0].nested).not.toBe(base.items[0].nested)864 })865866 test("map with transformation then mutate", () => {867 const base = createTestData()868 const result = produce(base, draft => {869 const mapped = draft.items.map(item => ({870 ...item,871 doubled: item.value * 2872 }))873 // This creates new objects, so mutations won't affect original874 mapped[0].value = 999875 })876 expect(result.items[0].value).toBe(10) // Unchanged877 })878879 test("map returning original items allows mutation", () => {880 const base = createTestData()881 const result = produce(base, draft => {882 const mapped = draft.items.map(item => item) // Identity map883 // Verify mapped items are drafts884 expect(isDraft(mapped[0])).toBe(true)885 mapped[0].value = 999886 })887 expect(result.items[0].value).toBe(999)888 // Verify base state unchanged889 expect(base.items[0].value).toBe(10)890 // Verify result is a copy891 expect(result.items[0]).not.toBe(base.items[0])892 })893 })894895 describe("find()", () => {896 test("returns found item", () => {897 const base = createTestData()898 const result = produce(base, draft => {899 const found = draft.items.find(item => item.id === 3)900 expect(found?.value).toBe(30)901 })902 expect(result).toBe(base)903 })904905 test("mutations to found item are reflected", () => {906 const base = createTestData()907 const result = produce(base, draft => {908 const found = draft.items.find(item => item.id === 3)909 // Verify found item is a draft910 expect(isDraft(found)).toBe(true)911 if (found) {912 found.value = 999913 }914 })915 expect(result.items[2].value).toBe(999)916 // Verify base state unchanged917 expect(base.items[2].value).toBe(30)918 // Verify result is a copy919 expect(result.items[2]).not.toBe(base.items[2])920 })921922 test("nested mutations on found item", () => {923 const base = createTestData()924 const result = produce(base, draft => {925 const found = draft.items.find(item => item.id === 2)926 // Verify found item is a draft927 expect(isDraft(found)).toBe(true)928 if (found) {929 found.nested.count = 200930 }931 })932 expect(result.items[1].nested.count).toBe(200)933 // Verify base state unchanged934 expect(base.items[1].nested.count).toBe(2)935 // Verify result is a copy936 expect(result.items[1].nested).not.toBe(base.items[1].nested)937 })938939 test("find returns undefined when not found", () => {940 const base = createTestData()941 const result = produce(base, draft => {942 const found = draft.items.find(item => item.id === 999)943 expect(found).toBeUndefined()944 })945 expect(result).toBe(base)946 })947 })948949 describe("findLast()", () => {950 test("returns last matching item", () => {951 const base = {952 items: [953 {id: 1, type: "A"},954 {id: 2, type: "B"},955 {id: 3, type: "A"}956 ]957 }958 const result = produce(base, draft => {959 const found = draft.items.findLast(item => item.type === "A")960 expect(found?.id).toBe(3)961 })962 expect(result).toBe(base)963 })964965 test("mutations to findLast result work", () => {966 const base = {967 items: [968 {id: 1, type: "A", value: 10},969 {id: 2, type: "B", value: 20},970 {id: 3, type: "A", value: 30}971 ]972 }973 const result = produce(base, draft => {974 const found = draft.items.findLast(item => item.type === "A")975 // Verify found item is a draft976 expect(isDraft(found)).toBe(true)977 if (found) {978 found.value = 999979 }980 })981 expect(result.items[2].value).toBe(999)982 // Verify base state unchanged983 expect(base.items[2].value).toBe(30)984 // Verify result is a copy985 expect(result.items[2]).not.toBe(base.items[2])986 })987 })988989 describe("slice()", () => {990 test("returns sliced array", () => {991 const base = createTestData()992 const result = produce(base, draft => {993 const sliced = draft.items.slice(1, 3)994 expect(sliced).toHaveLength(2)995 expect(sliced[0].id).toBe(2)996 })997 expect(result).toBe(base)998 })9991000 test("mutations to sliced items work", () => {1001 const base = createTestData()1002 const result = produce(base, draft => {1003 const sliced = draft.items.slice(1, 3)1004 // Verify sliced items are drafts1005 expect(isDraft(sliced[0])).toBe(true)1006 sliced[0].value = 9991007 })1008 expect(result.items[1].value).toBe(999)1009 // Verify base state unchanged1010 expect(base.items[1].value).toBe(20)1011 // Verify result is a copy1012 expect(result.items[1]).not.toBe(base.items[1])1013 })10141015 test("slice with negative indices", () => {1016 const base = createTestData()1017 const result = produce(base, draft => {1018 const sliced = draft.items.slice(-2)1019 // Verify sliced items are drafts1020 expect(isDraft(sliced[0])).toBe(true)1021 sliced[0].value = 9991022 })1023 expect(result.items[3].value).toBe(999)1024 // Verify base state unchanged1025 expect(base.items[3].value).toBe(40)1026 // Verify result is a copy1027 expect(result.items[3]).not.toBe(base.items[3])1028 })1029 })10301031 describe("flatMap()", () => {1032 test("returns flattened array", () => {1033 const base = {1034 groups: [{items: [{id: 1}, {id: 2}]}, {items: [{id: 3}, {id: 4}]}]1035 }1036 const result = produce(base, draft => {1037 const flat = draft.groups.flatMap(group => group.items)1038 expect(flat).toHaveLength(4)1039 expect(flat[0].id).toBe(1)1040 })1041 expect(result).toBe(base)1042 })10431044 test("mutations to flatMapped items work", () => {1045 const base = {1046 groups: [1047 {1048 items: [1049 {id: 1, value: 10},1050 {id: 2, value: 20}1051 ]1052 },1053 {items: [{id: 3, value: 30}]}1054 ]1055 }1056 const result = produce(base, draft => {1057 const flat = draft.groups.flatMap(group => group.items)1058 // Verify flatMapped items are drafts1059 expect(isDraft(flat[0])).toBe(true)1060 flat[0].value = 9991061 })1062 expect(result.groups[0].items[0].value).toBe(999)1063 // Verify base state unchanged1064 expect(base.groups[0].items[0].value).toBe(10)1065 // Verify result is a copy1066 expect(result.groups[0].items[0]).not.toBe(base.groups[0].items[0])1067 })1068 })10691070 describe("reduce()", () => {1071 test("returns accumulated value", () => {1072 const base = createTestData()1073 const result = produce(base, draft => {1074 const sum = draft.items.reduce((acc, item) => acc + item.value, 0)1075 expect(sum).toBe(150) // 10 + 20 + 30 + 40 + 501076 })1077 expect(result).toBe(base)1078 })10791080 test("mutations during reduce are reflected", () => {1081 const base = createTestData()1082 const result = produce(base, draft => {1083 const sum = draft.items.reduce((acc, item, index) => {1084 // Verify items in reduce are drafts1085 expect(isDraft(item)).toBe(true)1086 if (index === 2) {1087 item.value = 9991088 }1089 return acc + item.value1090 }, 0)1091 expect(sum).toBe(1119) // 10 + 20 + 999 + 40 + 501092 })1093 expect(result.items[2].value).toBe(999)1094 // Verify base state unchanged1095 expect(base.items[2].value).toBe(30)1096 // Verify result is a copy1097 expect(result.items[2]).not.toBe(base.items[2])1098 })10991100 test("reduce with object accumulator", () => {1101 const base = createTestData()1102 const result = produce(base, draft => {1103 const grouped = draft.items.reduce((acc, item) => {1104 const key = item.value > 25 ? "high" : "low"1105 if (!acc[key]) acc[key] = []1106 acc[key].push(item)1107 return acc1108 }, {})1109 expect(grouped.low).toHaveLength(2)1110 expect(grouped.high).toHaveLength(3)1111 })1112 expect(result).toBe(base)1113 })11141115 test("reduce then mutate accumulated items", () => {1116 const base = createTestData()1117 const result = produce(base, draft => {1118 const items = draft.items.reduce((acc, item) => {1119 // Verify items in reduce are drafts1120 expect(isDraft(item)).toBe(true)1121 if (item.value > 25) acc.push(item)1122 return acc1123 }, [])1124 // Verify accumulated items are drafts1125 expect(isDraft(items[0])).toBe(true)1126 items[0].value = 9991127 })1128 expect(result.items[2].value).toBe(999) // id: 3 is at index 21129 // Verify base state unchanged1130 expect(base.items[2].value).toBe(30)1131 // Verify result is a copy1132 expect(result.items[2]).not.toBe(base.items[2])1133 })1134 })11351136 describe("forEach()", () => {1137 test("iterates over all items", () => {1138 const base = createTestData()1139 const result = produce(base, draft => {1140 let count = 01141 draft.items.forEach(item => {1142 count++1143 expect(item.id).toBeDefined()1144 })1145 expect(count).toBe(5)1146 })1147 expect(result).toBe(base)1148 })11491150 test("mutations during forEach are reflected", () => {1151 const base = createTestData()1152 const result = produce(base, draft => {1153 draft.items.forEach((item, index) => {1154 // Verify items in forEach are drafts1155 expect(isDraft(item)).toBe(true)1156 if (index === 1) {1157 item.value = 9991158 }1159 })1160 })1161 expect(result.items[1].value).toBe(999)1162 // Verify base state unchanged1163 expect(base.items[1].value).toBe(20)1164 // Verify result is a copy1165 expect(result.items[1]).not.toBe(base.items[1])1166 })11671168 test("forEach with nested mutations", () => {1169 const base = createTestData()1170 const result = produce(base, draft => {1171 draft.items.forEach(item => {1172 // Verify items in forEach are drafts1173 expect(isDraft(item)).toBe(true)1174 item.nested.count *= 101175 })1176 })1177 expect(result.items[0].nested.count).toBe(10)1178 expect(result.items[4].nested.count).toBe(50)1179 // Verify base state unchanged1180 expect(base.items[0].nested.count).toBe(1)1181 // Verify result is a copy1182 expect(result.items[0].nested).not.toBe(base.items[0].nested)1183 })11841185 test("forEach returns undefined", () => {1186 const base = createTestData()1187 const result = produce(base, draft => {1188 const returnValue = draft.items.forEach(item => item.value)1189 expect(returnValue).toBeUndefined()1190 })1191 expect(result).toBe(base)1192 })1193 })11941195 describe("indexOf()", () => {1196 test("returns index of found item", () => {1197 const base = {items: [1, 2, 3, 4, 5]}1198 const result = produce(base, draft => {1199 const index = draft.items.indexOf(3)1200 expect(index).toBe(2)1201 })1202 expect(result).toBe(base)1203 })12041205 test("returns -1 when item not found", () => {1206 const base = {items: [1, 2, 3, 4, 5]}1207 const result = produce(base, draft => {1208 const index = draft.items.indexOf(99)1209 expect(index).toBe(-1)1210 })1211 expect(result).toBe(base)1212 })12131214 test("works with object references", () => {1215 const base = createTestData()1216 const result = produce(base, draft => {1217 const item = draft.items[2]1218 const index = draft.items.indexOf(item)1219 expect(index).toBe(2)1220 })1221 expect(result).toBe(base)1222 })12231224 test("indexOf with fromIndex parameter", () => {1225 const base = {items: [1, 2, 3, 2, 5]}1226 const result = produce(base, draft => {1227 const firstIndex = draft.items.indexOf(2)1228 const secondIndex = draft.items.indexOf(2, 2)1229 expect(firstIndex).toBe(1)1230 expect(secondIndex).toBe(3)1231 })1232 expect(result).toBe(base)1233 })1234 })12351236 describe("join()", () => {1237 test("returns joined string", () => {1238 const base = {items: [1, 2, 3, 4, 5]}1239 const result = produce(base, draft => {1240 const joined = draft.items.join(",")1241 expect(joined).toBe("1,2,3,4,5")1242 })1243 expect(result).toBe(base)1244 })12451246 test("join with custom separator", () => {1247 const base = {items: ["a", "b", "c"]}1248 const result = produce(base, draft => {1249 const joined = draft.items.join(" - ")1250 expect(joined).toBe("a - b - c")1251 })1252 expect(result).toBe(base)1253 })12541255 test("join with no separator uses comma", () => {1256 const base = {items: [1, 2, 3]}1257 const result = produce(base, draft => {1258 const joined = draft.items.join()1259 expect(joined).toBe("1,2,3")1260 })1261 expect(result).toBe(base)1262 })12631264 test("join with objects calls toString", () => {1265 const base = createTestData()1266 const result = produce(base, draft => {1267 const joined = draft.items.join(",")1268 expect(joined).toContain("[object Object]")1269 })1270 expect(result).toBe(base)1271 })1272 })1273

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

lines 1274–1828why, one step back
1274 describe("concat()", () => {
1275 test("returns new array with concatenated items", () => {1276 const base = {items: [{id: 1}, {id: 2}]}1277 const result = produce(base, draft => {1278 const concatenated = draft.items.concat([{id: 3}])1279 expect(concatenated).toHaveLength(3)1280 expect(concatenated[2].id).toBe(3)1281 })1282 expect(result).toBe(base)1283 })12841285 test("concat with no arguments creates shallow copy", () => {1286 const base = {1287 items: [1288 {id: 1, value: 10},1289 {id: 2, value: 20}1290 ]1291 }1292 const result = produce(base, draft => {1293 const copy = draft.items.concat()1294 expect(copy).toHaveLength(2)1295 expect(copy[0].id).toBe(1)1296 })1297 expect(result).toBe(base)1298 })12991300 // Behavior differs based on array methods plugin1301 if (useArrayMethods) {1302 test("concat returns base values, not drafts (with plugin)", () => {1303 const base = {items: [{id: 1, value: 10}]}1304 const result = produce(base, draft => {1305 const concatenated = draft.items.concat()1306 // With plugin: concat returns base values, not drafts1307 expect(isDraft(concatenated[0])).toBe(false)1308 })1309 expect(result).toBe(base)1310 })1311 } else {1312 test("concat returns draft proxies (default behavior)", () => {1313 const base = {items: [{id: 1, value: 10}]}1314 const result = produce(base, draft => {1315 const concatenated = draft.items.concat()1316 // Without plugin: concat returns draft proxies via get trap1317 expect(isDraft(concatenated[0])).toBe(true)1318 })1319 expect(result).toBe(base)1320 })1321 }13221323 test("concat with multiple arrays", () => {1324 const base = {items: [{id: 1}]}1325 const result = produce(base, draft => {1326 const concatenated = draft.items.concat([{id: 2}], [{id: 3}])1327 expect(concatenated).toHaveLength(3)1328 expect(concatenated[1].id).toBe(2)1329 expect(concatenated[2].id).toBe(3)1330 })1331 expect(result).toBe(base)1332 })13331334 test("concat result assigned to draft works", () => {1335 const base = {items: [{id: 1}], combined: []}1336 const result = produce(base, draft => {1337 draft.combined = draft.items.concat([{id: 2}])1338 })1339 expect(result.combined).toHaveLength(2)1340 expect(result.combined[0].id).toBe(1)1341 expect(result.combined[1].id).toBe(2)1342 })13431344 test("concat with primitives", () => {1345 const base = {numbers: [1, 2, 3]}1346 const result = produce(base, draft => {1347 const concatenated = draft.numbers.concat([4, 5])1348 expect(concatenated).toEqual([1, 2, 3, 4, 5])1349 })1350 expect(result).toBe(base)1351 })1352 })13531354 describe("flat()", () => {1355 test("returns flattened array", () => {1356 const base = {1357 nested: [1358 [1, 2],1359 [3, 4]1360 ]1361 }1362 const result = produce(base, draft => {1363 const flattened = draft.nested.flat()1364 expect(flattened).toEqual([1, 2, 3, 4])1365 })1366 expect(result).toBe(base)1367 })13681369 test("flat with depth parameter", () => {1370 const base = {nested: [[[1, 2]], [[3, 4]]]}1371 const result = produce(base, draft => {1372 const shallow = draft.nested.flat(1)1373 expect(shallow).toEqual([1374 [1, 2],1375 [3, 4]1376 ])1377 const deep = draft.nested.flat(2)1378 expect(deep).toEqual([1, 2, 3, 4])1379 })1380 expect(result).toBe(base)1381 })13821383 // Behavior differs based on array methods plugin1384 if (useArrayMethods) {1385 test("flat returns base values, not drafts (with plugin)", () => {1386 const base = {nested: [[{id: 1}], [{id: 2}]]}1387 const result = produce(base, draft => {1388 const flattened = draft.nested.flat()1389 expect(flattened).toHaveLength(2)1390 // With plugin: flat returns base values, not drafts1391 expect(isDraft(flattened[0])).toBe(false)1392 })1393 expect(result).toBe(base)1394 })1395 } else {1396 test("flat returns draft proxies (default behavior)", () => {1397 const base = {nested: [[{id: 1}], [{id: 2}]]}1398 const result = produce(base, draft => {1399 const flattened = draft.nested.flat()1400 expect(flattened).toHaveLength(2)1401 // Without plugin: flat returns draft proxies via get trap1402 expect(isDraft(flattened[0])).toBe(true)1403 })1404 expect(result).toBe(base)1405 })1406 }14071408 test("flat with empty nested arrays", () => {1409 const base = {nested: [[], [1, 2], []]}1410 const result = produce(base, draft => {1411 const flattened = draft.nested.flat()1412 expect(flattened).toEqual([1, 2])1413 })1414 expect(result).toBe(base)1415 })14161417 test("flat result assigned to draft works", () => {1418 const base = {nested: [[1], [2]], result: []}1419 const result = produce(base, draft => {1420 draft.result = draft.nested.flat()1421 })1422 expect(result.result).toEqual([1, 2])1423 })1424 })14251426 describe("findIndex()", () => {1427 test("returns index of found item", () => {1428 const base = createTestData()1429 const result = produce(base, draft => {1430 const index = draft.items.findIndex(item => item.id === 3)1431 expect(index).toBe(2)1432 })1433 expect(result).toBe(base)1434 })14351436 test("returns -1 when not found", () => {1437 const base = createTestData()1438 const result = produce(base, draft => {1439 const index = draft.items.findIndex(item => item.id === 999)1440 expect(index).toBe(-1)1441 })1442 expect(result).toBe(base)1443 })14441445 test("predicate receives correct arguments", () => {1446 const base = createTestData()1447 const result = produce(base, draft => {1448 draft.items.findIndex((item, index, array) => {1449 expect(typeof index).toBe("number")1450 expect(Array.isArray(array)).toBe(true)1451 return false1452 })1453 })1454 expect(result).toBe(base)1455 })14561457 test("works with primitives", () => {1458 const base = {numbers: [10, 20, 30, 40, 50]}1459 const result = produce(base, draft => {1460 const index = draft.numbers.findIndex(n => n > 25)1461 expect(index).toBe(2)1462 })1463 expect(result).toBe(base)1464 })1465 })14661467 describe("findLastIndex()", () => {1468 test("returns last matching index", () => {1469 const base = {1470 items: [1471 {id: 1, type: "A"},1472 {id: 2, type: "B"},1473 {id: 3, type: "A"}1474 ]1475 }1476 const result = produce(base, draft => {1477 const index = draft.items.findLastIndex(item => item.type === "A")1478 expect(index).toBe(2)1479 })1480 expect(result).toBe(base)1481 })14821483 test("returns -1 when not found", () => {1484 const base = {items: [{id: 1}, {id: 2}]}1485 const result = produce(base, draft => {1486 const index = draft.items.findLastIndex(item => item.id === 999)1487 expect(index).toBe(-1)1488 })1489 expect(result).toBe(base)1490 })14911492 test("predicate receives correct arguments", () => {1493 const base = createTestData()1494 const result = produce(base, draft => {1495 draft.items.findLastIndex((item, index, array) => {1496 expect(typeof index).toBe("number")1497 expect(Array.isArray(array)).toBe(true)1498 return false1499 })1500 })1501 expect(result).toBe(base)1502 })1503 })15041505 describe("some()", () => {1506 test("returns true when condition met", () => {1507 const base = createTestData()1508 const result = produce(base, draft => {1509 const hasHighValue = draft.items.some(item => item.value > 40)1510 expect(hasHighValue).toBe(true)1511 })1512 expect(result).toBe(base)1513 })15141515 test("returns false when no items match", () => {1516 const base = createTestData()1517 const result = produce(base, draft => {1518 const hasHugeValue = draft.items.some(item => item.value > 1000)1519 expect(hasHugeValue).toBe(false)1520 })1521 expect(result).toBe(base)1522 })15231524 test("short-circuits on first match", () => {1525 const base = {items: [{id: 1}, {id: 2}, {id: 3}]}1526 let callCount = 01527 const result = produce(base, draft => {1528 const found = draft.items.some(item => {1529 callCount++1530 return item.id === 11531 })1532 expect(found).toBe(true)1533 })1534 expect(callCount).toBe(1)1535 expect(result).toBe(base)1536 })15371538 test("predicate receives correct arguments", () => {1539 const base = createTestData()1540 const result = produce(base, draft => {1541 draft.items.some((item, index, array) => {1542 expect(typeof index).toBe("number")1543 expect(Array.isArray(array)).toBe(true)1544 return false1545 })1546 })1547 expect(result).toBe(base)1548 })15491550 test("works with primitives", () => {1551 const base = {numbers: [1, 2, 3, 4, 5]}1552 const result = produce(base, draft => {1553 expect(draft.numbers.some(n => n > 3)).toBe(true)1554 expect(draft.numbers.some(n => n > 10)).toBe(false)1555 })1556 expect(result).toBe(base)1557 })1558 })15591560 describe("every()", () => {1561 test("returns true when all items match", () => {1562 const base = createTestData()1563 const result = produce(base, draft => {1564 const allPositive = draft.items.every(item => item.value > 0)1565 expect(allPositive).toBe(true)1566 })1567 expect(result).toBe(base)1568 })15691570 test("returns false when any item fails", () => {1571 const base = createTestData()1572 const result = produce(base, draft => {1573 const allHighValue = draft.items.every(item => item.value > 30)1574 expect(allHighValue).toBe(false)1575 })1576 expect(result).toBe(base)1577 })15781579 test("short-circuits on first failure", () => {1580 const base = {items: [{id: 1}, {id: 2}, {id: 3}]}1581 let callCount = 01582 const result = produce(base, draft => {1583 const allMatch = draft.items.every(item => {1584 callCount++1585 return item.id === 9991586 })1587 expect(allMatch).toBe(false)1588 })1589 expect(callCount).toBe(1)1590 expect(result).toBe(base)1591 })15921593 test("predicate receives correct arguments", () => {1594 const base = createTestData()1595 const result = produce(base, draft => {1596 draft.items.every((item, index, array) => {1597 expect(typeof index).toBe("number")1598 expect(Array.isArray(array)).toBe(true)1599 return true1600 })1601 })1602 expect(result).toBe(base)1603 })16041605 test("returns true for empty array", () => {1606 const base = {items: []}1607 const result = produce(base, draft => {1608 const allMatch = draft.items.every(() => false)1609 expect(allMatch).toBe(true)1610 })1611 expect(result).toBe(base)1612 })1613 })16141615 describe("lastIndexOf()", () => {1616 test("returns last index of item", () => {1617 const base = {items: [1, 2, 3, 2, 1]}1618 const result = produce(base, draft => {1619 const index = draft.items.lastIndexOf(2)1620 expect(index).toBe(3)1621 })1622 expect(result).toBe(base)1623 })16241625 test("returns -1 when not found", () => {1626 const base = {items: [1, 2, 3]}1627 const result = produce(base, draft => {1628 const index = draft.items.lastIndexOf(99)1629 expect(index).toBe(-1)1630 })1631 expect(result).toBe(base)1632 })16331634 test("works with fromIndex parameter", () => {1635 const base = {items: [1, 2, 3, 2, 1]}1636 const result = produce(base, draft => {1637 const index = draft.items.lastIndexOf(2, 2)1638 expect(index).toBe(1)1639 })1640 expect(result).toBe(base)1641 })16421643 test("works with negative fromIndex", () => {1644 const base = {items: [1, 2, 3, 2, 1]}1645 const result = produce(base, draft => {1646 const index = draft.items.lastIndexOf(2, -2)1647 expect(index).toBe(3)1648 })1649 expect(result).toBe(base)1650 })1651 })16521653 describe("includes()", () => {1654 test("returns true when item exists", () => {1655 const base = {items: [1, 2, 3, 4, 5]}1656 const result = produce(base, draft => {1657 expect(draft.items.includes(3)).toBe(true)1658 })1659 expect(result).toBe(base)1660 })16611662 test("returns false when item doesn't exist", () => {1663 const base = {items: [1, 2, 3, 4, 5]}1664 const result = produce(base, draft => {1665 expect(draft.items.includes(99)).toBe(false)1666 })1667 expect(result).toBe(base)1668 })16691670 test("works with fromIndex parameter", () => {1671 const base = {items: [1, 2, 3, 4, 5]}1672 const result = produce(base, draft => {1673 expect(draft.items.includes(2, 2)).toBe(false)1674 expect(draft.items.includes(3, 2)).toBe(true)1675 })1676 expect(result).toBe(base)1677 })16781679 test("works with NaN", () => {1680 const base = {items: [1, NaN, 3]}1681 const result = produce(base, draft => {1682 expect(draft.items.includes(NaN)).toBe(true)1683 })1684 expect(result).toBe(base)1685 })16861687 test("works with negative fromIndex", () => {1688 const base = {items: [1, 2, 3, 4, 5]}1689 const result = produce(base, draft => {1690 expect(draft.items.includes(4, -2)).toBe(true)1691 expect(draft.items.includes(2, -2)).toBe(false)1692 })1693 expect(result).toBe(base)1694 })1695 })16961697 describe("toString()", () => {1698 test("returns string representation", () => {1699 const base = {items: [1, 2, 3]}1700 const result = produce(base, draft => {1701 const str = draft.items.toString()1702 expect(str).toBe("1,2,3")1703 })1704 expect(result).toBe(base)1705 })17061707 test("works with objects", () => {1708 const base = {items: [{id: 1}, {id: 2}]}1709 const result = produce(base, draft => {1710 const str = draft.items.toString()1711 expect(str).toContain("[object Object]")1712 })1713 expect(result).toBe(base)1714 })17151716 test("works with empty array", () => {1717 const base = {items: []}1718 const result = produce(base, draft => {1719 const str = draft.items.toString()1720 expect(str).toBe("")1721 })1722 expect(result).toBe(base)1723 })1724 })17251726 describe("toLocaleString()", () => {1727 test("returns locale string representation", () => {1728 const base = {items: [1, 2, 3]}1729 const result = produce(base, draft => {1730 const str = draft.items.toLocaleString()1731 expect(typeof str).toBe("string")1732 })1733 expect(result).toBe(base)1734 })17351736 test("works with numbers", () => {1737 const base = {numbers: [1000, 2000]}1738 const result = produce(base, draft => {1739 const str = draft.numbers.toLocaleString()1740 expect(typeof str).toBe("string")1741 })1742 expect(result).toBe(base)1743 })1744 })17451746 describe("findLast() additional edge cases", () => {1747 test("returns undefined when not found", () => {1748 const base = {items: [{id: 1}, {id: 2}, {id: 3}]}1749 const result = produce(base, draft => {1750 const found = draft.items.findLast(item => item.id === 999)1751 expect(found).toBeUndefined()1752 })1753 expect(result).toBe(base)1754 })17551756 test("nested mutations on findLast result", () => {1757 const base = {1758 items: [1759 {id: 1, type: "A", nested: {value: 10}},1760 {id: 2, type: "B", nested: {value: 20}},1761 {id: 3, type: "A", nested: {value: 30}}1762 ]1763 }1764 const result = produce(base, draft => {1765 const found = draft.items.findLast(item => item.type === "A")1766 expect(isDraft(found)).toBe(true)1767 if (found) {1768 found.nested.value = 9991769 }1770 })1771 expect(result.items[2].nested.value).toBe(999)1772 expect(base.items[2].nested.value).toBe(30)1773 })17741775 test("findLast on empty array", () => {1776 const base = {items: []}1777 const result = produce(base, draft => {1778 const found = draft.items.findLast(() => true)1779 expect(found).toBeUndefined()1780 })1781 expect(result).toBe(base)1782 })1783 })17841785 describe("comparison: filter vs concat behavior", () => {1786 test("filter returns drafts that can affect original", () => {1787 const base = {1788 items: [1789 {id: 1, value: 10},1790 {id: 2, value: 20}1791 ]1792 }1793 const result = produce(base, draft => {1794 const filtered = draft.items.filter(item => item.id === 1)1795 expect(isDraft(filtered[0])).toBe(true)1796 filtered[0].value = 9991797 })1798 expect(result.items[0].value).toBe(999)1799 })18001801 // Behavior differs based on array methods plugin1802 if (useArrayMethods) {1803 test("concat returns base values that cannot affect original (with plugin)", () => {1804 const base = {items: [{id: 1, value: 10}]}1805 const result = produce(base, draft => {1806 const concatenated = draft.items.concat()1807 // With plugin: concat returns base values, not drafts1808 expect(isDraft(concatenated[0])).toBe(false)1809 // Attempting to mutate won't affect the draft1810 // (would throw if autoFreeze is on, or just not be tracked)1811 })1812 expect(result).toBe(base)1813 })1814 } else {1815 test("concat returns drafts that can affect original (default behavior)", () => {1816 const base = {items: [{id: 1, value: 10}]}1817 const result = produce(base, draft => {1818 const concatenated = draft.items.concat()1819 // Without plugin: concat returns draft proxies via get trap1820 expect(isDraft(concatenated[0])).toBe(true)1821 // Mutations to concatenated items ARE tracked1822 concatenated[0].value = 9991823 })1824 expect(result.items[0].value).toBe(999)1825 })1826 }1827 })1828

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

lines 1829–2000why, one step back
1829 describe("combined operations", () => {
1830 test("chain filter then map then mutate", () => {1831 const base = createTestData()1832 const result = produce(base, draft => {1833 const filtered = draft.items.filter(item => item.value > 20)1834 // Verify filtered items are drafts1835 filtered.forEach(item => expect(isDraft(item)).toBe(true))1836 const nested = filtered.map(item => item.nested)1837 // Verify mapped items are drafts1838 expect(isDraft(nested[0])).toBe(true)1839 nested[0].count = 9991840 })1841 expect(result.items[2].nested.count).toBe(999)1842 // Verify base state unchanged1843 expect(base.items[2].nested.count).toBe(3)1844 // Verify result is a copy1845 expect(result.items[2].nested).not.toBe(base.items[2].nested)1846 })18471848 test("filter, find, then mutate", () => {1849 const base = createTestData()1850 const result = produce(base, draft => {1851 const filtered = draft.items.filter(item => item.value > 20)1852 // Verify filtered items are drafts1853 filtered.forEach(item => expect(isDraft(item)).toBe(true))1854 const found = filtered.find(item => item.id === 4)1855 // Verify found item is a draft1856 expect(isDraft(found)).toBe(true)1857 if (found) {1858 found.value = 9991859 }1860 })1861 expect(result.items[3].value).toBe(999)1862 // Verify base state unchanged1863 expect(base.items[3].value).toBe(40)1864 // Verify result is a copy1865 expect(result.items[3]).not.toBe(base.items[3])1866 })18671868 test("multiple filters with mutations", () => {1869 const base = createTestData()1870 const result = produce(base, draft => {1871 const filtered1 = draft.items.filter(item => item.value > 15)1872 // Verify first filter items are drafts1873 filtered1.forEach(item => expect(isDraft(item)).toBe(true))1874 const filtered2 = filtered1.filter(item => item.value < 45)1875 // Verify second filter items are drafts1876 filtered2.forEach(item => expect(isDraft(item)).toBe(true))1877 filtered2[0].value = 9991878 })1879 expect(result.items[1].value).toBe(999)1880 // Verify base state unchanged1881 expect(base.items[1].value).toBe(20)1882 // Verify result is a copy1883 expect(result.items[1]).not.toBe(base.items[1])1884 })1885 })18861887 describe("edge cases", () => {1888 test("empty filter result", () => {1889 const base = createTestData()1890 const result = produce(base, draft => {1891 const filtered = draft.items.filter(item => item.value > 1000)1892 expect(filtered).toHaveLength(0)1893 })1894 expect(result).toBe(base)1895 })18961897 test("filter with all items", () => {1898 const base = createTestData()1899 const result = produce(base, draft => {1900 const filtered = draft.items.filter(() => true)1901 filtered[0].value = 9991902 })1903 expect(result.items[0].value).toBe(999)1904 })19051906 test("primitive array filter", () => {1907 const base = {numbers: [1, 2, 3, 4, 5]}1908 const result = produce(base, draft => {1909 const filtered = draft.numbers.filter(n => n > 2)1910 expect(filtered).toEqual([3, 4, 5])1911 })1912 expect(result).toBe(base)1913 })19141915 test("mixed draft and non-draft in filter result", () => {1916 const base = createTestData()1917 const result = produce(base, draft => {1918 // Mutate some items before filtering1919 draft.items[0].value = 151920 draft.items[2].value = 3519211922 const filtered = draft.items.filter(item => item.value > 14)1923 filtered[0].value = 999 // Mutate already-mutated item1924 })1925 expect(result.items[0].value).toBe(999)1926 })19271928 test("assigning filtered array back to draft", () => {1929 const base = createTestData()1930 const result = produce(base, draft => {1931 draft.items = draft.items.filter(item => item.value > 25)1932 draft.items[0].value = 9991933 })1934 expect(result.items).toHaveLength(3)1935 expect(result.items[0].value).toBe(999)1936 expect(result.items[0].id).toBe(3)1937 })1938 })19391940 describe("performance-critical patterns", () => {1941 test("large array filter with read-only access", () => {1942 const base = {1943 items: Array.from({length: 1000}, (_, i) => ({1944 id: i,1945 value: i * 101946 }))1947 }1948 const result = produce(base, draft => {1949 const filtered = draft.items.filter(item => item.value > 5000)1950 const sum = filtered.reduce((acc, item) => acc + item.value, 0)1951 expect(sum).toBeGreaterThan(0)1952 })1953 expect(result).toBe(base) // No modifications1954 })19551956 test("multiple filters without mutations", () => {1957 const base = createTestData()1958 const result = produce(base, draft => {1959 const f1 = draft.items.filter(item => item.value > 10)1960 const f2 = draft.items.filter(item => item.value < 40)1961 const f3 = draft.items.filter(item => item.id % 2 === 0)1962 expect(f1.length + f2.length + f3.length).toBeGreaterThan(0)1963 })1964 expect(result).toBe(base)1965 })1966 })1967 })19681969 it("supports the same child reference multiple times in the same array via index assignment", () => {1970 const obj = {value: 1}1971 const baseState = {items: [obj, {}, {}, {}, {}]}19721973 const nextState = produce(baseState, draft => {1974 // Assign the same object to multiple indices1975 draft.items[0] = obj // Original position1976 draft.items[2] = obj // Same object at different index1977 draft.items[4] = obj // Same object at yet another index19781979 // Modify the object through one of the references1980 draft.items[0].value = 21981 })19821983 // Immer behavior: modified draft gets new object, unmodified drafts are optimized1984 expect(nextState.items[0]).not.toBe(nextState.items[2]) // Modified vs unmodified1985 expect(nextState.items[2]).toBe(nextState.items[4]) // Both unmodified, same reference1986 expect(nextState.items[0].value).toBe(2) // Modified1987 expect(nextState.items[2].value).toBe(1) // Unmodified (same as original)1988 expect(nextState.items[4].value).toBe(1) // Unmodified (same as original)19891990 // The unmodified items should be the same as the original object1991 expect(nextState.items[2]).toBe(obj)1992 expect(nextState.items[4]).toBe(obj)19931994 // Original object should be unchanged1995 expect(obj.value).toBe(1)19961997 // Verify array structure1998 expect(nextState.items.length).toBe(5)1999 })2000

The first 2,000 of 4,267 lines.