Skip to content
Myr

base.js

This file

Path

__tests__/base.js

2,467 lines · 61.9 KiB · JavaScript

Last changed by

Update base.jsvisciangbefore adoption15e221f

History of this file

Take it

1"use strict"2import {3	Immer,4	nothing,5	original,6	isDraft,7	immerable,8	enableAllPlugins9} from "../src/immer"10import {each, shallowCopy, DRAFT_STATE} from "../src/internal"11import deepFreeze from "deep-freeze"12import cloneDeep from "lodash.clonedeep"13import * as lodash from "lodash"1415jest.setTimeout(1000)1617enableAllPlugins()1819test("immer should have no dependencies", () => {20	expect(require("../package.json").dependencies).toBeUndefined()21})2223runBaseTest("proxy (no freeze)", true, false)24runBaseTest("proxy (autofreeze)", true, true)25runBaseTest("proxy (patch listener)", true, false, true)26runBaseTest("proxy (autofreeze)(patch listener)", true, true, true)2728runBaseTest("es5 (no freeze)", false, false)29runBaseTest("es5 (autofreeze)", false, true)30runBaseTest("es5 (patch listener)", false, false, true)31runBaseTest("es5 (autofreeze)(patch listener)", false, true, true)3233function runBaseTest(name, useProxies, autoFreeze, useListener) {34	const listener = useListener ? function() {} : undefined35	const {produce, produceWithPatches} = createPatchedImmer({36		useProxies,37		autoFreeze38	})3940	// When `useListener` is true, append a function to the arguments of every41	// uncurried `produce` call in every test. This makes tests easier to read.42	function createPatchedImmer(options) {43		const immer = new Immer(options)4445		const {produce} = immer46		immer.produce = function(...args) {47			return typeof args[1] === "function" && args.length < 348				? produce(...args, listener)49				: produce(...args)50		}5152		return immer53	}5455	describe(`base functionality - ${name}`, () => {56		let baseState57		let origBaseState5859		beforeEach(() => {60			origBaseState = baseState = createBaseState()61		})6263		it("returns the original state when no changes are made", () => {64			const nextState = produce(baseState, s => {65				expect(s.aProp).toBe("hi")66				expect(s.anObject.nested).toMatchObject({yummie: true})67			})68			expect(nextState).toBe(baseState)69		})7071		it("does structural sharing", () => {72			const random = Math.random()73			const nextState = produce(baseState, s => {74				s.aProp = random75			})76			expect(nextState).not.toBe(baseState)77			expect(nextState.aProp).toBe(random)78			expect(nextState.nested).toBe(baseState.nested)79		})8081		it("deep change bubbles up", () => {82			const nextState = produce(baseState, s => {83				s.anObject.nested.yummie = false84			})85			expect(nextState).not.toBe(baseState)86			expect(nextState.anObject).not.toBe(baseState.anObject)87			expect(baseState.anObject.nested.yummie).toBe(true)88			expect(nextState.anObject.nested.yummie).toBe(false)89			expect(nextState.anArray).toBe(baseState.anArray)90		})9192		it("can add props", () => {93			const nextState = produce(baseState, s => {94				s.anObject.cookie = {tasty: true}95			})96			expect(nextState).not.toBe(baseState)97			expect(nextState.anObject).not.toBe(baseState.anObject)98			expect(nextState.anObject.nested).toBe(baseState.anObject.nested)99			expect(nextState.anObject.cookie).toEqual({tasty: true})100		})101102		it("can delete props", () => {103			const nextState = produce(baseState, s => {104				delete s.anObject.nested105			})106			expect(nextState).not.toBe(baseState)107			expect(nextState.anObject).not.toBe(baseState.anObject)108			expect(nextState.anObject.nested).toBe(undefined)109		})110111		// Found by: https://github.com/mweststrate/immer/pull/267112		it("can delete props added in the producer", () => {113			const nextState = produce(baseState, s => {114				s.anObject.test = true115				delete s.anObject.test116			})117			if (useProxies) {118				expect(nextState).not.toBe(baseState)119				expect(nextState).toEqual(baseState)120			} else {121				// The copy is avoided in ES5.122				expect(nextState).toBe(baseState)123			}124		})125126		// Found by: https://github.com/mweststrate/immer/issues/328127		it("can set a property that was just deleted", () => {128			const baseState = {a: 1}129			const nextState = produce(baseState, s => {130				delete s.a131				s.a = 2132			})133			expect(nextState.a).toBe(2)134		})135136		it("can set a property to its original value after deleting it", () => {137			const baseState = {a: {b: 1}}138			const nextState = produce(baseState, s => {139				const a = s.a140				delete s.a141				s.a = a142			})143			if (useProxies) {144				expect(nextState).not.toBe(baseState)145				expect(nextState).toEqual(baseState)146			} else {147				// The copy is avoided in ES5.148				expect(nextState).toBe(baseState)149			}150		})151152		it("can get property descriptors", () => {153			const getDescriptor = Object.getOwnPropertyDescriptor154			const baseState = deepFreeze([{a: 1}])155			produce(baseState, arr => {156				const obj = arr[0]157				const desc = {158					configurable: true,159					enumerable: true,160					...(useProxies && {writable: true})161				}162163				// Known property164				expect(getDescriptor(obj, "a")).toMatchObject(desc)165				expect(getDescriptor(arr, 0)).toMatchObject(desc)166167				// Deleted property168				delete obj.a169				arr.pop()170				expect(getDescriptor(obj, "a")).toBeUndefined()171				expect(getDescriptor(arr, 0)).toBeUndefined()172173				// Unknown property174				expect(getDescriptor(obj, "b")).toBeUndefined()175				expect(getDescriptor(arr, 100)).toBeUndefined()176177				// Added property178				obj.b = 2179				arr[100] = 1180				expect(getDescriptor(obj, "b")).toBeDefined()181				expect(getDescriptor(arr, 100)).toBeDefined()182			})183		})184185		describe("array drafts", () => {186			it("supports Array.isArray()", () => {187				const nextState = produce(baseState, s => {188					expect(Array.isArray(s.anArray)).toBeTruthy()189					s.anArray.push(1)190				})191				expect(Array.isArray(nextState.anArray)).toBeTruthy()192			})193194			it("supports index access", () => {195				const value = baseState.anArray[0]196				const nextState = produce(baseState, s => {197					expect(s.anArray[0]).toBe(value)198				})199				expect(nextState).toBe(baseState)200			})201202			it("supports iteration", () => {203				const base = [204					{id: 1, a: 1},205					{id: 2, a: 1}206				]207				const findById = (collection, id) => {208					for (const item of collection) {209						if (item.id === id) return item210					}211					return null212				}213				const result = produce(base, draft => {214					const obj1 = findById(draft, 1)215					const obj2 = findById(draft, 2)216					obj1.a = 2217					obj2.a = 2218				})219				expect(result[0].a).toEqual(2)220				expect(result[1].a).toEqual(2)221			})222223			it("can assign an index via bracket notation", () => {224				const nextState = produce(baseState, s => {225					s.anArray[3] = true226				})227				expect(nextState).not.toBe(baseState)228				expect(nextState.anArray).not.toBe(baseState.anArray)229				expect(nextState.anArray[3]).toEqual(true)230			})231232			it("can use splice() to both add and remove items", () => {233				const nextState = produce(baseState, s => {234					s.anArray.splice(1, 1, "a", "b")235				})236				expect(nextState.anArray).not.toBe(baseState.anArray)237				expect(nextState.anArray[1]).toBe("a")238				expect(nextState.anArray[2]).toBe("b")239			})240241			it("can truncate via the length property", () => {242				const baseLength = baseState.anArray.length243				const nextState = produce(baseState, s => {244					s.anArray.length = baseLength - 1245				})246				expect(nextState.anArray).not.toBe(baseState.anArray)247				expect(nextState.anArray.length).toBe(baseLength - 1)248			})249250			it("can extend via the length property", () => {251				const baseLength = baseState.anArray.length252				const nextState = produce(baseState, s => {253					s.anArray.length = baseLength + 1254				})255				expect(nextState.anArray).not.toBe(baseState.anArray)256				expect(nextState.anArray.length).toBe(baseLength + 1)257			})258259			// Reported here: https://github.com/mweststrate/immer/issues/116260			it("can pop then push", () => {261				const nextState = produce([1, 2, 3], s => {262					s.pop()263					s.push(100)264				})265				expect(nextState).toEqual([1, 2, 100])266			})267268			it("can be sorted", () => {269				const baseState = [3, 1, 2]270				const nextState = produce(baseState, s => {271					s.sort()272				})273				expect(nextState).not.toBe(baseState)274				expect(nextState).toEqual([1, 2, 3])275			})276277			it("supports modifying nested objects", () => {278				const baseState = [{a: 1}, {}]279				const nextState = produce(baseState, s => {280					s[0].a++281					s[1].a = 0282				})283				expect(nextState).not.toBe(baseState)284				expect(nextState[0].a).toBe(2)285				expect(nextState[1].a).toBe(0)286			})287288			it("never preserves non-numeric properties", () => {289				const baseState = []290				baseState.x = 7291				const nextState = produce(baseState, s => {292					s.push(3)293				})294				expect("x" in nextState).toBeFalsy()295			})296297			if (useProxies && !global.USES_BUILD) {298				it("throws when a non-numeric property is added", () => {299					expect(() => {300						produce([], d => {301							d.x = 3302						})303					}).toThrowErrorMatchingSnapshot()304				})305306				it("throws when a non-numeric property is deleted", () => {307					expect(() => {308						const baseState = []309						baseState.x = 7310						produce(baseState, d => {311							delete d.x312						})313					}).toThrowErrorMatchingSnapshot()314				})315			}316		})317318		describe("map drafts", () => {319			it("supports key access", () => {320				const value = baseState.aMap.get("jedi")321				const nextState = produce(baseState, s => {322					expect(s.aMap.get("jedi")).toEqual(value)323				})324				expect(nextState).toBe(baseState)325			})326327			it("supports key access for non-primitive keys", () => {328				const key = {prop: "val"}329				const base = new Map([[key, {id: 1, a: 1}]])330				const value = base.get(key)331				const nextState = produce(base, s => {332					expect(s.get(key)).toEqual(value)333				})334				expect(nextState).toBe(base)335			})336337			it("supports iteration", () => {338				const base = new Map([339					["first", {id: 1, a: 1}],340					["second", {id: 2, a: 1}]341				])342				const findById = (map, id) => {343					for (const [, item] of map) {344						if (item.id === id) return item345					}346					return null347				}348				const result = produce(base, draft => {349					const obj1 = findById(draft, 1)350					const obj2 = findById(draft, 2)351					obj1.a = 2352					obj2.a = 2353				})354				expect(result).not.toBe(base)355				expect(result.get("first").a).toEqual(2)356				expect(result.get("second").a).toEqual(2)357			})358359			it("supports 'entries'", () => {360				const base = new Map([361					["first", {id: 1, a: 1}],362					["second", {id: 2, a: 1}]363				])364				const findById = (map, id) => {365					for (const [, item] of map.entries()) {366						if (item.id === id) return item367					}368					return null369				}370				const result = produce(base, draft => {371					const obj1 = findById(draft, 1)372					const obj2 = findById(draft, 2)373					obj1.a = 2374					obj2.a = 2375				})376				expect(result).not.toBe(base)377				expect(result.get("first").a).toEqual(2)378				expect(result.get("second").a).toEqual(2)379			})380381			it("supports 'values'", () => {382				const base = new Map([383					["first", {id: 1, a: 1}],384					["second", {id: 2, a: 1}]385				])386				const findById = (map, id) => {387					for (const item of map.values()) {388						if (item.id === id) return item389					}390					return null391				}392				const result = produce(base, draft => {393					const obj1 = findById(draft, 1)394					const obj2 = findById(draft, 2)395					obj1.a = 2396					obj2.a = 2397				})398				expect(result).not.toBe(base)399				expect(result.get("first").a).toEqual(2)400				expect(result.get("second").a).toEqual(2)401			})402403			it("supports 'keys", () => {404				const base = new Map([405					["first", Symbol()],406					["second", Symbol()]407				])408				const result = produce(base, draft => {409					expect([...draft.keys()]).toEqual(["first", "second"])410					draft.set("third", Symbol())411					expect([...draft.keys()]).toEqual(["first", "second", "third"])412				})413			})414415			it("supports forEach", () => {416				const key1 = {prop: "val1"}417				const key2 = {prop: "val2"}418				const base = new Map([419					["first", {id: 1, a: 1}],420					["second", {id: 2, a: 1}],421					[key1, {id: 3, a: 1}],422					[key2, {id: 4, a: 1}]423				])424				const result = produce(base, draft => {425					let sum1 = 0426					draft.forEach(({a}) => {427						sum1 += a428					})429					expect(sum1).toBe(4)430					let sum2 = 0431					draft.get("first").a = 10432					draft.get("second").a = 20433					draft.get(key1).a = 30434					draft.get(key2).a = 40435					draft.forEach(({a}) => {436						sum2 += a437					})438					expect(sum2).toBe(100)439				})440				expect(result).not.toBe(base)441				expect(base.get("first").a).toEqual(1)442				expect(base.get("second").a).toEqual(1)443				expect(base.get(key1).a).toEqual(1)444				expect(base.get(key2).a).toEqual(1)445				expect(result.get("first").a).toEqual(10)446				expect(result.get("second").a).toEqual(20)447				expect(result.get(key1).a).toEqual(30)448				expect(result.get(key2).a).toEqual(40)449			})450451			it("supports forEach mutation", () => {452				const base = new Map([453					["first", {id: 1, a: 1}],454					["second", {id: 2, a: 1}]455				])456				const result = produce(base, draft => {457					draft.forEach(item => {458						item.a = 100459					})460				})461				expect(result).not.toBe(base)462				expect(result.get("first").a).toEqual(100)463				expect(result.get("second").a).toEqual(100)464			})465466			it("can assign by key", () => {467				const nextState = produce(baseState, s => {468					// Map.prototype.set should return the Map itself469					const res = s.aMap.set("force", true)470					if (!global.USES_BUILD) expect(res).toBe(s.aMap[DRAFT_STATE].draft_)471				})472				expect(nextState).not.toBe(baseState)473				expect(nextState.aMap).not.toBe(baseState.aMap)474				expect(nextState.aMap.get("force")).toEqual(true)475			})476477			it("KO ==> new key with value=undefine", () => {478				const nextState = produce(baseState, s => {479					s.aMap.set("new_key", undefined)480				})481				expect(nextState.aMap.has("new_key")).toBe(true)482			})483484			it("OK ==> new key with value=undefine", () => {485				const nextState = produce(baseState, s => {486					s.aMap.set("new_key", 1)487					s.aMap.set("new_key", undefined)488				})489				expect(nextState.aMap.has("new_key")).toBe(true)490			})491            492			it("can assign by a non-primitive key", () => {493				const key = {prop: "val"}494				const value = {id: 1, a: 1}495				const base = new Map([[key, value]])496				const nextState = produce(base, s => {497					s.set(key, true)498				})499				expect(nextState).not.toBe(base)500				expect(base.get(key)).toEqual(value)501				expect(nextState.get(key)).toEqual(true)502			})503504			it("state stays the same if the the same item is assigned by key", () => {505				const nextState = produce(baseState, s => {506					s.aMap.set("jediTotal", 42)507				})508				expect(nextState).toBe(baseState)509				expect(nextState.aMap).toBe(baseState.aMap)510			})511512			it("returns 'size'", () => {513				const nextState = produce(baseState, s => {514					s.aMap.set("newKey", true)515					expect(s.aMap.size).toBe(baseState.aMap.size + 1)516				})517				expect(nextState).not.toBe(baseState)518				expect(nextState.aMap).not.toBe(baseState.aMap)519				expect(nextState.aMap.get("newKey")).toEqual(true)520				expect(nextState.aMap.size).toEqual(baseState.aMap.size + 1)521			})522523			it("can use 'delete' to remove items", () => {524				const nextState = produce(baseState, s => {525					expect(s.aMap.has("jedi")).toBe(true)526					expect(s.aMap.delete("jedi")).toBe(true)527					expect(s.aMap.has("jedi")).toBe(false)528				})529				expect(nextState.aMap).not.toBe(baseState.aMap)530				expect(nextState.aMap.size).toBe(baseState.aMap.size - 1)531				expect(baseState.aMap.has("jedi")).toBe(true)532				expect(nextState.aMap.has("jedi")).toBe(false)533			})534535			it("can use 'clear' to remove items", () => {536				const nextState = produce(baseState, s => {537					expect(s.aMap.size).not.toBe(0)538					s.aMap.clear()539					expect(s.aMap.size).toBe(0)540				})541				expect(nextState.aMap).not.toBe(baseState.aMap)542				expect(baseState.aMap.size).not.toBe(0)543				expect(nextState.aMap.size).toBe(0)544			})545546			it("support 'has'", () => {547				const nextState = produce(baseState, s => {548					expect(s.aMap.has("newKey")).toBe(false)549					s.aMap.set("newKey", true)550					expect(s.aMap.has("newKey")).toBe(true)551				})552				expect(nextState).not.toBe(baseState)553				expect(nextState.aMap).not.toBe(baseState.aMap)554				expect(baseState.aMap.has("newKey")).toBe(false)555				expect(nextState.aMap.has("newKey")).toBe(true)556			})557558			it("supports nested maps", () => {559				const base = new Map([["first", new Map([["second", {prop: "test"}]])]])560				const result = produce(base, draft => {561					draft.get("first").get("second").prop = "test1"562				})563				expect(result).not.toBe(base)564				expect(result.get("first")).not.toBe(base.get("first"))565				expect(result.get("first").get("second")).not.toBe(566					base.get("first").get("second")567				)568				expect(base.get("first").get("second").prop).toBe("test")569				expect(result.get("first").get("second").prop).toBe("test1")570			})571572			it("treats void deletes as no-op", () => {573				const base = new Map([["x", 1]])574				const next = produce(base, d => {575					expect(d.delete("y")).toBe(false)576				})577				expect(next).toBe(base)578			})579580			it("revokes map proxies", () => {581				let m582				produce(baseState, s => {583					m = s.aMap584				})585				expect(() => m.get("x")).toThrowErrorMatchingSnapshot()586				expect(() => m.set("x", 3)).toThrowErrorMatchingSnapshot()587			})588589			it("does not draft map keys", () => {590				// anything else would be terribly confusing591				const key = {a: 1}592				const map = new Map([[key, 2]])593				const next = produce(map, d => {594					const dKey = Array.from(d.keys())[0]595					expect(isDraft(dKey)).toBe(false)596					expect(dKey).toBe(key)597					dKey.a += 1598					d.set(dKey, d.get(dKey) + 1)599					d.set(key, d.get(key) + 1)600					expect(d.get(key)).toBe(4)601					expect(key.a).toBe(2)602				})603				const entries = Array.from(next.entries())604				expect(entries).toEqual([[key, 4]])605				expect(entries[0][0]).toBe(key)606				expect(entries[0][0].a).toBe(2)607			})608609			it("does support instanceof Map", () => {610				const map = new Map()611				produce(map, d => {612					expect(d instanceof Map).toBeTruthy()613				})614			})615616			it("handles clear correctly", () => {617				const map = new Map([618					["a", 1],619					["c", 3]620				])621				const next = produce(map, draft => {622					draft.delete("a")623					draft.set("b", 2)624					draft.set("c", 4)625					draft.clear()626				})627				expect(next).toEqual(new Map())628			})629		})630631		describe("set drafts", () => {632			it("supports iteration", () => {633				const base = new Set([634					{id: 1, a: 1},635					{id: 2, a: 1}636				])637				const findById = (set, id) => {638					for (const item of set) {639						if (item.id === id) return item640					}641					return null642				}643				const result = produce(base, draft => {644					const obj1 = findById(draft, 1)645					const obj2 = findById(draft, 2)646					obj1.a = 2647					obj2.a = 2648				})649				expect(result).not.toBe(base)650				expect(base).toEqual(651					new Set([652						{id: 1, a: 1},653						{id: 2, a: 1}654					])655				)656				expect(result).toEqual(657					new Set([658						{id: 1, a: 2},659						{id: 2, a: 2}660					])661				)662			})663664			it("supports 'entries'", () => {665				const base = new Set([666					{id: 1, a: 1},667					{id: 2, a: 1}668				])669				const findById = (set, id) => {670					for (const [item1, item2] of set.entries()) {671						expect(item1).toBe(item2)672						if (item1.id === id) return item1673					}674					return null675				}676				const result = produce(base, draft => {677					const obj1 = findById(draft, 1)678					const obj2 = findById(draft, 2)679					obj1.a = 2680					obj2.a = 2681				})682				expect(result).not.toBe(base)683				expect(base).toEqual(684					new Set([685						{id: 1, a: 1},686						{id: 2, a: 1}687					])688				)689				expect(result).toEqual(690					new Set([691						{id: 1, a: 2},692						{id: 2, a: 2}693					])694				)695			})696697			it("supports 'values'", () => {698				const base = new Set([699					{id: 1, a: 1},700					{id: 2, a: 1}701				])702				const findById = (set, id) => {703					for (const item of set.values()) {704						if (item.id === id) return item705					}706					return null707				}708				const result = produce(base, draft => {709					const obj1 = findById(draft, 1)710					const obj2 = findById(draft, 2)711					obj1.a = 2712					obj2.a = 2713				})714				expect(result).not.toBe(base)715				expect(base).toEqual(716					new Set([717						{id: 1, a: 1},718						{id: 2, a: 1}719					])720				)721				expect(result).toEqual(722					new Set([723						{id: 1, a: 2},724						{id: 2, a: 2}725					])726				)727			})728729			it("supports 'keys'", () => {730				const base = new Set([731					{id: 1, a: 1},732					{id: 2, a: 1}733				])734				const findById = (set, id) => {735					for (const item of set.keys()) {736						if (item.id === id) return item737					}738					return null739				}740				const result = produce(base, draft => {741					const obj1 = findById(draft, 1)742					const obj2 = findById(draft, 2)743					obj1.a = 2744					obj2.a = 2745				})746				expect(result).not.toBe(base)747				expect(base).toEqual(748					new Set([749						{id: 1, a: 1},750						{id: 2, a: 1}751					])752				)753				expect(result).toEqual(754					new Set([755						{id: 1, a: 2},756						{id: 2, a: 2}757					])758				)759			})760761			it("supports forEach with mutation after reads", () => {762				const base = new Set([763					{id: 1, a: 1},764					{id: 2, a: 2}765				])766				const result = produce(base, draft => {767					let sum1 = 0768					draft.forEach(({a}) => {769						sum1 += a770					})771					expect(sum1).toBe(3)772					let sum2 = 0773					draft.forEach(item => {774						item.a += 10775						sum2 += item.a776					})777					expect(sum2).toBe(23)778				})779				expect(result).not.toBe(base)780				expect(base).toEqual(781					new Set([782						{id: 1, a: 1},783						{id: 2, a: 2}784					])785				)786				expect(result).toEqual(787					new Set([788						{id: 1, a: 11},789						{id: 2, a: 12}790					])791				)792			})793794			it("state stays the same if the same item is added", () => {795				const nextState = produce(baseState, s => {796					s.aSet.add("Luke")797				})798				expect(nextState).toBe(baseState)799				expect(nextState.aSet).toBe(baseState.aSet)800			})801802			it("can add new items", () => {803				const nextState = produce(baseState, s => {804					// Set.prototype.set should return the Set itself805					const res = s.aSet.add("force")806					if (!global.USES_BUILD) expect(res).toBe(s.aSet[DRAFT_STATE].draft_)807				})808				expect(nextState).not.toBe(baseState)809				expect(nextState.aSet).not.toBe(baseState.aSet)810				expect(baseState.aSet.has("force")).toBe(false)811				expect(nextState.aSet.has("force")).toBe(true)812			})813814			it("returns 'size'", () => {815				const nextState = produce(baseState, s => {816					s.aSet.add("newKey")817					expect(s.aSet.size).toBe(baseState.aSet.size + 1)818				})819				expect(nextState).not.toBe(baseState)820				expect(nextState.aSet).not.toBe(baseState.aSet)821				expect(nextState.aSet.has("newKey")).toBe(true)822				expect(nextState.aSet.size).toEqual(baseState.aSet.size + 1)823			})824825			it("can use 'delete' to remove items", () => {826				const nextState = produce(baseState, s => {827					expect(s.aSet.has("Luke")).toBe(true)828					expect(s.aSet.delete("Luke")).toBe(true)829					expect(s.aSet.delete("Luke")).toBe(false)830					expect(s.aSet.has("Luke")).toBe(false)831				})832				expect(nextState.aSet).not.toBe(baseState.aSet)833				expect(baseState.aSet.has("Luke")).toBe(true)834				expect(nextState.aSet.has("Luke")).toBe(false)835				expect(nextState.aSet.size).toBe(baseState.aSet.size - 1)836			})837838			it("can use 'clear' to remove items", () => {839				const nextState = produce(baseState, s => {840					expect(s.aSet.size).not.toBe(0)841					s.aSet.clear()842					expect(s.aSet.size).toBe(0)843				})844				expect(nextState.aSet).not.toBe(baseState.aSet)845				expect(baseState.aSet.size).not.toBe(0)846				expect(nextState.aSet.size).toBe(0)847			})848849			it("supports 'has'", () => {850				const nextState = produce(baseState, s => {851					expect(s.aSet.has("newKey")).toBe(false)852					s.aSet.add("newKey")853					expect(s.aSet.has("newKey")).toBe(true)854				})855				expect(nextState).not.toBe(baseState)856				expect(nextState.aSet).not.toBe(baseState.aSet)857				expect(baseState.aSet.has("newKey")).toBe(false)858				expect(nextState.aSet.has("newKey")).toBe(true)859			})860861			it("supports nested sets", () => {862				const base = new Set([new Set(["Serenity"])])863				const result = produce(base, draft => {864					draft.forEach(nestedItem => nestedItem.add("Firefly"))865				})866				expect(result).not.toBe(base)867				expect(base).toEqual(new Set([new Set(["Serenity"])]))868				expect(result).toEqual(new Set([new Set(["Serenity", "Firefly"])]))869			})870871			it("supports has / delete on elements from the original", () => {872				const obj = {}873				const set = new Set([obj])874				const next = produce(set, d => {875					expect(d.has(obj)).toBe(true)876					d.add(3)877					expect(d.has(obj)).toBe(true)878					d.delete(obj)879					expect(d.has(obj)).toBe(false)880				})881				expect(next).toEqual(new Set([3]))882			})883884			it("revokes sets", () => {885				let m886				produce(baseState, s => {887					m = s.aSet888				})889				expect(() => m.has("x")).toThrowErrorMatchingSnapshot()890				expect(() => m.add("x")).toThrowErrorMatchingSnapshot()891			})892893			it("does support instanceof Set", () => {894				const set = new Set()895				produce(set, d => {896					expect(d instanceof Set).toBeTruthy()897				})898			})899		})900901		it("supports `immerable` symbol on constructor", () => {902			class One {}903			One[immerable] = true904			const baseState = new One()905			const nextState = produce(baseState, draft => {906				expect(draft).not.toBe(baseState)907				draft.foo = true908			})909			expect(nextState).not.toBe(baseState)910			expect(nextState.foo).toBeTruthy()911		})912913		it("preserves symbol properties", () => {914			const test = Symbol("test")915			const baseState = {[test]: true}916			const nextState = produce(baseState, s => {917				expect(s[test]).toBeTruthy()918				s.foo = true919			})920			expect(nextState).toEqual({921				[test]: true,922				foo: true923			})924		})925926		if (!global.USES_BUILD)927			it("preserves non-enumerable properties", () => {928				const baseState = {}929				// Non-enumerable object property930				Object.defineProperty(baseState, "foo", {931					value: {a: 1},932					enumerable: false933				})934				// Non-enumerable primitive property935				Object.defineProperty(baseState, "bar", {936					value: 1,937					enumerable: false938				})939				const nextState = produce(baseState, s => {940					expect(s.foo).toBeTruthy()941					expect(isEnumerable(s, "foo")).toBeFalsy()942					s.bar++943					expect(isEnumerable(s, "foo")).toBeFalsy()944					s.foo.a++945					expect(isEnumerable(s, "foo")).toBeFalsy()946				})947				expect(nextState.foo).toBeTruthy()948				expect(isEnumerable(nextState, "foo")).toBeFalsy()949			})950951		it("can work with own computed props", () => {952			const baseState = {953				x: 1,954				get y() {955					return this.x956				},957				set y(v) {958					this.x = v959				}960			}961962			const nextState = produce(baseState, d => {963				expect(d.y).toBe(1)964				d.x = 2965				expect(d.x).toBe(2)966				expect(d.y).toBe(1) // this has been copied!967				d.y = 3968				expect(d.x).toBe(2)969			})970			expect(baseState.x).toBe(1)971			expect(baseState.y).toBe(1)972973			expect(nextState.x).toBe(2)974			expect(nextState.y).toBe(3)975			if (!autoFreeze) {976				nextState.y = 4 // decoupled now!977				expect(nextState.y).toBe(4)978				expect(nextState.x).toBe(2)979				expect(Object.getOwnPropertyDescriptor(nextState, "y").value).toBe(4)980			}981		})982983		it("can work with class with computed props", () => {984			class State {985				[immerable] = true986987				x = 1988989				set y(v) {990					this.x = v991				}992993				get y() {994					return this.x995				}996			}997998			const baseState = new State()9991000			const nextState = produce(baseState, d => {1001				expect(d.y).toBe(1)1002				d.y = 21003				expect(d.x).toBe(2)1004				expect(d.y).toBe(2)1005				expect(Object.getOwnPropertyDescriptor(d, "y")).toBeUndefined()1006			})1007			expect(baseState.x).toBe(1)1008			expect(baseState.y).toBe(1)10091010			expect(nextState.x).toBe(2)1011			expect(nextState.y).toBe(2)1012			expect(Object.getOwnPropertyDescriptor(nextState, "y")).toBeUndefined()1013		})10141015		it("allows inherited computed properties", () => {1016			const proto = {}1017			Object.defineProperty(proto, "foo", {1018				get() {1019					return this.bar1020				},1021				set(val) {1022					this.bar = val1023				}1024			})1025			proto[immerable] = true1026			const baseState = Object.create(proto)1027			produce(baseState, s => {1028				expect(s.bar).toBeUndefined()1029				s.foo = {}1030				expect(s.bar).toBeDefined()1031				expect(s.foo).toBe(s.bar)1032			})1033		})10341035		it("optimization: does not visit properties of new data structures if autofreeze is disabled and no drafts are unfinalized", () => {1036			const newData = {}1037			Object.defineProperty(newData, "x", {1038				enumerable: true,1039				get() {1040					throw new Error("visited!")1041				}1042			})10431044			const run = () =>1045				produce({}, d => {1046					d.data = newData1047				})1048			if (autoFreeze) {1049				expect(run).toThrow("visited!")1050			} else {1051				expect(run).not.toThrow("visited!")1052			}1053		})10541055		it("same optimization doesn't cause draft from nested producers to be unfinished", () => {1056			const base = {1057				y: 1,1058				child: {1059					x: 11060				}1061			}1062			const wrap = produce(draft => {1063				return {1064					wrapped: draft1065				}1066			})10671068			const res = produce(base, draft => {1069				draft.y++1070				draft.child = wrap(draft.child)1071				draft.child.wrapped.x++1072			})10731074			expect(res).toEqual({1075				y: 2,1076				child: {wrapped: {x: 2}}1077			})1078		})10791080		it("supports a base state with multiple references to an object", () => {1081			const obj = {}1082			const res = produce({a: obj, b: obj}, d => {1083				// Two drafts are created for each occurrence of an object in the base state.1084				expect(d.a).not.toBe(d.b)1085				d.a.z = true1086				expect(d.b.z).toBeUndefined()1087			})1088			expect(res.b).toBe(obj)1089			expect(res.a).not.toBe(res.b)1090			expect(res.a.z).toBeTruthy()1091		})10921093		// NOTE: Except the root draft.1094		it("supports multiple references to any modified draft", () => {1095			const next = produce({a: {b: 1}}, d => {1096				d.a.b++1097				d.b = d.a1098			})1099			expect(next.a).toBe(next.b)1100		})11011102		it("can rename nested objects (no changes)", () => {1103			const nextState = produce({obj: {}}, s => {1104				s.foo = s.obj1105				delete s.obj1106			})1107			expect(nextState).toEqual({foo: {}})1108		})11091110		// Very similar to the test before, but the reused object has one1111		// property changed, one added, and one removed.1112		it("can rename nested objects (with changes)", () => {1113			const nextState = produce({obj: {a: 1, b: 1}}, s => {1114				s.obj.a = true // change1115				delete s.obj.b // delete1116				s.obj.c = true // add11171118				s.foo = s.obj1119				delete s.obj1120			})1121			expect(nextState).toEqual({foo: {a: true, c: true}})1122		})11231124		it("can nest a draft in a new object (no changes)", () => {1125			const baseState = {obj: {}}1126			const nextState = produce(baseState, s => {1127				s.foo = {bar: s.obj}1128				delete s.obj1129			})1130			expect(nextState.foo.bar).toBe(baseState.obj)1131		})11321133		it("can nest a modified draft in a new object", () => {1134			const nextState = produce({obj: {a: 1, b: 1}}, s => {1135				s.obj.a = true // change1136				delete s.obj.b // delete1137				s.obj.c = true // add11381139				s.foo = {bar: s.obj}1140				delete s.obj1141			})1142			expect(nextState).toEqual({foo: {bar: {a: true, c: true}}})1143		})11441145		it("supports assigning undefined to an existing property", () => {1146			const nextState = produce(baseState, s => {1147				s.aProp = undefined1148			})1149			expect(nextState).not.toBe(baseState)1150			expect(nextState.aProp).toBe(undefined)1151		})11521153		it("supports assigning undefined to a new property", () => {1154			const baseState = {}1155			const nextState = produce(baseState, s => {1156				s.aProp = undefined1157			})1158			expect(nextState).not.toBe(baseState)1159			expect(nextState.aProp).toBe(undefined)1160		})11611162		// NOTE: ES5 drafts only protect existing properties when revoked.1163		it("revokes the draft once produce returns", () => {1164			const expectRevoked = (fn, shouldThrow = true) => {1165				if (shouldThrow) expect(fn).toThrowErrorMatchingSnapshot()1166				else expect(fn).not.toThrow()1167			}11681169			// Test object drafts:1170			let draft1171			produce({a: 1, b: 1}, s => {1172				draft = s1173				delete s.b1174			})11751176			// Access known property on object draft.1177			expectRevoked(() => {1178				draft.a1179			})11801181			// Assign known property on object draft.1182			expectRevoked(() => {1183				draft.a = true1184			})11851186			// Access unknown property on object draft.1187			expectRevoked(() => {1188				draft.z1189			}, useProxies)11901191			// Assign unknown property on object draft.1192			expectRevoked(() => {1193				draft.z = true1194			}, useProxies)11951196			// Test array drafts:1197			produce([1, 2], s => {1198				draft = s1199				s.pop()1200			})12011202			// Access known index of an array draft.1203			expectRevoked(() => {1204				draft[0]1205			})12061207			// Assign known index of an array draft.1208			expectRevoked(() => {1209				draft[0] = true1210			})12111212			// Access unknown index of an array draft.1213			expectRevoked(() => {1214				draft[1]1215			}, useProxies)12161217			// Assign unknown index of an array draft.1218			expectRevoked(() => {1219				draft[1] = true1220			}, useProxies)1221		})12221223		it("can access a child draft that was created before the draft was modified", () => {1224			produce({a: {}}, s => {1225				const before = s.a1226				s.b = 11227				expect(s.a).toBe(before)1228			})1229		})12301231		it("should reflect all changes made in the draft immediately", () => {1232			produce(baseState, draft => {1233				draft.anArray[0] = 51234				draft.anArray.unshift("test")1235				if (!global.USES_BUILD)1236					expect(enumerableOnly(draft.anArray)).toEqual([1237						"test",1238						5,1239						2,1240						{c: 3},1241						11242					])1243				draft.stuffz = "coffee"1244				expect(draft.stuffz).toBe("coffee")1245			})1246		})12471248		if (useProxies)1249			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			expect(nextState).not.toBe(baseState)1294			expect(nextState).not.toEqual(baseState)1295		})12961297		// AKA: recursive produce calls1298		describe("nested producers", () => {1299			describe("when base state is not a draft", () => {1300				// This test ensures the global state used to manage proxies is1301				// never left in a corrupted state by a nested `produce` call.1302				it("never affects its parent producer implicitly", () => {1303					const base = {obj: {a: 1}}1304					const next = produce(base, draft => {1305						// Notice how `base.obj` is passed, not `draft.obj`1306						const obj2 = produce(base.obj, draft2 => {1307							draft2.a = 01308						})1309						expect(obj2.a).toBe(0)1310						expect(draft.obj.a).toBe(1) // effects should not be visible outside1311					})1312					expect(next).toBe(base)1313				})1314			})13151316			describe("when base state is a draft", () => {1317				it("always wraps the draft in a new draft", () => {1318					produce({}, parent => {1319						produce(parent, child => {1320							expect(child).not.toBe(parent)1321							expect(isDraft(child)).toBeTruthy()1322							expect(original(child)).toBe(parent)1323						})1324					})1325				})13261327				// Reported by: https://github.com/mweststrate/immer/issues/3431328				it("ensures each property is drafted", () => {1329					produce({a: {}, b: {}}, parent => {1330						parent.a // Access "a" but not "b"1331						produce(parent, child => {1332							child.c = 11333							expect(isDraft(child.a)).toBeTruthy()1334							expect(isDraft(child.b)).toBeTruthy()1335						})1336					})1337				})13381339				it("preserves any pending changes", () => {1340					produce({a: 1, b: 1, d: 1}, parent => {1341						parent.b = 21342						parent.c = 21343						delete parent.d1344						produce(parent, child => {1345							expect(child.a).toBe(1) // unchanged1346							expect(child.b).toBe(2) // changed1347							expect(child.c).toBe(2) // added1348							expect(child.d).toBeUndefined() // deleted1349						})1350					})1351				})1352			})13531354			describe("when base state contains a draft", () => {1355				it("wraps unowned draft with its own draft", () => {1356					produce({a: {}}, parent => {1357						produce({a: parent.a}, child => {1358							expect(child.a).not.toBe(parent.a)1359							expect(isDraft(child.a)).toBeTruthy()1360							expect(original(child.a)).toBe(parent.a)1361						})1362					})1363				})13641365				it("returns unowned draft if no changes were made", () => {1366					produce({a: {}}, parent => {1367						const result = produce({a: parent.a}, () => {})1368						expect(result.a).toBe(parent.a)1369					})1370				})13711372				it("clones the unowned draft when changes are made", () => {1373					produce({a: {}}, parent => {1374						const result = produce({a: parent.a}, child => {1375							child.a.b = 11376						})1377						expect(result.a).not.toBe(parent.a)1378						expect(result.a.b).toBe(1)1379						expect("b" in parent.a).toBeFalsy()1380					})1381				})13821383				// We cannot auto-freeze the result of a nested producer,1384				// because it may contain a draft from a parent producer.1385				it("never auto-freezes the result", () => {1386					produce({a: {}}, parent => {1387						const r = produce({a: parent.a}, child => {1388							child.b = 1 // Ensure a copy is returned.1389						})1390						expect(Object.isFrozen(r)).toBeFalsy()1391					})1392				})1393			})13941395			// "Upvalues" are variables from a parent scope.1396			it("does not finalize upvalue drafts", () => {1397				produce({a: {}, b: {}}, parent => {1398					expect(produce({}, () => parent)).toBe(parent)1399					parent.x // Ensure proxy not revoked.14001401					expect(produce({}, () => [parent])[0]).toBe(parent)1402					parent.x // Ensure proxy not revoked.14031404					expect(produce({}, () => parent.a)).toBe(parent.a)1405					parent.a.x // Ensure proxy not revoked.14061407					// Modified parent test1408					parent.c = 11409					expect(produce({}, () => [parent.b])[0]).toBe(parent.b)1410					parent.b.x // Ensure proxy not revoked.1411				})1412			})14131414			it("works with interweaved Immer instances", () => {1415				const options = {useProxies, autoFreeze}1416				const one = createPatchedImmer(options)1417				const two = createPatchedImmer(options)14181419				const base = {}1420				const result = one.produce(base, s1 =>1421					two.produce({s1}, s2 => {1422						expect(original(s2.s1)).toBe(s1)1423						s2.n = 11424						s2.s1 = one.produce({s2}, s3 => {1425							expect(original(s3.s2)).toBe(s2)1426							expect(original(s3.s2.s1)).toBe(s2.s1)1427							return s3.s2.s11428						})1429					})1430				)1431				expect(result.n).toBe(1)1432				expect(result.s1).toBe(base)1433			})1434		})14351436		if (useProxies)1437			it("throws when Object.setPrototypeOf() is used on a draft", () => {1438				produce({}, draft => {1439					expect(() =>1440						Object.setPrototypeOf(draft, Array)1441					).toThrowErrorMatchingSnapshot()1442				})1443			})14441445		it("supports the 'in' operator", () => {1446			produce(baseState, draft => {1447				// Known property1448				expect("anArray" in draft).toBe(true)1449				expect(Reflect.has(draft, "anArray")).toBe(true)14501451				// Unknown property1452				expect("bla" in draft).toBe(false)1453				expect(Reflect.has(draft, "bla")).toBe(false)14541455				// Known index1456				expect(0 in draft.anArray).toBe(true)1457				expect("0" in draft.anArray).toBe(true)1458				expect(Reflect.has(draft.anArray, 0)).toBe(true)1459				expect(Reflect.has(draft.anArray, "0")).toBe(true)14601461				// Unknown index1462				expect(17 in draft.anArray).toBe(false)1463				expect("17" in draft.anArray).toBe(false)1464				expect(Reflect.has(draft.anArray, 17)).toBe(false)1465				expect(Reflect.has(draft.anArray, "17")).toBe(false)1466			})1467		})14681469		it("'this' should not be bound anymore - 1", () => {1470			const base = {x: 3}1471			const next1 = produce(base, function() {1472				expect(this).toBe(undefined)1473			})1474		})14751476		it("'this' should not be bound anymore - 2", () => {1477			const incrementor = produce(function() {1478				expect(this).toBe(undefined)1479			})1480			incrementor()1481		})14821483		it("should be possible to use dynamic bound this", () => {1484			const world = {1485				counter: {count: 1},1486				inc: produce(function(draft) {1487					expect(this).toBe(world)1488					draft.counter.count = this.counter.count + 11489				})1490			}14911492			expect(world.inc(world).counter.count).toBe(2)1493		})14941495		it("doesnt recurse into frozen structures if external data is frozen", () => {1496			const frozen = {}1497			Object.defineProperty(frozen, "x", {1498				get() {1499					throw "oops"1500				},1501				enumerable: true,1502				configurable: true1503			})1504			Object.freeze(frozen)15051506			expect(() => {1507				produce({}, d => {1508					d.x = frozen1509				})1510			}).not.toThrow()1511		})15121513		// See here: https://github.com/mweststrate/immer/issues/891514		it("supports the spread operator", () => {1515			const base = {foo: {x: 0, y: 0}, bar: [0, 0]}1516			const result = produce(base, draft => {1517				draft.foo = {x: 1, ...draft.foo, y: 1}1518				draft.bar = [1, ...draft.bar, 1]1519			})1520			expect(result).toEqual({1521				foo: {x: 0, y: 1},1522				bar: [1, 0, 0, 1]1523			})1524		})15251526		it("processes with lodash.set", () => {1527			const base = [{id: 1, a: 1}]1528			const result = produce(base, draft => {1529				lodash.set(draft, "[0].a", 2)1530			})1531			expect(base[0].a).toEqual(1)1532			expect(result[0].a).toEqual(2)1533		})15341535		it("processes with lodash.find", () => {1536			const base = [{id: 1, a: 1}]1537			const result = produce(base, draft => {1538				const obj1 = lodash.find(draft, {id: 1})1539				lodash.set(obj1, "a", 2)1540			})1541			expect(base[0].a).toEqual(1)1542			expect(result[0].a).toEqual(2)1543		})15441545		it("does not draft external data", () => {1546			const externalData = {x: 3}1547			const base = {}1548			const next = produce(base, draft => {1549				// potentially, we *could* draft external data automatically, but only if those statements are not switched...1550				draft.y = externalData1551				draft.y.x += 11552				externalData.x += 11553			})1554			expect(next).toEqual({y: {x: 5}})1555			expect(externalData.x).toBe(5)1556			expect(next.y).toBe(externalData)1557		})15581559		it("does not create new state unnecessary, #491", () => {1560			const a = {highlight: true}1561			const next1 = produce(a, draft => {1562				draft.highlight = false1563				draft.highlight = true1564			})1565			// See explanation in issue1566			expect(next1).not.toBe(a)15671568			const next2 = produce(a, draft => {1569				draft.highlight = true1570			})1571			expect(next2).toBe(a)1572		})15731574		autoFreeze &&1575			test("issue #462 - frozen", () => {1576				var origin = {1577					a: {1578						value: "no"1579					},1580					b: {1581						value: "no"1582					}1583				}1584				const next = produce(origin, draft => {1585					draft.a.value = "im"1586				})1587				expect(() => {1588					origin.b.value = "yes"1589				}).toThrowError(1590					"Cannot assign to read only property 'value' of object '#<Object>'"1591				)1592				expect(() => {1593					next.b.value = "yes"1594				}).toThrowError(1595					"Cannot assign to read only property 'value' of object '#<Object>'"1596				) // should throw!1597			})15981599		autoFreeze &&1600			test("issue #469, state not frozen", () => {1601				const project = produce(1602					{1603						id: 1,1604						tracks: [{id: 1}]1605					},1606					() => {}1607				)16081609				expect(() => {1610					project.id = 2 // Does not throw error1611				}).toThrowError("Cannot assign to read only property 'id'")16121613				expect(() => {1614					Object.assign(project, {id: 2}) // Uncaught TypeError: Cannot assign to read only property 'id' of object '#<Object>'1615				}).toThrowError("Cannot assign to read only property 'id'")1616			})16171618		describe("recipe functions", () => {1619			it("can return a new object", () => {1620				const base = {x: 3}1621				const res = produce(base, d => {1622					return {x: d.x + 1}1623				})1624				expect(res).not.toBe(base)1625				expect(res).toEqual({x: 4})1626			})16271628			it("can return the draft", () => {1629				const base = {x: 3}1630				const res = produce(base, d => {1631					d.x = 41632					return d1633				})1634				expect(res).not.toBe(base)1635				expect(res).toEqual({x: 4})1636			})16371638			it("can return an unmodified child draft", () => {1639				const base = {a: {}}1640				const res = produce(base, d => {1641					return d.a1642				})1643				expect(res).toBe(base.a)1644			})16451646			// TODO: Avoid throwing if only the child draft was modified.1647			it("cannot return a modified child draft", () => {1648				const base = {a: {}}1649				expect(() => {1650					produce(base, d => {1651						d.a.b = 11652						return d.a1653					})1654				}).toThrowErrorMatchingSnapshot()1655			})16561657			it("can return a frozen object", () => {1658				const res = deepFreeze([{x: 3}])1659				expect(produce({}, () => res)).toBe(res)1660			})16611662			it("can return an object with two references to another object", () => {1663				const next = produce({}, d => {1664					const obj = {}1665					return {obj, arr: [obj]}1666				})1667				expect(next.obj).toBe(next.arr[0])1668			})16691670			it("can return an object with two references to an unmodified draft", () => {1671				const base = {a: {}}1672				const next = produce(base, d => {1673					return [d.a, d.a]1674				})1675				expect(next[0]).toBe(base.a)1676				expect(next[0]).toBe(next[1])1677			})16781679			it("cannot return an object that references itself", () => {1680				const res = {}1681				res.self = res1682				expect(() => {1683					produce(res, () => res.self)1684				}).toThrowErrorMatchingSnapshot()1685			})1686		})16871688		describe("async recipe function", () => {1689			it("can modify the draft", () => {1690				const base = {a: 0, b: 0}1691				return produce(base, async d => {1692					d.a = 11693					await Promise.resolve()1694					d.b = 11695				}).then(res => {1696					expect(res).not.toBe(base)1697					expect(res).toEqual({a: 1, b: 1})1698				})1699			})17001701			it("works with rejected promises", () => {1702				let draft1703				const base = {a: 0, b: 0}1704				const err = new Error("passed")1705				return produce(base, d => {1706					draft = d1707					draft.b = 11708					return Promise.reject(err)1709				}).then(1710					() => {1711						throw "failed"1712					},1713					e => {1714						expect(e).toBe(err)1715						expect(() => draft.a).toThrowErrorMatchingSnapshot()1716					}1717				)1718			})17191720			it("supports recursive produce calls after await", () => {1721				const base = {obj: {k: 1}}1722				return produce(base, d => {1723					const obj = d.obj1724					delete d.obj1725					return Promise.resolve().then(() => {1726						d.a = produce({}, d => {1727							d.b = obj // Assign a draft owned by the parent scope.1728						})17291730						// Auto-freezing is prevented when an unowned draft exists.1731						expect(Object.isFrozen(d.a)).toBeFalsy()17321733						// Ensure `obj` is not revoked.1734						obj.c = 11735					})1736				}).then(res => {1737					expect(res).not.toBe(base)1738					expect(res).toEqual({1739						a: {b: {k: 1, c: 1}}1740					})1741				})1742			})1743		})17441745		it("throws when the draft is modified and another object is returned", () => {1746			const base = {x: 3}1747			expect(() => {1748				produce(base, draft => {1749					draft.x = 41750					return {x: 5}1751				})1752			}).toThrowErrorMatchingSnapshot()1753		})17541755		it("should fix #117 - 1", () => {1756			const reducer = (state, action) =>1757				produce(state, draft => {1758					switch (action.type) {1759						case "SET_STARTING_DOTS":1760							return draft.availableStartingDots.map(a => a)1761						default:1762							break1763					}1764				})1765			const base = {1766				availableStartingDots: [1767					{dots: 4, count: 1},1768					{dots: 3, count: 2},1769					{dots: 2, count: 3},1770					{dots: 1, count: 4}1771				]1772			}1773			const next = reducer(base, {type: "SET_STARTING_DOTS"})1774			expect(next).toEqual(base.availableStartingDots)1775			expect(next).not.toBe(base.availableStartingDots)1776		})17771778		it("should fix #117 - 2", () => {1779			const reducer = (state, action) =>1780				produce(state, draft => {1781					switch (action.type) {1782						case "SET_STARTING_DOTS":1783							return {1784								dots: draft.availableStartingDots.map(a => a)1785							}1786						default:1787							break1788					}1789				})1790			const base = {1791				availableStartingDots: [1792					{dots: 4, count: 1},1793					{dots: 3, count: 2},1794					{dots: 2, count: 3},1795					{dots: 1, count: 4}1796				]1797			}1798			const next = reducer(base, {type: "SET_STARTING_DOTS"})1799			expect(next).toEqual({dots: base.availableStartingDots})1800		})18011802		it("cannot always detect noop assignments - 0", () => {1803			const baseState = {x: {y: 3}}1804			const nextState = produce(baseState, d => {1805				const a = d.x1806				d.x = a1807			})1808			expect(nextState).toBe(baseState)1809		})18101811		it("cannot always detect noop assignments - 1", () => {1812			const baseState = {x: {y: 3}}1813			const nextState = produce(baseState, d => {1814				const a = d.x1815				d.x = 41816				d.x = a1817			})1818			// Ideally, this should actually be the same instances1819			// but this would be pretty expensive to detect,1820			// so we don't atm1821			expect(nextState).not.toBe(baseState)1822		})18231824		it("cannot always detect noop assignments - 2", () => {1825			const baseState = {x: {y: 3}}1826			const nextState = produce(baseState, d => {1827				const a = d.x1828				const stuff = a.y + 31829				d.x = 41830				d.x = a1831			})1832			// Ideally, this should actually be the same instances1833			// but this would be pretty expensive to detect,1834			// so we don't atm1835			expect(nextState).not.toBe(baseState)1836		})18371838		it("cannot always detect noop assignments - 3", () => {1839			const baseState = {x: 3}1840			const nextState = produce(baseState, d => {1841				d.x = 31842			})1843			expect(nextState).toBe(baseState)1844		})18451846		it("cannot always detect noop assignments - 4", () => {1847			const baseState = {x: 3}1848			const nextState = produce(baseState, d => {1849				d.x = 41850				d.x = 31851			})1852			// Ideally, this should actually be the same instances1853			// but this would be pretty expensive to detect,1854			// so we don't atm1855			expect(nextState).not.toBe(baseState)1856		})18571858		it("cannot always detect noop assignments - 4", () => {1859			const baseState = {}1860			const [nextState, patches] = produceWithPatches(baseState, d => {1861				d.x = 41862				delete d.x1863			})1864			expect(nextState).toEqual({})1865			expect(patches).toEqual([])1866			// This differs between ES5 and proxy, and ES5 does it better :(1867			if (useProxies) expect(nextState).not.toBe(baseState)1868			else expect(nextState).toBe(baseState)1869		})18701871		it("cannot produce undefined by returning undefined", () => {1872			const base = 31873			expect(produce(base, () => 4)).toBe(4)1874			expect(produce(base, () => null)).toBe(null)1875			expect(produce(base, () => undefined)).toBe(3)1876			expect(produce(base, () => {})).toBe(3)1877			expect(produce(base, () => nothing)).toBe(undefined)18781879			expect(produce({}, () => undefined)).toEqual({})1880			expect(produce({}, () => nothing)).toBe(undefined)1881			expect(produce(3, () => nothing)).toBe(undefined)18821883			expect(produce(() => undefined)({})).toEqual({})1884			expect(produce(() => nothing)({})).toBe(undefined)1885			expect(produce(() => nothing)(3)).toBe(undefined)1886		})18871888		describe("base state type", () => {1889			if (!global.USES_BUILD) testObjectTypes(produce)1890			testLiteralTypes(produce)1891		})18921893		afterEach(() => {1894			expect(baseState).toBe(origBaseState)1895			expect(baseState).toEqual(createBaseState())1896		})18971898		class Foo {}1899		function createBaseState() {1900			const data = {1901				anInstance: new Foo(),1902				anArray: [3, 2, {c: 3}, 1],1903				aMap: new Map([1904					["jedi", {name: "Luke", skill: 10}],1905					["jediTotal", 42],1906					["force", "these aren't the droids you're looking for"]1907				]),1908				aSet: new Set([1909					"Luke",1910					42,1911					{1912						jedi: "Yoda"1913					}1914				]),1915				aProp: "hi",1916				anObject: {1917					nested: {1918						yummie: true1919					},1920					coffee: false1921				}1922			}1923			return autoFreeze ? deepFreeze(data) : data1924		}1925	})19261927	it(`works with spread #524 - ${name}`, () => {1928		const state = {1929			level1: {1930				level2: {1931					level3: "data"1932				}1933			}1934		}19351936		const nextState = produce(state, draft => {1937			return {...draft}1938		})1939		const nextState1 = produce(state, draft => {1940			const newLevel1 = produce(draft.level1, level1Draft => {1941				return {...level1Draft}1942			})1943			draft.level1 = newLevel11944		})19451946		const nextState2 = produce(state, draft => {1947			const newLevel1 = produce(draft.level1, level1Draft => {1948				return {...level1Draft}1949			})1950			return {1951				level1: newLevel11952			}1953		})19541955		const nextState3 = produce(state, draft => {1956			const newLevel1 = produce(draft.level1, level1Draft => {1957				return Object.assign({}, level1Draft)1958			})1959			return Object.assign(draft, {1960				level1: newLevel11961			})1962		})19631964		const expected = {level1: {level2: {level3: "data"}}}1965		expect(nextState1).toEqual(expected)1966		expect(nextState2).toEqual(expected)1967		expect(nextState3).toEqual(expected)1968	})19691970	it(`Something with nested producers #522 ${name}`, () => {1971		function initialStateFactory() {1972			return {1973				substate: {1974					array: [1975						{id: "id1", value: 0},1976						{id: "id2", value: 0}1977					]1978				},1979				array: [1980					{id: "id1", value: 0},1981					{id: "id2", value: 0}1982				]1983			}1984		}19851986		const globalProducer = produce(draft => {1987			draft.substate = subProducer(draft.substate)1988			draft.array = arrayProducer(draft.array)1989		}, initialStateFactory())19901991		const subProducer = produce(draftSubState => {1992			draftSubState.array = arrayProducer(draftSubState.array)1993		})19941995		const arrayProducer = produce(draftArray => {1996			draftArray[0].value += 51997		})19981999		{2000			const state = globalProducer(undefined)

The first 2,000 of 2,467 lines.