base.js
This file
Path
__tests__/base.js
4,180 lines · 116.1 KiB · JavaScript
Last changed by
Merge pull request #1210 from immerjs/bugfix/1209-array-plugin-nested-draftsMark Eriksonbefore adoptioncdccf1a
History of this fileTake it
The walk stopped after 200 commits: lines older than 3f9a94e point at it.
chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 1why, one step back1"use strict"chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5
line 2why, one step back2import {vi} from "vitest"chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 3–8why, one step back3import {4 Immer,5 nothing,6 original,7 isDraft,8 immerable,Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 9why, one step back9 enablePatches,feat: Override array methods to avoid proxy creation while iterating and updating (#1184)Mark Eriksonbefore adoptiond38a9d0
lines 10–11why, one step back10 enableMapSet,11 enableArrayMethodschore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 12why, one step back12} from "../src/immer"feat: Override array methods to avoid proxy creation while iterating and updating (#1184)Mark Eriksonbefore adoptiond38a9d0
lines 13–20why, one step back1314import {15 each,16 shallowCopy,17 DRAFT_STATE,18 clearPlugin,19 PluginArrayMethods20} from "../src/internal"chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 21–23why, one step back21import deepFreeze from "deep-freeze"22import * as lodash from "lodash"23Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 24–26why, one step back24enablePatches()25enableMapSet()26chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5
line 27why, one step back27vi.setConfig({Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 28–30why, one step back28 testTimeout: 200029})30chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 31why, one step back31const isProd = process.env.NODE_ENV === "production"Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
line 32why, one step back32chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 33–34why, one step back33test("immer should have no dependencies", () => {34 expect(require("../package.json").dependencies).toBeUndefined()Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 35–36why, one step back35})36Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 37–43why, one step back37for (const autoFreeze of [true, false]) {38 for (const useStrictShallowCopy of [true, false]) {39 for (const useListener of [true, false]) {40 const name = `${autoFreeze ? "auto-freeze=true" : "auto-freeze=false"}:${41 useStrictShallowCopy ? "shallow-copy=true" : "shallow-copy=false"42 }:${useListener ? "use-listener=true" : "use-listener=false"}`43 runBaseTest(name, autoFreeze, useStrictShallowCopy, useListener)Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 44–45why, one step back44 }45 }Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 46why, one step back46}Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
line 47why, one step back47feat: Override array methods to avoid proxy creation while iterating and updating (#1184)Mark Eriksonbefore adoptiond38a9d0
lines 48–52why, one step back48// Run one additional test suite with the array methods plugin enabled,49// as that should be a separate concern from the other settings50const testArrayMethodsName = `array-plugin=true:auto-freeze=true:shallow-copy=false:use-listener=false`51runBaseTest(testArrayMethodsName, true, false, false, true)52Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 53why, one step back53class Foo {}Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
line 54why, one step back54feat: Override array methods to avoid proxy creation while iterating and updating (#1184)Mark Eriksonbefore adoptiond38a9d0
lines 55–61why, one step back55function runBaseTest(56 name,57 autoFreeze,58 useStrictShallowCopy,59 useListener,60 useArrayMethods = false61) {chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 62why, one step back62 const listener = useListener ? function() {} : undefinedfeat: Override array methods to avoid proxy creation while iterating and updating (#1184)Mark Eriksonbefore adoptiond38a9d0
line 63why, one step back63chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 64why, one step back64 const {produce, produceWithPatches} = createPatchedImmer({Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 65–66why, one step back65 autoFreeze,66 useStrictShallowCopychore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 67why, one step back67 })Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
line 68why, one step back68chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 69–72why, one step back69 // When `useListener` is true, append a function to the arguments of every70 // uncurried `produce` call in every test. This makes tests easier to read.71 function createPatchedImmer(options) {72 const immer = new Immer(options)Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
line 73why, one step back73chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 74–78why, one step back74 const {produce} = immer75 immer.produce = function(...args) {76 return typeof args[1] === "function" && args.length < 377 ? produce(...args, listener)78 : produce(...args)Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 79–80why, one step back79 }80chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 81why, one step back81 return immerMerge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 82–83why, one step back82 }83chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 84–86why, one step back84 describe(`base functionality - ${name}`, () => {85 let baseState86 let origBaseStateMerge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
line 87why, one step back87chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 88–89why, one step back88 beforeEach(() => {89 origBaseState = baseState = createBaseState()feat: Override array methods to avoid proxy creation while iterating and updating (#1184)Mark Eriksonbefore adoptiond38a9d0
lines 90–96why, one step back9091 // Allow running our tests with and without the array method plugin92 if (useArrayMethods) {93 enableArrayMethods()94 } else {95 clearPlugin(PluginArrayMethods)96 }Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 97–98why, one step back97 })98chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 99why, one step back99 it("returns the original state when no changes are made", () => {Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
line 100why, one step back100 const nextState = produce(baseState, s => {chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 101–102why, one step back101 expect(s.aProp).toBe("hi")102 expect(s.anObject.nested).toMatchObject({yummie: true})Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 103–106why, one step back103 })104 expect(nextState).toBe(baseState)105 })106chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 107–108why, one step back107 it("does structural sharing", () => {108 const random = Math.random()Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
line 109why, one step back109 const nextState = produce(baseState, s => {chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 110why, one step back110 s.aProp = randomMerge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 111–112why, one step back111 })112 expect(nextState).not.toBe(baseState)chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 113–114why, one step back113 expect(nextState.aProp).toBe(random)114 expect(nextState.nested).toBe(baseState.nested)Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 115–116why, one step back115 })116chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 117why, one step back117 it("deep change bubbles up", () => {Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
line 118why, one step back118 const nextState = produce(baseState, s => {chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 119why, one step back119 s.anObject.nested.yummie = falseMerge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 120–122why, one step back120 })121 expect(nextState).not.toBe(baseState)122 expect(nextState.anObject).not.toBe(baseState.anObject)chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 123–125why, one step back123 expect(baseState.anObject.nested.yummie).toBe(true)124 expect(nextState.anObject.nested.yummie).toBe(false)125 expect(nextState.anArray).toBe(baseState.anArray)Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 126–127why, one step back126 })127chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 128why, one step back128 it("can add props", () => {Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
line 129why, one step back129 const nextState = produce(baseState, s => {chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 130why, one step back130 s.anObject.cookie = {tasty: true}Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 131–133why, one step back131 })132 expect(nextState).not.toBe(baseState)133 expect(nextState.anObject).not.toBe(baseState.anObject)chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 134–135why, one step back134 expect(nextState.anObject.nested).toBe(baseState.anObject.nested)135 expect(nextState.anObject.cookie).toEqual({tasty: true})Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 136–137why, one step back136 })137chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 138why, one step back138 it("can delete props", () => {Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
line 139why, one step back139 const nextState = produce(baseState, s => {chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 140why, one step back140 delete s.anObject.nestedMerge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 141–144why, one step back141 delete s.nonexisting142 })143 expect(nextState).not.toBe(baseState)144 expect(nextState.anObject).not.toBe(baseState.anObject)chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 145why, one step back145 expect(nextState.anObject.nested).toBe(undefined)Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 146–147why, one step back146 })147Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 148why, one step back148 it("can delete props - 2", () => {Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 149–154why, one step back149 const nextState = produce(baseState, s => {150 delete s.nonexisting151 })152 expect(nextState).toBe(baseState)153 })154chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 155–156why, one step back155 // Found by: https://github.com/mweststrate/immer/pull/267156 it("can delete props added in the producer", () => {Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
line 157why, one step back157 const nextState = produce(baseState, s => {chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 158–159why, one step back158 s.anObject.test = true159 delete s.anObject.testMerge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 160–164why, one step back160 })161 expect(nextState).not.toBe(baseState)162 expect(nextState).toEqual(baseState)163 })164chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 165–167why, one step back165 // Found by: https://github.com/mweststrate/immer/issues/328166 it("can set a property that was just deleted", () => {167 const baseState = {a: 1}Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 168–169why, one step back168 const nextState = produce(baseState, s => {169 delete s.achore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 170why, one step back170 s.a = 2Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
line 171why, one step back171 })chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 172why, one step back172 expect(nextState.a).toBe(2)Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 173–174why, one step back173 })174chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 175–176why, one step back175 it("can set a property to its original value after deleting it", () => {176 const baseState = {a: {b: 1}}Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
line 177why, one step back177 const nextState = produce(baseState, s => {chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 178why, one step back178 const a = s.aMerge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
line 179why, one step back179 delete s.achore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 180why, one step back180 s.a = aMerge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 181–185why, one step back181 })182 expect(nextState).not.toBe(baseState)183 expect(nextState).toEqual(baseState)184 })185chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 186–193why, one step back186 it("can get property descriptors", () => {187 const getDescriptor = Object.getOwnPropertyDescriptor188 const baseState = deepFreeze([{a: 1}])189 produce(baseState, arr => {190 const obj = arr[0]191 const desc = {192 configurable: true,193 enumerable: true,Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 194why, one step back194 writable: trueMerge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 195–197why, one step back195 }196197 // Known propertychore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 198–199why, one step back198 expect(getDescriptor(obj, "a")).toMatchObject(desc)199 expect(getDescriptor(arr, 0)).toMatchObject(desc)Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
line 200why, one step back200chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 201–205why, one step back201 // Deleted property202 delete obj.a203 arr.pop()204 expect(getDescriptor(obj, "a")).toBeUndefined()205 expect(getDescriptor(arr, 0)).toBeUndefined()Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 206–207why, one step back206207 // Unknown propertychore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 208–209why, one step back208 expect(getDescriptor(obj, "b")).toBeUndefined()209 expect(getDescriptor(arr, 100)).toBeUndefined()Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
line 210why, one step back210chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 211–215why, one step back211 // Added property212 obj.b = 2213 arr[100] = 1214 expect(getDescriptor(obj, "b")).toBeDefined()215 expect(getDescriptor(arr, 100)).toBeDefined()Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 216–218why, one step back216 })217 })218chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 219–220why, one step back219 describe("array drafts", () => {220 it("supports Array.isArray()", () => {Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
line 221why, one step back221 const nextState = produce(baseState, s => {chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 222–223why, one step back222 expect(Array.isArray(s.anArray)).toBeTruthy()223 s.anArray.push(1)Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
line 224why, one step back224 })chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 225why, one step back225 expect(Array.isArray(nextState.anArray)).toBeTruthy()Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 226–227why, one step back226 })227chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 228–229why, one step back228 it("supports index access", () => {229 const value = baseState.anArray[0]Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
line 230why, one step back230 const nextState = produce(baseState, s => {chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 231why, one step back231 expect(s.anArray[0]).toBe(value)Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 232–236why, one step back232 })233 expect(nextState).toBe(baseState)234 })235236 it("supports iteration", () => {chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 237why, one step back237 const base = [Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 238–239why, one step back238 {id: 1, a: 1},239 {id: 2, a: 1}chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 240–242why, one step back240 ]241 const findById = (collection, id) => {242 for (const item of collection) {Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 243–252why, one step back243 if (item.id === id) return item244 }245 return null246 }247 const result = produce(base, draft => {248 const obj1 = findById(draft, 1)249 const obj2 = findById(draft, 2)250 obj1.a = 2251 obj2.a = 2252 })chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 253–254why, one step back253 expect(result[0].a).toEqual(2)254 expect(result[1].a).toEqual(2)Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 255–256why, one step back255 })256chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 257why, one step back257 it("can assign an index via bracket notation", () => {Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
line 258why, one step back258 const nextState = produce(baseState, s => {chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 259why, one step back259 s.anArray[3] = trueMerge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 260–262why, one step back260 })261 expect(nextState).not.toBe(baseState)262 expect(nextState.anArray).not.toBe(baseState.anArray)chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 263why, one step back263 expect(nextState.anArray[3]).toEqual(true)Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 264–265why, one step back264 })265chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 266why, one step back266 it("can use splice() to both add and remove items", () => {Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
line 267why, one step back267 const nextState = produce(baseState, s => {chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 268why, one step back268 s.anArray.splice(1, 1, "a", "b")Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 269–270why, one step back269 })270 expect(nextState.anArray).not.toBe(baseState.anArray)chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 271–272why, one step back271 expect(nextState.anArray[1]).toBe("a")272 expect(nextState.anArray[2]).toBe("b")Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 273–274why, one step back273 })274chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 275why, one step back275 it("can truncate via the length property", () => {Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 276–277why, one step back276 const baseLength = baseState.anArray.length277 const nextState = produce(baseState, s => {chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 278why, one step back278 s.anArray.length = baseLength - 1Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 279–280why, one step back279 })280 expect(nextState.anArray).not.toBe(baseState.anArray)chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 281why, one step back281 expect(nextState.anArray.length).toBe(baseLength - 1)Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 282–283why, one step back282 })283chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 284why, one step back284 it("can extend via the length property", () => {Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 285–286why, one step back285 const baseLength = baseState.anArray.length286 const nextState = produce(baseState, s => {chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 287why, one step back287 s.anArray.length = baseLength + 1Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 288–289why, one step back288 })289 expect(nextState.anArray).not.toBe(baseState.anArray)chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 290why, one step back290 expect(nextState.anArray.length).toBe(baseLength + 1)Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 291–730why, one step back291 })292293 describe("mutating array methods", () => {294 // Reported here: https://github.com/mweststrate/immer/issues/116295 it("can pop then push", () => {296 const nextState = produce([1, 2, 3], s => {297 s.pop()298 s.push(100)299 })300 expect(nextState).toEqual([1, 2, 100])301 })302303 it("can be sorted", () => {304 const baseState = [{value: 3}, {value: 1}, {value: 2}]305 const nextState = produce(baseState, s => {306 s.sort((a, b) => a.value - b.value)307 })308 expect(nextState).not.toBe(baseState)309 expect(nextState).toEqual([{value: 1}, {value: 2}, {value: 3}])310 })311312 it("can be reversed", () => {313 const baseState = [{value: 1}, {value: 2}, {value: 3}]314 const nextState = produce(baseState, s => {315 s.reverse()316 })317 expect(nextState).not.toBe(baseState)318 expect(nextState).toEqual([{value: 3}, {value: 2}, {value: 1}])319 })320321 it("can be sorted with existing proxies", () => {322 const baseState = [{value: 3}, {value: 1}, {value: 2}]323 const nextState = produce(baseState, s => {324 // First mutate a nested object to create a proxy325 s[0].value = 4326 // Then sort the array327 s.sort((a, b) => a.value - b.value)328 })329 expect(nextState).not.toBe(baseState)330 expect(nextState).toEqual([{value: 1}, {value: 2}, {value: 4}])331 })332333 it("can be reversed with existing proxies", () => {334 const baseState = [{value: 1}, {value: 2}, {value: 3}]335 const nextState = produce(baseState, s => {336 // First mutate a nested object to create a proxy337 s[1].value = 5338 // Then reverse the array339 s.reverse()340 })341 expect(nextState).not.toBe(baseState)342 expect(nextState).toEqual([{value: 3}, {value: 5}, {value: 1}])343 })344345 it("can be sorted with unmodified existing proxies", () => {346 const baseState = [{value: 3}, {value: 1}, {value: 2}]347 const nextState = produce(baseState, s => {348 // Access a nested object to create a proxy, but don't modify it349 const firstValue = s[0].value // This creates a proxy for s[0]350 expect(firstValue).toBe(3) // But we don't modify it351352 // Then sort the array353 s.sort((a, b) => a.value - b.value)354 })355 expect(nextState).not.toBe(baseState)356 expect(nextState).toEqual([{value: 1}, {value: 2}, {value: 3}])357 })358359 describe("push()", () => {360 test("push single item", () => {361 const base = {items: [{id: 1}, {id: 2}]}362 const result = produce(base, draft => {363 draft.items.push({id: 3})364 })365 expect(result.items).toHaveLength(3)366 expect(result.items[2].id).toBe(3)367 })368369 test("push multiple items", () => {370 const base = {items: [{id: 1}]}371 const result = produce(base, draft => {372 draft.items.push({id: 2}, {id: 3}, {id: 4})373 })374 expect(result.items).toHaveLength(4)375 expect(result.items[3].id).toBe(4)376 })377378 test("push then mutate pushed item", () => {379 const base = {items: [{id: 1}]}380 const result = produce(base, draft => {381 draft.items.push({id: 2, value: 10})382 draft.items[1].value = 20383 })384 expect(result.items[1].value).toBe(20)385 })386387 test("push returns new length", () => {388 const base = {items: [1, 2]}389 produce(base, draft => {390 const newLength = draft.items.push(3, 4)391 expect(newLength).toBe(4)392 })393 })394 })395396 describe("unshift()", () => {397 test("unshift single item", () => {398 const base = {items: [{id: 2}, {id: 3}]}399 const result = produce(base, draft => {400 draft.items.unshift({id: 1})401 })402 expect(result.items).toHaveLength(3)403 expect(result.items[0].id).toBe(1)404 expect(result.items[1].id).toBe(2)405 })406407 test("unshift multiple items", () => {408 const base = {items: [{id: 4}]}409 const result = produce(base, draft => {410 draft.items.unshift({id: 1}, {id: 2}, {id: 3})411 })412 expect(result.items).toHaveLength(4)413 expect(result.items[0].id).toBe(1)414 expect(result.items[3].id).toBe(4)415 })416417 test("unshift then mutate unshifted item", () => {418 const base = {items: [{id: 2}]}419 const result = produce(base, draft => {420 draft.items.unshift({id: 1, value: 10})421 draft.items[0].value = 20422 })423 expect(result.items[0].value).toBe(20)424 })425426 test("unshift returns new length", () => {427 const base = {items: [3, 4]}428 produce(base, draft => {429 const newLength = draft.items.unshift(1, 2)430 expect(newLength).toBe(4)431 })432 })433 })434435 describe("shift()", () => {436 test("shift removes first item", () => {437 const base = {items: [{id: 1}, {id: 2}, {id: 3}]}438 const result = produce(base, draft => {439 const removed = draft.items.shift()440 expect(removed.id).toBe(1)441 })442 expect(result.items).toHaveLength(2)443 expect(result.items[0].id).toBe(2)444 })445446 test("shift on empty array returns undefined", () => {447 const base = {items: []}448 produce(base, draft => {449 const removed = draft.items.shift()450 expect(removed).toBeUndefined()451 })452 })453454 test("shift then mutate remaining items", () => {455 const base = {items: [{id: 1}, {id: 2, value: 10}]}456 const result = produce(base, draft => {457 draft.items.shift()458 draft.items[0].value = 20459 })460 expect(result.items).toHaveLength(1)461 expect(result.items[0].value).toBe(20)462 })463464 test("multiple shifts", () => {465 const base = {items: [{id: 1}, {id: 2}, {id: 3}]}466 const result = produce(base, draft => {467 draft.items.shift()468 draft.items.shift()469 })470 expect(result.items).toHaveLength(1)471 expect(result.items[0].id).toBe(3)472 })473 })474475 describe("splice() edge cases", () => {476 test("splice with only deleteCount (no items to add)", () => {477 const base = {items: [{id: 1}, {id: 2}, {id: 3}, {id: 4}]}478 const result = produce(base, draft => {479 const removed = draft.items.splice(1, 2)480 expect(removed).toHaveLength(2)481 expect(removed[0].id).toBe(2)482 })483 expect(result.items).toHaveLength(2)484 expect(result.items[0].id).toBe(1)485 expect(result.items[1].id).toBe(4)486 })487488 test("splice with negative start index", () => {489 const base = {items: [{id: 1}, {id: 2}, {id: 3}]}490 const result = produce(base, draft => {491 draft.items.splice(-1, 1, {id: 99})492 })493 expect(result.items[2].id).toBe(99)494 })495496 test("splice at start (index 0)", () => {497 const base = {items: [{id: 1}, {id: 2}]}498 const result = produce(base, draft => {499 draft.items.splice(0, 0, {id: 0})500 })501 expect(result.items[0].id).toBe(0)502 expect(result.items).toHaveLength(3)503 })504505 test("splice at end", () => {506 const base = {items: [{id: 1}, {id: 2}]}507 const result = produce(base, draft => {508 draft.items.splice(2, 0, {id: 3})509 })510 expect(result.items[2].id).toBe(3)511 })512513 test("splice then mutate spliced-in items", () => {514 const base = {items: [{id: 1}, {id: 3}]}515 const result = produce(base, draft => {516 draft.items.splice(1, 0, {id: 2, value: 10})517 draft.items[1].value = 20518 })519 expect(result.items[1].value).toBe(20)520 })521 })522523 describe("combined operations", () => {524 test("sort then push", () => {525 const base = {items: [{value: 3}, {value: 1}]}526 const result = produce(base, draft => {527 draft.items.sort((a, b) => a.value - b.value)528 draft.items.push({value: 4})529 })530 expect(result.items.map(i => i.value)).toEqual([1, 3, 4])531 })532533 test("reverse then unshift", () => {534 const base = {items: [{id: 1}, {id: 2}, {id: 3}]}535 const result = produce(base, draft => {536 draft.items.reverse()537 draft.items.unshift({id: 0})538 })539 expect(result.items.map(i => i.id)).toEqual([0, 3, 2, 1])540 })541542 test("splice then sort", () => {543 const base = {items: [{value: 5}, {value: 2}, {value: 8}]}544 const result = produce(base, draft => {545 draft.items.splice(1, 1, {value: 1})546 draft.items.sort((a, b) => a.value - b.value)547 })548 expect(result.items.map(i => i.value)).toEqual([1, 5, 8])549 })550551 test("push, pop, push sequence", () => {552 const base = {items: [{id: 1}]}553 const result = produce(base, draft => {554 draft.items.push({id: 2})555 draft.items.pop()556 draft.items.push({id: 3})557 })558 expect(result.items.map(i => i.id)).toEqual([1, 3])559 })560 })561562 describe("bulk operations with pre-existing proxies", () => {563 test("access items before sort", () => {564 const base = {items: [{value: 3}, {value: 1}, {value: 2}]}565 const result = produce(base, draft => {566 // Access all items to create proxies567 draft.items.forEach(item => item.value)568 // Then sort569 draft.items.sort((a, b) => a.value - b.value)570 })571 expect(result.items.map(i => i.value)).toEqual([1, 2, 3])572 })573574 test("mutate items before reverse", () => {575 const base = {576 items: [577 {id: 1, value: 10},578 {id: 2, value: 20}579 ]580 }581 const result = produce(base, draft => {582 // Mutate to create modified proxies583 draft.items[0].value = 15584 draft.items[1].value = 25585 // Then reverse586 draft.items.reverse()587 })588 expect(result.items[0].id).toBe(2)589 expect(result.items[0].value).toBe(25)590 expect(result.items[1].id).toBe(1)591 expect(result.items[1].value).toBe(15)592 })593 })594595 describe("return values", () => {596 test("pop returns removed item", () => {597 const base = {items: [{id: 1}, {id: 2}]}598 produce(base, draft => {599 const removed = draft.items.pop()600 expect(removed.id).toBe(2)601 // Verify we can mutate the returned item602 removed.modified = true603 expect(draft.items[0].modified).toBeUndefined()604 })605 })606607 test("shift returns removed item", () => {608 const base = {items: [{id: 1}, {id: 2}]}609 produce(base, draft => {610 const removed = draft.items.shift()611 expect(removed.id).toBe(1)612 })613 })614615 test("splice returns array of removed items", () => {616 const base = {items: [{id: 1}, {id: 2}, {id: 3}]}617 produce(base, draft => {618 const removed = draft.items.splice(0, 2)619 expect(removed).toHaveLength(2)620 expect(removed[0].id).toBe(1)621 expect(removed[1].id).toBe(2)622 })623 })624 })625 })626627 describe("non-mutating array methods", () => {628 // Test data factory629 const createTestData = () => ({630 items: [631 {id: 1, value: 10, nested: {count: 1}},632 {id: 2, value: 20, nested: {count: 2}},633 {id: 3, value: 30, nested: {count: 3}},634 {id: 4, value: 40, nested: {count: 4}},635 {id: 5, value: 50, nested: {count: 5}}636 ],637 other: {data: "test"}638 })639640 describe("filter()", () => {641 test("returns new array with filtered items", () => {642 const base = createTestData()643 const result = produce(base, draft => {644 const filtered = draft.items.filter(item => item.value > 25)645 expect(filtered).toHaveLength(3)646 expect(filtered[0].id).toBe(3)647 })648 expect(result).toBe(base) // No modifications649 })650651 test("mutations to filtered items are reflected in result", () => {652 const base = createTestData()653 const result = produce(base, draft => {654 const filtered = draft.items.filter(item => item.value > 25)655 // Verify filtered items are drafts656 expect(isDraft(filtered[0])).toBe(true)657 filtered[0].value = 999658 })659 expect(result.items[2].value).toBe(999) // id: 3 is at index 2660 expect(result.items[0].value).toBe(10) // Unchanged661 // Verify base state unchanged662 expect(base.items[2].value).toBe(30)663 // Verify result is a copy664 expect(result.items[2]).not.toBe(base.items[2])665 })666667 test("mutations to nested properties work correctly", () => {668 const base = createTestData()669 const result = produce(base, draft => {670 const filtered = draft.items.filter(item => item.id > 2)671 // Verify filtered items are drafts672 expect(isDraft(filtered[0])).toBe(true)673 filtered[0].nested.count = 100674 })675 expect(result.items[2].nested.count).toBe(100)676 // Verify base state unchanged677 expect(base.items[2].nested.count).toBe(3)678 // Verify result is a copy679 expect(result.items[2].nested).not.toBe(base.items[2].nested)680 })681682 test("multiple mutations to different filtered items", () => {683 const base = createTestData()684 const result = produce(base, draft => {685 const filtered = draft.items.filter(item => item.value > 15)686 // Verify all filtered items are drafts687 filtered.forEach(item => expect(isDraft(item)).toBe(true))688 filtered[0].value = 200 // id: 2689 filtered[1].value = 300 // id: 3690 filtered[2].value = 400 // id: 4691 })692 expect(result.items[1].value).toBe(200)693 expect(result.items[2].value).toBe(300)694 expect(result.items[3].value).toBe(400)695 // Verify base state unchanged696 expect(base.items[1].value).toBe(20)697 expect(base.items[2].value).toBe(30)698 expect(base.items[3].value).toBe(40)699 })700701 test("filtered array can be assigned to another property", () => {702 const base = createTestData()703 const result = produce(base, draft => {704 const filtered = draft.items.filter(item => item.value > 25)705 draft.other.filtered = filtered706 })707 expect(result.other.filtered).toHaveLength(3)708 expect(result.other.filtered[0].id).toBe(3)709 })710711 test("cross-reference: mutating filtered item affects original location", () => {712 const base = createTestData()713 const result = produce(base, draft => {714 const filtered = draft.items.filter(item => item.id === 3)715 draft.other.ref = filtered[0]716 filtered[0].value = 999717 })718 expect(result.items[2].value).toBe(999)719 expect(result.other.ref.value).toBe(999)720 })721722 test("read-only access doesn't cause mutations", () => {723 const base = createTestData()724 const result = produce(base, draft => {725 const filtered = draft.items.filter(item => item.value > 25)726 const sum = filtered.reduce((acc, item) => acc + item.value, 0)727 expect(sum).toBe(120) // 30 + 40 + 50728 })729 expect(result).toBe(base) // No modifications730 })feat: Override array methods to avoid proxy creation while iterating and updating (#1184)Mark Eriksonbefore adoptiond38a9d0
lines 731–750why, one step back731732 test("mutating item in filter result updates original value", () => {733 const initialState = {734 largeArray: Array.from({length: 10}).map((_, i) => ({735 id: i,736 value: i * 10737 }))738 }739740 const result = produce(initialState, draft => {741 const filtered = draft.largeArray.filter(item => item.id <= 5)742743 filtered[0].value = 999744 draft.filtered = filtered745 })746747 expect(result.largeArray[0].value).toBe(999)748 expect(result.filtered[0].value).toBe(999)749 expect(result.largeArray[0]).toBe(result.filtered[0])750 })Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 751–1186why, one step back751 })752753 describe("map()", () => {754 test("returns new array with mapped items", () => {755 const base = createTestData()756 const result = produce(base, draft => {757 const mapped = draft.items.map(item => item.nested)758 expect(mapped).toHaveLength(5)759 expect(mapped[0].count).toBe(1)760 })761 expect(result).toBe(base)762 })763764 test("mutations to mapped nested objects work", () => {765 const base = createTestData()766 const result = produce(base, draft => {767 const nested = draft.items.map(item => item.nested)768 // Verify mapped items are drafts769 expect(isDraft(nested[0])).toBe(true)770 nested[0].count = 100771 })772 expect(result.items[0].nested.count).toBe(100)773 // Verify base state unchanged774 expect(base.items[0].nested.count).toBe(1)775 // Verify result is a copy776 expect(result.items[0].nested).not.toBe(base.items[0].nested)777 })778779 test("map with transformation then mutate", () => {780 const base = createTestData()781 const result = produce(base, draft => {782 const mapped = draft.items.map(item => ({783 ...item,784 doubled: item.value * 2785 }))786 // This creates new objects, so mutations won't affect original787 mapped[0].value = 999788 })789 expect(result.items[0].value).toBe(10) // Unchanged790 })791792 test("map returning original items allows mutation", () => {793 const base = createTestData()794 const result = produce(base, draft => {795 const mapped = draft.items.map(item => item) // Identity map796 // Verify mapped items are drafts797 expect(isDraft(mapped[0])).toBe(true)798 mapped[0].value = 999799 })800 expect(result.items[0].value).toBe(999)801 // Verify base state unchanged802 expect(base.items[0].value).toBe(10)803 // Verify result is a copy804 expect(result.items[0]).not.toBe(base.items[0])805 })806 })807808 describe("find()", () => {809 test("returns found item", () => {810 const base = createTestData()811 const result = produce(base, draft => {812 const found = draft.items.find(item => item.id === 3)813 expect(found?.value).toBe(30)814 })815 expect(result).toBe(base)816 })817818 test("mutations to found item are reflected", () => {819 const base = createTestData()820 const result = produce(base, draft => {821 const found = draft.items.find(item => item.id === 3)822 // Verify found item is a draft823 expect(isDraft(found)).toBe(true)824 if (found) {825 found.value = 999826 }827 })828 expect(result.items[2].value).toBe(999)829 // Verify base state unchanged830 expect(base.items[2].value).toBe(30)831 // Verify result is a copy832 expect(result.items[2]).not.toBe(base.items[2])833 })834835 test("nested mutations on found item", () => {836 const base = createTestData()837 const result = produce(base, draft => {838 const found = draft.items.find(item => item.id === 2)839 // Verify found item is a draft840 expect(isDraft(found)).toBe(true)841 if (found) {842 found.nested.count = 200843 }844 })845 expect(result.items[1].nested.count).toBe(200)846 // Verify base state unchanged847 expect(base.items[1].nested.count).toBe(2)848 // Verify result is a copy849 expect(result.items[1].nested).not.toBe(base.items[1].nested)850 })851852 test("find returns undefined when not found", () => {853 const base = createTestData()854 const result = produce(base, draft => {855 const found = draft.items.find(item => item.id === 999)856 expect(found).toBeUndefined()857 })858 expect(result).toBe(base)859 })860 })861862 describe("findLast()", () => {863 test("returns last matching item", () => {864 const base = {865 items: [866 {id: 1, type: "A"},867 {id: 2, type: "B"},868 {id: 3, type: "A"}869 ]870 }871 const result = produce(base, draft => {872 const found = draft.items.findLast(item => item.type === "A")873 expect(found?.id).toBe(3)874 })875 expect(result).toBe(base)876 })877878 test("mutations to findLast result work", () => {879 const base = {880 items: [881 {id: 1, type: "A", value: 10},882 {id: 2, type: "B", value: 20},883 {id: 3, type: "A", value: 30}884 ]885 }886 const result = produce(base, draft => {887 const found = draft.items.findLast(item => item.type === "A")888 // Verify found item is a draft889 expect(isDraft(found)).toBe(true)890 if (found) {891 found.value = 999892 }893 })894 expect(result.items[2].value).toBe(999)895 // Verify base state unchanged896 expect(base.items[2].value).toBe(30)897 // Verify result is a copy898 expect(result.items[2]).not.toBe(base.items[2])899 })900 })901902 describe("slice()", () => {903 test("returns sliced array", () => {904 const base = createTestData()905 const result = produce(base, draft => {906 const sliced = draft.items.slice(1, 3)907 expect(sliced).toHaveLength(2)908 expect(sliced[0].id).toBe(2)909 })910 expect(result).toBe(base)911 })912913 test("mutations to sliced items work", () => {914 const base = createTestData()915 const result = produce(base, draft => {916 const sliced = draft.items.slice(1, 3)917 // Verify sliced items are drafts918 expect(isDraft(sliced[0])).toBe(true)919 sliced[0].value = 999920 })921 expect(result.items[1].value).toBe(999)922 // Verify base state unchanged923 expect(base.items[1].value).toBe(20)924 // Verify result is a copy925 expect(result.items[1]).not.toBe(base.items[1])926 })927928 test("slice with negative indices", () => {929 const base = createTestData()930 const result = produce(base, draft => {931 const sliced = draft.items.slice(-2)932 // Verify sliced items are drafts933 expect(isDraft(sliced[0])).toBe(true)934 sliced[0].value = 999935 })936 expect(result.items[3].value).toBe(999)937 // Verify base state unchanged938 expect(base.items[3].value).toBe(40)939 // Verify result is a copy940 expect(result.items[3]).not.toBe(base.items[3])941 })942 })943944 describe("flatMap()", () => {945 test("returns flattened array", () => {946 const base = {947 groups: [{items: [{id: 1}, {id: 2}]}, {items: [{id: 3}, {id: 4}]}]948 }949 const result = produce(base, draft => {950 const flat = draft.groups.flatMap(group => group.items)951 expect(flat).toHaveLength(4)952 expect(flat[0].id).toBe(1)953 })954 expect(result).toBe(base)955 })956957 test("mutations to flatMapped items work", () => {958 const base = {959 groups: [960 {961 items: [962 {id: 1, value: 10},963 {id: 2, value: 20}964 ]965 },966 {items: [{id: 3, value: 30}]}967 ]968 }969 const result = produce(base, draft => {970 const flat = draft.groups.flatMap(group => group.items)971 // Verify flatMapped items are drafts972 expect(isDraft(flat[0])).toBe(true)973 flat[0].value = 999974 })975 expect(result.groups[0].items[0].value).toBe(999)976 // Verify base state unchanged977 expect(base.groups[0].items[0].value).toBe(10)978 // Verify result is a copy979 expect(result.groups[0].items[0]).not.toBe(base.groups[0].items[0])980 })981 })982983 describe("reduce()", () => {984 test("returns accumulated value", () => {985 const base = createTestData()986 const result = produce(base, draft => {987 const sum = draft.items.reduce((acc, item) => acc + item.value, 0)988 expect(sum).toBe(150) // 10 + 20 + 30 + 40 + 50989 })990 expect(result).toBe(base)991 })992993 test("mutations during reduce are reflected", () => {994 const base = createTestData()995 const result = produce(base, draft => {996 const sum = draft.items.reduce((acc, item, index) => {997 // Verify items in reduce are drafts998 expect(isDraft(item)).toBe(true)999 if (index === 2) {1000 item.value = 9991001 }1002 return acc + item.value1003 }, 0)1004 expect(sum).toBe(1119) // 10 + 20 + 999 + 40 + 501005 })1006 expect(result.items[2].value).toBe(999)1007 // Verify base state unchanged1008 expect(base.items[2].value).toBe(30)1009 // Verify result is a copy1010 expect(result.items[2]).not.toBe(base.items[2])1011 })10121013 test("reduce with object accumulator", () => {1014 const base = createTestData()1015 const result = produce(base, draft => {1016 const grouped = draft.items.reduce((acc, item) => {1017 const key = item.value > 25 ? "high" : "low"1018 if (!acc[key]) acc[key] = []1019 acc[key].push(item)1020 return acc1021 }, {})1022 expect(grouped.low).toHaveLength(2)1023 expect(grouped.high).toHaveLength(3)1024 })1025 expect(result).toBe(base)1026 })10271028 test("reduce then mutate accumulated items", () => {1029 const base = createTestData()1030 const result = produce(base, draft => {1031 const items = draft.items.reduce((acc, item) => {1032 // Verify items in reduce are drafts1033 expect(isDraft(item)).toBe(true)1034 if (item.value > 25) acc.push(item)1035 return acc1036 }, [])1037 // Verify accumulated items are drafts1038 expect(isDraft(items[0])).toBe(true)1039 items[0].value = 9991040 })1041 expect(result.items[2].value).toBe(999) // id: 3 is at index 21042 // Verify base state unchanged1043 expect(base.items[2].value).toBe(30)1044 // Verify result is a copy1045 expect(result.items[2]).not.toBe(base.items[2])1046 })1047 })10481049 describe("forEach()", () => {1050 test("iterates over all items", () => {1051 const base = createTestData()1052 const result = produce(base, draft => {1053 let count = 01054 draft.items.forEach(item => {1055 count++1056 expect(item.id).toBeDefined()1057 })1058 expect(count).toBe(5)1059 })1060 expect(result).toBe(base)1061 })10621063 test("mutations during forEach are reflected", () => {1064 const base = createTestData()1065 const result = produce(base, draft => {1066 draft.items.forEach((item, index) => {1067 // Verify items in forEach are drafts1068 expect(isDraft(item)).toBe(true)1069 if (index === 1) {1070 item.value = 9991071 }1072 })1073 })1074 expect(result.items[1].value).toBe(999)1075 // Verify base state unchanged1076 expect(base.items[1].value).toBe(20)1077 // Verify result is a copy1078 expect(result.items[1]).not.toBe(base.items[1])1079 })10801081 test("forEach with nested mutations", () => {1082 const base = createTestData()1083 const result = produce(base, draft => {1084 draft.items.forEach(item => {1085 // Verify items in forEach are drafts1086 expect(isDraft(item)).toBe(true)1087 item.nested.count *= 101088 })1089 })1090 expect(result.items[0].nested.count).toBe(10)1091 expect(result.items[4].nested.count).toBe(50)1092 // Verify base state unchanged1093 expect(base.items[0].nested.count).toBe(1)1094 // Verify result is a copy1095 expect(result.items[0].nested).not.toBe(base.items[0].nested)1096 })10971098 test("forEach returns undefined", () => {1099 const base = createTestData()1100 const result = produce(base, draft => {1101 const returnValue = draft.items.forEach(item => item.value)1102 expect(returnValue).toBeUndefined()1103 })1104 expect(result).toBe(base)1105 })1106 })11071108 describe("indexOf()", () => {1109 test("returns index of found item", () => {1110 const base = {items: [1, 2, 3, 4, 5]}1111 const result = produce(base, draft => {1112 const index = draft.items.indexOf(3)1113 expect(index).toBe(2)1114 })1115 expect(result).toBe(base)1116 })11171118 test("returns -1 when item not found", () => {1119 const base = {items: [1, 2, 3, 4, 5]}1120 const result = produce(base, draft => {1121 const index = draft.items.indexOf(99)1122 expect(index).toBe(-1)1123 })1124 expect(result).toBe(base)1125 })11261127 test("works with object references", () => {1128 const base = createTestData()1129 const result = produce(base, draft => {1130 const item = draft.items[2]1131 const index = draft.items.indexOf(item)1132 expect(index).toBe(2)1133 })1134 expect(result).toBe(base)1135 })11361137 test("indexOf with fromIndex parameter", () => {1138 const base = {items: [1, 2, 3, 2, 5]}1139 const result = produce(base, draft => {1140 const firstIndex = draft.items.indexOf(2)1141 const secondIndex = draft.items.indexOf(2, 2)1142 expect(firstIndex).toBe(1)1143 expect(secondIndex).toBe(3)1144 })1145 expect(result).toBe(base)1146 })1147 })11481149 describe("join()", () => {1150 test("returns joined string", () => {1151 const base = {items: [1, 2, 3, 4, 5]}1152 const result = produce(base, draft => {1153 const joined = draft.items.join(",")1154 expect(joined).toBe("1,2,3,4,5")1155 })1156 expect(result).toBe(base)1157 })11581159 test("join with custom separator", () => {1160 const base = {items: ["a", "b", "c"]}1161 const result = produce(base, draft => {1162 const joined = draft.items.join(" - ")1163 expect(joined).toBe("a - b - c")1164 })1165 expect(result).toBe(base)1166 })11671168 test("join with no separator uses comma", () => {1169 const base = {items: [1, 2, 3]}1170 const result = produce(base, draft => {1171 const joined = draft.items.join()1172 expect(joined).toBe("1,2,3")1173 })1174 expect(result).toBe(base)1175 })11761177 test("join with objects calls toString", () => {1178 const base = createTestData()1179 const result = produce(base, draft => {1180 const joined = draft.items.join(",")1181 expect(joined).toContain("[object Object]")1182 })1183 expect(result).toBe(base)1184 })1185 })1186feat: Override array methods to avoid proxy creation while iterating and updating (#1184)Mark Eriksonbefore adoptiond38a9d0
lines 1187–1741why, one step back1187 describe("concat()", () => {1188 test("returns new array with concatenated items", () => {1189 const base = {items: [{id: 1}, {id: 2}]}1190 const result = produce(base, draft => {1191 const concatenated = draft.items.concat([{id: 3}])1192 expect(concatenated).toHaveLength(3)1193 expect(concatenated[2].id).toBe(3)1194 })1195 expect(result).toBe(base)1196 })11971198 test("concat with no arguments creates shallow copy", () => {1199 const base = {1200 items: [1201 {id: 1, value: 10},1202 {id: 2, value: 20}1203 ]1204 }1205 const result = produce(base, draft => {1206 const copy = draft.items.concat()1207 expect(copy).toHaveLength(2)1208 expect(copy[0].id).toBe(1)1209 })1210 expect(result).toBe(base)1211 })12121213 // Behavior differs based on array methods plugin1214 if (useArrayMethods) {1215 test("concat returns base values, not drafts (with plugin)", () => {1216 const base = {items: [{id: 1, value: 10}]}1217 const result = produce(base, draft => {1218 const concatenated = draft.items.concat()1219 // With plugin: concat returns base values, not drafts1220 expect(isDraft(concatenated[0])).toBe(false)1221 })1222 expect(result).toBe(base)1223 })1224 } else {1225 test("concat returns draft proxies (default behavior)", () => {1226 const base = {items: [{id: 1, value: 10}]}1227 const result = produce(base, draft => {1228 const concatenated = draft.items.concat()1229 // Without plugin: concat returns draft proxies via get trap1230 expect(isDraft(concatenated[0])).toBe(true)1231 })1232 expect(result).toBe(base)1233 })1234 }12351236 test("concat with multiple arrays", () => {1237 const base = {items: [{id: 1}]}1238 const result = produce(base, draft => {1239 const concatenated = draft.items.concat([{id: 2}], [{id: 3}])1240 expect(concatenated).toHaveLength(3)1241 expect(concatenated[1].id).toBe(2)1242 expect(concatenated[2].id).toBe(3)1243 })1244 expect(result).toBe(base)1245 })12461247 test("concat result assigned to draft works", () => {1248 const base = {items: [{id: 1}], combined: []}1249 const result = produce(base, draft => {1250 draft.combined = draft.items.concat([{id: 2}])1251 })1252 expect(result.combined).toHaveLength(2)1253 expect(result.combined[0].id).toBe(1)1254 expect(result.combined[1].id).toBe(2)1255 })12561257 test("concat with primitives", () => {1258 const base = {numbers: [1, 2, 3]}1259 const result = produce(base, draft => {1260 const concatenated = draft.numbers.concat([4, 5])1261 expect(concatenated).toEqual([1, 2, 3, 4, 5])1262 })1263 expect(result).toBe(base)1264 })1265 })12661267 describe("flat()", () => {1268 test("returns flattened array", () => {1269 const base = {1270 nested: [1271 [1, 2],1272 [3, 4]1273 ]1274 }1275 const result = produce(base, draft => {1276 const flattened = draft.nested.flat()1277 expect(flattened).toEqual([1, 2, 3, 4])1278 })1279 expect(result).toBe(base)1280 })12811282 test("flat with depth parameter", () => {1283 const base = {nested: [[[1, 2]], [[3, 4]]]}1284 const result = produce(base, draft => {1285 const shallow = draft.nested.flat(1)1286 expect(shallow).toEqual([1287 [1, 2],1288 [3, 4]1289 ])1290 const deep = draft.nested.flat(2)1291 expect(deep).toEqual([1, 2, 3, 4])1292 })1293 expect(result).toBe(base)1294 })12951296 // Behavior differs based on array methods plugin1297 if (useArrayMethods) {1298 test("flat returns base values, not drafts (with plugin)", () => {1299 const base = {nested: [[{id: 1}], [{id: 2}]]}1300 const result = produce(base, draft => {1301 const flattened = draft.nested.flat()1302 expect(flattened).toHaveLength(2)1303 // With plugin: flat returns base values, not drafts1304 expect(isDraft(flattened[0])).toBe(false)1305 })1306 expect(result).toBe(base)1307 })1308 } else {1309 test("flat returns draft proxies (default behavior)", () => {1310 const base = {nested: [[{id: 1}], [{id: 2}]]}1311 const result = produce(base, draft => {1312 const flattened = draft.nested.flat()1313 expect(flattened).toHaveLength(2)1314 // Without plugin: flat returns draft proxies via get trap1315 expect(isDraft(flattened[0])).toBe(true)1316 })1317 expect(result).toBe(base)1318 })1319 }13201321 test("flat with empty nested arrays", () => {1322 const base = {nested: [[], [1, 2], []]}1323 const result = produce(base, draft => {1324 const flattened = draft.nested.flat()1325 expect(flattened).toEqual([1, 2])1326 })1327 expect(result).toBe(base)1328 })13291330 test("flat result assigned to draft works", () => {1331 const base = {nested: [[1], [2]], result: []}1332 const result = produce(base, draft => {1333 draft.result = draft.nested.flat()1334 })1335 expect(result.result).toEqual([1, 2])1336 })1337 })13381339 describe("findIndex()", () => {1340 test("returns index of found item", () => {1341 const base = createTestData()1342 const result = produce(base, draft => {1343 const index = draft.items.findIndex(item => item.id === 3)1344 expect(index).toBe(2)1345 })1346 expect(result).toBe(base)1347 })13481349 test("returns -1 when not found", () => {1350 const base = createTestData()1351 const result = produce(base, draft => {1352 const index = draft.items.findIndex(item => item.id === 999)1353 expect(index).toBe(-1)1354 })1355 expect(result).toBe(base)1356 })13571358 test("predicate receives correct arguments", () => {1359 const base = createTestData()1360 const result = produce(base, draft => {1361 draft.items.findIndex((item, index, array) => {1362 expect(typeof index).toBe("number")1363 expect(Array.isArray(array)).toBe(true)1364 return false1365 })1366 })1367 expect(result).toBe(base)1368 })13691370 test("works with primitives", () => {1371 const base = {numbers: [10, 20, 30, 40, 50]}1372 const result = produce(base, draft => {1373 const index = draft.numbers.findIndex(n => n > 25)1374 expect(index).toBe(2)1375 })1376 expect(result).toBe(base)1377 })1378 })13791380 describe("findLastIndex()", () => {1381 test("returns last matching index", () => {1382 const base = {1383 items: [1384 {id: 1, type: "A"},1385 {id: 2, type: "B"},1386 {id: 3, type: "A"}1387 ]1388 }1389 const result = produce(base, draft => {1390 const index = draft.items.findLastIndex(item => item.type === "A")1391 expect(index).toBe(2)1392 })1393 expect(result).toBe(base)1394 })13951396 test("returns -1 when not found", () => {1397 const base = {items: [{id: 1}, {id: 2}]}1398 const result = produce(base, draft => {1399 const index = draft.items.findLastIndex(item => item.id === 999)1400 expect(index).toBe(-1)1401 })1402 expect(result).toBe(base)1403 })14041405 test("predicate receives correct arguments", () => {1406 const base = createTestData()1407 const result = produce(base, draft => {1408 draft.items.findLastIndex((item, index, array) => {1409 expect(typeof index).toBe("number")1410 expect(Array.isArray(array)).toBe(true)1411 return false1412 })1413 })1414 expect(result).toBe(base)1415 })1416 })14171418 describe("some()", () => {1419 test("returns true when condition met", () => {1420 const base = createTestData()1421 const result = produce(base, draft => {1422 const hasHighValue = draft.items.some(item => item.value > 40)1423 expect(hasHighValue).toBe(true)1424 })1425 expect(result).toBe(base)1426 })14271428 test("returns false when no items match", () => {1429 const base = createTestData()1430 const result = produce(base, draft => {1431 const hasHugeValue = draft.items.some(item => item.value > 1000)1432 expect(hasHugeValue).toBe(false)1433 })1434 expect(result).toBe(base)1435 })14361437 test("short-circuits on first match", () => {1438 const base = {items: [{id: 1}, {id: 2}, {id: 3}]}1439 let callCount = 01440 const result = produce(base, draft => {1441 const found = draft.items.some(item => {1442 callCount++1443 return item.id === 11444 })1445 expect(found).toBe(true)1446 })1447 expect(callCount).toBe(1)1448 expect(result).toBe(base)1449 })14501451 test("predicate receives correct arguments", () => {1452 const base = createTestData()1453 const result = produce(base, draft => {1454 draft.items.some((item, index, array) => {1455 expect(typeof index).toBe("number")1456 expect(Array.isArray(array)).toBe(true)1457 return false1458 })1459 })1460 expect(result).toBe(base)1461 })14621463 test("works with primitives", () => {1464 const base = {numbers: [1, 2, 3, 4, 5]}1465 const result = produce(base, draft => {1466 expect(draft.numbers.some(n => n > 3)).toBe(true)1467 expect(draft.numbers.some(n => n > 10)).toBe(false)1468 })1469 expect(result).toBe(base)1470 })1471 })14721473 describe("every()", () => {1474 test("returns true when all items match", () => {1475 const base = createTestData()1476 const result = produce(base, draft => {1477 const allPositive = draft.items.every(item => item.value > 0)1478 expect(allPositive).toBe(true)1479 })1480 expect(result).toBe(base)1481 })14821483 test("returns false when any item fails", () => {1484 const base = createTestData()1485 const result = produce(base, draft => {1486 const allHighValue = draft.items.every(item => item.value > 30)1487 expect(allHighValue).toBe(false)1488 })1489 expect(result).toBe(base)1490 })14911492 test("short-circuits on first failure", () => {1493 const base = {items: [{id: 1}, {id: 2}, {id: 3}]}1494 let callCount = 01495 const result = produce(base, draft => {1496 const allMatch = draft.items.every(item => {1497 callCount++1498 return item.id === 9991499 })1500 expect(allMatch).toBe(false)1501 })1502 expect(callCount).toBe(1)1503 expect(result).toBe(base)1504 })15051506 test("predicate receives correct arguments", () => {1507 const base = createTestData()1508 const result = produce(base, draft => {1509 draft.items.every((item, index, array) => {1510 expect(typeof index).toBe("number")1511 expect(Array.isArray(array)).toBe(true)1512 return true1513 })1514 })1515 expect(result).toBe(base)1516 })15171518 test("returns true for empty array", () => {1519 const base = {items: []}1520 const result = produce(base, draft => {1521 const allMatch = draft.items.every(() => false)1522 expect(allMatch).toBe(true)1523 })1524 expect(result).toBe(base)1525 })1526 })15271528 describe("lastIndexOf()", () => {1529 test("returns last index of item", () => {1530 const base = {items: [1, 2, 3, 2, 1]}1531 const result = produce(base, draft => {1532 const index = draft.items.lastIndexOf(2)1533 expect(index).toBe(3)1534 })1535 expect(result).toBe(base)1536 })15371538 test("returns -1 when not found", () => {1539 const base = {items: [1, 2, 3]}1540 const result = produce(base, draft => {1541 const index = draft.items.lastIndexOf(99)1542 expect(index).toBe(-1)1543 })1544 expect(result).toBe(base)1545 })15461547 test("works with fromIndex parameter", () => {1548 const base = {items: [1, 2, 3, 2, 1]}1549 const result = produce(base, draft => {1550 const index = draft.items.lastIndexOf(2, 2)1551 expect(index).toBe(1)1552 })1553 expect(result).toBe(base)1554 })15551556 test("works with negative fromIndex", () => {1557 const base = {items: [1, 2, 3, 2, 1]}1558 const result = produce(base, draft => {1559 const index = draft.items.lastIndexOf(2, -2)1560 expect(index).toBe(3)1561 })1562 expect(result).toBe(base)1563 })1564 })15651566 describe("includes()", () => {1567 test("returns true when item exists", () => {1568 const base = {items: [1, 2, 3, 4, 5]}1569 const result = produce(base, draft => {1570 expect(draft.items.includes(3)).toBe(true)1571 })1572 expect(result).toBe(base)1573 })15741575 test("returns false when item doesn't exist", () => {1576 const base = {items: [1, 2, 3, 4, 5]}1577 const result = produce(base, draft => {1578 expect(draft.items.includes(99)).toBe(false)1579 })1580 expect(result).toBe(base)1581 })15821583 test("works with fromIndex parameter", () => {1584 const base = {items: [1, 2, 3, 4, 5]}1585 const result = produce(base, draft => {1586 expect(draft.items.includes(2, 2)).toBe(false)1587 expect(draft.items.includes(3, 2)).toBe(true)1588 })1589 expect(result).toBe(base)1590 })15911592 test("works with NaN", () => {1593 const base = {items: [1, NaN, 3]}1594 const result = produce(base, draft => {1595 expect(draft.items.includes(NaN)).toBe(true)1596 })1597 expect(result).toBe(base)1598 })15991600 test("works with negative fromIndex", () => {1601 const base = {items: [1, 2, 3, 4, 5]}1602 const result = produce(base, draft => {1603 expect(draft.items.includes(4, -2)).toBe(true)1604 expect(draft.items.includes(2, -2)).toBe(false)1605 })1606 expect(result).toBe(base)1607 })1608 })16091610 describe("toString()", () => {1611 test("returns string representation", () => {1612 const base = {items: [1, 2, 3]}1613 const result = produce(base, draft => {1614 const str = draft.items.toString()1615 expect(str).toBe("1,2,3")1616 })1617 expect(result).toBe(base)1618 })16191620 test("works with objects", () => {1621 const base = {items: [{id: 1}, {id: 2}]}1622 const result = produce(base, draft => {1623 const str = draft.items.toString()1624 expect(str).toContain("[object Object]")1625 })1626 expect(result).toBe(base)1627 })16281629 test("works with empty array", () => {1630 const base = {items: []}1631 const result = produce(base, draft => {1632 const str = draft.items.toString()1633 expect(str).toBe("")1634 })1635 expect(result).toBe(base)1636 })1637 })16381639 describe("toLocaleString()", () => {1640 test("returns locale string representation", () => {1641 const base = {items: [1, 2, 3]}1642 const result = produce(base, draft => {1643 const str = draft.items.toLocaleString()1644 expect(typeof str).toBe("string")1645 })1646 expect(result).toBe(base)1647 })16481649 test("works with numbers", () => {1650 const base = {numbers: [1000, 2000]}1651 const result = produce(base, draft => {1652 const str = draft.numbers.toLocaleString()1653 expect(typeof str).toBe("string")1654 })1655 expect(result).toBe(base)1656 })1657 })16581659 describe("findLast() additional edge cases", () => {1660 test("returns undefined when not found", () => {1661 const base = {items: [{id: 1}, {id: 2}, {id: 3}]}1662 const result = produce(base, draft => {1663 const found = draft.items.findLast(item => item.id === 999)1664 expect(found).toBeUndefined()1665 })1666 expect(result).toBe(base)1667 })16681669 test("nested mutations on findLast result", () => {1670 const base = {1671 items: [1672 {id: 1, type: "A", nested: {value: 10}},1673 {id: 2, type: "B", nested: {value: 20}},1674 {id: 3, type: "A", nested: {value: 30}}1675 ]1676 }1677 const result = produce(base, draft => {1678 const found = draft.items.findLast(item => item.type === "A")1679 expect(isDraft(found)).toBe(true)1680 if (found) {1681 found.nested.value = 9991682 }1683 })1684 expect(result.items[2].nested.value).toBe(999)1685 expect(base.items[2].nested.value).toBe(30)1686 })16871688 test("findLast on empty array", () => {1689 const base = {items: []}1690 const result = produce(base, draft => {1691 const found = draft.items.findLast(() => true)1692 expect(found).toBeUndefined()1693 })1694 expect(result).toBe(base)1695 })1696 })16971698 describe("comparison: filter vs concat behavior", () => {1699 test("filter returns drafts that can affect original", () => {1700 const base = {1701 items: [1702 {id: 1, value: 10},1703 {id: 2, value: 20}1704 ]1705 }1706 const result = produce(base, draft => {1707 const filtered = draft.items.filter(item => item.id === 1)1708 expect(isDraft(filtered[0])).toBe(true)1709 filtered[0].value = 9991710 })1711 expect(result.items[0].value).toBe(999)1712 })17131714 // Behavior differs based on array methods plugin1715 if (useArrayMethods) {1716 test("concat returns base values that cannot affect original (with plugin)", () => {1717 const base = {items: [{id: 1, value: 10}]}1718 const result = produce(base, draft => {1719 const concatenated = draft.items.concat()1720 // With plugin: concat returns base values, not drafts1721 expect(isDraft(concatenated[0])).toBe(false)1722 // Attempting to mutate won't affect the draft1723 // (would throw if autoFreeze is on, or just not be tracked)1724 })1725 expect(result).toBe(base)1726 })1727 } else {1728 test("concat returns drafts that can affect original (default behavior)", () => {1729 const base = {items: [{id: 1, value: 10}]}1730 const result = produce(base, draft => {1731 const concatenated = draft.items.concat()1732 // Without plugin: concat returns draft proxies via get trap1733 expect(isDraft(concatenated[0])).toBe(true)1734 // Mutations to concatenated items ARE tracked1735 concatenated[0].value = 9991736 })1737 expect(result.items[0].value).toBe(999)1738 })1739 }1740 })1741Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 1742–1913why, one step back1742 describe("combined operations", () => {1743 test("chain filter then map then mutate", () => {1744 const base = createTestData()1745 const result = produce(base, draft => {1746 const filtered = draft.items.filter(item => item.value > 20)1747 // Verify filtered items are drafts1748 filtered.forEach(item => expect(isDraft(item)).toBe(true))1749 const nested = filtered.map(item => item.nested)1750 // Verify mapped items are drafts1751 expect(isDraft(nested[0])).toBe(true)1752 nested[0].count = 9991753 })1754 expect(result.items[2].nested.count).toBe(999)1755 // Verify base state unchanged1756 expect(base.items[2].nested.count).toBe(3)1757 // Verify result is a copy1758 expect(result.items[2].nested).not.toBe(base.items[2].nested)1759 })17601761 test("filter, find, then mutate", () => {1762 const base = createTestData()1763 const result = produce(base, draft => {1764 const filtered = draft.items.filter(item => item.value > 20)1765 // Verify filtered items are drafts1766 filtered.forEach(item => expect(isDraft(item)).toBe(true))1767 const found = filtered.find(item => item.id === 4)1768 // Verify found item is a draft1769 expect(isDraft(found)).toBe(true)1770 if (found) {1771 found.value = 9991772 }1773 })1774 expect(result.items[3].value).toBe(999)1775 // Verify base state unchanged1776 expect(base.items[3].value).toBe(40)1777 // Verify result is a copy1778 expect(result.items[3]).not.toBe(base.items[3])1779 })17801781 test("multiple filters with mutations", () => {1782 const base = createTestData()1783 const result = produce(base, draft => {1784 const filtered1 = draft.items.filter(item => item.value > 15)1785 // Verify first filter items are drafts1786 filtered1.forEach(item => expect(isDraft(item)).toBe(true))1787 const filtered2 = filtered1.filter(item => item.value < 45)1788 // Verify second filter items are drafts1789 filtered2.forEach(item => expect(isDraft(item)).toBe(true))1790 filtered2[0].value = 9991791 })1792 expect(result.items[1].value).toBe(999)1793 // Verify base state unchanged1794 expect(base.items[1].value).toBe(20)1795 // Verify result is a copy1796 expect(result.items[1]).not.toBe(base.items[1])1797 })1798 })17991800 describe("edge cases", () => {1801 test("empty filter result", () => {1802 const base = createTestData()1803 const result = produce(base, draft => {1804 const filtered = draft.items.filter(item => item.value > 1000)1805 expect(filtered).toHaveLength(0)1806 })1807 expect(result).toBe(base)1808 })18091810 test("filter with all items", () => {1811 const base = createTestData()1812 const result = produce(base, draft => {1813 const filtered = draft.items.filter(() => true)1814 filtered[0].value = 9991815 })1816 expect(result.items[0].value).toBe(999)1817 })18181819 test("primitive array filter", () => {1820 const base = {numbers: [1, 2, 3, 4, 5]}1821 const result = produce(base, draft => {1822 const filtered = draft.numbers.filter(n => n > 2)1823 expect(filtered).toEqual([3, 4, 5])1824 })1825 expect(result).toBe(base)1826 })18271828 test("mixed draft and non-draft in filter result", () => {1829 const base = createTestData()1830 const result = produce(base, draft => {1831 // Mutate some items before filtering1832 draft.items[0].value = 151833 draft.items[2].value = 3518341835 const filtered = draft.items.filter(item => item.value > 14)1836 filtered[0].value = 999 // Mutate already-mutated item1837 })1838 expect(result.items[0].value).toBe(999)1839 })18401841 test("assigning filtered array back to draft", () => {1842 const base = createTestData()1843 const result = produce(base, draft => {1844 draft.items = draft.items.filter(item => item.value > 25)1845 draft.items[0].value = 9991846 })1847 expect(result.items).toHaveLength(3)1848 expect(result.items[0].value).toBe(999)1849 expect(result.items[0].id).toBe(3)1850 })1851 })18521853 describe("performance-critical patterns", () => {1854 test("large array filter with read-only access", () => {1855 const base = {1856 items: Array.from({length: 1000}, (_, i) => ({1857 id: i,1858 value: i * 101859 }))1860 }1861 const result = produce(base, draft => {1862 const filtered = draft.items.filter(item => item.value > 5000)1863 const sum = filtered.reduce((acc, item) => acc + item.value, 0)1864 expect(sum).toBeGreaterThan(0)1865 })1866 expect(result).toBe(base) // No modifications1867 })18681869 test("multiple filters without mutations", () => {1870 const base = createTestData()1871 const result = produce(base, draft => {1872 const f1 = draft.items.filter(item => item.value > 10)1873 const f2 = draft.items.filter(item => item.value < 40)1874 const f3 = draft.items.filter(item => item.id % 2 === 0)1875 expect(f1.length + f2.length + f3.length).toBeGreaterThan(0)1876 })1877 expect(result).toBe(base)1878 })1879 })1880 })18811882 it("supports the same child reference multiple times in the same array via index assignment", () => {1883 const obj = {value: 1}1884 const baseState = {items: [obj, {}, {}, {}, {}]}18851886 const nextState = produce(baseState, draft => {1887 // Assign the same object to multiple indices1888 draft.items[0] = obj // Original position1889 draft.items[2] = obj // Same object at different index1890 draft.items[4] = obj // Same object at yet another index18911892 // Modify the object through one of the references1893 draft.items[0].value = 21894 })18951896 // Immer behavior: modified draft gets new object, unmodified drafts are optimized1897 expect(nextState.items[0]).not.toBe(nextState.items[2]) // Modified vs unmodified1898 expect(nextState.items[2]).toBe(nextState.items[4]) // Both unmodified, same reference1899 expect(nextState.items[0].value).toBe(2) // Modified1900 expect(nextState.items[2].value).toBe(1) // Unmodified (same as original)1901 expect(nextState.items[4].value).toBe(1) // Unmodified (same as original)19021903 // The unmodified items should be the same as the original object1904 expect(nextState.items[2]).toBe(obj)1905 expect(nextState.items[4]).toBe(obj)19061907 // Original object should be unchanged1908 expect(obj.value).toBe(1)19091910 // Verify array structure1911 expect(nextState.items.length).toBe(5)1912 })1913chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 1914–1915why, one step back1914 it("supports modifying nested objects", () => {1915 const baseState = [{a: 1}, {}]Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 1916–1918why, one step back1916 const nextState = produce(baseState, s => {1917 s[0].a++1918 s[0].a++chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 1919why, one step back1919 s[1].a = 0Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 1920–1922why, one step back1920 s[0].a--1921 })1922 expect(nextState).not.toBe(baseState)chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 1923–1924why, one step back1923 expect(nextState[0].a).toBe(2)1924 expect(nextState[1].a).toBe(0)Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 1925–1926why, one step back1925 })1926chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 1927–1929why, one step back1927 it("never preserves non-numeric properties", () => {1928 const baseState = []1929 baseState.x = 7Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
line 1930why, one step back1930 const nextState = produce(baseState, s => {chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 1931why, one step back1931 s.push(3)Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
line 1932why, one step back1932 })chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 1933why, one step back1933 expect("x" in nextState).toBeFalsy()Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 1934–1935why, one step back1934 })1935Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 1936why, one step back1936 if (!global.USES_BUILD) {chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 1937why, one step back1937 it("throws when a non-numeric property is added", () => {Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
line 1938why, one step back1938 expect(() => {chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 1939–1940why, one step back1939 produce([], d => {1940 d.x = 3Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 1941–1944why, one step back1941 })1942 }).toThrowErrorMatchingSnapshot()1943 })1944chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 1945why, one step back1945 it("throws when a non-numeric property is deleted", () => {Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
line 1946why, one step back1946 expect(() => {chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 1947–1950why, one step back1947 const baseState = []1948 baseState.x = 71949 produce(baseState, d => {1950 delete d.xMerge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 1951–1956why, one step back1951 })1952 }).toThrowErrorMatchingSnapshot()1953 })1954 }1955 })1956chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 1957–1959why, one step back1957 describe("map drafts", () => {1958 it("supports key access", () => {1959 const value = baseState.aMap.get("jedi")Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
line 1960why, one step back1960 const nextState = produce(baseState, s => {chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 1961why, one step back1961 expect(s.aMap.get("jedi")).toEqual(value)Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 1962–1965why, one step back1962 })1963 expect(nextState).toBe(baseState)1964 })1965chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 1966why, one step back1966 it("supports key access for non-primitive keys", () => {Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
line 1967why, one step back1967 const key = {prop: "val"}chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 1968–1969why, one step back1968 const base = new Map([[key, {id: 1, a: 1}]])1969 const value = base.get(key)Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
line 1970why, one step back1970 const nextState = produce(base, s => {chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 1971why, one step back1971 expect(s.get(key)).toEqual(value)Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
line 1972why, one step back1972 })chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 1973why, one step back1973 expect(nextState).toBe(base)Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 1974–1981why, one step back1974 })19751976 it("supports iteration", () => {1977 const base = new Map([1978 ["first", {id: 1, a: 1}],1979 ["second", {id: 2, a: 1}]1980 ])1981 const findById = (map, id) => {chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 1982why, one step back1982 for (const [, item] of map) {Merge pull request #1181 from immerjs/feature/more-array-testsMark Eriksonbefore adoption6b23c37
lines 1983–2003why, one step back1983 if (item.id === id) return item1984 }1985 return null1986 }1987 const result = produce(base, draft => {1988 const obj1 = findById(draft, 1)1989 const obj2 = findById(draft, 2)1990 obj1.a = 21991 obj2.a = 21992 })1993 expect(result).not.toBe(base)1994 expect(result.get("first").a).toEqual(2)1995 expect(result.get("second").a).toEqual(2)1996 })19971998 it("supports 'entries'", () => {1999 const base = new Map([2000 ["first", {id: 1, a: 1}],The first 2,000 of 4,180 lines.