Merge pull request #1181 from immerjs/feature/more-array-tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Add many more tests for array behavior and update scenarios
authored by Mark Erikson · committed by GitHub · parents 87b4f5d, 773455a · Browse files · 2 files·+1619 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+1619 added−14 removed
- tests:+1619 added−14 removed
Provenance
- Commit
6b23c37- Tree
7dc665c- Author
Mark Erikson <mark@isquaredsoftware.com>- Committer
GitHub <noreply@github.com>- Parent 1
87b4f5d- Parent 2
773455a
2 files changed+1619 added−14 removed
modified:__tests__/base.js+1062 added-14 removed
15 unmodified lines16 enablePatches()17 enableMapSet()18 19 vi.setConfig({20- testTimeout: 100020+ testTimeout: 200021 })22 23 const isProd = process.env.NODE_ENV === "production"24 237 unmodified lines262 expect(nextState.anArray).not.toBe(baseState.anArray)263 expect(nextState.anArray.length).toBe(baseLength + 1)264 })265 266+ describe("mutating array methods", () => {266- // Reported here: https://github.com/mweststrate/immer/issues/116267- it("can pop then push", () => {268- const nextState = produce([1, 2, 3], s => {269- s.pop()270- s.push(100)267+ // Reported here: https://github.com/mweststrate/immer/issues/116268+ it("can pop then push", () => {269+ const nextState = produce([1, 2, 3], s => {270+ s.pop()271+ s.push(100)272+ })273+ expect(nextState).toEqual([1, 2, 100])274 })275+276+ it("can be sorted", () => {277+ const baseState = [{value: 3}, {value: 1}, {value: 2}]278+ const nextState = produce(baseState, s => {279+ s.sort((a, b) => a.value - b.value)280+ })272- expect(nextState).toEqual([1, 2, 100])281+ expect(nextState).not.toBe(baseState)282+ expect(nextState).toEqual([{value: 1}, {value: 2}, {value: 3}])283+ })284+285+ it("can be reversed", () => {286+ const baseState = [{value: 1}, {value: 2}, {value: 3}]287+ const nextState = produce(baseState, s => {288+ s.reverse()289+ })290+ expect(nextState).not.toBe(baseState)291+ expect(nextState).toEqual([{value: 3}, {value: 2}, {value: 1}])292+ })293+294+ it("can be sorted with existing proxies", () => {295+ const baseState = [{value: 3}, {value: 1}, {value: 2}]296+ const nextState = produce(baseState, s => {297+ // First mutate a nested object to create a proxy298+ s[0].value = 4299+ // Then sort the array300+ s.sort((a, b) => a.value - b.value)301+ })302+ expect(nextState).not.toBe(baseState)303+ expect(nextState).toEqual([{value: 1}, {value: 2}, {value: 4}])304+ })305+306+ it("can be reversed with existing proxies", () => {307+ const baseState = [{value: 1}, {value: 2}, {value: 3}]308+ const nextState = produce(baseState, s => {309+ // First mutate a nested object to create a proxy310+ s[1].value = 5311+ // Then reverse the array312+ s.reverse()313+ })314+ expect(nextState).not.toBe(baseState)315+ expect(nextState).toEqual([{value: 3}, {value: 5}, {value: 1}])316+ })317+318+ it("can be sorted with unmodified existing proxies", () => {319+ const baseState = [{value: 3}, {value: 1}, {value: 2}]320+ const nextState = produce(baseState, s => {321+ // Access a nested object to create a proxy, but don't modify it322+ const firstValue = s[0].value // This creates a proxy for s[0]323+ expect(firstValue).toBe(3) // But we don't modify it324+325+ // Then sort the array326+ s.sort((a, b) => a.value - b.value)327+ })328+ expect(nextState).not.toBe(baseState)329+ expect(nextState).toEqual([{value: 1}, {value: 2}, {value: 3}])330+ })331+332+ describe("push()", () => {333+ test("push single item", () => {334+ const base = {items: [{id: 1}, {id: 2}]}335+ const result = produce(base, draft => {336+ draft.items.push({id: 3})337+ })338+ expect(result.items).toHaveLength(3)339+ expect(result.items[2].id).toBe(3)340+ })341+342+ test("push multiple items", () => {343+ const base = {items: [{id: 1}]}344+ const result = produce(base, draft => {345+ draft.items.push({id: 2}, {id: 3}, {id: 4})346+ })347+ expect(result.items).toHaveLength(4)348+ expect(result.items[3].id).toBe(4)349+ })350+351+ test("push then mutate pushed item", () => {352+ const base = {items: [{id: 1}]}353+ const result = produce(base, draft => {354+ draft.items.push({id: 2, value: 10})355+ draft.items[1].value = 20356+ })357+ expect(result.items[1].value).toBe(20)358+ })359+360+ test("push returns new length", () => {361+ const base = {items: [1, 2]}362+ produce(base, draft => {363+ const newLength = draft.items.push(3, 4)364+ expect(newLength).toBe(4)365+ })366+ })367+ })368+369+ describe("unshift()", () => {370+ test("unshift single item", () => {371+ const base = {items: [{id: 2}, {id: 3}]}372+ const result = produce(base, draft => {373+ draft.items.unshift({id: 1})374+ })375+ expect(result.items).toHaveLength(3)376+ expect(result.items[0].id).toBe(1)377+ expect(result.items[1].id).toBe(2)378+ })379+380+ test("unshift multiple items", () => {381+ const base = {items: [{id: 4}]}382+ const result = produce(base, draft => {383+ draft.items.unshift({id: 1}, {id: 2}, {id: 3})384+ })385+ expect(result.items).toHaveLength(4)386+ expect(result.items[0].id).toBe(1)387+ expect(result.items[3].id).toBe(4)388+ })389+390+ test("unshift then mutate unshifted item", () => {391+ const base = {items: [{id: 2}]}392+ const result = produce(base, draft => {393+ draft.items.unshift({id: 1, value: 10})394+ draft.items[0].value = 20395+ })396+ expect(result.items[0].value).toBe(20)397+ })398+399+ test("unshift returns new length", () => {400+ const base = {items: [3, 4]}401+ produce(base, draft => {402+ const newLength = draft.items.unshift(1, 2)403+ expect(newLength).toBe(4)404+ })405+ })406+ })407+408+ describe("shift()", () => {409+ test("shift removes first item", () => {410+ const base = {items: [{id: 1}, {id: 2}, {id: 3}]}411+ const result = produce(base, draft => {412+ const removed = draft.items.shift()413+ expect(removed.id).toBe(1)414+ })415+ expect(result.items).toHaveLength(2)416+ expect(result.items[0].id).toBe(2)417+ })418+419+ test("shift on empty array returns undefined", () => {420+ const base = {items: []}421+ produce(base, draft => {422+ const removed = draft.items.shift()423+ expect(removed).toBeUndefined()424+ })425+ })426+427+ test("shift then mutate remaining items", () => {428+ const base = {items: [{id: 1}, {id: 2, value: 10}]}429+ const result = produce(base, draft => {430+ draft.items.shift()431+ draft.items[0].value = 20432+ })433+ expect(result.items).toHaveLength(1)434+ expect(result.items[0].value).toBe(20)435+ })436+437+ test("multiple shifts", () => {438+ const base = {items: [{id: 1}, {id: 2}, {id: 3}]}439+ const result = produce(base, draft => {440+ draft.items.shift()441+ draft.items.shift()442+ })443+ expect(result.items).toHaveLength(1)444+ expect(result.items[0].id).toBe(3)445+ })446+ })447+448+ describe("splice() edge cases", () => {449+ test("splice with only deleteCount (no items to add)", () => {450+ const base = {items: [{id: 1}, {id: 2}, {id: 3}, {id: 4}]}451+ const result = produce(base, draft => {452+ const removed = draft.items.splice(1, 2)453+ expect(removed).toHaveLength(2)454+ expect(removed[0].id).toBe(2)455+ })456+ expect(result.items).toHaveLength(2)457+ expect(result.items[0].id).toBe(1)458+ expect(result.items[1].id).toBe(4)459+ })460+461+ test("splice with negative start index", () => {462+ const base = {items: [{id: 1}, {id: 2}, {id: 3}]}463+ const result = produce(base, draft => {464+ draft.items.splice(-1, 1, {id: 99})465+ })466+ expect(result.items[2].id).toBe(99)467+ })468+469+ test("splice at start (index 0)", () => {470+ const base = {items: [{id: 1}, {id: 2}]}471+ const result = produce(base, draft => {472+ draft.items.splice(0, 0, {id: 0})473+ })474+ expect(result.items[0].id).toBe(0)475+ expect(result.items).toHaveLength(3)476+ })477+478+ test("splice at end", () => {479+ const base = {items: [{id: 1}, {id: 2}]}480+ const result = produce(base, draft => {481+ draft.items.splice(2, 0, {id: 3})482+ })483+ expect(result.items[2].id).toBe(3)484+ })485+486+ test("splice then mutate spliced-in items", () => {487+ const base = {items: [{id: 1}, {id: 3}]}488+ const result = produce(base, draft => {489+ draft.items.splice(1, 0, {id: 2, value: 10})490+ draft.items[1].value = 20491+ })492+ expect(result.items[1].value).toBe(20)493+ })494+ })495+496+ describe("combined operations", () => {497+ test("sort then push", () => {498+ const base = {items: [{value: 3}, {value: 1}]}499+ const result = produce(base, draft => {500+ draft.items.sort((a, b) => a.value - b.value)501+ draft.items.push({value: 4})502+ })503+ expect(result.items.map(i => i.value)).toEqual([1, 3, 4])504+ })505+506+ test("reverse then unshift", () => {507+ const base = {items: [{id: 1}, {id: 2}, {id: 3}]}508+ const result = produce(base, draft => {509+ draft.items.reverse()510+ draft.items.unshift({id: 0})511+ })512+ expect(result.items.map(i => i.id)).toEqual([0, 3, 2, 1])513+ })514+515+ test("splice then sort", () => {516+ const base = {items: [{value: 5}, {value: 2}, {value: 8}]}517+ const result = produce(base, draft => {518+ draft.items.splice(1, 1, {value: 1})519+ draft.items.sort((a, b) => a.value - b.value)520+ })521+ expect(result.items.map(i => i.value)).toEqual([1, 5, 8])522+ })523+524+ test("push, pop, push sequence", () => {525+ const base = {items: [{id: 1}]}526+ const result = produce(base, draft => {527+ draft.items.push({id: 2})528+ draft.items.pop()529+ draft.items.push({id: 3})530+ })531+ expect(result.items.map(i => i.id)).toEqual([1, 3])532+ })533+ })534+535+ describe("bulk operations with pre-existing proxies", () => {536+ test("access items before sort", () => {537+ const base = {items: [{value: 3}, {value: 1}, {value: 2}]}538+ const result = produce(base, draft => {539+ // Access all items to create proxies540+ draft.items.forEach(item => item.value)541+ // Then sort542+ draft.items.sort((a, b) => a.value - b.value)543+ })544+ expect(result.items.map(i => i.value)).toEqual([1, 2, 3])545+ })546+547+ test("mutate items before reverse", () => {548+ const base = {549+ items: [550+ {id: 1, value: 10},551+ {id: 2, value: 20}552+ ]553+ }554+ const result = produce(base, draft => {555+ // Mutate to create modified proxies556+ draft.items[0].value = 15557+ draft.items[1].value = 25558+ // Then reverse559+ draft.items.reverse()560+ })561+ expect(result.items[0].id).toBe(2)562+ expect(result.items[0].value).toBe(25)563+ expect(result.items[1].id).toBe(1)564+ expect(result.items[1].value).toBe(15)565+ })566+ })567+568+ describe("return values", () => {569+ test("pop returns removed item", () => {570+ const base = {items: [{id: 1}, {id: 2}]}571+ produce(base, draft => {572+ const removed = draft.items.pop()573+ expect(removed.id).toBe(2)574+ // Verify we can mutate the returned item575+ removed.modified = true576+ expect(draft.items[0].modified).toBeUndefined()577+ })578+ })579+580+ test("shift returns removed item", () => {581+ const base = {items: [{id: 1}, {id: 2}]}582+ produce(base, draft => {583+ const removed = draft.items.shift()584+ expect(removed.id).toBe(1)585+ })586+ })587+588+ test("splice returns array of removed items", () => {589+ const base = {items: [{id: 1}, {id: 2}, {id: 3}]}590+ produce(base, draft => {591+ const removed = draft.items.splice(0, 2)592+ expect(removed).toHaveLength(2)593+ expect(removed[0].id).toBe(1)594+ expect(removed[1].id).toBe(2)595+ })596+ })597+ })598 })599 275- it("can be sorted", () => {276- const baseState = [3, 1, 2]277- const nextState = produce(baseState, s => {278- s.sort()600+ describe("non-mutating array methods", () => {601+ // Test data factory602+ const createTestData = () => ({603+ items: [604+ {id: 1, value: 10, nested: {count: 1}},605+ {id: 2, value: 20, nested: {count: 2}},606+ {id: 3, value: 30, nested: {count: 3}},607+ {id: 4, value: 40, nested: {count: 4}},608+ {id: 5, value: 50, nested: {count: 5}}609+ ],610+ other: {data: "test"}611 })280- expect(nextState).not.toBe(baseState)281- expect(nextState).toEqual([1, 2, 3])612+613+ describe("filter()", () => {614+ test("returns new array with filtered items", () => {615+ const base = createTestData()616+ const result = produce(base, draft => {617+ const filtered = draft.items.filter(item => item.value > 25)618+ expect(filtered).toHaveLength(3)619+ expect(filtered[0].id).toBe(3)620+ })621+ expect(result).toBe(base) // No modifications622+ })623+624+ test("mutations to filtered items are reflected in result", () => {625+ const base = createTestData()626+ const result = produce(base, draft => {627+ const filtered = draft.items.filter(item => item.value > 25)628+ // Verify filtered items are drafts629+ expect(isDraft(filtered[0])).toBe(true)630+ filtered[0].value = 999631+ })632+ expect(result.items[2].value).toBe(999) // id: 3 is at index 2633+ expect(result.items[0].value).toBe(10) // Unchanged634+ // Verify base state unchanged635+ expect(base.items[2].value).toBe(30)636+ // Verify result is a copy637+ expect(result.items[2]).not.toBe(base.items[2])638+ })639+640+ test("mutations to nested properties work correctly", () => {641+ const base = createTestData()642+ const result = produce(base, draft => {643+ const filtered = draft.items.filter(item => item.id > 2)644+ // Verify filtered items are drafts645+ expect(isDraft(filtered[0])).toBe(true)646+ filtered[0].nested.count = 100647+ })648+ expect(result.items[2].nested.count).toBe(100)649+ // Verify base state unchanged650+ expect(base.items[2].nested.count).toBe(3)651+ // Verify result is a copy652+ expect(result.items[2].nested).not.toBe(base.items[2].nested)653+ })654+655+ test("multiple mutations to different filtered items", () => {656+ const base = createTestData()657+ const result = produce(base, draft => {658+ const filtered = draft.items.filter(item => item.value > 15)659+ // Verify all filtered items are drafts660+ filtered.forEach(item => expect(isDraft(item)).toBe(true))661+ filtered[0].value = 200 // id: 2662+ filtered[1].value = 300 // id: 3663+ filtered[2].value = 400 // id: 4664+ })665+ expect(result.items[1].value).toBe(200)666+ expect(result.items[2].value).toBe(300)667+ expect(result.items[3].value).toBe(400)668+ // Verify base state unchanged669+ expect(base.items[1].value).toBe(20)670+ expect(base.items[2].value).toBe(30)671+ expect(base.items[3].value).toBe(40)672+ })673+674+ test("filtered array can be assigned to another property", () => {675+ const base = createTestData()676+ const result = produce(base, draft => {677+ const filtered = draft.items.filter(item => item.value > 25)678+ draft.other.filtered = filtered679+ })680+ expect(result.other.filtered).toHaveLength(3)681+ expect(result.other.filtered[0].id).toBe(3)682+ })683+684+ test("cross-reference: mutating filtered item affects original location", () => {685+ const base = createTestData()686+ const result = produce(base, draft => {687+ const filtered = draft.items.filter(item => item.id === 3)688+ draft.other.ref = filtered[0]689+ filtered[0].value = 999690+ })691+ expect(result.items[2].value).toBe(999)692+ expect(result.other.ref.value).toBe(999)693+ })694+695+ test("read-only access doesn't cause mutations", () => {696+ const base = createTestData()697+ const result = produce(base, draft => {698+ const filtered = draft.items.filter(item => item.value > 25)699+ const sum = filtered.reduce((acc, item) => acc + item.value, 0)700+ expect(sum).toBe(120) // 30 + 40 + 50701+ })702+ expect(result).toBe(base) // No modifications703+ })704+ })705+706+ describe("map()", () => {707+ test("returns new array with mapped items", () => {708+ const base = createTestData()709+ const result = produce(base, draft => {710+ const mapped = draft.items.map(item => item.nested)711+ expect(mapped).toHaveLength(5)712+ expect(mapped[0].count).toBe(1)713+ })714+ expect(result).toBe(base)715+ })716+717+ test("mutations to mapped nested objects work", () => {718+ const base = createTestData()719+ const result = produce(base, draft => {720+ const nested = draft.items.map(item => item.nested)721+ // Verify mapped items are drafts722+ expect(isDraft(nested[0])).toBe(true)723+ nested[0].count = 100724+ })725+ expect(result.items[0].nested.count).toBe(100)726+ // Verify base state unchanged727+ expect(base.items[0].nested.count).toBe(1)728+ // Verify result is a copy729+ expect(result.items[0].nested).not.toBe(base.items[0].nested)730+ })731+732+ test("map with transformation then mutate", () => {733+ const base = createTestData()734+ const result = produce(base, draft => {735+ const mapped = draft.items.map(item => ({736+ ...item,737+ doubled: item.value * 2738+ }))739+ // This creates new objects, so mutations won't affect original740+ mapped[0].value = 999741+ })742+ expect(result.items[0].value).toBe(10) // Unchanged743+ })744+745+ test("map returning original items allows mutation", () => {746+ const base = createTestData()747+ const result = produce(base, draft => {748+ const mapped = draft.items.map(item => item) // Identity map749+ // Verify mapped items are drafts750+ expect(isDraft(mapped[0])).toBe(true)751+ mapped[0].value = 999752+ })753+ expect(result.items[0].value).toBe(999)754+ // Verify base state unchanged755+ expect(base.items[0].value).toBe(10)756+ // Verify result is a copy757+ expect(result.items[0]).not.toBe(base.items[0])758+ })759+ })760+761+ describe("find()", () => {762+ test("returns found item", () => {763+ const base = createTestData()764+ const result = produce(base, draft => {765+ const found = draft.items.find(item => item.id === 3)766+ expect(found?.value).toBe(30)767+ })768+ expect(result).toBe(base)769+ })770+771+ test("mutations to found item are reflected", () => {772+ const base = createTestData()773+ const result = produce(base, draft => {774+ const found = draft.items.find(item => item.id === 3)775+ // Verify found item is a draft776+ expect(isDraft(found)).toBe(true)777+ if (found) {778+ found.value = 999779+ }780+ })781+ expect(result.items[2].value).toBe(999)782+ // Verify base state unchanged783+ expect(base.items[2].value).toBe(30)784+ // Verify result is a copy785+ expect(result.items[2]).not.toBe(base.items[2])786+ })787+788+ test("nested mutations on found item", () => {789+ const base = createTestData()790+ const result = produce(base, draft => {791+ const found = draft.items.find(item => item.id === 2)792+ // Verify found item is a draft793+ expect(isDraft(found)).toBe(true)794+ if (found) {795+ found.nested.count = 200796+ }797+ })798+ expect(result.items[1].nested.count).toBe(200)799+ // Verify base state unchanged800+ expect(base.items[1].nested.count).toBe(2)801+ // Verify result is a copy802+ expect(result.items[1].nested).not.toBe(base.items[1].nested)803+ })804+805+ test("find returns undefined when not found", () => {806+ const base = createTestData()807+ const result = produce(base, draft => {808+ const found = draft.items.find(item => item.id === 999)809+ expect(found).toBeUndefined()810+ })811+ expect(result).toBe(base)812+ })813+ })814+815+ describe("findLast()", () => {816+ test("returns last matching item", () => {817+ const base = {818+ items: [819+ {id: 1, type: "A"},820+ {id: 2, type: "B"},821+ {id: 3, type: "A"}822+ ]823+ }824+ const result = produce(base, draft => {825+ const found = draft.items.findLast(item => item.type === "A")826+ expect(found?.id).toBe(3)827+ })828+ expect(result).toBe(base)829+ })830+831+ test("mutations to findLast result work", () => {832+ const base = {833+ items: [834+ {id: 1, type: "A", value: 10},835+ {id: 2, type: "B", value: 20},836+ {id: 3, type: "A", value: 30}837+ ]838+ }839+ const result = produce(base, draft => {840+ const found = draft.items.findLast(item => item.type === "A")841+ // Verify found item is a draft842+ expect(isDraft(found)).toBe(true)843+ if (found) {844+ found.value = 999845+ }846+ })847+ expect(result.items[2].value).toBe(999)848+ // Verify base state unchanged849+ expect(base.items[2].value).toBe(30)850+ // Verify result is a copy851+ expect(result.items[2]).not.toBe(base.items[2])852+ })853+ })854+855+ describe("slice()", () => {856+ test("returns sliced array", () => {857+ const base = createTestData()858+ const result = produce(base, draft => {859+ const sliced = draft.items.slice(1, 3)860+ expect(sliced).toHaveLength(2)861+ expect(sliced[0].id).toBe(2)862+ })863+ expect(result).toBe(base)864+ })865+866+ test("mutations to sliced items work", () => {867+ const base = createTestData()868+ const result = produce(base, draft => {869+ const sliced = draft.items.slice(1, 3)870+ // Verify sliced items are drafts871+ expect(isDraft(sliced[0])).toBe(true)872+ sliced[0].value = 999873+ })874+ expect(result.items[1].value).toBe(999)875+ // Verify base state unchanged876+ expect(base.items[1].value).toBe(20)877+ // Verify result is a copy878+ expect(result.items[1]).not.toBe(base.items[1])879+ })880+881+ test("slice with negative indices", () => {882+ const base = createTestData()883+ const result = produce(base, draft => {884+ const sliced = draft.items.slice(-2)885+ // Verify sliced items are drafts886+ expect(isDraft(sliced[0])).toBe(true)887+ sliced[0].value = 999888+ })889+ expect(result.items[3].value).toBe(999)890+ // Verify base state unchanged891+ expect(base.items[3].value).toBe(40)892+ // Verify result is a copy893+ expect(result.items[3]).not.toBe(base.items[3])894+ })895+ })896+897+ describe("flatMap()", () => {898+ test("returns flattened array", () => {899+ const base = {900+ groups: [{items: [{id: 1}, {id: 2}]}, {items: [{id: 3}, {id: 4}]}]901+ }902+ const result = produce(base, draft => {903+ const flat = draft.groups.flatMap(group => group.items)904+ expect(flat).toHaveLength(4)905+ expect(flat[0].id).toBe(1)906+ })907+ expect(result).toBe(base)908+ })909+910+ test("mutations to flatMapped items work", () => {911+ const base = {912+ groups: [913+ {914+ items: [915+ {id: 1, value: 10},916+ {id: 2, value: 20}917+ ]918+ },919+ {items: [{id: 3, value: 30}]}920+ ]921+ }922+ const result = produce(base, draft => {923+ const flat = draft.groups.flatMap(group => group.items)924+ // Verify flatMapped items are drafts925+ expect(isDraft(flat[0])).toBe(true)926+ flat[0].value = 999927+ })928+ expect(result.groups[0].items[0].value).toBe(999)929+ // Verify base state unchanged930+ expect(base.groups[0].items[0].value).toBe(10)931+ // Verify result is a copy932+ expect(result.groups[0].items[0]).not.toBe(base.groups[0].items[0])933+ })934+ })935+936+ describe("reduce()", () => {937+ test("returns accumulated value", () => {938+ const base = createTestData()939+ const result = produce(base, draft => {940+ const sum = draft.items.reduce((acc, item) => acc + item.value, 0)941+ expect(sum).toBe(150) // 10 + 20 + 30 + 40 + 50942+ })943+ expect(result).toBe(base)944+ })945+946+ test("mutations during reduce are reflected", () => {947+ const base = createTestData()948+ const result = produce(base, draft => {949+ const sum = draft.items.reduce((acc, item, index) => {950+ // Verify items in reduce are drafts951+ expect(isDraft(item)).toBe(true)952+ if (index === 2) {953+ item.value = 999954+ }955+ return acc + item.value956+ }, 0)957+ expect(sum).toBe(1119) // 10 + 20 + 999 + 40 + 50958+ })959+ expect(result.items[2].value).toBe(999)960+ // Verify base state unchanged961+ expect(base.items[2].value).toBe(30)962+ // Verify result is a copy963+ expect(result.items[2]).not.toBe(base.items[2])964+ })965+966+ test("reduce with object accumulator", () => {967+ const base = createTestData()968+ const result = produce(base, draft => {969+ const grouped = draft.items.reduce((acc, item) => {970+ const key = item.value > 25 ? "high" : "low"971+ if (!acc[key]) acc[key] = []972+ acc[key].push(item)973+ return acc974+ }, {})975+ expect(grouped.low).toHaveLength(2)976+ expect(grouped.high).toHaveLength(3)977+ })978+ expect(result).toBe(base)979+ })980+981+ test("reduce then mutate accumulated items", () => {982+ const base = createTestData()983+ const result = produce(base, draft => {984+ const items = draft.items.reduce((acc, item) => {985+ // Verify items in reduce are drafts986+ expect(isDraft(item)).toBe(true)987+ if (item.value > 25) acc.push(item)988+ return acc989+ }, [])990+ // Verify accumulated items are drafts991+ expect(isDraft(items[0])).toBe(true)992+ items[0].value = 999993+ })994+ expect(result.items[2].value).toBe(999) // id: 3 is at index 2995+ // Verify base state unchanged996+ expect(base.items[2].value).toBe(30)997+ // Verify result is a copy998+ expect(result.items[2]).not.toBe(base.items[2])999+ })1000+ })1001+1002+ describe("forEach()", () => {1003+ test("iterates over all items", () => {1004+ const base = createTestData()1005+ const result = produce(base, draft => {1006+ let count = 01007+ draft.items.forEach(item => {1008+ count++1009+ expect(item.id).toBeDefined()1010+ })1011+ expect(count).toBe(5)1012+ })1013+ expect(result).toBe(base)1014+ })1015+1016+ test("mutations during forEach are reflected", () => {1017+ const base = createTestData()1018+ const result = produce(base, draft => {1019+ draft.items.forEach((item, index) => {1020+ // Verify items in forEach are drafts1021+ expect(isDraft(item)).toBe(true)1022+ if (index === 1) {1023+ item.value = 9991024+ }1025+ })1026+ })1027+ expect(result.items[1].value).toBe(999)1028+ // Verify base state unchanged1029+ expect(base.items[1].value).toBe(20)1030+ // Verify result is a copy1031+ expect(result.items[1]).not.toBe(base.items[1])1032+ })1033+1034+ test("forEach with nested mutations", () => {1035+ const base = createTestData()1036+ const result = produce(base, draft => {1037+ draft.items.forEach(item => {1038+ // Verify items in forEach are drafts1039+ expect(isDraft(item)).toBe(true)1040+ item.nested.count *= 101041+ })1042+ })1043+ expect(result.items[0].nested.count).toBe(10)1044+ expect(result.items[4].nested.count).toBe(50)1045+ // Verify base state unchanged1046+ expect(base.items[0].nested.count).toBe(1)1047+ // Verify result is a copy1048+ expect(result.items[0].nested).not.toBe(base.items[0].nested)1049+ })1050+1051+ test("forEach returns undefined", () => {1052+ const base = createTestData()1053+ const result = produce(base, draft => {1054+ const returnValue = draft.items.forEach(item => item.value)1055+ expect(returnValue).toBeUndefined()1056+ })1057+ expect(result).toBe(base)1058+ })1059+ })1060+1061+ describe("indexOf()", () => {1062+ test("returns index of found item", () => {1063+ const base = {items: [1, 2, 3, 4, 5]}1064+ const result = produce(base, draft => {1065+ const index = draft.items.indexOf(3)1066+ expect(index).toBe(2)1067+ })1068+ expect(result).toBe(base)1069+ })1070+1071+ test("returns -1 when item not found", () => {1072+ const base = {items: [1, 2, 3, 4, 5]}1073+ const result = produce(base, draft => {1074+ const index = draft.items.indexOf(99)1075+ expect(index).toBe(-1)1076+ })1077+ expect(result).toBe(base)1078+ })1079+1080+ test("works with object references", () => {1081+ const base = createTestData()1082+ const result = produce(base, draft => {1083+ const item = draft.items[2]1084+ const index = draft.items.indexOf(item)1085+ expect(index).toBe(2)1086+ })1087+ expect(result).toBe(base)1088+ })1089+1090+ test("indexOf with fromIndex parameter", () => {1091+ const base = {items: [1, 2, 3, 2, 5]}1092+ const result = produce(base, draft => {1093+ const firstIndex = draft.items.indexOf(2)1094+ const secondIndex = draft.items.indexOf(2, 2)1095+ expect(firstIndex).toBe(1)1096+ expect(secondIndex).toBe(3)1097+ })1098+ expect(result).toBe(base)1099+ })1100+ })1101+1102+ describe("join()", () => {1103+ test("returns joined string", () => {1104+ const base = {items: [1, 2, 3, 4, 5]}1105+ const result = produce(base, draft => {1106+ const joined = draft.items.join(",")1107+ expect(joined).toBe("1,2,3,4,5")1108+ })1109+ expect(result).toBe(base)1110+ })1111+1112+ test("join with custom separator", () => {1113+ const base = {items: ["a", "b", "c"]}1114+ const result = produce(base, draft => {1115+ const joined = draft.items.join(" - ")1116+ expect(joined).toBe("a - b - c")1117+ })1118+ expect(result).toBe(base)1119+ })1120+1121+ test("join with no separator uses comma", () => {1122+ const base = {items: [1, 2, 3]}1123+ const result = produce(base, draft => {1124+ const joined = draft.items.join()1125+ expect(joined).toBe("1,2,3")1126+ })1127+ expect(result).toBe(base)1128+ })1129+1130+ test("join with objects calls toString", () => {1131+ const base = createTestData()1132+ const result = produce(base, draft => {1133+ const joined = draft.items.join(",")1134+ expect(joined).toContain("[object Object]")1135+ })1136+ expect(result).toBe(base)1137+ })1138+ })1139+1140+ describe("combined operations", () => {1141+ test("chain filter then map then mutate", () => {1142+ const base = createTestData()1143+ const result = produce(base, draft => {1144+ const filtered = draft.items.filter(item => item.value > 20)1145+ // Verify filtered items are drafts1146+ filtered.forEach(item => expect(isDraft(item)).toBe(true))1147+ const nested = filtered.map(item => item.nested)1148+ // Verify mapped items are drafts1149+ expect(isDraft(nested[0])).toBe(true)1150+ nested[0].count = 9991151+ })1152+ expect(result.items[2].nested.count).toBe(999)1153+ // Verify base state unchanged1154+ expect(base.items[2].nested.count).toBe(3)1155+ // Verify result is a copy1156+ expect(result.items[2].nested).not.toBe(base.items[2].nested)1157+ })1158+1159+ test("filter, find, then mutate", () => {1160+ const base = createTestData()1161+ const result = produce(base, draft => {1162+ const filtered = draft.items.filter(item => item.value > 20)1163+ // Verify filtered items are drafts1164+ filtered.forEach(item => expect(isDraft(item)).toBe(true))1165+ const found = filtered.find(item => item.id === 4)1166+ // Verify found item is a draft1167+ expect(isDraft(found)).toBe(true)1168+ if (found) {1169+ found.value = 9991170+ }1171+ })1172+ expect(result.items[3].value).toBe(999)1173+ // Verify base state unchanged1174+ expect(base.items[3].value).toBe(40)1175+ // Verify result is a copy1176+ expect(result.items[3]).not.toBe(base.items[3])1177+ })1178+1179+ test("multiple filters with mutations", () => {1180+ const base = createTestData()1181+ const result = produce(base, draft => {1182+ const filtered1 = draft.items.filter(item => item.value > 15)1183+ // Verify first filter items are drafts1184+ filtered1.forEach(item => expect(isDraft(item)).toBe(true))1185+ const filtered2 = filtered1.filter(item => item.value < 45)1186+ // Verify second filter items are drafts1187+ filtered2.forEach(item => expect(isDraft(item)).toBe(true))1188+ filtered2[0].value = 9991189+ })1190+ expect(result.items[1].value).toBe(999)1191+ // Verify base state unchanged1192+ expect(base.items[1].value).toBe(20)1193+ // Verify result is a copy1194+ expect(result.items[1]).not.toBe(base.items[1])1195+ })1196+ })1197+1198+ describe("edge cases", () => {1199+ test("empty filter result", () => {1200+ const base = createTestData()1201+ const result = produce(base, draft => {1202+ const filtered = draft.items.filter(item => item.value > 1000)1203+ expect(filtered).toHaveLength(0)1204+ })1205+ expect(result).toBe(base)1206+ })1207+1208+ test("filter with all items", () => {1209+ const base = createTestData()1210+ const result = produce(base, draft => {1211+ const filtered = draft.items.filter(() => true)1212+ filtered[0].value = 9991213+ })1214+ expect(result.items[0].value).toBe(999)1215+ })1216+1217+ test("mutation during filter callback", () => {1218+ const base = createTestData()1219+ const result = produce(base, draft => {1220+ const filtered = draft.items.filter(item => {1221+ // Verify items in filter callback are drafts1222+ expect(isDraft(item)).toBe(true)1223+ item.touched = true1224+ return item.value > 251225+ })1226+ expect(filtered).toHaveLength(3)1227+ })1228+ expect(result.items[0].touched).toBe(true)1229+ expect(result.items[3].touched).toBe(true)1230+ // Verify base state unchanged1231+ expect(base.items[0].touched).toBeUndefined()1232+ })1233+1234+ test("primitive array filter", () => {1235+ const base = {numbers: [1, 2, 3, 4, 5]}1236+ const result = produce(base, draft => {1237+ const filtered = draft.numbers.filter(n => n > 2)1238+ expect(filtered).toEqual([3, 4, 5])1239+ })1240+ expect(result).toBe(base)1241+ })1242+1243+ test("mixed draft and non-draft in filter result", () => {1244+ const base = createTestData()1245+ const result = produce(base, draft => {1246+ // Mutate some items before filtering1247+ draft.items[0].value = 151248+ draft.items[2].value = 351249+1250+ const filtered = draft.items.filter(item => item.value > 14)1251+ filtered[0].value = 999 // Mutate already-mutated item1252+ })1253+ expect(result.items[0].value).toBe(999)1254+ })1255+1256+ test("assigning filtered array back to draft", () => {1257+ const base = createTestData()1258+ const result = produce(base, draft => {1259+ draft.items = draft.items.filter(item => item.value > 25)1260+ draft.items[0].value = 9991261+ })1262+ expect(result.items).toHaveLength(3)1263+ expect(result.items[0].value).toBe(999)1264+ expect(result.items[0].id).toBe(3)1265+ })1266+ })1267+1268+ describe("performance-critical patterns", () => {1269+ test("large array filter with read-only access", () => {1270+ const base = {1271+ items: Array.from({length: 1000}, (_, i) => ({1272+ id: i,1273+ value: i * 101274+ }))1275+ }1276+ const result = produce(base, draft => {1277+ const filtered = draft.items.filter(item => item.value > 5000)1278+ const sum = filtered.reduce((acc, item) => acc + item.value, 0)1279+ expect(sum).toBeGreaterThan(0)1280+ })1281+ expect(result).toBe(base) // No modifications1282+ })1283+1284+ test("multiple filters without mutations", () => {1285+ const base = createTestData()1286+ const result = produce(base, draft => {1287+ const f1 = draft.items.filter(item => item.value > 10)1288+ const f2 = draft.items.filter(item => item.value < 40)1289+ const f3 = draft.items.filter(item => item.id % 2 === 0)1290+ expect(f1.length + f2.length + f3.length).toBeGreaterThan(0)1291+ })1292+ expect(result).toBe(base)1293+ })1294+ })1295 })1296 1297+ it("supports the same child reference multiple times in the same array via index assignment", () => {1298+ const obj = {value: 1}1299+ const baseState = {items: [obj, {}, {}, {}, {}]}1300+1301+ const nextState = produce(baseState, draft => {1302+ // Assign the same object to multiple indices1303+ draft.items[0] = obj // Original position1304+ draft.items[2] = obj // Same object at different index1305+ draft.items[4] = obj // Same object at yet another index1306+1307+ // Modify the object through one of the references1308+ draft.items[0].value = 21309+ })1310+1311+ // Immer behavior: modified draft gets new object, unmodified drafts are optimized1312+ expect(nextState.items[0]).not.toBe(nextState.items[2]) // Modified vs unmodified1313+ expect(nextState.items[2]).toBe(nextState.items[4]) // Both unmodified, same reference1314+ expect(nextState.items[0].value).toBe(2) // Modified1315+ expect(nextState.items[2].value).toBe(1) // Unmodified (same as original)1316+ expect(nextState.items[4].value).toBe(1) // Unmodified (same as original)1317+1318+ // The unmodified items should be the same as the original object1319+ expect(nextState.items[2]).toBe(obj)1320+ expect(nextState.items[4]).toBe(obj)1321+1322+ // Original object should be unchanged1323+ expect(obj.value).toBe(1)1324+1325+ // Verify array structure1326+ expect(nextState.items.length).toBe(5)1327+ })1328+1329 it("supports modifying nested objects", () => {1330 const baseState = [{a: 1}, {}]1331 const nextState = produce(baseState, s => {1332 s[0].a++1333+ s[0].a++1334 s[1].a = 01335+ s[0].a--1336 })1337 expect(nextState).not.toBe(baseState)1338 expect(nextState[0].a).toBe(2)1339 expect(nextState[1].a).toBe(0)788 unmodified lines2128 })2129 })2130 2131 it("supports a base state with multiple references to an object", () => {1085- const obj = {}2132+ const obj = {notEmpty: true}2133 const res = produce({a: obj, b: obj}, d => {2134 // Two drafts are created for each occurrence of an object in the base state.2135 expect(d.a).not.toBe(d.b)2136 d.a.z = true618 unmodified lines2755 expect(next[0]).toBe(base.a)2756 expect(next[0]).toBe(next[1])2757 })2758 2759+ // This actually seems to pass now!2760 it("cannot return an object that references itself", () => {2761 const res = {}2762 res.self = res2763 expect(() => {744 unmodified linesadded:__tests__/updateScenarios.js+557 added
1+import {describe, test, expect, beforeEach} from "vitest"2+import {produce} from "../src/immer"3+4+// Test configuration - smaller values for faster tests5+const TEST_CONFIG = {6+ arraySize: 10,7+ nestedArraySize: 2,8+ multiUpdateCount: 5,9+ reuseStateIterations: 510+}11+12+function createInitialState(arraySize = TEST_CONFIG.arraySize) {13+ const initialState = {14+ largeArray: Array.from({length: arraySize}, (_, i) => ({15+ id: i,16+ value: Math.random(),17+ nested: {key: `key-${i}`, data: Math.random()},18+ moreNested: {19+ items: Array.from({length: TEST_CONFIG.nestedArraySize}, (_, i) => ({20+ id: i,21+ name: String(i)22+ }))23+ }24+ })),25+ otherData: Array.from({length: arraySize}, (_, i) => ({26+ id: i,27+ name: `name-${i}`,28+ isActive: i % 2 === 029+ }))30+ }31+ return initialState32+}33+34+// Utility functions for calculating array indices based on size35+const getValidIndex = (arraySize = TEST_CONFIG.arraySize) => {36+ // Return a valid index (not the last one to avoid edge cases)37+ return Math.min(arraySize - 2, Math.max(0, arraySize - 2))38+}39+40+const getValidId = (arraySize = TEST_CONFIG.arraySize) => {41+ // Return a valid ID that exists in the array42+ return Math.min(arraySize - 2, Math.max(0, arraySize - 2))43+}44+45+const getHighIndex = (offset = 0, arraySize = TEST_CONFIG.arraySize) => {46+ // Calculate high index (80% through the array + offset)47+ return Math.floor(arraySize * 0.8) + (offset % Math.floor(arraySize * 0.2))48+}49+50+// Action creators51+const add = index => ({52+ type: "test/addItem",53+ payload: {id: index, value: index, nested: {data: index}}54+})55+56+const remove = index => ({type: "test/removeItem", payload: index})57+58+const filter = percentToKeep => ({59+ type: "test/filterItem",60+ payload: percentToKeep61+})62+63+const update = index => ({64+ type: "test/updateItem",65+ payload: {id: index, value: index, nestedData: index}66+})67+68+const concat = index => ({69+ type: "test/concatArray",70+ payload: Array.from({length: 50}, (_, i) => ({id: i, value: index}))71+})72+73+const updateHigh = index => ({74+ type: "test/updateHighIndex",75+ payload: {76+ id: getHighIndex(index),77+ value: index,78+ nestedData: index79+ }80+})81+82+const updateMultiple = index => ({83+ type: "test/updateMultiple",84+ payload: Array.from({length: TEST_CONFIG.multiUpdateCount}, (_, i) => ({85+ id: (index + i) % TEST_CONFIG.arraySize,86+ value: index + i,87+ nestedData: index + i88+ }))89+})90+91+const removeHigh = index => ({92+ type: "test/removeHighIndex",93+ payload: getHighIndex(index)94+})95+96+const sortByIdReverse = () => ({97+ type: "test/sortByIdReverse"98+})99+100+const reverseArray = () => ({101+ type: "test/reverseArray"102+})103+104+const actions = {105+ add,106+ remove,107+ filter,108+ update,109+ concat,110+ updateHigh,111+ updateMultiple,112+ removeHigh,113+ sortByIdReverse,114+ reverseArray115+}116+117+// Vanilla reducer for comparison118+const vanillaReducer = (state = createInitialState(), action) => {119+ switch (action.type) {120+ case "test/addItem":121+ return {122+ ...state,123+ largeArray: [...state.largeArray, action.payload]124+ }125+ case "test/removeItem": {126+ const newArray = state.largeArray.slice()127+ newArray.splice(action.payload, 1)128+ return {129+ ...state,130+ largeArray: newArray131+ }132+ }133+ case "test/filterItem": {134+ const length = state.largeArray.length135+ const newArray = state.largeArray.filter(136+ (item, i) => i / length < action.payload137+ )138+ return {139+ ...state,140+ largeArray: newArray141+ }142+ }143+ case "test/updateItem": {144+ return {145+ ...state,146+ largeArray: state.largeArray.map(item =>147+ item.id === action.payload.id148+ ? {149+ ...item,150+ value: action.payload.value,151+ nested: {...item.nested, data: action.payload.nestedData}152+ }153+ : item154+ )155+ }156+ }157+ case "test/concatArray": {158+ const length = state.largeArray.length159+ const newArray = action.payload.concat(state.largeArray)160+ newArray.length = length161+ return {162+ ...state,163+ largeArray: newArray164+ }165+ }166+ case "test/updateHighIndex": {167+ return {168+ ...state,169+ largeArray: state.largeArray.map(item =>170+ item.id === action.payload.id171+ ? {172+ ...item,173+ value: action.payload.value,174+ nested: {...item.nested, data: action.payload.nestedData}175+ }176+ : item177+ )178+ }179+ }180+ case "test/updateMultiple": {181+ const updates = new Map(action.payload.map(p => [p.id, p]))182+ return {183+ ...state,184+ largeArray: state.largeArray.map(item => {185+ const update = updates.get(item.id)186+ return update187+ ? {188+ ...item,189+ value: update.value,190+ nested: {...item.nested, data: update.nestedData}191+ }192+ : item193+ })194+ }195+ }196+ case "test/removeHighIndex": {197+ const newArray = state.largeArray.slice()198+ const indexToRemove = newArray.findIndex(199+ item => item.id === action.payload200+ )201+ if (indexToRemove !== -1) {202+ newArray.splice(indexToRemove, 1)203+ }204+ return {205+ ...state,206+ largeArray: newArray207+ }208+ }209+ case "test/sortByIdReverse": {210+ const newArray = state.largeArray.slice()211+ newArray.sort((a, b) => b.id - a.id) // Sort by ID in reverse order212+ return {213+ ...state,214+ largeArray: newArray215+ }216+ }217+ case "test/reverseArray": {218+ const newArray = state.largeArray.slice()219+ newArray.reverse()220+ return {221+ ...state,222+ largeArray: newArray223+ }224+ }225+ default:226+ return state227+ }228+}229+230+// Immer reducer231+const createImmerReducer = produce => {232+ const immerReducer = (state = createInitialState(), action) =>233+ produce(state, draft => {234+ switch (action.type) {235+ case "test/addItem":236+ draft.largeArray.push(action.payload)237+ break238+ case "test/removeItem":239+ draft.largeArray.splice(action.payload, 1)240+ break241+ case "test/filterItem": {242+ const keepPercentage = action.payload / 10243+ const length = state.largeArray.length244+ draft.largeArray = draft.largeArray.filter(245+ (item, i) => i / length < action.payload246+ )247+ break248+ }249+ case "test/updateItem": {250+ const item = draft.largeArray.find(251+ item => item.id === action.payload.id252+ )253+ if (item) {254+ item.value = action.payload.value255+ item.nested.data = action.payload.nestedData256+ }257+ break258+ }259+ case "test/concatArray": {260+ const length = state.largeArray.length261+ const newArray = action.payload.concat(state.largeArray)262+ newArray.length = length263+ draft.largeArray = newArray264+ break265+ }266+ case "test/updateHighIndex": {267+ const item = draft.largeArray.find(268+ item => item.id === action.payload.id269+ )270+ if (item) {271+ item.value = action.payload.value272+ item.nested.data = action.payload.nestedData273+ }274+ break275+ }276+ case "test/updateMultiple": {277+ action.payload.forEach(update => {278+ const item = draft.largeArray.find(item => item.id === update.id)279+ if (item) {280+ item.value = update.value281+ item.nested.data = update.nestedData282+ }283+ })284+ break285+ }286+ case "test/removeHighIndex": {287+ const indexToRemove = draft.largeArray.findIndex(288+ item => item.id === action.payload289+ )290+ if (indexToRemove !== -1) {291+ draft.largeArray.splice(indexToRemove, 1)292+ }293+ break294+ }295+ case "test/sortByIdReverse": {296+ draft.largeArray.sort((a, b) => b.id - a.id)297+ break298+ }299+ case "test/reverseArray": {300+ draft.largeArray.reverse()301+ break302+ }303+ }304+ })305+306+ return immerReducer307+}308+309+const immerReducer = createImmerReducer(produce)310+311+describe("Update Scenarios - Single Operations", () => {312+ let initialState313+314+ beforeEach(() => {315+ initialState = createInitialState()316+ })317+318+ test("add scenario", () => {319+ const action = actions.add(0)320+ const vanillaResult = vanillaReducer(initialState, action)321+ const immerResult = immerReducer(initialState, action)322+323+ expect(immerResult.largeArray.length).toBe(vanillaResult.largeArray.length)324+ expect(immerResult.largeArray[immerResult.largeArray.length - 1]).toEqual(325+ action.payload326+ )327+ })328+329+ test("remove scenario", () => {330+ const action = actions.remove(getValidIndex())331+ const vanillaResult = vanillaReducer(initialState, action)332+ const immerResult = immerReducer(initialState, action)333+334+ expect(immerResult.largeArray.length).toBe(vanillaResult.largeArray.length)335+ expect(immerResult.largeArray.length).toBe(336+ initialState.largeArray.length - 1337+ )338+ })339+340+ test("filter scenario", () => {341+ // Keep 60% of the items342+ const percentage = 0.6343+ const action = actions.filter(percentage)344+ const vanillaResult = vanillaReducer(initialState, action)345+ const immerResult = immerReducer(initialState, action)346+347+ expect(immerResult.largeArray.length).toBe(vanillaResult.largeArray.length)348+ expect(immerResult.largeArray.length).toBe(349+ initialState.largeArray.length * percentage350+ )351+ })352+353+ test("update scenario", () => {354+ const targetId = getValidId()355+ const action = actions.update(targetId)356+ const vanillaResult = vanillaReducer(initialState, action)357+ const immerResult = immerReducer(initialState, action)358+359+ const updatedItem = immerResult.largeArray.find(360+ item => item.id === targetId361+ )362+ expect(updatedItem.value).toBe(targetId)363+ expect(updatedItem.nested.data).toBe(targetId)364+ })365+366+ test("concat scenario", () => {367+ const action = actions.concat(1)368+ const vanillaResult = vanillaReducer(initialState, action)369+ const immerResult = immerReducer(initialState, action)370+371+ expect(immerResult.largeArray.length).toBe(vanillaResult.largeArray.length)372+ expect(immerResult.largeArray.length).toBe(initialState.largeArray.length)373+ })374+375+ test("updateHigh scenario", () => {376+ const action = actions.updateHigh(2)377+ const vanillaResult = vanillaReducer(initialState, action)378+ const immerResult = immerReducer(initialState, action)379+380+ const targetId = action.payload.id381+ const updatedItem = immerResult.largeArray.find(382+ item => item.id === targetId383+ )384+ expect(updatedItem.value).toBe(2)385+ expect(updatedItem.nested.data).toBe(2)386+ })387+388+ test("updateMultiple scenario", () => {389+ const action = actions.updateMultiple(3)390+ const vanillaResult = vanillaReducer(initialState, action)391+ const immerResult = immerReducer(initialState, action)392+393+ action.payload.forEach(update => {394+ const updatedItem = immerResult.largeArray.find(395+ item => item.id === update.id396+ )397+ expect(updatedItem.value).toBe(update.value)398+ expect(updatedItem.nested.data).toBe(update.nestedData)399+ })400+ })401+402+ test("removeHigh scenario", () => {403+ const action = actions.removeHigh(1)404+ const vanillaResult = vanillaReducer(initialState, action)405+ const immerResult = immerReducer(initialState, action)406+407+ expect(immerResult.largeArray.length).toBe(vanillaResult.largeArray.length)408+ const removedItem = immerResult.largeArray.find(409+ item => item.id === action.payload410+ )411+ expect(removedItem).toBeUndefined()412+ })413+414+ test("sortByIdReverse scenario", () => {415+ const action = actions.sortByIdReverse()416+ const vanillaResult = vanillaReducer(initialState, action)417+ const immerResult = immerReducer(initialState, action)418+419+ expect(immerResult.largeArray.length).toBe(vanillaResult.largeArray.length)420+ expect(immerResult.largeArray.length).toBe(initialState.largeArray.length)421+422+ // Verify sorting - first element should have highest ID423+ expect(immerResult.largeArray[0].id).toBe(TEST_CONFIG.arraySize - 1)424+ expect(immerResult.largeArray[immerResult.largeArray.length - 1].id).toBe(0)425+426+ // Verify arrays match427+ expect(immerResult.largeArray.map(item => item.id)).toEqual(428+ vanillaResult.largeArray.map(item => item.id)429+ )430+ })431+432+ test("reverseArray scenario", () => {433+ const action = actions.reverseArray()434+ const vanillaResult = vanillaReducer(initialState, action)435+ const immerResult = immerReducer(initialState, action)436+437+ expect(immerResult.largeArray.length).toBe(vanillaResult.largeArray.length)438+ expect(immerResult.largeArray.length).toBe(initialState.largeArray.length)439+440+ // Verify reversal - first element should be what was last441+ expect(immerResult.largeArray[0].id).toBe(TEST_CONFIG.arraySize - 1)442+ expect(immerResult.largeArray[immerResult.largeArray.length - 1].id).toBe(0)443+444+ // Verify arrays match445+ expect(immerResult.largeArray.map(item => item.id)).toEqual(446+ vanillaResult.largeArray.map(item => item.id)447+ )448+ })449+})450+451+describe("Update Scenarios - State Reuse", () => {452+ test("update-reuse scenario", () => {453+ let currentState = createInitialState()454+455+ for (let i = 0; i < TEST_CONFIG.reuseStateIterations; i++) {456+ currentState = immerReducer(currentState, actions.update(i))457+ }458+459+ // Verify the final state has all updates applied460+ for (let i = 0; i < TEST_CONFIG.reuseStateIterations; i++) {461+ const item = currentState.largeArray.find(item => item.id === i)462+ expect(item.value).toBe(i)463+ expect(item.nested.data).toBe(i)464+ }465+ })466+467+ test("updateHigh-reuse scenario", () => {468+ let currentState = createInitialState()469+470+ for (let i = 0; i < TEST_CONFIG.reuseStateIterations; i++) {471+ currentState = immerReducer(currentState, actions.updateHigh(i))472+ }473+474+ // Verify updates were applied to high indices475+ expect(currentState.largeArray.length).toBe(TEST_CONFIG.arraySize)476+ })477+478+ test("remove-reuse scenario", () => {479+ let currentState = createInitialState()480+ const originalLength = currentState.largeArray.length481+482+ for (let i = 0; i < TEST_CONFIG.reuseStateIterations; i++) {483+ currentState = immerReducer(currentState, actions.remove(0)) // Always remove first item484+ }485+486+ expect(currentState.largeArray.length).toBe(487+ originalLength - TEST_CONFIG.reuseStateIterations488+ )489+ })490+491+ test("removeHigh-reuse scenario", () => {492+ let currentState = createInitialState()493+ const originalLength = currentState.largeArray.length494+495+ for (let i = 0; i < TEST_CONFIG.reuseStateIterations; i++) {496+ currentState = immerReducer(currentState, actions.removeHigh(i))497+ }498+499+ expect(currentState.largeArray.length).toBeLessThan(originalLength)500+ })501+})502+503+describe("Update Scenarios - Mixed Sequence", () => {504+ test("mixed-sequence scenario", () => {505+ let state = createInitialState()506+ const originalLength = state.largeArray.length507+508+ // Perform a sequence of different operations (typical workflow)509+ state = immerReducer(state, actions.add(1))510+ expect(state.largeArray.length).toBe(originalLength + 1)511+512+ const targetId = getValidId()513+ state = immerReducer(state, actions.update(targetId))514+ const updatedItem = state.largeArray.find(item => item.id === targetId)515+ expect(updatedItem.value).toBe(targetId)516+517+ state = immerReducer(state, actions.updateHigh(2))518+ state = immerReducer(state, actions.updateMultiple(3))519+520+ state = immerReducer(state, actions.remove(getValidIndex()))521+ expect(state.largeArray.length).toBe(originalLength) // +1 from add, -1 from remove522+523+ // Verify final state integrity524+ expect(state.largeArray).toBeDefined()525+ expect(state.otherData).toBeDefined()526+ expect(state.largeArray.every(item => item.id !== undefined)).toBe(true)527+ })528+})529+530+describe("Update Scenarios - Performance Focused", () => {531+ test("large array operations", () => {532+ const largeState = createInitialState(5000)533+534+ // Test that operations complete without timeout535+ const result1 = immerReducer(largeState, actions.update(1000))536+ expect(result1.largeArray.length).toBe(5000)537+538+ const result2 = immerReducer(largeState, actions.updateHigh(10))539+ expect(result2.largeArray.length).toBe(5000)540+541+ const result3 = immerReducer(largeState, actions.updateMultiple(20))542+ expect(result3.largeArray.length).toBe(5000)543+ })544+545+ test("nested data modifications", () => {546+ const state = createInitialState(100)547+548+ const result = immerReducer(state, actions.update(50))549+ const updatedItem = result.largeArray.find(item => item.id === 50)550+551+ expect(updatedItem.nested.data).toBe(50)552+ expect(updatedItem.moreNested.items).toBeDefined()553+ expect(updatedItem.moreNested.items.length).toBe(554+ TEST_CONFIG.nestedArraySize555+ )556+ })557+})