Skip to content
Myr

base.js

This file

Path

__tests__/base.js

2,456 lines · 61.7 KiB · JavaScript

Last changed by

Demonstrate possible regressionMichel Weststratebefore adoption8e465ab

History of this file

Take it

1"use strict"2import {3	Immer,4	nothing,5	original,6	isDraft,7	immerable,8	enablePatches,9	enableMapSet10} from "../src/immer"11import {each, shallowCopy, DRAFT_STATE} from "../src/internal"12import deepFreeze from "deep-freeze"13import * as lodash from "lodash"1415enablePatches()16enableMapSet()1718jest.setTimeout(1000)1920const isProd = process.env.NODE_ENV === "production"2122test("immer should have no dependencies", () => {23	expect(require("../package.json").dependencies).toBeUndefined()24})2526for (const autoFreeze of [true, false]) {27	for (const useStrictShallowCopy of [true, false]) {28		for (const useListener of [true, false]) {29			const name = `${autoFreeze ? "auto-freeze=true" : "auto-freeze=false"}:${30				useStrictShallowCopy ? "shallow-copy=true" : "shallow-copy=false"31			}:${useListener ? "use-listener=true" : "use-listener=false"}`32			runBaseTest(name, autoFreeze, useStrictShallowCopy, useListener)33		}34	}35}3637class Foo {}3839function runBaseTest(name, autoFreeze, useStrictShallowCopy, useListener) {40	const listener = useListener ? function() {} : undefined41	const {produce, produceWithPatches} = createPatchedImmer({42		autoFreeze,43		useStrictShallowCopy44	})4546	// When `useListener` is true, append a function to the arguments of every47	// uncurried `produce` call in every test. This makes tests easier to read.48	function createPatchedImmer(options) {49		const immer = new Immer(options)5051		const {produce} = immer52		immer.produce = function(...args) {53			return typeof args[1] === "function" && args.length < 354				? produce(...args, listener)55				: produce(...args)56		}5758		return immer59	}6061	describe(`base functionality - ${name}`, () => {62		let baseState63		let origBaseState6465		beforeEach(() => {66			origBaseState = baseState = createBaseState()67		})6869		it("returns the original state when no changes are made", () => {70			const nextState = produce(baseState, s => {71				expect(s.aProp).toBe("hi")72				expect(s.anObject.nested).toMatchObject({yummie: true})73			})74			expect(nextState).toBe(baseState)75		})7677		it("does structural sharing", () => {78			const random = Math.random()79			const nextState = produce(baseState, s => {80				s.aProp = random81			})82			expect(nextState).not.toBe(baseState)83			expect(nextState.aProp).toBe(random)84			expect(nextState.nested).toBe(baseState.nested)85		})8687		it("deep change bubbles up", () => {88			const nextState = produce(baseState, s => {89				s.anObject.nested.yummie = false90			})91			expect(nextState).not.toBe(baseState)92			expect(nextState.anObject).not.toBe(baseState.anObject)93			expect(baseState.anObject.nested.yummie).toBe(true)94			expect(nextState.anObject.nested.yummie).toBe(false)95			expect(nextState.anArray).toBe(baseState.anArray)96		})9798		it("can add props", () => {99			const nextState = produce(baseState, s => {100				s.anObject.cookie = {tasty: true}101			})102			expect(nextState).not.toBe(baseState)103			expect(nextState.anObject).not.toBe(baseState.anObject)104			expect(nextState.anObject.nested).toBe(baseState.anObject.nested)105			expect(nextState.anObject.cookie).toEqual({tasty: true})106		})107108		it("can delete props", () => {109			const nextState = produce(baseState, s => {110				delete s.anObject.nested111				delete s.nonexisting112			})113			expect(nextState).not.toBe(baseState)114			expect(nextState.anObject).not.toBe(baseState.anObject)115			expect(nextState.anObject.nested).toBe(undefined)116		})117118		it("can delete props - 2", () => {119			const nextState = produce(baseState, s => {120				delete s.nonexisting121			})122			expect(nextState).toBe(baseState)123		})124125		// Found by: https://github.com/mweststrate/immer/pull/267126		it("can delete props added in the producer", () => {127			const nextState = produce(baseState, s => {128				s.anObject.test = true129				delete s.anObject.test130			})131			expect(nextState).not.toBe(baseState)132			expect(nextState).toEqual(baseState)133		})134135		// Found by: https://github.com/mweststrate/immer/issues/328136		it("can set a property that was just deleted", () => {137			const baseState = {a: 1}138			const nextState = produce(baseState, s => {139				delete s.a140				s.a = 2141			})142			expect(nextState.a).toBe(2)143		})144145		it("can set a property to its original value after deleting it", () => {146			const baseState = {a: {b: 1}}147			const nextState = produce(baseState, s => {148				const a = s.a149				delete s.a150				s.a = a151			})152			expect(nextState).not.toBe(baseState)153			expect(nextState).toEqual(baseState)154		})155156		it("can get property descriptors", () => {157			const getDescriptor = Object.getOwnPropertyDescriptor158			const baseState = deepFreeze([{a: 1}])159			produce(baseState, arr => {160				const obj = arr[0]161				const desc = {162					configurable: true,163					enumerable: true,164					writable: true165				}166167				// Known property168				expect(getDescriptor(obj, "a")).toMatchObject(desc)169				expect(getDescriptor(arr, 0)).toMatchObject(desc)170171				// Deleted property172				delete obj.a173				arr.pop()174				expect(getDescriptor(obj, "a")).toBeUndefined()175				expect(getDescriptor(arr, 0)).toBeUndefined()176177				// Unknown property178				expect(getDescriptor(obj, "b")).toBeUndefined()179				expect(getDescriptor(arr, 100)).toBeUndefined()180181				// Added property182				obj.b = 2183				arr[100] = 1184				expect(getDescriptor(obj, "b")).toBeDefined()185				expect(getDescriptor(arr, 100)).toBeDefined()186			})187		})188189		describe("array drafts", () => {190			it("supports Array.isArray()", () => {191				const nextState = produce(baseState, s => {192					expect(Array.isArray(s.anArray)).toBeTruthy()193					s.anArray.push(1)194				})195				expect(Array.isArray(nextState.anArray)).toBeTruthy()196			})197198			it("supports index access", () => {199				const value = baseState.anArray[0]200				const nextState = produce(baseState, s => {201					expect(s.anArray[0]).toBe(value)202				})203				expect(nextState).toBe(baseState)204			})205206			it("supports iteration", () => {207				const base = [208					{id: 1, a: 1},209					{id: 2, a: 1}210				]211				const findById = (collection, id) => {212					for (const item of collection) {213						if (item.id === id) return item214					}215					return null216				}217				const result = produce(base, draft => {218					const obj1 = findById(draft, 1)219					const obj2 = findById(draft, 2)220					obj1.a = 2221					obj2.a = 2222				})223				expect(result[0].a).toEqual(2)224				expect(result[1].a).toEqual(2)225			})226227			it("can assign an index via bracket notation", () => {228				const nextState = produce(baseState, s => {229					s.anArray[3] = true230				})231				expect(nextState).not.toBe(baseState)232				expect(nextState.anArray).not.toBe(baseState.anArray)233				expect(nextState.anArray[3]).toEqual(true)234			})235236			it("can use splice() to both add and remove items", () => {237				const nextState = produce(baseState, s => {238					s.anArray.splice(1, 1, "a", "b")239				})240				expect(nextState.anArray).not.toBe(baseState.anArray)241				expect(nextState.anArray[1]).toBe("a")242				expect(nextState.anArray[2]).toBe("b")243			})244245			it("can truncate via the length property", () => {246				const baseLength = baseState.anArray.length247				const nextState = produce(baseState, s => {248					s.anArray.length = baseLength - 1249				})250				expect(nextState.anArray).not.toBe(baseState.anArray)251				expect(nextState.anArray.length).toBe(baseLength - 1)252			})253254			it("can extend via the length property", () => {255				const baseLength = baseState.anArray.length256				const nextState = produce(baseState, s => {257					s.anArray.length = baseLength + 1258				})259				expect(nextState.anArray).not.toBe(baseState.anArray)260				expect(nextState.anArray.length).toBe(baseLength + 1)261			})262263			// Reported here: https://github.com/mweststrate/immer/issues/116264			it("can pop then push", () => {265				const nextState = produce([1, 2, 3], s => {266					s.pop()267					s.push(100)268				})269				expect(nextState).toEqual([1, 2, 100])270			})271272			it("can be sorted", () => {273				const baseState = [3, 1, 2]274				const nextState = produce(baseState, s => {275					s.sort()276				})277				expect(nextState).not.toBe(baseState)278				expect(nextState).toEqual([1, 2, 3])279			})280281			it("supports modifying nested objects", () => {282				const baseState = [{a: 1}, {}]283				const nextState = produce(baseState, s => {284					s[0].a++285					s[1].a = 0286				})287				expect(nextState).not.toBe(baseState)288				expect(nextState[0].a).toBe(2)289				expect(nextState[1].a).toBe(0)290			})291292			it("never preserves non-numeric properties", () => {293				const baseState = []294				baseState.x = 7295				const nextState = produce(baseState, s => {296					s.push(3)297				})298				expect("x" in nextState).toBeFalsy()299			})300301			if (!global.USES_BUILD) {302				it("throws when a non-numeric property is added", () => {303					expect(() => {304						produce([], d => {305							d.x = 3306						})307					}).toThrowErrorMatchingSnapshot()308				})309310				it("throws when a non-numeric property is deleted", () => {311					expect(() => {312						const baseState = []313						baseState.x = 7314						produce(baseState, d => {315							delete d.x316						})317					}).toThrowErrorMatchingSnapshot()318				})319			}320		})321322		describe("map drafts", () => {323			it("supports key access", () => {324				const value = baseState.aMap.get("jedi")325				const nextState = produce(baseState, s => {326					expect(s.aMap.get("jedi")).toEqual(value)327				})328				expect(nextState).toBe(baseState)329			})330331			it("supports key access for non-primitive keys", () => {332				const key = {prop: "val"}333				const base = new Map([[key, {id: 1, a: 1}]])334				const value = base.get(key)335				const nextState = produce(base, s => {336					expect(s.get(key)).toEqual(value)337				})338				expect(nextState).toBe(base)339			})340341			it("supports iteration", () => {342				const base = new Map([343					["first", {id: 1, a: 1}],344					["second", {id: 2, a: 1}]345				])346				const findById = (map, id) => {347					for (const [, item] of map) {348						if (item.id === id) return item349					}350					return null351				}352				const result = produce(base, draft => {353					const obj1 = findById(draft, 1)354					const obj2 = findById(draft, 2)355					obj1.a = 2356					obj2.a = 2357				})358				expect(result).not.toBe(base)359				expect(result.get("first").a).toEqual(2)360				expect(result.get("second").a).toEqual(2)361			})362363			it("supports 'entries'", () => {364				const base = new Map([365					["first", {id: 1, a: 1}],366					["second", {id: 2, a: 1}]367				])368				const findById = (map, id) => {369					for (const [, item] of map.entries()) {370						if (item.id === id) return item371					}372					return null373				}374				const result = produce(base, draft => {375					const obj1 = findById(draft, 1)376					const obj2 = findById(draft, 2)377					obj1.a = 2378					obj2.a = 2379				})380				expect(result).not.toBe(base)381				expect(result.get("first").a).toEqual(2)382				expect(result.get("second").a).toEqual(2)383			})384385			it("supports 'values'", () => {386				const base = new Map([387					["first", {id: 1, a: 1}],388					["second", {id: 2, a: 1}]389				])390				const findById = (map, id) => {391					for (const item of map.values()) {392						if (item.id === id) return item393					}394					return null395				}396				const result = produce(base, draft => {397					const obj1 = findById(draft, 1)398					const obj2 = findById(draft, 2)399					obj1.a = 2400					obj2.a = 2401				})402				expect(result).not.toBe(base)403				expect(result.get("first").a).toEqual(2)404				expect(result.get("second").a).toEqual(2)405			})406407			it("supports 'keys", () => {408				const base = new Map([409					["first", Symbol()],410					["second", Symbol()]411				])412				const result = produce(base, draft => {413					expect([...draft.keys()]).toEqual(["first", "second"])414					draft.set("third", Symbol())415					expect([...draft.keys()]).toEqual(["first", "second", "third"])416				})417			})418419			it("supports forEach", () => {420				const key1 = {prop: "val1"}421				const key2 = {prop: "val2"}422				const base = new Map([423					["first", {id: 1, a: 1}],424					["second", {id: 2, a: 1}],425					[key1, {id: 3, a: 1}],426					[key2, {id: 4, a: 1}]427				])428				const result = produce(base, draft => {429					let sum1 = 0430					draft.forEach(({a}) => {431						sum1 += a432					})433					expect(sum1).toBe(4)434					let sum2 = 0435					draft.get("first").a = 10436					draft.get("second").a = 20437					draft.get(key1).a = 30438					draft.get(key2).a = 40439					draft.forEach(({a}) => {440						sum2 += a441					})442					expect(sum2).toBe(100)443				})444				expect(result).not.toBe(base)445				expect(base.get("first").a).toEqual(1)446				expect(base.get("second").a).toEqual(1)447				expect(base.get(key1).a).toEqual(1)448				expect(base.get(key2).a).toEqual(1)449				expect(result.get("first").a).toEqual(10)450				expect(result.get("second").a).toEqual(20)451				expect(result.get(key1).a).toEqual(30)452				expect(result.get(key2).a).toEqual(40)453			})454455			it("supports forEach mutation", () => {456				const base = new Map([457					["first", {id: 1, a: 1}],458					["second", {id: 2, a: 1}]459				])460				const result = produce(base, draft => {461					draft.forEach(item => {462						item.a = 100463					})464				})465				expect(result).not.toBe(base)466				expect(result.get("first").a).toEqual(100)467				expect(result.get("second").a).toEqual(100)468			})469470			it("can assign by key", () => {471				const nextState = produce(baseState, s => {472					// Map.prototype.set should return the Map itself473					const res = s.aMap.set("force", true)474					if (!global.USES_BUILD) expect(res).toBe(s.aMap[DRAFT_STATE].draft_)475				})476				expect(nextState).not.toBe(baseState)477				expect(nextState.aMap).not.toBe(baseState.aMap)478				expect(nextState.aMap.get("force")).toEqual(true)479			})480481			it("can assign by a non-primitive key", () => {482				const key = {prop: "val"}483				const value = {id: 1, a: 1}484				const base = new Map([[key, value]])485				const nextState = produce(base, s => {486					s.set(key, true)487				})488				expect(nextState).not.toBe(base)489				expect(base.get(key)).toEqual(value)490				expect(nextState.get(key)).toEqual(true)491			})492493			it("state stays the same if the the same item is assigned by key", () => {494				const nextState = produce(baseState, s => {495					s.aMap.set("jediTotal", 42)496				})497				expect(nextState).toBe(baseState)498				expect(nextState.aMap).toBe(baseState.aMap)499			})500501			it("returns 'size'", () => {502				const nextState = produce(baseState, s => {503					s.aMap.set("newKey", true)504					expect(s.aMap.size).toBe(baseState.aMap.size + 1)505				})506				expect(nextState).not.toBe(baseState)507				expect(nextState.aMap).not.toBe(baseState.aMap)508				expect(nextState.aMap.get("newKey")).toEqual(true)509				expect(nextState.aMap.size).toEqual(baseState.aMap.size + 1)510			})511512			it("can use 'delete' to remove items", () => {513				const nextState = produce(baseState, s => {514					expect(s.aMap.has("jedi")).toBe(true)515					expect(s.aMap.delete("jedi")).toBe(true)516					expect(s.aMap.has("jedi")).toBe(false)517				})518				expect(nextState.aMap).not.toBe(baseState.aMap)519				expect(nextState.aMap.size).toBe(baseState.aMap.size - 1)520				expect(baseState.aMap.has("jedi")).toBe(true)521				expect(nextState.aMap.has("jedi")).toBe(false)522			})523524			it("can use 'clear' to remove items", () => {525				const nextState = produce(baseState, s => {526					expect(s.aMap.size).not.toBe(0)527					s.aMap.clear()528					expect(s.aMap.size).toBe(0)529				})530				expect(nextState.aMap).not.toBe(baseState.aMap)531				expect(baseState.aMap.size).not.toBe(0)532				expect(nextState.aMap.size).toBe(0)533			})534535			it("support 'has'", () => {536				const nextState = produce(baseState, s => {537					expect(s.aMap.has("newKey")).toBe(false)538					s.aMap.set("newKey", true)539					expect(s.aMap.has("newKey")).toBe(true)540				})541				expect(nextState).not.toBe(baseState)542				expect(nextState.aMap).not.toBe(baseState.aMap)543				expect(baseState.aMap.has("newKey")).toBe(false)544				expect(nextState.aMap.has("newKey")).toBe(true)545			})546547			it("supports nested maps", () => {548				const base = new Map([["first", new Map([["second", {prop: "test"}]])]])549				const result = produce(base, draft => {550					draft.get("first").get("second").prop = "test1"551				})552				expect(result).not.toBe(base)553				expect(result.get("first")).not.toBe(base.get("first"))554				expect(result.get("first").get("second")).not.toBe(555					base.get("first").get("second")556				)557				expect(base.get("first").get("second").prop).toBe("test")558				expect(result.get("first").get("second").prop).toBe("test1")559			})560561			it("treats void deletes as no-op", () => {562				const base = new Map([["x", 1]])563				const next = produce(base, d => {564					expect(d.delete("y")).toBe(false)565				})566				expect(next).toBe(base)567			})568569			it("revokes map proxies", () => {570				let m571				produce(baseState, s => {572					m = s.aMap573				})574				expect(() => m.get("x")).toThrowErrorMatchingSnapshot()575				expect(() => m.set("x", 3)).toThrowErrorMatchingSnapshot()576			})577578			it("does not draft map keys", () => {579				// anything else would be terribly confusing580				const key = {a: 1}581				const map = new Map([[key, 2]])582				const next = produce(map, d => {583					const dKey = Array.from(d.keys())[0]584					expect(isDraft(dKey)).toBe(false)585					expect(dKey).toBe(key)586					dKey.a += 1587					d.set(dKey, d.get(dKey) + 1)588					d.set(key, d.get(key) + 1)589					expect(d.get(key)).toBe(4)590					expect(key.a).toBe(2)591				})592				const entries = Array.from(next.entries())593				expect(entries).toEqual([[key, 4]])594				expect(entries[0][0]).toBe(key)595				expect(entries[0][0].a).toBe(2)596			})597598			it("does support instanceof Map", () => {599				const map = new Map()600				produce(map, d => {601					expect(d instanceof Map).toBeTruthy()602				})603			})604605			it("handles clear correctly", () => {606				const map = new Map([607					["a", 1],608					["c", 3]609				])610				const next = produce(map, draft => {611					draft.delete("a")612					draft.set("b", 2)613					draft.set("c", 4)614					draft.clear()615				})616				expect(next).toEqual(new Map())617			})618		})619620		describe("set drafts", () => {621			it("supports iteration", () => {622				const base = new Set([623					{id: 1, a: 1},624					{id: 2, a: 1}625				])626				const findById = (set, id) => {627					for (const item of set) {628						if (item.id === id) return item629					}630					return null631				}632				const result = produce(base, draft => {633					const obj1 = findById(draft, 1)634					const obj2 = findById(draft, 2)635					obj1.a = 2636					obj2.a = 2637				})638				expect(result).not.toBe(base)639				expect(base).toEqual(640					new Set([641						{id: 1, a: 1},642						{id: 2, a: 1}643					])644				)645				expect(result).toEqual(646					new Set([647						{id: 1, a: 2},648						{id: 2, a: 2}649					])650				)651			})652653			it("supports 'entries'", () => {654				const base = new Set([655					{id: 1, a: 1},656					{id: 2, a: 1}657				])658				const findById = (set, id) => {659					for (const [item1, item2] of set.entries()) {660						expect(item1).toBe(item2)661						if (item1.id === id) return item1662					}663					return null664				}665				const result = produce(base, draft => {666					const obj1 = findById(draft, 1)667					const obj2 = findById(draft, 2)668					obj1.a = 2669					obj2.a = 2670				})671				expect(result).not.toBe(base)672				expect(base).toEqual(673					new Set([674						{id: 1, a: 1},675						{id: 2, a: 1}676					])677				)678				expect(result).toEqual(679					new Set([680						{id: 1, a: 2},681						{id: 2, a: 2}682					])683				)684			})685686			it("supports 'values'", () => {687				const base = new Set([688					{id: 1, a: 1},689					{id: 2, a: 1}690				])691				const findById = (set, id) => {692					for (const item of set.values()) {693						if (item.id === id) return item694					}695					return null696				}697				const result = produce(base, draft => {698					const obj1 = findById(draft, 1)699					const obj2 = findById(draft, 2)700					obj1.a = 2701					obj2.a = 2702				})703				expect(result).not.toBe(base)704				expect(base).toEqual(705					new Set([706						{id: 1, a: 1},707						{id: 2, a: 1}708					])709				)710				expect(result).toEqual(711					new Set([712						{id: 1, a: 2},713						{id: 2, a: 2}714					])715				)716			})717718			it("supports 'keys'", () => {719				const base = new Set([720					{id: 1, a: 1},721					{id: 2, a: 1}722				])723				const findById = (set, id) => {724					for (const item of set.keys()) {725						if (item.id === id) return item726					}727					return null728				}729				const result = produce(base, draft => {730					const obj1 = findById(draft, 1)731					const obj2 = findById(draft, 2)732					obj1.a = 2733					obj2.a = 2734				})735				expect(result).not.toBe(base)736				expect(base).toEqual(737					new Set([738						{id: 1, a: 1},739						{id: 2, a: 1}740					])741				)742				expect(result).toEqual(743					new Set([744						{id: 1, a: 2},745						{id: 2, a: 2}746					])747				)748			})749750			it("supports forEach with mutation after reads", () => {751				const base = new Set([752					{id: 1, a: 1},753					{id: 2, a: 2}754				])755				const result = produce(base, draft => {756					let sum1 = 0757					draft.forEach(({a}) => {758						sum1 += a759					})760					expect(sum1).toBe(3)761					let sum2 = 0762					draft.forEach(item => {763						item.a += 10764						sum2 += item.a765					})766					expect(sum2).toBe(23)767				})768				expect(result).not.toBe(base)769				expect(base).toEqual(770					new Set([771						{id: 1, a: 1},772						{id: 2, a: 2}773					])774				)775				expect(result).toEqual(776					new Set([777						{id: 1, a: 11},778						{id: 2, a: 12}779					])780				)781			})782783			it("state stays the same if the same item is added", () => {784				const nextState = produce(baseState, s => {785					s.aSet.add("Luke")786				})787				expect(nextState).toBe(baseState)788				expect(nextState.aSet).toBe(baseState.aSet)789			})790791			it("can add new items", () => {792				const nextState = produce(baseState, s => {793					// Set.prototype.set should return the Set itself794					const res = s.aSet.add("force")795					if (!global.USES_BUILD) expect(res).toBe(s.aSet[DRAFT_STATE].draft_)796				})797				expect(nextState).not.toBe(baseState)798				expect(nextState.aSet).not.toBe(baseState.aSet)799				expect(baseState.aSet.has("force")).toBe(false)800				expect(nextState.aSet.has("force")).toBe(true)801			})802803			it("returns 'size'", () => {804				const nextState = produce(baseState, s => {805					s.aSet.add("newKey")806					expect(s.aSet.size).toBe(baseState.aSet.size + 1)807				})808				expect(nextState).not.toBe(baseState)809				expect(nextState.aSet).not.toBe(baseState.aSet)810				expect(nextState.aSet.has("newKey")).toBe(true)811				expect(nextState.aSet.size).toEqual(baseState.aSet.size + 1)812			})813814			it("can use 'delete' to remove items", () => {815				const nextState = produce(baseState, s => {816					expect(s.aSet.has("Luke")).toBe(true)817					expect(s.aSet.delete("Luke")).toBe(true)818					expect(s.aSet.delete("Luke")).toBe(false)819					expect(s.aSet.has("Luke")).toBe(false)820				})821				expect(nextState.aSet).not.toBe(baseState.aSet)822				expect(baseState.aSet.has("Luke")).toBe(true)823				expect(nextState.aSet.has("Luke")).toBe(false)824				expect(nextState.aSet.size).toBe(baseState.aSet.size - 1)825			})826827			it("can use 'clear' to remove items", () => {828				const nextState = produce(baseState, s => {829					expect(s.aSet.size).not.toBe(0)830					s.aSet.clear()831					expect(s.aSet.size).toBe(0)832				})833				expect(nextState.aSet).not.toBe(baseState.aSet)834				expect(baseState.aSet.size).not.toBe(0)835				expect(nextState.aSet.size).toBe(0)836			})837838			it("supports 'has'", () => {839				const nextState = produce(baseState, s => {840					expect(s.aSet.has("newKey")).toBe(false)841					s.aSet.add("newKey")842					expect(s.aSet.has("newKey")).toBe(true)843				})844				expect(nextState).not.toBe(baseState)845				expect(nextState.aSet).not.toBe(baseState.aSet)846				expect(baseState.aSet.has("newKey")).toBe(false)847				expect(nextState.aSet.has("newKey")).toBe(true)848			})849850			it("supports nested sets", () => {851				const base = new Set([new Set(["Serenity"])])852				const result = produce(base, draft => {853					draft.forEach(nestedItem => nestedItem.add("Firefly"))854				})855				expect(result).not.toBe(base)856				expect(base).toEqual(new Set([new Set(["Serenity"])]))857				expect(result).toEqual(new Set([new Set(["Serenity", "Firefly"])]))858			})859860			it("supports has / delete on elements from the original", () => {861				const obj = {}862				const set = new Set([obj])863				const next = produce(set, d => {864					expect(d.has(obj)).toBe(true)865					d.add(3)866					expect(d.has(obj)).toBe(true)867					d.delete(obj)868					expect(d.has(obj)).toBe(false)869				})870				expect(next).toEqual(new Set([3]))871			})872873			it("revokes sets", () => {874				let m875				produce(baseState, s => {876					m = s.aSet877				})878				expect(() => m.has("x")).toThrowErrorMatchingSnapshot()879				expect(() => m.add("x")).toThrowErrorMatchingSnapshot()880			})881882			it("does support instanceof Set", () => {883				const set = new Set()884				produce(set, d => {885					expect(d instanceof Set).toBeTruthy()886				})887			})888		})889890		it("supports `immerable` symbol on constructor", () => {891			class One {}892			One[immerable] = true893			const baseState = new One()894			const nextState = produce(baseState, draft => {895				expect(draft).not.toBe(baseState)896				draft.foo = true897			})898			expect(nextState).not.toBe(baseState)899			expect(nextState.foo).toBeTruthy()900		})901902		it("preserves symbol properties", () => {903			const test = Symbol("test")904			const baseState = {[test]: true}905			const nextState = produce(baseState, s => {906				expect(s[test]).toBeTruthy()907				s.foo = true908			})909			expect(nextState).toEqual({910				[test]: true,911				foo: true912			})913		})914915		if (!global.USES_BUILD)916			it("preserves non-enumerable properties", () => {917				const baseState = {}918				// Non-enumerable object property919				Object.defineProperty(baseState, "foo", {920					value: {a: 1},921					enumerable: false922				})923				// Non-enumerable primitive property924				Object.defineProperty(baseState, "bar", {925					value: 1,926					enumerable: false927				})928				// Non-enumerable primitive property that won't modified.929				Object.defineProperty(baseState, "baz", {930					value: 1,931					enumerable: false932				})933934				// When using Proxy, even non-enumerable keys will be copied if it's changed.935				const canReferNonEnumerableProperty = useStrictShallowCopy936				const nextState = produce(baseState, s => {937					if (canReferNonEnumerableProperty) expect(s.foo).toBeTruthy()938					if (useStrictShallowCopy) expect(isEnumerable(s, "foo")).toBeFalsy()939					if (canReferNonEnumerableProperty) s.bar++940					if (useStrictShallowCopy) expect(isEnumerable(s, "foo")).toBeFalsy()941					if (canReferNonEnumerableProperty) s.foo.a++942					if (useStrictShallowCopy) expect(isEnumerable(s, "foo")).toBeFalsy()943				})944				if (canReferNonEnumerableProperty) {945					expect(nextState.foo).toEqual({a: 2})946				}947				if (useStrictShallowCopy)948					expect(isEnumerable(nextState, "foo")).toBeFalsy()949				if (useStrictShallowCopy) expect(nextState.baz).toBeTruthy()950			})951952		it("can work with own computed props", () => {953			const baseState = {954				x: 1,955				get y() {956					return this.x957				},958				set y(v) {959					this.x = v960				}961			}962963			const nextState = produce(baseState, d => {964				expect(d.y).toBe(1)965				d.x = 2966				expect(d.x).toBe(2)967				expect(d.y).toBe(1) // this has been copied!968				d.y = 3969				expect(d.x).toBe(2)970			})971			expect(baseState.x).toBe(1)972			expect(baseState.y).toBe(1)973974			expect(nextState.x).toBe(2)975			expect(nextState.y).toBe(3)976			if (!autoFreeze) {977				nextState.y = 4 // decoupled now!978				expect(nextState.y).toBe(4)979				expect(nextState.x).toBe(2)980				expect(Object.getOwnPropertyDescriptor(nextState, "y").value).toBe(4)981			}982		})983984		it("can work with class with computed props", () => {985			class State {986				[immerable] = true987988				x = 1989990				set y(v) {991					this.x = v992				}993994				get y() {995					return this.x996				}997			}998999			const baseState = new State()10001001			const nextState = produce(baseState, d => {1002				expect(d.y).toBe(1)1003				d.y = 21004				expect(d.x).toBe(2)1005				expect(d.y).toBe(2)1006				expect(Object.getOwnPropertyDescriptor(d, "y")).toBeUndefined()1007			})1008			expect(baseState.x).toBe(1)1009			expect(baseState.y).toBe(1)10101011			expect(nextState.x).toBe(2)1012			expect(nextState.y).toBe(2)1013			expect(Object.getOwnPropertyDescriptor(nextState, "y")).toBeUndefined()1014		})10151016		it("allows inherited computed properties", () => {1017			const proto = {}1018			Object.defineProperty(proto, "foo", {1019				get() {1020					return this.bar1021				},1022				set(val) {1023					this.bar = val1024				}1025			})1026			proto[immerable] = true1027			const baseState = Object.create(proto)1028			produce(baseState, s => {1029				expect(s.bar).toBeUndefined()1030				s.foo = {}1031				expect(s.bar).toBeDefined()1032				expect(s.foo).toBe(s.bar)1033			})1034		})10351036		it("optimization: does not visit properties of new data structures if autofreeze is disabled and no drafts are unfinalized", () => {1037			const newData = {}1038			Object.defineProperty(newData, "x", {1039				enumerable: true,1040				get() {1041					throw new Error("visited!")1042				}1043			})10441045			const run = () =>1046				produce({}, d => {1047					d.data = newData1048				})1049			if (autoFreeze) {1050				expect(run).toThrow("visited!")1051			} else {1052				expect(run).not.toThrow("visited!")1053			}1054		})10551056		it("same optimization doesn't cause draft from nested producers to be unfinished", () => {1057			const base = {1058				y: 1,1059				child: {1060					x: 11061				}1062			}1063			const wrap = produce(draft => {1064				return {1065					wrapped: draft1066				}1067			})10681069			const res = produce(base, draft => {1070				draft.y++1071				draft.child = wrap(draft.child)1072				draft.child.wrapped.x++1073			})10741075			expect(res).toEqual({1076				y: 2,1077				child: {wrapped: {x: 2}}1078			})1079		})10801081		it("supports a base state with multiple references to an object", () => {1082			const obj = {}1083			const res = produce({a: obj, b: obj}, d => {1084				// Two drafts are created for each occurrence of an object in the base state.1085				expect(d.a).not.toBe(d.b)1086				d.a.z = true1087				expect(d.b.z).toBeUndefined()1088			})1089			expect(res.b).toBe(obj)1090			expect(res.a).not.toBe(res.b)1091			expect(res.a.z).toBeTruthy()1092		})10931094		// NOTE: Except the root draft.1095		it("supports multiple references to any modified draft", () => {1096			const next = produce({a: {b: 1}}, d => {1097				d.a.b++1098				d.b = d.a1099			})1100			expect(next.a).toBe(next.b)1101		})11021103		it("can rename nested objects (no changes)", () => {1104			const nextState = produce({obj: {}}, s => {1105				s.foo = s.obj1106				delete s.obj1107			})1108			expect(nextState).toEqual({foo: {}})1109		})11101111		// Very similar to the test before, but the reused object has one1112		// property changed, one added, and one removed.1113		it("can rename nested objects (with changes)", () => {1114			const nextState = produce({obj: {a: 1, b: 1}}, s => {1115				s.obj.a = true // change1116				delete s.obj.b // delete1117				s.obj.c = true // add11181119				s.foo = s.obj1120				delete s.obj1121			})1122			expect(nextState).toEqual({foo: {a: true, c: true}})1123		})11241125		it("can nest a draft in a new object (no changes)", () => {1126			const baseState = {obj: {}}1127			const nextState = produce(baseState, s => {1128				s.foo = {bar: s.obj}1129				delete s.obj1130			})1131			expect(nextState.foo.bar).toBe(baseState.obj)1132		})11331134		it("can nest a modified draft in a new object", () => {1135			const nextState = produce({obj: {a: 1, b: 1}}, s => {1136				s.obj.a = true // change1137				delete s.obj.b // delete1138				s.obj.c = true // add11391140				s.foo = {bar: s.obj}1141				delete s.obj1142			})1143			expect(nextState).toEqual({foo: {bar: {a: true, c: true}}})1144		})11451146		it("supports assigning undefined to an existing property", () => {1147			const nextState = produce(baseState, s => {1148				s.aProp = undefined1149			})1150			expect(nextState).not.toBe(baseState)1151			expect(nextState.aProp).toBe(undefined)1152		})11531154		it("supports assigning undefined to a new property", () => {1155			const baseState = {}1156			const nextState = produce(baseState, s => {1157				s.aProp = undefined1158			})1159			expect(nextState).not.toBe(baseState)1160			expect(nextState.aProp).toBe(undefined)1161		})11621163		// NOTE: ES5 drafts only protect existing properties when revoked.1164		if (!isProd)1165			it("revokes the draft once produce returns", () => {1166				const expectRevoked = fn => {1167					expect(fn).toThrowErrorMatchingSnapshot()1168				}11691170				// Test object drafts:1171				let draft1172				produce({a: 1, b: 1}, s => {1173					draft = s1174					delete s.b1175				})11761177				// Access known property on object draft.1178				expectRevoked(() => {1179					draft.a1180				})11811182				// Assign known property on object draft.1183				expectRevoked(() => {1184					draft.a = true1185				})11861187				// Access unknown property on object draft.1188				expectRevoked(() => {1189					draft.z1190				})11911192				// Assign unknown property on object draft.1193				expectRevoked(() => {1194					draft.z = true1195				})11961197				// Test array drafts:1198				produce([1, 2], s => {1199					draft = s1200					s.pop()1201				})12021203				// Access known index of an array draft.1204				expectRevoked(() => {1205					draft[0]1206				})12071208				// Assign known index of an array draft.1209				expectRevoked(() => {1210					draft[0] = true1211				})12121213				// Access unknown index of an array draft.1214				expectRevoked(() => {1215					draft[1]1216				})12171218				// Assign unknown index of an array draft.1219				expectRevoked(() => {1220					draft[1] = true1221				})1222			})12231224		it("can access a child draft that was created before the draft was modified", () => {1225			produce({a: {}}, s => {1226				const before = s.a1227				s.b = 11228				expect(s.a).toBe(before)1229			})1230		})12311232		it("should reflect all changes made in the draft immediately", () => {1233			produce(baseState, draft => {1234				draft.anArray[0] = 51235				draft.anArray.unshift("test")1236				if (!global.USES_BUILD)1237					expect(enumerableOnly(draft.anArray)).toEqual([1238						"test",1239						5,1240						2,1241						{c: 3},1242						11243					])1244				draft.stuffz = "coffee"1245				expect(draft.stuffz).toBe("coffee")1246			})1247		})12481249		it("throws when Object.defineProperty() is used on drafts", () => {1250			expect(() => {1251				produce({}, draft => {1252					Object.defineProperty(draft, "xx", {1253						enumerable: true,1254						writeable: true,1255						value: 21256					})1257				})1258			}).toThrowErrorMatchingSnapshot()1259		})12601261		it("should handle constructor correctly", () => {1262			const baseState = {1263				arr: new Array(),1264				obj: new Object()1265			}1266			const result = produce(baseState, draft => {1267				draft.arrConstructed = draft.arr.constructor(1)1268				draft.objConstructed = draft.obj.constructor(1)1269			})1270			expect(result.arrConstructed).toEqual(new Array().constructor(1))1271			expect(result.objConstructed).toEqual(new Object().constructor(1))1272		})12731274		it("should handle equality correctly - 1", () => {1275			const baseState = {1276				y: 3 / 0,1277				z: NaN1278			}1279			const nextState = produce(baseState, draft => {1280				draft.y = 4 / 01281				draft.z = NaN1282			})1283			expect(nextState).toBe(baseState)1284		})12851286		it("should handle equality correctly - 2", () => {1287			const baseState = {1288				x: -01289			}1290			const nextState = produce(baseState, draft => {1291				draft.x = +01292			})1293			// 0 === -0 // true1294			// Object.is(0, -0) // false1295			//1296			// MWE:1297			// Immer preserves the difference between -0 and +0,1298			// so a new state is created.1299			// This isn't defined anywhere explicitly, so could be changed1300			// in the future to follow === behavior, rather than Object.is.1301			// But I think this is fine as is.1302			expect(nextState).not.toBe(baseState)1303			expect(nextState.x).toBe(-0)1304			expect(nextState.x).not.toBe(+0)1305			// however, toEqual treats -0 === +0 (which is true!)1306			expect(nextState).toEqual(baseState)1307		})13081309		it("should handle equality correctly - 3", () => {1310			const baseState = {1311				x: "s1",1312				y: 1,1313				z: NaN1314			}1315			const nextState = produce(baseState, draft => {1316				draft.x = "s2"1317				draft.y = 11318				draft.z = NaN1319				if (!isProd) {1320					expect(draft[DRAFT_STATE].assigned_.x).toBe(true)1321					expect(draft[DRAFT_STATE].assigned_.y).toBe(undefined)1322					expect(draft[DRAFT_STATE].assigned_.z).toBe(undefined)1323				}1324			})1325			expect(nextState.x).toBe("s2")1326		})13271328		// AKA: recursive produce calls1329		describe("nested producers", () => {1330			describe("when base state is not a draft", () => {1331				// This test ensures the global state used to manage proxies is1332				// never left in a corrupted state by a nested `produce` call.1333				it("never affects its parent producer implicitly", () => {1334					const base = {obj: {a: 1}}1335					const next = produce(base, draft => {1336						// Notice how `base.obj` is passed, not `draft.obj`1337						const obj2 = produce(base.obj, draft2 => {1338							draft2.a = 01339						})1340						expect(obj2.a).toBe(0)1341						expect(draft.obj.a).toBe(1) // effects should not be visible outside1342					})1343					expect(next).toBe(base)1344				})1345			})13461347			describe("when base state is a draft", () => {1348				it("always wraps the draft in a new draft", () => {1349					produce({}, parent => {1350						produce(parent, child => {1351							expect(child).not.toBe(parent)1352							expect(isDraft(child)).toBeTruthy()1353							expect(original(child)).toBe(parent)1354						})1355					})1356				})13571358				// Reported by: https://github.com/mweststrate/immer/issues/3431359				it("ensures each property is drafted", () => {1360					produce({a: {}, b: {}}, parent => {1361						parent.a // Access "a" but not "b"1362						produce(parent, child => {1363							child.c = 11364							expect(isDraft(child.a)).toBeTruthy()1365							expect(isDraft(child.b)).toBeTruthy()1366						})1367					})1368				})13691370				it("preserves any pending changes", () => {1371					produce({a: 1, b: 1, d: 1}, parent => {1372						parent.b = 21373						parent.c = 21374						delete parent.d1375						produce(parent, child => {1376							expect(child.a).toBe(1) // unchanged1377							expect(child.b).toBe(2) // changed1378							expect(child.c).toBe(2) // added1379							expect(child.d).toBeUndefined() // deleted1380						})1381					})1382				})1383			})13841385			describe("when base state contains a draft", () => {1386				it("wraps unowned draft with its own draft", () => {1387					produce({a: {}}, parent => {1388						produce({a: parent.a}, child => {1389							expect(child.a).not.toBe(parent.a)1390							expect(isDraft(child.a)).toBeTruthy()1391							expect(original(child.a)).toBe(parent.a)1392						})1393					})1394				})13951396				it("returns unowned draft if no changes were made", () => {1397					produce({a: {}}, parent => {1398						const result = produce({a: parent.a}, () => {})1399						expect(result.a).toBe(parent.a)1400					})1401				})14021403				it("clones the unowned draft when changes are made", () => {1404					produce({a: {}}, parent => {1405						const result = produce({a: parent.a}, child => {1406							child.a.b = 11407						})1408						expect(result.a).not.toBe(parent.a)1409						expect(result.a.b).toBe(1)1410						expect("b" in parent.a).toBeFalsy()1411					})1412				})14131414				// We cannot auto-freeze the result of a nested producer,1415				// because it may contain a draft from a parent producer.1416				it("never auto-freezes the result", () => {1417					produce({a: {}}, parent => {1418						const r = produce({a: parent.a}, child => {1419							child.b = 1 // Ensure a copy is returned.1420						})1421						expect(Object.isFrozen(r)).toBeFalsy()1422					})1423				})1424			})14251426			// "Upvalues" are variables from a parent scope.1427			it("does not finalize upvalue drafts", () => {1428				produce({a: {}, b: {}}, parent => {1429					expect(produce({}, () => parent)).toBe(parent)1430					parent.x // Ensure proxy not revoked.14311432					expect(produce({}, () => [parent])[0]).toBe(parent)1433					parent.x // Ensure proxy not revoked.14341435					expect(produce({}, () => parent.a)).toBe(parent.a)1436					parent.a.x // Ensure proxy not revoked.14371438					// Modified parent test1439					parent.c = 11440					expect(produce({}, () => [parent.b])[0]).toBe(parent.b)1441					parent.b.x // Ensure proxy not revoked.1442				})1443			})14441445			it("works with interweaved Immer instances", () => {1446				const options = {autoFreeze}1447				const one = createPatchedImmer(options)1448				const two = createPatchedImmer(options)14491450				const base = {}1451				const result = one.produce(base, s1 =>1452					two.produce({s1}, s2 => {1453						expect(original(s2.s1)).toBe(s1)1454						s2.n = 11455						s2.s1 = one.produce({s2}, s3 => {1456							expect(original(s3.s2)).toBe(s2)1457							expect(original(s3.s2.s1)).toBe(s2.s1)1458							return s3.s2.s11459						})1460					})1461				)1462				expect(result.n).toBe(1)1463				expect(result.s1).toBe(base)1464			})1465		})14661467		it("throws when Object.setPrototypeOf() is used on a draft", () => {1468			produce({}, draft => {1469				expect(() =>1470					Object.setPrototypeOf(draft, Array)1471				).toThrowErrorMatchingSnapshot()1472			})1473		})14741475		it("supports the 'in' operator", () => {1476			produce(baseState, draft => {1477				// Known property1478				expect("anArray" in draft).toBe(true)1479				expect(Reflect.has(draft, "anArray")).toBe(true)14801481				// Unknown property1482				expect("bla" in draft).toBe(false)1483				expect(Reflect.has(draft, "bla")).toBe(false)14841485				// Known index1486				expect(0 in draft.anArray).toBe(true)1487				expect("0" in draft.anArray).toBe(true)1488				expect(Reflect.has(draft.anArray, 0)).toBe(true)1489				expect(Reflect.has(draft.anArray, "0")).toBe(true)14901491				// Unknown index1492				expect(17 in draft.anArray).toBe(false)1493				expect("17" in draft.anArray).toBe(false)1494				expect(Reflect.has(draft.anArray, 17)).toBe(false)1495				expect(Reflect.has(draft.anArray, "17")).toBe(false)1496			})1497		})14981499		it("'this' should not be bound anymore - 1", () => {1500			const base = {x: 3}1501			const next1 = produce(base, function() {1502				expect(this).toBe(undefined)1503			})1504		})15051506		it("'this' should not be bound anymore - 2", () => {1507			const incrementor = produce(function() {1508				expect(this).toBe(undefined)1509			})1510			incrementor()1511		})15121513		it("should be possible to use dynamic bound this", () => {1514			const world = {1515				counter: {count: 1},1516				inc: produce(function(draft) {1517					expect(this).toBe(world)1518					draft.counter.count = this.counter.count + 11519				})1520			}15211522			expect(world.inc(world).counter.count).toBe(2)1523		})15241525		it("doesnt recurse into frozen structures if external data is frozen", () => {1526			const frozen = {}1527			Object.defineProperty(frozen, "x", {1528				get() {1529					throw "oops"1530				},1531				enumerable: true,1532				configurable: true1533			})1534			Object.freeze(frozen)15351536			expect(() => {1537				produce({}, d => {1538					d.x = frozen1539				})1540			}).not.toThrow()1541		})15421543		// See here: https://github.com/mweststrate/immer/issues/891544		it("supports the spread operator", () => {1545			const base = {foo: {x: 0, y: 0}, bar: [0, 0]}1546			const result = produce(base, draft => {1547				draft.foo = {x: 1, ...draft.foo, y: 1}1548				draft.bar = [1, ...draft.bar, 1]1549			})1550			expect(result).toEqual({1551				foo: {x: 0, y: 1},1552				bar: [1, 0, 0, 1]1553			})1554		})15551556		it("processes with lodash.set", () => {1557			const base = [{id: 1, a: 1}]1558			const result = produce(base, draft => {1559				lodash.set(draft, "[0].a", 2)1560			})1561			expect(base[0].a).toEqual(1)1562			expect(result[0].a).toEqual(2)1563		})15641565		it("processes with lodash.find", () => {1566			const base = [{id: 1, a: 1}]1567			const result = produce(base, draft => {1568				const obj1 = lodash.find(draft, {id: 1})1569				lodash.set(obj1, "a", 2)1570			})1571			expect(base[0].a).toEqual(1)1572			expect(result[0].a).toEqual(2)1573		})15741575		it("does not draft external data", () => {1576			const externalData = {x: 3}1577			const base = {}1578			const next = produce(base, draft => {1579				// potentially, we *could* draft external data automatically, but only if those statements are not switched...1580				draft.y = externalData1581				draft.y.x += 11582				externalData.x += 11583			})1584			expect(next).toEqual({y: {x: 5}})1585			expect(externalData.x).toBe(5)1586			expect(next.y).toBe(externalData)1587		})15881589		it("does not create new state unnecessary, #491", () => {1590			const a = {highlight: true}1591			const next1 = produce(a, draft => {1592				draft.highlight = false1593				draft.highlight = true1594			})1595			// See explanation in issue1596			expect(next1).not.toBe(a)15971598			const next2 = produce(a, draft => {1599				draft.highlight = true1600			})1601			expect(next2).toBe(a)1602		})16031604		autoFreeze &&1605			test("issue #462 - frozen", () => {1606				var origin = {1607					a: {1608						value: "no"1609					},1610					b: {1611						value: "no"1612					}1613				}1614				const next = produce(origin, draft => {1615					draft.a.value = "im"1616				})1617				expect(() => {1618					origin.b.value = "yes"1619				}).toThrowError(1620					"Cannot assign to read only property 'value' of object '#<Object>'"1621				)1622				expect(() => {1623					next.b.value = "yes"1624				}).toThrowError(1625					"Cannot assign to read only property 'value' of object '#<Object>'"1626				) // should throw!1627			})16281629		autoFreeze &&1630			test("issue #469, state not frozen", () => {1631				const project = produce(1632					{1633						id: 1,1634						tracks: [{id: 1}]1635					},1636					() => {}1637				)16381639				expect(() => {1640					project.id = 2 // Does not throw error1641				}).toThrowError("Cannot assign to read only property 'id'")16421643				expect(() => {1644					Object.assign(project, {id: 2}) // Uncaught TypeError: Cannot assign to read only property 'id' of object '#<Object>'1645				}).toThrowError("Cannot assign to read only property 'id'")1646			})16471648		describe("recipe functions", () => {1649			it("can return a new object", () => {1650				const base = {x: 3}1651				const res = produce(base, d => {1652					return {x: d.x + 1}1653				})1654				expect(res).not.toBe(base)1655				expect(res).toEqual({x: 4})1656			})16571658			it("can return the draft", () => {1659				const base = {x: 3}1660				const res = produce(base, d => {1661					d.x = 41662					return d1663				})1664				expect(res).not.toBe(base)1665				expect(res).toEqual({x: 4})1666			})16671668			it("can return an unmodified child draft", () => {1669				const base = {a: {}}1670				const res = produce(base, d => {1671					return d.a1672				})1673				expect(res).toBe(base.a)1674			})16751676			// TODO: Avoid throwing if only the child draft was modified.1677			it("cannot return a modified child draft", () => {1678				const base = {a: {}}1679				expect(() => {1680					produce(base, d => {1681						d.a.b = 11682						return d.a1683					})1684				}).toThrowErrorMatchingSnapshot()1685			})16861687			it("can return a frozen object", () => {1688				const res = deepFreeze([{x: 3}])1689				expect(produce({}, () => res)).toBe(res)1690			})16911692			it("can return an object with two references to another object", () => {1693				const next = produce({}, d => {1694					const obj = {}1695					return {obj, arr: [obj]}1696				})1697				expect(next.obj).toBe(next.arr[0])1698			})16991700			it("can return an object with two references to an unmodified draft", () => {1701				const base = {a: {}}1702				const next = produce(base, d => {1703					return [d.a, d.a]1704				})1705				expect(next[0]).toBe(base.a)1706				expect(next[0]).toBe(next[1])1707			})17081709			it("cannot return an object that references itself", () => {1710				const res = {}1711				res.self = res1712				expect(() => {1713					produce(res, () => res.self)1714				}).toThrowErrorMatchingSnapshot()1715			})1716		})17171718		it("throws when the draft is modified and another object is returned", () => {1719			const base = {x: 3}1720			expect(() => {1721				produce(base, draft => {1722					draft.x = 41723					return {x: 5}1724				})1725			}).toThrowErrorMatchingSnapshot()1726		})17271728		it("should fix #117 - 1", () => {1729			const reducer = (state, action) =>1730				produce(state, draft => {1731					switch (action.type) {1732						case "SET_STARTING_DOTS":1733							return draft.availableStartingDots.map(a => a)1734						default:1735							break1736					}1737				})1738			const base = {1739				availableStartingDots: [1740					{dots: 4, count: 1},1741					{dots: 3, count: 2},1742					{dots: 2, count: 3},1743					{dots: 1, count: 4}1744				]1745			}1746			const next = reducer(base, {type: "SET_STARTING_DOTS"})1747			expect(next).toEqual(base.availableStartingDots)1748			expect(next).not.toBe(base.availableStartingDots)1749		})17501751		it("should fix #117 - 2", () => {1752			const reducer = (state, action) =>1753				produce(state, draft => {1754					switch (action.type) {1755						case "SET_STARTING_DOTS":1756							return {1757								dots: draft.availableStartingDots.map(a => a)1758							}1759						default:1760							break1761					}1762				})1763			const base = {1764				availableStartingDots: [1765					{dots: 4, count: 1},1766					{dots: 3, count: 2},1767					{dots: 2, count: 3},1768					{dots: 1, count: 4}1769				]1770			}1771			const next = reducer(base, {type: "SET_STARTING_DOTS"})1772			expect(next).toEqual({dots: base.availableStartingDots})1773		})17741775		it("cannot always detect noop assignments - 0", () => {1776			const baseState = {x: {y: 3}}1777			const nextState = produce(baseState, d => {1778				const a = d.x1779				d.x = a1780			})1781			expect(nextState).toBe(baseState)1782		})17831784		it("cannot always detect noop assignments - 1", () => {1785			const baseState = {x: {y: 3}}1786			const nextState = produce(baseState, d => {1787				const a = d.x1788				d.x = 41789				d.x = a1790			})1791			// Ideally, this should actually be the same instances1792			// but this would be pretty expensive to detect,1793			// so we don't atm1794			expect(nextState).not.toBe(baseState)1795		})17961797		it("cannot always detect noop assignments - 2", () => {1798			const baseState = {x: {y: 3}}1799			const nextState = produce(baseState, d => {1800				const a = d.x1801				const stuff = a.y + 31802				d.x = 41803				d.x = a1804			})1805			// Ideally, this should actually be the same instances1806			// but this would be pretty expensive to detect,1807			// so we don't atm1808			expect(nextState).not.toBe(baseState)1809		})18101811		it("cannot always detect noop assignments - 3", () => {1812			const baseState = {x: 3}1813			const nextState = produce(baseState, d => {1814				d.x = 31815			})1816			expect(nextState).toBe(baseState)1817		})18181819		it("cannot always detect noop assignments - 4", () => {1820			const baseState = {x: 3}1821			const nextState = produce(baseState, d => {1822				d.x = 41823				d.x = 31824			})1825			// Ideally, this should actually be the same instances1826			// but this would be pretty expensive to detect,1827			// so we don't atm1828			expect(nextState).not.toBe(baseState)1829		})18301831		it("cannot always detect noop assignments - 4", () => {1832			const baseState = {}1833			const [nextState, patches] = produceWithPatches(baseState, d => {1834				d.x = 41835				delete d.x1836			})1837			expect(nextState).toEqual({})1838			expect(patches).toEqual([])1839			// This differs between ES5 and proxy, and ES5 does it better :(1840			expect(nextState).not.toBe(baseState)1841		})18421843		it("cannot produce undefined by returning undefined", () => {1844			const base = 31845			expect(produce(base, () => 4)).toBe(4)1846			expect(produce(base, () => null)).toBe(null)1847			expect(produce(base, () => undefined)).toBe(3)1848			expect(produce(base, () => {})).toBe(3)1849			expect(produce(base, () => nothing)).toBe(undefined)18501851			expect(produce({}, () => undefined)).toEqual({})1852			expect(produce({}, () => nothing)).toBe(undefined)1853			expect(produce(3, () => nothing)).toBe(undefined)18541855			expect(produce(() => undefined)({})).toEqual({})1856			expect(produce(() => nothing)({})).toBe(undefined)1857			expect(produce(() => nothing)(3)).toBe(undefined)1858		})18591860		describe("base state type", () => {1861			if (!global.USES_BUILD) testObjectTypes(produce)1862			testLiteralTypes(produce)1863		})18641865		afterEach(() => {1866			expect(baseState).toBe(origBaseState)1867			expect(baseState).toEqual(createBaseState())1868		})18691870		function createBaseState() {1871			const data = {1872				anInstance: new Foo(),1873				anArray: [3, 2, {c: 3}, 1],1874				aMap: new Map([1875					["jedi", {name: "Luke", skill: 10}],1876					["jediTotal", 42],1877					["force", "these aren't the droids you're looking for"]1878				]),1879				aSet: new Set([1880					"Luke",1881					42,1882					{1883						jedi: "Yoda"1884					}1885				]),1886				aProp: "hi",1887				anObject: {1888					nested: {1889						yummie: true1890					},1891					coffee: false1892				}1893			}1894			return autoFreeze ? deepFreeze(data) : data1895		}1896	})18971898	it(`works with spread #524 - ${name}`, () => {1899		const state = {1900			level1: {1901				level2: {1902					level3: "data"1903				}1904			}1905		}19061907		const nextState = produce(state, draft => {1908			return {...draft}1909		})1910		const nextState1 = produce(state, draft => {1911			const newLevel1 = produce(draft.level1, level1Draft => {1912				return {...level1Draft}1913			})1914			draft.level1 = newLevel11915		})19161917		const nextState2 = produce(state, draft => {1918			const newLevel1 = produce(draft.level1, level1Draft => {1919				return {...level1Draft}1920			})1921			return {1922				level1: newLevel11923			}1924		})19251926		const nextState3 = produce(state, draft => {1927			const newLevel1 = produce(draft.level1, level1Draft => {1928				return Object.assign({}, level1Draft)1929			})1930			return Object.assign(draft, {1931				level1: newLevel11932			})1933		})19341935		const expected = {level1: {level2: {level3: "data"}}}1936		expect(nextState1).toEqual(expected)1937		expect(nextState2).toEqual(expected)1938		expect(nextState3).toEqual(expected)1939	})19401941	it(`Something with nested producers #522 ${name}`, () => {1942		function initialStateFactory() {1943			return {1944				substate: {1945					array: [1946						{id: "id1", value: 0},1947						{id: "id2", value: 0}1948					]1949				},1950				array: [1951					{id: "id1", value: 0},1952					{id: "id2", value: 0}1953				]1954			}1955		}19561957		const globalProducer = produce(draft => {1958			draft.substate = subProducer(draft.substate)1959			draft.array = arrayProducer(draft.array)1960		}, initialStateFactory())19611962		const subProducer = produce(draftSubState => {1963			draftSubState.array = arrayProducer(draftSubState.array)1964		})19651966		const arrayProducer = produce(draftArray => {1967			draftArray[0].value += 51968		})19691970		{1971			const state = globalProducer(undefined)1972			expect(state.array[0].value).toBe(5)1973			expect(state.array.length).toBe(2)1974			expect(state.array[1]).toMatchObject({1975				id: "id2",1976				value: 01977			})1978		}19791980		{1981			const state = globalProducer(undefined)1982			expect(state.substate.array[0].value).toBe(5)1983			expect(state.substate.array.length).toBe(2)1984			expect(state.substate.array[1]).toMatchObject({1985				id: "id2",1986				value: 01987			})1988			expect(state.substate.array).toMatchObject(state.array)1989		}1990	})19911992	describe(`isDraft - ${name}`, () => {1993		it("returns true for object drafts", () => {1994			produce({}, state => {1995				expect(isDraft(state)).toBeTruthy()1996			})1997		})1998		it("returns true for array drafts", () => {1999			produce([], state => {2000				expect(isDraft(state)).toBeTruthy()

The first 2,000 of 2,456 lines.