Skip to content
Myr

base.js

This file

Path

__tests__/base.js

1,319 lines · 43.0 KiB · JavaScript

Last changed by

Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2

History of this file

Take it

1"use strict"2import produce, {setAutoFreeze, setUseProxies, nothing} from "../src/immer"3import deepFreeze from "deep-freeze"4import cloneDeep from "lodash.clonedeep"5import * as lodash from "lodash"67jest.setTimeout(1000)89runBaseTest("proxy (no freeze)", true, false)10runBaseTest("proxy (autofreeze)", true, true)11runBaseTest("proxy (autofreeze)(patch listener)", true, true, true)12runBaseTest("es5 (no freeze)", false, false)13runBaseTest("es5 (autofreeze)", false, true)14runBaseTest("es5 (autofreeze)(patch listener)", false, true, true)1516function runBaseTest(name, useProxies, freeze, useListener) {17    describe(`base functionality - ${name}`, () => {18        let baseState19        let origBaseState20        // make sure logic doesn't break when listener is attached21        let listener = useListener ? function() {} : undefined2223        beforeEach(() => {24            setAutoFreeze(freeze)25            setUseProxies(useProxies)26            origBaseState = baseState = createBaseState()27        })2829        it("should return the original without modifications", () => {30            const nextState = produce(baseState, () => {}, listener)31            expect(nextState).toBe(baseState)32        })3334        it("should return the original without modifications when reading stuff", () => {35            const nextState = produce(36                baseState,37                s => {38                    expect(s.aProp).toBe("hi")39                    expect(s.anObject.nested).toMatchObject({yummie: true})40                },41                listener42            )43            expect(nextState).toBe(baseState)44        })4546        it("should return a copy when modifying stuff", () => {47            const nextState = produce(48                baseState,49                s => {50                    s.aProp = "hello world"51                },52                listener53            )54            expect(nextState).not.toBe(baseState)55            expect(baseState.aProp).toBe("hi")56            expect(nextState.aProp).toBe("hello world")57            // structural sharing?58            expect(nextState.nested).toBe(baseState.nested)59        })6061        it("should preserve type", () => {62            const nextState = produce(63                baseState,64                s => {65                    expect(Object.getPrototypeOf(s)).toBe(Object.prototype)66                    expect(Array.isArray(s.anArray)).toBe(true)67                    s.anArray.push(3)68                    s.aProp = "hello world"69                    expect(Object.getPrototypeOf(s)).toBe(Object.prototype)70                    expect(Array.isArray(s.anArray)).toBe(true)71                },72                listener73            )74            expect(Object.getPrototypeOf(nextState)).toBe(Object.prototype)75            expect(Array.isArray(nextState.anArray)).toBe(true)76        })7778        it("deep change bubbles up", () => {79            const nextState = produce(80                baseState,81                s => {82                    s.anObject.nested.yummie = false83                },84                listener85            )86            expect(nextState).not.toBe(baseState)87            expect(nextState.anObject).not.toBe(baseState.anObject)88            expect(baseState.anObject.nested.yummie).toBe(true)89            expect(nextState.anObject.nested.yummie).toBe(false)90            expect(nextState.anArray).toBe(baseState.anArray)91        })9293        it("can add props", () => {94            const nextState = produce(95                baseState,96                s => {97                    s.anObject.cookie = {tasty: true}98                },99                listener100            )101            expect(nextState).not.toBe(baseState)102            expect(nextState.anObject).not.toBe(baseState.anObject)103            expect(nextState.anObject.nested).toBe(baseState.anObject.nested)104            expect(nextState.anObject.cookie).toEqual({tasty: true})105        })106107        it("can delete props", () => {108            const nextState = produce(109                baseState,110                s => {111                    delete s.anObject.nested112                },113                listener114            )115            expect(nextState).not.toBe(baseState)116            expect(nextState.anObject).not.toBe(baseState.anObject)117            expect(nextState.anObject.nested).toBe(undefined)118        })119120        it("ignores single non-modification", () => {121            const nextState = produce(122                baseState,123                s => {124                    s.aProp = "hi"125                },126                listener127            )128            expect(nextState).toBe(baseState)129        })130131        it("processes single modification", () => {132            const nextState = produce(133                baseState,134                s => {135                    s.aProp = "hello"136                    s.aProp = "hi"137                },138                listener139            )140            expect(nextState).not.toBe(baseState)141            expect(nextState).toEqual(baseState)142        })143144        it("processes with for loop", () => {145            const base = [{id: 1, a: 1}, {id: 2, a: 1}]146            const findById = (collection, id) => {147                for (const item of collection) {148                    if (item.id === id) return item149                }150                return null151            }152            const result = produce(base, draft => {153                const obj1 = findById(draft, 1)154                const obj2 = findById(draft, 2)155                obj1.a = 2156                obj2.a = 2157            })158            expect(result[0].a).toEqual(2)159            expect(result[1].a).toEqual(2)160        })161162        it("works with objects without proto", () => {163            const base = Object.create(null)164            base.x = 1165            base.y = Object.create(null)166            base.y.y = 2167            expect(base.__proto__).toBe(undefined)168            const next = produce(169                base,170                draft => {171                    draft.y.z = 3172                    draft.y.y++173                    draft.x++174                },175                listener176            )177            expect(next).toEqual({178                x: 2,179                y: {y: 3, z: 3}180            })181            expect(next.__proto__).toBe(undefined)182        })183184        it("should support reading arrays", () => {185            const nextState = produce(186                baseState,187                s => {188                    s.anArray.slice()189                },190                listener191            )192            expect(nextState.anArray).toBe(baseState.anArray)193            expect(nextState).toBe(baseState)194        })195196        it("should support changing arrays", () => {197            const nextState = produce(198                baseState,199                s => {200                    s.anArray[3] = true201                },202                listener203            )204            expect(nextState).not.toBe(baseState)205            expect(nextState.anArray).not.toBe(baseState.anArray)206            expect(nextState.anArray[3]).toEqual(true)207        })208209        it("should support changing arrays - 2", () => {210            const nextState = produce(211                baseState,212                s => {213                    s.anArray.splice(1, 1, "a", "b")214                },215                listener216            )217            expect(nextState).not.toBe(baseState)218            expect(nextState.anArray).not.toBe(baseState.anArray)219220            expect(enumerableOnly(nextState.anArray)).toEqual([221                3,222                "a",223                "b",224                {c: 3},225                1226            ])227        })228229        it("can delete array items", () => {230            const nextState = produce(231                baseState,232                s => {233                    s.anArray.length = 3234                },235                listener236            )237            expect(nextState).not.toBe(baseState)238            expect(nextState.anObject).toBe(baseState.anObject)239            expect(nextState.anArray).not.toBe(baseState.anArray)240            expect(nextState.anArray).toEqual([3, 2, {c: 3}])241        })242243        it("should support sorting arrays", () => {244            const nextState = produce(245                baseState,246                s => {247                    s.anArray[2].c = 4248                    s.anArray.sort()249                    s.anArray[3].c = 5250                },251                listener252            )253            expect(nextState).not.toBe(baseState)254            expect(nextState.anArray).not.toBe(baseState.anArray)255            expect(enumerableOnly(nextState.anArray)).toEqual([1, 2, 3, {c: 5}])256        })257258        it("should expose property descriptors", () => {259            const nextState = produce(260                [],261                s => {262                    expect(Object.getOwnPropertyDescriptor(s, 0)).toBe(263                        undefined264                    )265                    s.unshift("x")266                    expect(Object.getOwnPropertyDescriptor(s, 0)).toEqual({267                        configurable: true,268                        enumerable: true,269                        value: "x",270                        writable: true271                    })272                    expect(s.length).toBe(1)273                    expect(s[0] === "x").toBe(true)274                },275                listener276            )277            expect(nextState).toEqual(["x"])278            expect(Object.getOwnPropertyDescriptor(nextState, 0)).toEqual({279                configurable: !freeze,280                enumerable: true,281                value: "x",282                writable: !freeze283            })284        })285286        it("should support sorting arrays - 2", () => {287            const nextState = produce(288                baseState,289                s => {290                    s.anArray.unshift("x")291                    s.anArray[3].c = 4292                    s.anArray.sort()293                    s.anArray[3].c = 5294                    s.anArray.unshift("y")295                },296                listener297            )298            expect(nextState).not.toBe(baseState)299            expect(nextState.anArray).not.toBe(baseState.anArray)300            expect(enumerableOnly(nextState.anArray)).toEqual([301                "y",302                1,303                2,304                3,305                {c: 5},306                "x"307            ])308        })309310        it("should updating inside arrays", () => {311            const nextState = produce(312                baseState,313                s => {314                    s.anArray[2].test = true315                },316                listener317            )318            expect(nextState).not.toBe(baseState)319            expect(nextState.anArray).not.toBe(baseState.anArray)320            expect(nextState.anArray).toEqual([3, 2, {c: 3, test: true}, 1])321        })322323        it("reusing object should work", () => {324            const nextState = produce(325                baseState,326                s => {327                    const obj = s.anObject328                    delete s.anObject329                    s.messy = obj330                },331                listener332            )333            expect(nextState).not.toBe(baseState)334            expect(nextState.anArray).toBe(baseState.anArray)335            expect(enumerableOnly(nextState)).toEqual({336                anArray: [3, 2, {c: 3}, 1],337                aProp: "hi",338                messy: {339                    nested: {340                        yummie: true341                    },342                    coffee: false343                }344            })345            expect(nextState.messy.nested).toBe(baseState.anObject.nested)346        })347348        it("refs should be transparent", () => {349            const nextState = produce(350                baseState,351                s => {352                    const obj = s.anObject353                    s.aProp = "hello"354                    delete s.anObject355                    obj.coffee = true356                    s.messy = obj357                },358                listener359            )360            expect(nextState).not.toBe(baseState)361            expect(nextState.anArray).toBe(baseState.anArray)362            expect(enumerableOnly(nextState)).toEqual({363                anArray: [3, 2, {c: 3}, 1],364                aProp: "hello",365                messy: {366                    nested: {367                        yummie: true368                    },369                    coffee: true370                }371            })372            expect(nextState.messy.nested).toBe(baseState.anObject.nested)373        })374375        it("should allow setting to undefined a defined draft property", () => {376            const nextState = produce(377                baseState,378                s => {379                    s.aProp = undefined380                },381                listener382            )383            expect(nextState).not.toBe(baseState)384            expect(baseState.aProp).toBe("hi")385            expect(nextState.aProp).toBe(undefined)386        })387388        // ES implementation does't protect against all outside modifications, just some..389        if (useProxies) {390            it("should revoke the proxy of the baseState after immer function is executed", () => {391                let proxy392                const nextState = produce(393                    baseState,394                    s => {395                        proxy = s396                        s.aProp = "hello"397                    },398                    listener399                )400                expect(nextState).not.toBe(baseState)401                expect(baseState.aProp).toBe("hi")402                expect(nextState.aProp).toBe("hello")403404                expect(() => {405                    proxy.aProp = "Hallo"406                }).toThrowError(/revoked/)407                expect(() => {408                    const aProp = proxy.aProp409                }).toThrowError(/revoked/)410411                expect(nextState).not.toBe(baseState)412                expect(baseState.aProp).toBe("hi")413                expect(nextState.aProp).toBe("hello")414            })415        }416417        it("should revoke the proxy of the baseState after immer function is executed - 2", () => {418            let proxy419            const nextState = produce(420                baseState,421                s => {422                    proxy = s.anObject423                },424                listener425            )426            expect(nextState).toBe(baseState)427            expect(() => {428                // In ES5 implemenation only protects existing props, but alas..429                proxy.coffee = "Hallo"430            }).toThrowError(/revoked/)431            expect(() => {432                const test = proxy.coffee433            }).toThrowError(/revoked/)434        })435436        it("should reflect all changes made in the draft immediately", () => {437            produce(438                baseState,439                draft => {440                    draft.anArray[0] = 5441                    draft.anArray.unshift("test")442                    // sliced here; jest will also compare non-enumerable keys, which would include the immer Symbols443                    expect(draft.anArray.slice()).toMatchObject([444                        "test",445                        5,446                        2,447                        {c: 3},448                        1449                    ])450                    draft.stuffz = "coffee"451                    expect(draft.stuffz).toBe("coffee")452                },453                listener454            )455        })456457        it("should be able to get property descriptors from objects", () => {458            produce(459                {a: 1},460                draft => {461                    expect("a" in draft).toBe(true)462                    expect("b" in draft).toBe(false)463                    expect(464                        Reflect.ownKeys(draft).filter(465                            x => typeof x === "string"466                        )467                    ).toEqual(["a"])468469                    expect(470                        Object.getOwnPropertyDescriptor(draft, "a")471                    ).toMatchObject({472                        configurable: true,473                        enumerable: true474                    })475                    draft.a = 2476                    expect(477                        Object.getOwnPropertyDescriptor(draft, "a")478                    ).toMatchObject({479                        configurable: true,480                        enumerable: true481                    })482                    expect(483                        Object.getOwnPropertyDescriptor(draft, "b")484                    ).toBeUndefined()485                    draft.b = 2486                    expect(487                        Object.getOwnPropertyDescriptor(draft, "b")488                    ).toMatchObject({489                        configurable: true,490                        enumerable: true491                    })492                    expect("a" in draft).toBe(true)493                    expect("b" in draft).toBe(true)494                    expect(495                        Reflect.ownKeys(draft).filter(496                            x => typeof x === "string"497                        )498                    ).toEqual(["a", "b"])499                },500                listener501            )502        })503504        it("should be able to get property descriptors from arrays", () => {505            produce(506                [1],507                draft => {508                    expect(0 in draft).toBe(true)509                    expect(1 in draft).toBe(false)510                    expect("0" in draft).toBe(true)511                    expect("1" in draft).toBe(false)512                    expect(length in draft).toBe(true)513                    expect(514                        Reflect.ownKeys(draft).filter(515                            x => typeof x === "string"516                        )517                    ).toEqual(["0", "length"])518519                    expect(520                        Object.getOwnPropertyDescriptor(draft, "length")521                    ).toMatchObject({522                        configurable: false,523                        enumerable: false524                    })525                    draft[0] = 2526                    expect(527                        Object.getOwnPropertyDescriptor(draft, 0)528                    ).toMatchObject({529                        configurable: true,530                        enumerable: true531                    })532                    expect(533                        Object.getOwnPropertyDescriptor(draft, 0)534                    ).toMatchObject({535                        configurable: true,536                        enumerable: true537                    })538                    expect(539                        Object.getOwnPropertyDescriptor(draft, 1)540                    ).toBeUndefined()541                    draft[1] = 2542                    expect(543                        Object.getOwnPropertyDescriptor(draft, 1)544                    ).toMatchObject({545                        configurable: true,546                        enumerable: true547                    })548                    expect(549                        Reflect.ownKeys(draft).filter(550                            x => typeof x === "string"551                        )552                    ).toEqual(["0", "1", "length"])553                },554                listener555            )556        })557558        if (useProxies === true) {559            it("should not be possible to set property descriptors", () => {560                expect(() => {561                    produce(562                        {},563                        draft => {564                            Object.defineProperty(draft, "xx", {565                                enumerable: true,566                                writeable: true,567                                value: 2568                            })569                        },570                        listener571                    )572                }).toThrowError(/not support/)573            })574575            it("should not be possible to add properties to arrays", () => {576                expect(() => {577                    produce([], d => {578                        d.x = 3579                    })580                }).toThrow(581                    "Immer does not support setting non-numeric properties on arrays"582                )583            })584585            it("should not be possible to remove properties from arrays", () => {586                expect(() => {587                    const base = []588                    base.x = 7589                    produce(base, d => {590                        delete d.x591                    })592                }).toThrow(593                    "Immer does not support deleting properties from arrays"594                )595            })596        }597598        it("non-numeric array properties will be lost", () => {599            const base = []600            base.x = 7601            const next = produce(base, d => {602                d.push(3)603            })604            expect(next.x).toBe(undefined)605        })606607        it("should not throw error, see #53 - 1", () => {608            const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}609            const result = produce(610                base,611                draft => {612                    draft.arr = draft.arr.filter(item => item.count > 2)613                },614                listener615            )616            expect(result.arr[0].count).toEqual(3)617            expect(result).toEqual({618                arr: [{count: 3}]619            })620            expect(result.arr[0]).toBe(base.arr[2])621        })622623        it("should not throw error, see #53 - 2", () => {624            const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}625            const result = produce(626                base,627                draft => {628                    draft.newArr = draft.arr.filter(item => item.count > 2)629                },630                listener631            )632            expect(result.newArr[0].count).toEqual(3)633            expect(result.arr).toBe(base.arr)634            expect(result).toEqual({635                arr: [636                    {637                        count: 1638                    },639                    {640                        count: 2641                    },642                    {643                        count: 3644                    }645                ],646                newArr: [647                    {648                        count: 3649                    }650                ]651            })652            expect(result.newArr[0]).toBe(base.arr[2])653            expect(result.arr[2]).toBe(base.arr[2])654        })655656        it("should not throw error, see #53 - 3", () => {657            const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}658            const result = produce(659                base,660                draft => {661                    draft.newArr = draft.arr.filter(item => item.count > 2)662                    delete draft.arr663                },664                listener665            )666            expect(result.newArr[0].count).toEqual(3)667            expect(result).toEqual({668                newArr: [{count: 3}]669            })670            expect(result.newArr[0]).toBe(base.arr[2])671        })672673        it("should not throw error, see #53 - 4", () => {674            const base = {bear: {age: 10}}675            const result = produce(676                base,677                draft => {678                    draft.bear.legs = 4679                    draft.room = {elephant: {kiddo: draft.bear}}680                },681                listener682            )683            expect(result).toEqual({684                bear: {age: 10, legs: 4},685                room: {elephant: {kiddo: {age: 10, legs: 4}}}686            })687688            const result2 = produce(result, draft => {689                draft.bear.age = 11690                draft.room.elephant.kiddo.legs = 5691            })692            expect(result2).toEqual({693                bear: {age: 11, legs: 4},694                room: {elephant: {kiddo: {age: 10, legs: 5}}}695            })696        })697698        it("should not throw error #78", () => {699            const base = {700                arr: [701                    {702                        id: "100",703                        arr: [{id: "1", no: 1}, {id: "2", no: 2}]704                    },705                    {id: "3", no: 3}706                ]707            }708            const result = produce(base, draft => {709                const base = draft.arr[0]710                draft.arr = draft.arr.slice(0, 1)711                const newArr = {712                    id: "101",713                    arr: [base, {id: "3"}]714                }715                draft.arr.splice(0, 1, newArr)716                draft.arr[0].arr[1].no = 3717            })718            expect(result).toEqual({719                arr: [720                    {721                        id: "101",722                        arr: [723                            {724                                id: "100",725                                arr: [{id: "1", no: 1}, {id: "2", no: 2}]726                            },727                            {id: "3", no: 3}728                        ]729                    }730                ]731            })732        })733734        it("should not throw error #78 - 2", () => {735            const base = {736                arr: [{count: 1}, {count: 2}]737            }738            const result = produce(base, draft => {739                draft.arr = [draft.arr[1]]740                draft.arr[0].count = 1741            })742            expect(result).toEqual({743                arr: [{count: 1}]744            })745        })746747        it("should handle constructor correctly", () => {748            const base = {749                arr: new Array(),750                obj: new Object()751            }752            const result = produce(base, draft => {753                draft.arrConstructed = draft.arr.constructor(1)754                draft.objConstructed = draft.obj.constructor(1)755            })756            expect(result.arrConstructed).toEqual(new Array().constructor(1))757            expect(result.objConstructed).toEqual(new Object().constructor(1))758        })759760        it("should handle dates correctly", () => {761            const data = {date: new Date()}762            const next = produce(data, draft => {763                draft.x = true764            })765            expect(next).toEqual({x: true, date: data.date})766            expect(next.date).toBe(data.date)767            const next2 = produce(next, draft => {768                draft.date.setYear(2015)769            })770            // This still holds; because produce won't proxy Date objects771            // and the original is actually modified!772            expect(next2).toEqual({x: true, date: data.date})773            expect(next2.date).toBe(next.date)774            expect(next2.date).toBe(data.date)775            expect(next2).toBe(next)776        })777778        it("should handle equality correctly - 1", () => {779            const base = {780                y: 3 / 0,781                z: NaN782            }783            const next = produce(base, draft => {784                draft.y = 4 / 0785                draft.z = NaN786            })787            expect(next).toEqual(base)788            expect(next).toBe(base)789        })790791        it("should handle equality correctly - 2", () => {792            const base = {793                x: -0794            }795            const next = produce(base, draft => {796                draft.x = +1797            })798            expect(next).not.toEqual(base)799            expect(next).not.toBe(base)800        })801802        it("should handle nested immer calls correctly - 1", () => {803            const bear = {paw: {honey: true}}804            const next = produce(805                bear,806                draft => {807                    const paw2 = produce(bear.paw, draft => {808                        draft.honey = false809                    })810                    expect(paw2.honey).toBe(false)811                    expect(draft.paw.honey).toBe(true) // effects should not be visible outside812                },813                listener814            )815            expect(next.paw.honey).toBe(true)816            expect(next).toBe(bear)817        })818819        it("should handle nested immer calls correctly - 2", () => {820            const bear = {paw: {honey: true}}821            const next = produce(822                bear,823                draft => {824                    const paw2 = produce(bear.paw, draft => {825                        draft.honey = false826                    })827                    expect(paw2.honey).toBe(false)828                    expect(draft.paw.honey).toBe(true)829                    draft.paw = paw2830                    expect(draft.paw.honey).toBe(false)831                },832                listener833            )834            expect(next.paw.honey).toBe(false)835            expect(next).not.toBe(bear)836        })837838        it("should not try to change immutable data, see #66", () => {839            const user = require("./test-data")840841            const base = {}842            const next = produce(843                base,844                draft => {845                    draft.user = user846                },847                listener848            )849            expect(next.user).toBe(user)850            expect(next).not.toBe(base)851            expect(next.user).toEqual(user)852        })853854        it("should not try to change immutable data, see #66 - 2", () => {855            const user = deepFreeze(cloneDeep(require("./test-data")))856857            const base = {}858            const next = produce(859                base,860                draft => {861                    draft.user = user862                },863                listener864            )865            expect(next.user).toBe(user)866            expect(next).not.toBe(base)867            expect(next.user).toEqual(user)868        })869870        if (freeze)871            it("should freeze new data well", () => {872                const base = {}873                const next = produce(874                    base,875                    draft => {876                        draft.x = {y: [{z: true}]}877                    },878                    listener879                )880                expect(Object.isFrozen(next)).toBe(true)881                expect(Object.isFrozen(next.x)).toBe(true)882                expect(Object.isFrozen(next.x.y)).toBe(true)883                expect(Object.isFrozen(next.x.y[0].z)).toBe(true)884            })885886        it("should structurally share identical objects in the tree", () => {887            const base = {bear: {legs: 4}, eagle: {legs: 3}}888            const next = produce(889                base,890                draft => {891                    const animal = draft.bear892                    animal.legs = animal.legs + 1893                    draft.bear = animal894                    draft.eagle = animal895                    draft.cow = animal896                    draft.kiddo = animal897                },898                listener899            )900            expect(next).toEqual({901                bear: {legs: 5},902                eagle: {legs: 5},903                cow: {legs: 5},904                kiddo: {legs: 5}905            })906            expect(next.bear).toBe(next.cow)907            expect(next.kiddo).toBe(next.cow)908        })909910        if (useProxies)911            it("should not allow changing prototype", () => {912                produce(913                    {},914                    draft => {915                        expect(() =>916                            Object.setPrototypeOf(draft, Array)917                        ).toThrow(/does not support `setPrototype/)918                    },919                    listener920                )921            })922923        it("'in' should work", () => {924            produce(925                createBaseState(),926                draft => {927                    expect("anArray" in draft).toBe(true)928                    expect(Reflect.has(draft, "anArray")).toBe(true)929930                    expect("bla" in draft).toBe(false)931                    expect(Reflect.has(draft, "bla")).toBe(false)932933                    expect(0 in draft.anArray).toBe(true)934                    expect("0" in draft.anArray).toBe(true)935                    expect(Reflect.has(draft.anArray, 0)).toBe(true)936                    expect(Reflect.has(draft.anArray, "0")).toBe(true)937938                    expect(17 in draft.anArray).toBe(false)939                    expect("17" in draft.anArray).toBe(false)940                    expect(Reflect.has(draft.anArray, 17)).toBe(false)941                    expect(Reflect.has(draft.anArray, "17")).toBe(false)942                },943                listener944            )945        })946947        it("'this' should work - 1", () => {948            const base = {x: 3}949            const next1 = produce(950                base,951                function() {952                    this.x = 4953                },954                listener955            )956            expect(next1).not.toBe(base)957            expect(next1.x).toBe(4)958        })959960        it("'this' should work - 1", () => {961            const base = {x: 3}962            const incrementor = produce(function() {963                this.x = 4964            })965            const next1 = incrementor(base)966            expect(next1).not.toBe(base)967            expect(next1.x).toBe(4)968        })969970        it("issue #89", () => {971            const state = {972                users: {973                    existingID: {974                        loading: false,975                        value: "val"976                    }977                }978            }979            const nextState = produce(980                state,981                draft => {982                    draft.users.existingID = {983                        ...draft.users.existingID,984                        loading: true985                    }986                },987                listener988            )989            expect(nextState).toEqual({990                users: {991                    existingID: {992                        loading: true,993                        value: "val"994                    }995                }996            })997        })998999        it("processes with lodash.set", () => {1000            const base = [{id: 1, a: 1}]1001            const result = produce(1002                base,1003                draft => {1004                    lodash.set(draft, "[0].a", 2)1005                },1006                listener1007            )1008            expect(base[0].a).toEqual(1)1009            expect(result[0].a).toEqual(2)1010        })10111012        it("processes with lodash.find", () => {1013            const base = [{id: 1, a: 1}]1014            const result = produce(1015                base,1016                draft => {1017                    const obj1 = lodash.find(draft, {id: 1})1018                    lodash.set(obj1, "a", 2)1019                },1020                listener1021            )1022            expect(base[0].a).toEqual(1)1023            expect(result[0].a).toEqual(2)1024        })10251026        it("can produce from no state", () => {1027            expect(1028                produce(1029                    3,1030                    draft => {1031                        expect(draft).toBe(3)1032                        return 51033                    },1034                    listener1035                )1036            ).toBe(5)1037        })10381039        it("can return something new ", () => {1040            const base = {x: 3}1041            const res = produce(1042                base,1043                draft => {1044                    return {x: draft.x + 1}1045                },1046                listener1047            )1048            expect(res).not.toBe(base)1049            expect(res).toEqual({x: 4})1050        })10511052        it("can return the draft new ", () => {1053            const base = {x: 3}1054            const res = produce(1055                base,1056                draft => {1057                    draft.x = 41058                    return draft1059                },1060                listener1061            )1062            expect(res).not.toBe(base)1063            expect(res).toEqual({x: 4})1064        })10651066        it("should throw if modifying the draft and returning something new", () => {1067            const base = {x: 3}1068            expect(() => {1069                produce(1070                    base,1071                    draft => {1072                        draft.x = 41073                        return {x: 5}1074                    },1075                    listener1076                )1077            }).toThrow(/An immer producer returned a new value/)1078        })10791080        it("should fix #116", () => {1081            const nextState = produce(1082                [1, 2, 3],1083                s => {1084                    s.pop()1085                    s.push(100)1086                },1087                listener1088            )1089            expect(nextState).toEqual([1, 2, 100])1090        })10911092        it("should fix #117", () => {1093            const reducer = (state, action) =>1094                produce(1095                    state,1096                    draft => {1097                        switch (action.type) {1098                            case "SET_STARTING_DOTS":1099                                return draft.availableStartingDots.map(a => a)1100                            default:1101                                break1102                        }1103                    },1104                    listener1105                )1106            const base = {1107                availableStartingDots: [1108                    {dots: 4, count: 1},1109                    {dots: 3, count: 2},1110                    {dots: 2, count: 3},1111                    {dots: 1, count: 4}1112                ]1113            }1114            const next = reducer(base, {type: "SET_STARTING_DOTS"})1115            expect(next).toEqual(base.availableStartingDots)1116            expect(next).not.toBe(base.availableStartingDots)1117        })11181119        it("should fix #117 - 2", () => {1120            const reducer = (state, action) =>1121                produce(1122                    state,1123                    draft => {1124                        switch (action.type) {1125                            case "SET_STARTING_DOTS":1126                                return {1127                                    dots: draft.availableStartingDots.map(1128                                        a => a1129                                    )1130                                }1131                            default:1132                                break1133                        }1134                    },1135                    listener1136                )1137            const base = {1138                availableStartingDots: [1139                    {dots: 4, count: 1},1140                    {dots: 3, count: 2},1141                    {dots: 2, count: 3},1142                    {dots: 1, count: 4}1143                ]1144            }1145            const next = reducer(base, {type: "SET_STARTING_DOTS"})1146            expect(next).toEqual({dots: base.availableStartingDots})1147        })11481149        it("should return an unmodified primitive baseState (#148)", () => {1150            const baseState = "some string"1151            const nextState = produce(1152                baseState,1153                () => {1154                    /* no modification  */1155                },1156                listener1157            )1158            expect(nextState).toBe(baseState)1159        })11601161        it("should return an unmodified null baseState (#148)", () => {1162            const baseState = null1163            const nextState = produce(1164                baseState,1165                () => {1166                    /* no modification  */1167                },1168                listener1169            )1170            expect(nextState).toBe(baseState)1171        })11721173        it("should not detect noop assignments - 0", () => {1174            const baseState = {x: {y: 3}}1175            const nextState = produce(1176                baseState,1177                d => {1178                    const a = d.x1179                    d.x = a1180                },1181                listener1182            )1183            expect(nextState).toBe(baseState)1184        })11851186        it("should not detect noop assignments - 1", () => {1187            const baseState = {x: {y: 3}}1188            const nextState = produce(1189                baseState,1190                d => {1191                    const a = d.x1192                    d.x = 41193                    d.x = a1194                },1195                listener1196            )1197            // Ideally, this should actually be the same instances1198            // but this would be pretty expensive to detect,1199            // so we don't atm1200            expect(nextState).not.toBe(baseState)1201        })12021203        it("should not detect noop assignments - 2", () => {1204            const baseState = {x: {y: 3}}1205            const nextState = produce(1206                baseState,1207                d => {1208                    const a = d.x1209                    const stuff = a.y + 31210                    d.x = 41211                    d.x = a1212                },1213                listener1214            )1215            // Ideally, this should actually be the same instances1216            // but this would be pretty expensive to detect,1217            // so we don't atm1218            expect(nextState).not.toBe(baseState)1219        })12201221        it("should not detect noop assignments - 3", () => {1222            const baseState = {x: 3}1223            const nextState = produce(1224                baseState,1225                d => {1226                    d.x = 31227                },1228                listener1229            )1230            expect(nextState).toBe(baseState)1231        })12321233        it("should not detect noop assignments - 4", () => {1234            const baseState = {x: 3}1235            const nextState = produce(1236                baseState,1237                d => {1238                    d.x = 41239                    d.x = 31240                },1241                listener1242            )1243            // Ideally, this should actually be the same instances1244            // but this would be pretty expensive to detect,1245            // so we don't atm1246            expect(nextState).not.toBe(baseState)1247        })12481249        it("immer should have no dependencies", () => {1250            expect(require("../package.json").dependencies).toEqual(undefined)1251        })12521253        it("#174", () => {1254            const nextState = produce([1, 2, 3], s => {1255                s.pop()1256                s.push(100)1257            })1258            expect(nextState).toEqual([1, 2, 100])1259        })12601261        it("#195 should be able to find items", () => {1262            const state = {1263                items: [1264                    {1265                        id: 0,1266                        task: "drink milk"1267                    },1268                    {id: 1, task: "eat cookie"}1269                ]1270            }1271            produce(state, draft => {1272                expect(draft.items.find(({id}) => id === 1).task).toBe(1273                    "eat cookie"1274                )1275            })1276        })12771278        it("cannot return and produce undefined!", () => {1279            const base = 31280            expect(produce(base, () => 4)).toBe(4)1281            expect(produce(base, () => null)).toBe(null)1282            expect(produce(base, () => undefined)).toBe(3)1283            expect(produce(base, () => {})).toBe(3)1284            expect(produce(base, () => nothing)).toBe(undefined)12851286            expect(produce({}, () => undefined)).toEqual({})1287            expect(produce({}, () => nothing)).toBe(undefined)1288            expect(produce(3, () => nothing)).toBe(undefined)12891290            expect(produce(() => undefined)({})).toEqual({})1291            expect(produce(() => nothing)({})).toBe(undefined)1292            expect(produce(() => nothing)(3)).toBe(undefined)1293        })12941295        afterEach(() => {1296            expect(baseState).toBe(origBaseState)1297            expect(baseState).toEqual(createBaseState())1298        })12991300        function createBaseState() {1301            const data = {1302                anArray: [3, 2, {c: 3}, 1],1303                aProp: "hi",1304                anObject: {1305                    nested: {1306                        yummie: true1307                    },1308                    coffee: false1309                }1310            }1311            return freeze ? deepFreeze(data) : data1312        }1313    })1314}13151316function enumerableOnly(x) {1317    // this can be done better...1318    return JSON.parse(JSON.stringify(x))1319}