Skip to content
Myr

base.js

This file

Path

__tests__/base.js

2,103 lines · 54.2 KiB · JavaScript

Last changed by

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

History of this file

Take it

The walk stopped after 200 commits: lines older than b053637 point at it.

test: returning an upvalue draft from a nested produceraleclarsonbefore adoptionb053637

line 1why, one step back
1"use strict"

New build set. Current statsMichel Weststratebefore adoption9aec0fe

lines 2–9why, one step back
2import {
3 Immer,4 nothing,5 original,6 isDraft,7 immerable,8 enableAllPlugins9} from "../src/immer"

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

line 10why, one step back
10import {each, shallowCopy, isEnumerable, DRAFT_STATE} from "../src/internal"

test: returning an upvalue draft from a nested produceraleclarsonbefore adoptionb053637

lines 11–15why, one step back
11import deepFreeze from "deep-freeze"
12import cloneDeep from "lodash.clonedeep"13import * as lodash from "lodash"1415jest.setTimeout(1000)

New build set. Current statsMichel Weststratebefore adoption9aec0fe

lines 16–17why, one step back
16
17enableAllPlugins()

test: returning an upvalue draft from a nested produceraleclarsonbefore adoptionb053637

line 18why, one step back
18

chore: improve more testsaleclarsonbefore adoption4f59b12

line 19why, one step back
19test("immer should have no dependencies", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 20why, one step back
20 expect(require("../package.json").dependencies).toBeUndefined()

chore: improve more testsaleclarsonbefore adoption4f59b12

line 21why, one step back
21})

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 22why, one step back
22

test: returning an upvalue draft from a nested produceraleclarsonbefore adoptionb053637

lines 23–25why, one step back
23runBaseTest("proxy (no freeze)", true, false)
24runBaseTest("proxy (autofreeze)", true, true)25runBaseTest("proxy (autofreeze)(patch listener)", true, true, true)

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

lines 26–28why, one step back
26// runBaseTest("proxy (patch listener)", true, ufalse, true)
2728// TODO: no freeze + patch

test: returning an upvalue draft from a nested produceraleclarsonbefore adoptionb053637

lines 29–31why, one step back
29runBaseTest("es5 (no freeze)", false, false)
30runBaseTest("es5 (autofreeze)", false, true)31runBaseTest("es5 (autofreeze)(patch listener)", false, true, true)

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

line 32why, one step back
32// TODO: no freeze + patch

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 33why, one step back
33

test: returning an upvalue draft from a nested produceraleclarsonbefore adoptionb053637

line 34why, one step back
34function runBaseTest(name, useProxies, autoFreeze, useListener) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 35why, one step back
35 const listener = useListener ? function() {} : undefined

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

line 36why, one step back
36 const {produce, produceWithPatches} = createPatchedImmer({

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 37–46why, one step back
37 useProxies,
38 autoFreeze39 })4041 // When `useListener` is true, append a function to the arguments of every42 // uncurried `produce` call in every test. This makes tests easier to read.43 function createPatchedImmer(options) {44 const immer = new Immer(options)4546 const {produce} = immer

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 47–48why, one step back
47 immer.produce = function(...args) {
48 return typeof args[1] === "function" && args.length < 3

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 49–50why, one step back
49 ? produce(...args, listener)
50 : produce(...args)

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

line 51why, one step back
51 }

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 52–203why, one step back
52
53 return immer54 }5556 describe(`base functionality - ${name}`, () => {57 let baseState58 let origBaseState5960 beforeEach(() => {61 origBaseState = baseState = createBaseState()62 })6364 it("returns the original state when no changes are made", () => {65 const nextState = produce(baseState, s => {66 expect(s.aProp).toBe("hi")67 expect(s.anObject.nested).toMatchObject({yummie: true})68 })69 expect(nextState).toBe(baseState)70 })7172 it("does structural sharing", () => {73 const random = Math.random()74 const nextState = produce(baseState, s => {75 s.aProp = random76 })77 expect(nextState).not.toBe(baseState)78 expect(nextState.aProp).toBe(random)79 expect(nextState.nested).toBe(baseState.nested)80 })8182 it("deep change bubbles up", () => {83 const nextState = produce(baseState, s => {84 s.anObject.nested.yummie = false85 })86 expect(nextState).not.toBe(baseState)87 expect(nextState.anObject).not.toBe(baseState.anObject)88 expect(baseState.anObject.nested.yummie).toBe(true)89 expect(nextState.anObject.nested.yummie).toBe(false)90 expect(nextState.anArray).toBe(baseState.anArray)91 })9293 it("can add props", () => {94 const nextState = produce(baseState, s => {95 s.anObject.cookie = {tasty: true}96 })97 expect(nextState).not.toBe(baseState)98 expect(nextState.anObject).not.toBe(baseState.anObject)99 expect(nextState.anObject.nested).toBe(baseState.anObject.nested)100 expect(nextState.anObject.cookie).toEqual({tasty: true})101 })102103 it("can delete props", () => {104 const nextState = produce(baseState, s => {105 delete s.anObject.nested106 })107 expect(nextState).not.toBe(baseState)108 expect(nextState.anObject).not.toBe(baseState.anObject)109 expect(nextState.anObject.nested).toBe(undefined)110 })111112 // Found by: https://github.com/mweststrate/immer/pull/267113 it("can delete props added in the producer", () => {114 const nextState = produce(baseState, s => {115 s.anObject.test = true116 delete s.anObject.test117 })118 if (useProxies) {119 expect(nextState).not.toBe(baseState)120 expect(nextState).toEqual(baseState)121 } else {122 // The copy is avoided in ES5.123 expect(nextState).toBe(baseState)124 }125 })126127 // Found by: https://github.com/mweststrate/immer/issues/328128 it("can set a property that was just deleted", () => {129 const baseState = {a: 1}130 const nextState = produce(baseState, s => {131 delete s.a132 s.a = 2133 })134 expect(nextState.a).toBe(2)135 })136137 it("can set a property to its original value after deleting it", () => {138 const baseState = {a: {b: 1}}139 const nextState = produce(baseState, s => {140 const a = s.a141 delete s.a142 s.a = a143 })144 if (useProxies) {145 expect(nextState).not.toBe(baseState)146 expect(nextState).toEqual(baseState)147 } else {148 // The copy is avoided in ES5.149 expect(nextState).toBe(baseState)150 }151 })152153 it("can get property descriptors", () => {154 const getDescriptor = Object.getOwnPropertyDescriptor155 const baseState = deepFreeze([{a: 1}])156 produce(baseState, arr => {157 const obj = arr[0]158 const desc = {159 configurable: true,160 enumerable: true,161 ...(useProxies && {writable: true})162 }163164 // Known property165 expect(getDescriptor(obj, "a")).toMatchObject(desc)166 expect(getDescriptor(arr, 0)).toMatchObject(desc)167168 // Deleted property169 delete obj.a170 arr.pop()171 expect(getDescriptor(obj, "a")).toBeUndefined()172 expect(getDescriptor(arr, 0)).toBeUndefined()173174 // Unknown property175 expect(getDescriptor(obj, "b")).toBeUndefined()176 expect(getDescriptor(arr, 100)).toBeUndefined()177178 // Added property179 obj.b = 2180 arr[100] = 1181 expect(getDescriptor(obj, "b")).toBeDefined()182 expect(getDescriptor(arr, 100)).toBeDefined()183 })184 })185186 describe("array drafts", () => {187 it("supports Array.isArray()", () => {188 const nextState = produce(baseState, s => {189 expect(Array.isArray(s.anArray)).toBeTruthy()190 s.anArray.push(1)191 })192 expect(Array.isArray(nextState.anArray)).toBeTruthy()193 })194195 it("supports index access", () => {196 const value = baseState.anArray[0]197 const nextState = produce(baseState, s => {198 expect(s.anArray[0]).toBe(value)199 })200 expect(nextState).toBe(baseState)201 })202203 it("supports iteration", () => {

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 204–207why, one step back
204 const base = [
205 {id: 1, a: 1},206 {id: 2, a: 1}207 ]

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 208–318why, one step back
208 const findById = (collection, id) => {
209 for (const item of collection) {210 if (item.id === id) return item211 }212 return null213 }214 const result = produce(base, draft => {215 const obj1 = findById(draft, 1)216 const obj2 = findById(draft, 2)217 obj1.a = 2218 obj2.a = 2219 })220 expect(result[0].a).toEqual(2)221 expect(result[1].a).toEqual(2)222 })223224 it("can assign an index via bracket notation", () => {225 const nextState = produce(baseState, s => {226 s.anArray[3] = true227 })228 expect(nextState).not.toBe(baseState)229 expect(nextState.anArray).not.toBe(baseState.anArray)230 expect(nextState.anArray[3]).toEqual(true)231 })232233 it("can use splice() to both add and remove items", () => {234 const nextState = produce(baseState, s => {235 s.anArray.splice(1, 1, "a", "b")236 })237 expect(nextState.anArray).not.toBe(baseState.anArray)238 expect(nextState.anArray[1]).toBe("a")239 expect(nextState.anArray[2]).toBe("b")240 })241242 it("can truncate via the length property", () => {243 const baseLength = baseState.anArray.length244 const nextState = produce(baseState, s => {245 s.anArray.length = baseLength - 1246 })247 expect(nextState.anArray).not.toBe(baseState.anArray)248 expect(nextState.anArray.length).toBe(baseLength - 1)249 })250251 it("can extend via the length property", () => {252 const baseLength = baseState.anArray.length253 const nextState = produce(baseState, s => {254 s.anArray.length = baseLength + 1255 })256 expect(nextState.anArray).not.toBe(baseState.anArray)257 expect(nextState.anArray.length).toBe(baseLength + 1)258 })259260 // Reported here: https://github.com/mweststrate/immer/issues/116261 it("can pop then push", () => {262 const nextState = produce([1, 2, 3], s => {263 s.pop()264 s.push(100)265 })266 expect(nextState).toEqual([1, 2, 100])267 })268269 it("can be sorted", () => {270 const baseState = [3, 1, 2]271 const nextState = produce(baseState, s => {272 s.sort()273 })274 expect(nextState).not.toBe(baseState)275 expect(nextState).toEqual([1, 2, 3])276 })277278 it("supports modifying nested objects", () => {279 const baseState = [{a: 1}, {}]280 const nextState = produce(baseState, s => {281 s[0].a++282 s[1].a = 0283 })284 expect(nextState).not.toBe(baseState)285 expect(nextState[0].a).toBe(2)286 expect(nextState[1].a).toBe(0)287 })288289 it("never preserves non-numeric properties", () => {290 const baseState = []291 baseState.x = 7292 const nextState = produce(baseState, s => {293 s.push(3)294 })295 expect("x" in nextState).toBeFalsy()296 })297298 if (useProxies) {299 it("throws when a non-numeric property is added", () => {300 expect(() => {301 produce([], d => {302 d.x = 3303 })304 }).toThrowErrorMatchingSnapshot()305 })306307 it("throws when a non-numeric property is deleted", () => {308 expect(() => {309 const baseState = []310 baseState.x = 7311 produce(baseState, d => {312 delete d.x313 })314 }).toThrowErrorMatchingSnapshot()315 })316 }317 })318

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 319–404why, one step back
319 describe("map drafts", () => {
320 it("supports key access", () => {321 const value = baseState.aMap.get("jedi")322 const nextState = produce(baseState, s => {323 expect(s.aMap.get("jedi")).toEqual(value)324 })325 expect(nextState).toBe(baseState)326 })327328 it("supports key access for non-primitive keys", () => {329 const key = {prop: "val"}330 const base = new Map([[key, {id: 1, a: 1}]])331 const value = base.get(key)332 const nextState = produce(base, s => {333 expect(s.get(key)).toEqual(value)334 })335 expect(nextState).toBe(base)336 })337338 it("supports iteration", () => {339 const base = new Map([340 ["first", {id: 1, a: 1}],341 ["second", {id: 2, a: 1}]342 ])343 const findById = (map, id) => {344 for (const [, item] of map) {345 if (item.id === id) return item346 }347 return null348 }349 const result = produce(base, draft => {350 const obj1 = findById(draft, 1)351 const obj2 = findById(draft, 2)352 obj1.a = 2353 obj2.a = 2354 })355 expect(result).not.toBe(base)356 expect(result.get("first").a).toEqual(2)357 expect(result.get("second").a).toEqual(2)358 })359360 it("supports 'entries'", () => {361 const base = new Map([362 ["first", {id: 1, a: 1}],363 ["second", {id: 2, a: 1}]364 ])365 const findById = (map, id) => {366 for (const [, item] of map.entries()) {367 if (item.id === id) return item368 }369 return null370 }371 const result = produce(base, draft => {372 const obj1 = findById(draft, 1)373 const obj2 = findById(draft, 2)374 obj1.a = 2375 obj2.a = 2376 })377 expect(result).not.toBe(base)378 expect(result.get("first").a).toEqual(2)379 expect(result.get("second").a).toEqual(2)380 })381382 it("supports 'values'", () => {383 const base = new Map([384 ["first", {id: 1, a: 1}],385 ["second", {id: 2, a: 1}]386 ])387 const findById = (map, id) => {388 for (const item of map.values()) {389 if (item.id === id) return item390 }391 return null392 }393 const result = produce(base, draft => {394 const obj1 = findById(draft, 1)395 const obj2 = findById(draft, 2)396 obj1.a = 2397 obj2.a = 2398 })399 expect(result).not.toBe(base)400 expect(result.get("first").a).toEqual(2)401 expect(result.get("second").a).toEqual(2)402 })403404 it("supports 'keys", () => {

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 405–408why, one step back
405 const base = new Map([
406 ["first", Symbol()],407 ["second", Symbol()]408 ])

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 409–470why, one step back
409 const result = produce(base, draft => {
410 expect([...draft.keys()]).toEqual(["first", "second"])411 draft.set("third", Symbol())412 expect([...draft.keys()]).toEqual(["first", "second", "third"])413 })414 })415416 it("supports forEach", () => {417 const key1 = {prop: "val1"}418 const key2 = {prop: "val2"}419 const base = new Map([420 ["first", {id: 1, a: 1}],421 ["second", {id: 2, a: 1}],422 [key1, {id: 3, a: 1}],423 [key2, {id: 4, a: 1}]424 ])425 const result = produce(base, draft => {426 let sum1 = 0427 draft.forEach(({a}) => {428 sum1 += a429 })430 expect(sum1).toBe(4)431 let sum2 = 0432 draft.get("first").a = 10433 draft.get("second").a = 20434 draft.get(key1).a = 30435 draft.get(key2).a = 40436 draft.forEach(({a}) => {437 sum2 += a438 })439 expect(sum2).toBe(100)440 })441 expect(result).not.toBe(base)442 expect(base.get("first").a).toEqual(1)443 expect(base.get("second").a).toEqual(1)444 expect(base.get(key1).a).toEqual(1)445 expect(base.get(key2).a).toEqual(1)446 expect(result.get("first").a).toEqual(10)447 expect(result.get("second").a).toEqual(20)448 expect(result.get(key1).a).toEqual(30)449 expect(result.get(key2).a).toEqual(40)450 })451452 it("supports forEach mutation", () => {453 const base = new Map([454 ["first", {id: 1, a: 1}],455 ["second", {id: 2, a: 1}]456 ])457 const result = produce(base, draft => {458 draft.forEach(item => {459 item.a = 100460 })461 })462 expect(result).not.toBe(base)463 expect(result.get("first").a).toEqual(100)464 expect(result.get("second").a).toEqual(100)465 })466467 it("can assign by key", () => {468 const nextState = produce(baseState, s => {469 // Map.prototype.set should return the Map itself470 const res = s.aMap.set("force", true)

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 471why, one step back
471 if (!global.USES_BUILD) expect(res).toBe(s.aMap[DRAFT_STATE].draft_)

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 472–511why, one step back
472 })
473 expect(nextState).not.toBe(baseState)474 expect(nextState.aMap).not.toBe(baseState.aMap)475 expect(nextState.aMap.get("force")).toEqual(true)476 })477478 it("can assign by a non-primitive key", () => {479 const key = {prop: "val"}480 const value = {id: 1, a: 1}481 const base = new Map([[key, value]])482 const nextState = produce(base, s => {483 s.set(key, true)484 })485 expect(nextState).not.toBe(base)486 expect(base.get(key)).toEqual(value)487 expect(nextState.get(key)).toEqual(true)488 })489490 it("state stays the same if the the same item is assigned by key", () => {491 const nextState = produce(baseState, s => {492 s.aMap.set("jediTotal", 42)493 })494 expect(nextState).toBe(baseState)495 expect(nextState.aMap).toBe(baseState.aMap)496 })497498 it("returns 'size'", () => {499 const nextState = produce(baseState, s => {500 s.aMap.set("newKey", true)501 expect(s.aMap.size).toBe(baseState.aMap.size + 1)502 })503 expect(nextState).not.toBe(baseState)504 expect(nextState.aMap).not.toBe(baseState.aMap)505 expect(nextState.aMap.get("newKey")).toEqual(true)506 expect(nextState.aMap.size).toEqual(baseState.aMap.size + 1)507 })508509 it("can use 'delete' to remove items", () => {510 const nextState = produce(baseState, s => {511 expect(s.aMap.has("jedi")).toBe(true)

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 512why, one step back
512 expect(s.aMap.delete("jedi")).toBe(true)

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 513–556why, one step back
513 expect(s.aMap.has("jedi")).toBe(false)
514 })515 expect(nextState.aMap).not.toBe(baseState.aMap)516 expect(nextState.aMap.size).toBe(baseState.aMap.size - 1)517 expect(baseState.aMap.has("jedi")).toBe(true)518 expect(nextState.aMap.has("jedi")).toBe(false)519 })520521 it("can use 'clear' to remove items", () => {522 const nextState = produce(baseState, s => {523 expect(s.aMap.size).not.toBe(0)524 s.aMap.clear()525 expect(s.aMap.size).toBe(0)526 })527 expect(nextState.aMap).not.toBe(baseState.aMap)528 expect(baseState.aMap.size).not.toBe(0)529 expect(nextState.aMap.size).toBe(0)530 })531532 it("support 'has'", () => {533 const nextState = produce(baseState, s => {534 expect(s.aMap.has("newKey")).toBe(false)535 s.aMap.set("newKey", true)536 expect(s.aMap.has("newKey")).toBe(true)537 })538 expect(nextState).not.toBe(baseState)539 expect(nextState.aMap).not.toBe(baseState.aMap)540 expect(baseState.aMap.has("newKey")).toBe(false)541 expect(nextState.aMap.has("newKey")).toBe(true)542 })543544 it("supports nested maps", () => {545 const base = new Map([["first", new Map([["second", {prop: "test"}]])]])546 const result = produce(base, draft => {547 draft.get("first").get("second").prop = "test1"548 })549 expect(result).not.toBe(base)550 expect(result.get("first")).not.toBe(base.get("first"))551 expect(result.get("first").get("second")).not.toBe(552 base.get("first").get("second")553 )554 expect(base.get("first").get("second").prop).toBe("test")555 expect(result.get("first").get("second").prop).toBe("test1")556 })

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 557–597why, one step back
557
558 it("treats void deletes as no-op", () => {559 const base = new Map([["x", 1]])560 const next = produce(base, d => {561 expect(d.delete("y")).toBe(false)562 })563 expect(next).toBe(base)564 })565566 it("revokes map proxies", () => {567 let m568 produce(baseState, s => {569 m = s.aMap570 })571 expect(() => m.get("x")).toThrow(572 "Cannot use a proxy that has been revoked"573 )574 expect(() => m.set("x", 3)).toThrow(575 "Cannot use a proxy that has been revoked"576 )577 })578579 it("does not draft map keys", () => {580 // anything else would be terribly confusing581 const key = {a: 1}582 const map = new Map([[key, 2]])583 const next = produce(map, d => {584 const dKey = Array.from(d.keys())[0]585 expect(isDraft(dKey)).toBe(false)586 expect(dKey).toBe(key)587 dKey.a += 1588 d.set(dKey, d.get(dKey) + 1)589 d.set(key, d.get(key) + 1)590 expect(d.get(key)).toBe(4)591 expect(key.a).toBe(2)592 })593 const entries = Array.from(next.entries())594 expect(entries).toEqual([[key, 4]])595 expect(entries[0][0]).toBe(key)596 expect(entries[0][0].a).toBe(2)597 })

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

lines 598–604why, one step back
598
599 it("does support instanceof Map", () => {600 const map = new Map()601 produce(map, d => {602 expect(d instanceof Map).toBeTruthy()603 })604 })

feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1

lines 605–618why, one step back
605
606 it("handles clear correctly", () => {607 const map = new Map([608 ["a", 1],609 ["c", 3]610 ])611 const next = produce(map, draft => {612 draft.delete("a")613 draft.set("b", 2)614 draft.set("c", 4)615 draft.clear()616 })617 expect(next).toEqual(new Map())618 })

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 619–622why, one step back
619 })
620621 describe("set drafts", () => {622 it("supports iteration", () => {

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 623–626why, one step back
623 const base = new Set([
624 {id: 1, a: 1},625 {id: 2, a: 1}626 ])

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 627–639why, one step back
627 const findById = (set, id) => {
628 for (const item of set) {629 if (item.id === id) return item630 }631 return null632 }633 const result = produce(base, draft => {634 const obj1 = findById(draft, 1)635 const obj2 = findById(draft, 2)636 obj1.a = 2637 obj2.a = 2638 })639 expect(result).not.toBe(base)

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 640–651why, one step back
640 expect(base).toEqual(
641 new Set([642 {id: 1, a: 1},643 {id: 2, a: 1}644 ])645 )646 expect(result).toEqual(647 new Set([648 {id: 1, a: 2},649 {id: 2, a: 2}650 ])651 )

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 652–654why, one step back
652 })
653654 it("supports 'entries'", () => {

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 655–658why, one step back
655 const base = new Set([
656 {id: 1, a: 1},657 {id: 2, a: 1}658 ])

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 659–672why, one step back
659 const findById = (set, id) => {
660 for (const [item1, item2] of set.entries()) {661 expect(item1).toBe(item2)662 if (item1.id === id) return item1663 }664 return null665 }666 const result = produce(base, draft => {667 const obj1 = findById(draft, 1)668 const obj2 = findById(draft, 2)669 obj1.a = 2670 obj2.a = 2671 })672 expect(result).not.toBe(base)

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 673–684why, one step back
673 expect(base).toEqual(
674 new Set([675 {id: 1, a: 1},676 {id: 2, a: 1}677 ])678 )679 expect(result).toEqual(680 new Set([681 {id: 1, a: 2},682 {id: 2, a: 2}683 ])684 )

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 685–687why, one step back
685 })
686687 it("supports 'values'", () => {

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 688–691why, one step back
688 const base = new Set([
689 {id: 1, a: 1},690 {id: 2, a: 1}691 ])

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 692–704why, one step back
692 const findById = (set, id) => {
693 for (const item of set.values()) {694 if (item.id === id) return item695 }696 return null697 }698 const result = produce(base, draft => {699 const obj1 = findById(draft, 1)700 const obj2 = findById(draft, 2)701 obj1.a = 2702 obj2.a = 2703 })704 expect(result).not.toBe(base)

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 705–716why, one step back
705 expect(base).toEqual(
706 new Set([707 {id: 1, a: 1},708 {id: 2, a: 1}709 ])710 )711 expect(result).toEqual(712 new Set([713 {id: 1, a: 2},714 {id: 2, a: 2}715 ])716 )

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 717–719why, one step back
717 })
718719 it("supports 'keys'", () => {

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 720–723why, one step back
720 const base = new Set([
721 {id: 1, a: 1},722 {id: 2, a: 1}723 ])

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 724–736why, one step back
724 const findById = (set, id) => {
725 for (const item of set.keys()) {726 if (item.id === id) return item727 }728 return null729 }730 const result = produce(base, draft => {731 const obj1 = findById(draft, 1)732 const obj2 = findById(draft, 2)733 obj1.a = 2734 obj2.a = 2735 })736 expect(result).not.toBe(base)

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 737–748why, one step back
737 expect(base).toEqual(
738 new Set([739 {id: 1, a: 1},740 {id: 2, a: 1}741 ])742 )743 expect(result).toEqual(744 new Set([745 {id: 1, a: 2},746 {id: 2, a: 2}747 ])748 )

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 749–751why, one step back
749 })
750751 it("supports forEach with mutation after reads", () => {

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 752–755why, one step back
752 const base = new Set([
753 {id: 1, a: 1},754 {id: 2, a: 2}755 ])

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 756–769why, one step back
756 const result = produce(base, draft => {
757 let sum1 = 0758 draft.forEach(({a}) => {759 sum1 += a760 })761 expect(sum1).toBe(3)762 let sum2 = 0763 draft.forEach(item => {764 item.a += 10765 sum2 += item.a766 })767 expect(sum2).toBe(23)768 })769 expect(result).not.toBe(base)

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 770–781why, one step back
770 expect(base).toEqual(
771 new Set([772 {id: 1, a: 1},773 {id: 2, a: 2}774 ])775 )776 expect(result).toEqual(777 new Set([778 {id: 1, a: 11},779 {id: 2, a: 12}780 ])781 )

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 782–795why, one step back
782 })
783784 it("state stays the same if the same item is added", () => {785 const nextState = produce(baseState, s => {786 s.aSet.add("Luke")787 })788 expect(nextState).toBe(baseState)789 expect(nextState.aSet).toBe(baseState.aSet)790 })791792 it("can add new items", () => {793 const nextState = produce(baseState, s => {794 // Set.prototype.set should return the Set itself795 const res = s.aSet.add("force")

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 796why, one step back
796 if (!global.USES_BUILD) expect(res).toBe(s.aSet[DRAFT_STATE].draft_)

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 797–817why, one step back
797 })
798 expect(nextState).not.toBe(baseState)799 expect(nextState.aSet).not.toBe(baseState.aSet)800 expect(baseState.aSet.has("force")).toBe(false)801 expect(nextState.aSet.has("force")).toBe(true)802 })803804 it("returns 'size'", () => {805 const nextState = produce(baseState, s => {806 s.aSet.add("newKey")807 expect(s.aSet.size).toBe(baseState.aSet.size + 1)808 })809 expect(nextState).not.toBe(baseState)810 expect(nextState.aSet).not.toBe(baseState.aSet)811 expect(nextState.aSet.has("newKey")).toBe(true)812 expect(nextState.aSet.size).toEqual(baseState.aSet.size + 1)813 })814815 it("can use 'delete' to remove items", () => {816 const nextState = produce(baseState, s => {817 expect(s.aSet.has("Luke")).toBe(true)

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 818–819why, one step back
818 expect(s.aSet.delete("Luke")).toBe(true)
819 expect(s.aSet.delete("Luke")).toBe(false)

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 820–824why, one step back
820 expect(s.aSet.has("Luke")).toBe(false)
821 })822 expect(nextState.aSet).not.toBe(baseState.aSet)823 expect(baseState.aSet.has("Luke")).toBe(true)824 expect(nextState.aSet.has("Luke")).toBe(false)

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 825why, one step back
825 expect(nextState.aSet.size).toBe(baseState.aSet.size - 1)

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 826–859why, one step back
826 })
827828 it("can use 'clear' to remove items", () => {829 const nextState = produce(baseState, s => {830 expect(s.aSet.size).not.toBe(0)831 s.aSet.clear()832 expect(s.aSet.size).toBe(0)833 })834 expect(nextState.aSet).not.toBe(baseState.aSet)835 expect(baseState.aSet.size).not.toBe(0)836 expect(nextState.aSet.size).toBe(0)837 })838839 it("supports 'has'", () => {840 const nextState = produce(baseState, s => {841 expect(s.aSet.has("newKey")).toBe(false)842 s.aSet.add("newKey")843 expect(s.aSet.has("newKey")).toBe(true)844 })845 expect(nextState).not.toBe(baseState)846 expect(nextState.aSet).not.toBe(baseState.aSet)847 expect(baseState.aSet.has("newKey")).toBe(false)848 expect(nextState.aSet.has("newKey")).toBe(true)849 })850851 it("supports nested sets", () => {852 const base = new Set([new Set(["Serenity"])])853 const result = produce(base, draft => {854 draft.forEach(nestedItem => nestedItem.add("Firefly"))855 })856 expect(result).not.toBe(base)857 expect(base).toEqual(new Set([new Set(["Serenity"])]))858 expect(result).toEqual(new Set([new Set(["Serenity", "Firefly"])]))859 })

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 860–884why, one step back
860
861 it("supports has / delete on elements from the original", () => {862 const obj = {}863 const set = new Set([obj])864 const next = produce(set, d => {865 expect(d.has(obj)).toBe(true)866 d.add(3)867 expect(d.has(obj)).toBe(true)868 d.delete(obj)869 expect(d.has(obj)).toBe(false)870 })871 expect(next).toEqual(new Set([3]))872 })873874 it("revokes sets", () => {875 let m876 produce(baseState, s => {877 m = s.aSet878 })879 expect(() => m.has("x")).toThrow(880 "Cannot use a proxy that has been revoked"881 )882 expect(() => m.add("x")).toThrow(883 "Cannot use a proxy that has been revoked"884 )

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

lines 885–891why, one step back
885 })
886887 it("does support instanceof Set", () => {888 const set = new Set()889 produce(set, d => {890 expect(d instanceof Set).toBeTruthy()891 })

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 892why, one step back
892 })

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 893–894why, one step back
893 })
894

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 895–919why, one step back
895 it("supports `immerable` symbol on constructor", () => {
896 class One {}897 One[immerable] = true898 const baseState = new One()899 const nextState = produce(baseState, draft => {900 expect(draft).not.toBe(baseState)901 draft.foo = true902 })903 expect(nextState).not.toBe(baseState)904 expect(nextState.foo).toBeTruthy()905 })906907 it("preserves symbol properties", () => {908 const test = Symbol("test")909 const baseState = {[test]: true}910 const nextState = produce(baseState, s => {911 expect(s[test]).toBeTruthy()912 s.foo = true913 })914 expect(nextState).toEqual({915 [test]: true,916 foo: true917 })918 })919

chore: verify build during testMichel Weststratebefore adoptionef8990b

lines 920–942why, one step back
920 if (!global.USES_BUILD)
921 it("preserves non-enumerable properties", () => {922 const baseState = {}923 // Non-enumerable object property924 Object.defineProperty(baseState, "foo", {925 value: {a: 1},926 enumerable: false927 })928 // Non-enumerable primitive property929 Object.defineProperty(baseState, "bar", {930 value: 1,931 enumerable: false932 })933 const nextState = produce(baseState, s => {934 expect(s.foo).toBeTruthy()935 expect(isEnumerable(s, "foo")).toBeFalsy()936 s.bar++937 expect(isEnumerable(s, "foo")).toBeFalsy()938 s.foo.a++939 expect(isEnumerable(s, "foo")).toBeFalsy()940 })941 expect(nextState.foo).toBeTruthy()942 expect(isEnumerable(nextState, "foo")).toBeFalsy()

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 943–1134why, one step back
943 })
944945 it("throws on computed properties", () => {946 const baseState = {}947 Object.defineProperty(baseState, "foo", {948 get: () => {},949 enumerable: true950 })951 expect(() => {952 produce(baseState, s => {953 // Proxies only throw once a change is made.954 if (useProxies) {955 s.modified = true956 }957 })958 }).toThrowErrorMatchingSnapshot()959 })960961 it("allows inherited computed properties", () => {962 const proto = {}963 Object.defineProperty(proto, "foo", {964 get() {965 return this.bar966 },967 set(val) {968 this.bar = val969 }970 })971 const baseState = Object.create(proto)972 produce(baseState, s => {973 expect(s.bar).toBeUndefined()974 s.foo = {}975 expect(s.bar).toBeDefined()976 expect(s.foo).toBe(s.bar)977 })978 })979980 it("supports a base state with multiple references to an object", () => {981 const obj = {}982 const res = produce({a: obj, b: obj}, d => {983 // Two drafts are created for each occurrence of an object in the base state.984 expect(d.a).not.toBe(d.b)985 d.a.z = true986 expect(d.b.z).toBeUndefined()987 })988 expect(res.b).toBe(obj)989 expect(res.a).not.toBe(res.b)990 expect(res.a.z).toBeTruthy()991 })992993 // NOTE: Except the root draft.994 it("supports multiple references to any modified draft", () => {995 const next = produce({a: {b: 1}}, d => {996 d.a.b++997 d.b = d.a998 })999 expect(next.a).toBe(next.b)1000 })10011002 it("can rename nested objects (no changes)", () => {1003 const nextState = produce({obj: {}}, s => {1004 s.foo = s.obj1005 delete s.obj1006 })1007 expect(nextState).toEqual({foo: {}})1008 })10091010 // Very similar to the test before, but the reused object has one1011 // property changed, one added, and one removed.1012 it("can rename nested objects (with changes)", () => {1013 const nextState = produce({obj: {a: 1, b: 1}}, s => {1014 s.obj.a = true // change1015 delete s.obj.b // delete1016 s.obj.c = true // add10171018 s.foo = s.obj1019 delete s.obj1020 })1021 expect(nextState).toEqual({foo: {a: true, c: true}})1022 })10231024 it("can nest a draft in a new object (no changes)", () => {1025 const baseState = {obj: {}}1026 const nextState = produce(baseState, s => {1027 s.foo = {bar: s.obj}1028 delete s.obj1029 })1030 expect(nextState.foo.bar).toBe(baseState.obj)1031 })10321033 it("can nest a modified draft in a new object", () => {1034 const nextState = produce({obj: {a: 1, b: 1}}, s => {1035 s.obj.a = true // change1036 delete s.obj.b // delete1037 s.obj.c = true // add10381039 s.foo = {bar: s.obj}1040 delete s.obj1041 })1042 expect(nextState).toEqual({foo: {bar: {a: true, c: true}}})1043 })10441045 it("supports assigning undefined to an existing property", () => {1046 const nextState = produce(baseState, s => {1047 s.aProp = undefined1048 })1049 expect(nextState).not.toBe(baseState)1050 expect(nextState.aProp).toBe(undefined)1051 })10521053 it("supports assigning undefined to a new property", () => {1054 const baseState = {}1055 const nextState = produce(baseState, s => {1056 s.aProp = undefined1057 })1058 expect(nextState).not.toBe(baseState)1059 expect(nextState.aProp).toBe(undefined)1060 })10611062 // NOTE: ES5 drafts only protect existing properties when revoked.1063 it("revokes the draft once produce returns", () => {1064 const expectRevoked = (fn, shouldThrow = true) => {1065 if (shouldThrow) expect(fn).toThrowErrorMatchingSnapshot()1066 else expect(fn).not.toThrow()1067 }10681069 // Test object drafts:1070 let draft1071 produce({a: 1, b: 1}, s => {1072 draft = s1073 delete s.b1074 })10751076 // Access known property on object draft.1077 expectRevoked(() => {1078 draft.a1079 })10801081 // Assign known property on object draft.1082 expectRevoked(() => {1083 draft.a = true1084 })10851086 // Access unknown property on object draft.1087 expectRevoked(() => {1088 draft.z1089 }, useProxies)10901091 // Assign unknown property on object draft.1092 expectRevoked(() => {1093 draft.z = true1094 }, useProxies)10951096 // Test array drafts:1097 produce([1, 2], s => {1098 draft = s1099 s.pop()1100 })11011102 // Access known index of an array draft.1103 expectRevoked(() => {1104 draft[0]1105 })11061107 // Assign known index of an array draft.1108 expectRevoked(() => {1109 draft[0] = true1110 })11111112 // Access unknown index of an array draft.1113 expectRevoked(() => {1114 draft[1]1115 }, useProxies)11161117 // Assign unknown index of an array draft.1118 expectRevoked(() => {1119 draft[1] = true1120 }, useProxies)1121 })11221123 it("can access a child draft that was created before the draft was modified", () => {1124 produce({a: {}}, s => {1125 const before = s.a1126 s.b = 11127 expect(s.a).toBe(before)1128 })1129 })11301131 it("should reflect all changes made in the draft immediately", () => {1132 produce(baseState, draft => {1133 draft.anArray[0] = 51134 draft.anArray.unshift("test")

chore: verify build during testMichel Weststratebefore adoptionef8990b

lines 1135–1142why, one step back
1135 if (!global.USES_BUILD)
1136 expect(enumerableOnly(draft.anArray)).toEqual([1137 "test",1138 5,1139 2,1140 {c: 3},1141 11142 ])

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 1143–1426why, one step back
1143 draft.stuffz = "coffee"
1144 expect(draft.stuffz).toBe("coffee")1145 })1146 })11471148 if (useProxies)1149 it("throws when Object.defineProperty() is used on drafts", () => {1150 expect(() => {1151 produce({}, draft => {1152 Object.defineProperty(draft, "xx", {1153 enumerable: true,1154 writeable: true,1155 value: 21156 })1157 })1158 }).toThrowErrorMatchingSnapshot()1159 })11601161 it("should handle constructor correctly", () => {1162 const baseState = {1163 arr: new Array(),1164 obj: new Object()1165 }1166 const result = produce(baseState, draft => {1167 draft.arrConstructed = draft.arr.constructor(1)1168 draft.objConstructed = draft.obj.constructor(1)1169 })1170 expect(result.arrConstructed).toEqual(new Array().constructor(1))1171 expect(result.objConstructed).toEqual(new Object().constructor(1))1172 })11731174 it("should handle equality correctly - 1", () => {1175 const baseState = {1176 y: 3 / 0,1177 z: NaN1178 }1179 const nextState = produce(baseState, draft => {1180 draft.y = 4 / 01181 draft.z = NaN1182 })1183 expect(nextState).toBe(baseState)1184 })11851186 it("should handle equality correctly - 2", () => {1187 const baseState = {1188 x: -01189 }1190 const nextState = produce(baseState, draft => {1191 draft.x = +01192 })1193 expect(nextState).not.toBe(baseState)1194 expect(nextState).not.toEqual(baseState)1195 })11961197 // AKA: recursive produce calls1198 describe("nested producers", () => {1199 describe("when base state is not a draft", () => {1200 // This test ensures the global state used to manage proxies is1201 // never left in a corrupted state by a nested `produce` call.1202 it("never affects its parent producer implicitly", () => {1203 const base = {obj: {a: 1}}1204 const next = produce(base, draft => {1205 // Notice how `base.obj` is passed, not `draft.obj`1206 const obj2 = produce(base.obj, draft2 => {1207 draft2.a = 01208 })1209 expect(obj2.a).toBe(0)1210 expect(draft.obj.a).toBe(1) // effects should not be visible outside1211 })1212 expect(next).toBe(base)1213 })1214 })12151216 describe("when base state is a draft", () => {1217 it("always wraps the draft in a new draft", () => {1218 produce({}, parent => {1219 produce(parent, child => {1220 expect(child).not.toBe(parent)1221 expect(isDraft(child)).toBeTruthy()1222 expect(original(child)).toBe(parent)1223 })1224 })1225 })12261227 // Reported by: https://github.com/mweststrate/immer/issues/3431228 it("ensures each property is drafted", () => {1229 produce({a: {}, b: {}}, parent => {1230 parent.a // Access "a" but not "b"1231 produce(parent, child => {1232 child.c = 11233 expect(isDraft(child.a)).toBeTruthy()1234 expect(isDraft(child.b)).toBeTruthy()1235 })1236 })1237 })12381239 it("preserves any pending changes", () => {1240 produce({a: 1, b: 1, d: 1}, parent => {1241 parent.b = 21242 parent.c = 21243 delete parent.d1244 produce(parent, child => {1245 expect(child.a).toBe(1) // unchanged1246 expect(child.b).toBe(2) // changed1247 expect(child.c).toBe(2) // added1248 expect(child.d).toBeUndefined() // deleted1249 })1250 })1251 })1252 })12531254 describe("when base state contains a draft", () => {1255 it("wraps unowned draft with its own draft", () => {1256 produce({a: {}}, parent => {1257 produce({a: parent.a}, child => {1258 expect(child.a).not.toBe(parent.a)1259 expect(isDraft(child.a)).toBeTruthy()1260 expect(original(child.a)).toBe(parent.a)1261 })1262 })1263 })12641265 it("returns unowned draft if no changes were made", () => {1266 produce({a: {}}, parent => {1267 const result = produce({a: parent.a}, () => {})1268 expect(result.a).toBe(parent.a)1269 })1270 })12711272 it("clones the unowned draft when changes are made", () => {1273 produce({a: {}}, parent => {1274 const result = produce({a: parent.a}, child => {1275 child.a.b = 11276 })1277 expect(result.a).not.toBe(parent.a)1278 expect(result.a.b).toBe(1)1279 expect("b" in parent.a).toBeFalsy()1280 })1281 })12821283 // We cannot auto-freeze the result of a nested producer,1284 // because it may contain a draft from a parent producer.1285 it("never auto-freezes the result", () => {1286 produce({a: {}}, parent => {1287 const r = produce({a: parent.a}, child => {1288 child.b = 1 // Ensure a copy is returned.1289 })1290 expect(Object.isFrozen(r)).toBeFalsy()1291 })1292 })1293 })12941295 // "Upvalues" are variables from a parent scope.1296 it("does not finalize upvalue drafts", () => {1297 produce({a: {}, b: {}}, parent => {1298 expect(produce({}, () => parent)).toBe(parent)1299 parent.x // Ensure proxy not revoked.13001301 expect(produce({}, () => [parent])[0]).toBe(parent)1302 parent.x // Ensure proxy not revoked.13031304 expect(produce({}, () => parent.a)).toBe(parent.a)1305 parent.a.x // Ensure proxy not revoked.13061307 // Modified parent test1308 parent.c = 11309 expect(produce({}, () => [parent.b])[0]).toBe(parent.b)1310 parent.b.x // Ensure proxy not revoked.1311 })1312 })13131314 it("works with interweaved Immer instances", () => {1315 const options = {useProxies, autoFreeze}1316 const one = createPatchedImmer(options)1317 const two = createPatchedImmer(options)13181319 const base = {}1320 const result = one.produce(base, s1 =>1321 two.produce({s1}, s2 => {1322 expect(original(s2.s1)).toBe(s1)1323 s2.n = 11324 s2.s1 = one.produce({s2}, s3 => {1325 expect(original(s3.s2)).toBe(s2)1326 expect(original(s3.s2.s1)).toBe(s2.s1)1327 return s3.s2.s11328 })1329 })1330 )1331 expect(result.n).toBe(1)1332 expect(result.s1).toBe(base)1333 })1334 })13351336 if (useProxies)1337 it("throws when Object.setPrototypeOf() is used on a draft", () => {1338 produce({}, draft => {1339 expect(() =>1340 Object.setPrototypeOf(draft, Array)1341 ).toThrowErrorMatchingSnapshot()1342 })1343 })13441345 it("supports the 'in' operator", () => {1346 produce(baseState, draft => {1347 // Known property1348 expect("anArray" in draft).toBe(true)1349 expect(Reflect.has(draft, "anArray")).toBe(true)13501351 // Unknown property1352 expect("bla" in draft).toBe(false)1353 expect(Reflect.has(draft, "bla")).toBe(false)13541355 // Known index1356 expect(0 in draft.anArray).toBe(true)1357 expect("0" in draft.anArray).toBe(true)1358 expect(Reflect.has(draft.anArray, 0)).toBe(true)1359 expect(Reflect.has(draft.anArray, "0")).toBe(true)13601361 // Unknown index1362 expect(17 in draft.anArray).toBe(false)1363 expect("17" in draft.anArray).toBe(false)1364 expect(Reflect.has(draft.anArray, 17)).toBe(false)1365 expect(Reflect.has(draft.anArray, "17")).toBe(false)1366 })1367 })13681369 it("'this' should not be bound anymore - 1", () => {1370 const base = {x: 3}1371 const next1 = produce(base, function() {1372 expect(this).toBe(undefined)1373 })1374 })13751376 it("'this' should not be bound anymore - 2", () => {1377 const incrementor = produce(function() {1378 expect(this).toBe(undefined)1379 })1380 incrementor()1381 })13821383 it("should be possible to use dynamic bound this", () => {1384 const world = {1385 counter: {count: 1},1386 inc: produce(function(draft) {1387 expect(this).toBe(world)1388 draft.counter.count = this.counter.count + 11389 })1390 }13911392 expect(world.inc(world).counter.count).toBe(2)1393 })13941395 // See here: https://github.com/mweststrate/immer/issues/891396 it("supports the spread operator", () => {1397 const base = {foo: {x: 0, y: 0}, bar: [0, 0]}1398 const result = produce(base, draft => {1399 draft.foo = {x: 1, ...draft.foo, y: 1}1400 draft.bar = [1, ...draft.bar, 1]1401 })1402 expect(result).toEqual({1403 foo: {x: 0, y: 1},1404 bar: [1, 0, 0, 1]1405 })1406 })14071408 it("processes with lodash.set", () => {1409 const base = [{id: 1, a: 1}]1410 const result = produce(base, draft => {1411 lodash.set(draft, "[0].a", 2)1412 })1413 expect(base[0].a).toEqual(1)1414 expect(result[0].a).toEqual(2)1415 })14161417 it("processes with lodash.find", () => {1418 const base = [{id: 1, a: 1}]1419 const result = produce(base, draft => {1420 const obj1 = lodash.find(draft, {id: 1})1421 lodash.set(obj1, "a", 2)1422 })1423 expect(base[0].a).toEqual(1)1424 expect(result[0].a).toEqual(2)1425 })1426

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 1427–1440why, one step back
1427 it("does not draft external data", () => {
1428 const externalData = {x: 3}1429 const base = {}1430 const next = produce(base, draft => {1431 // potentially, we *could* draft external data automatically, but only if those statements are not switched...1432 draft.y = externalData1433 draft.y.x += 11434 externalData.x += 11435 })1436 expect(next).toEqual({y: {x: 5}})1437 expect(externalData.x).toBe(5)1438 expect(next.y).toBe(externalData)1439 })1440

chore: added test for #491Michel Weststratebefore adoption788dc03

lines 1441–1455why, one step back
1441 it("does not create new state unnecessary, #491", () => {
1442 const a = {highlight: true}1443 const next1 = produce(a, draft => {1444 draft.highlight = false1445 draft.highlight = true1446 })1447 // See explanation in issue1448 expect(next1).not.toBe(a)14491450 const next2 = produce(a, draft => {1451 draft.highlight = true1452 })1453 expect(next2).toBe(a)1454 })1455

test: tried to reproduce #469Michel Weststratebefore adoptione504e0b

line 1456why, one step back
1456 autoFreeze &&

chore: add test for #462Michel Weststratebefore adoptionce6a3c2

lines 1457–1474why, one step back
1457 test.skip("issue #462 - frozen", () => {
1458 var origin = {1459 a: {1460 value: "no"1461 },1462 b: {1463 value: "no"1464 }1465 }1466 var im = produce(origin, origin => {1467 origin.a.value = "im"1468 })1469 expect(() => (origin.b.value = "yes")).toThrow() // should throw!1470 // to prevent this...:1471 // expect(im.b.value).toBe('no');1472 })14731474 autoFreeze &&

test: tried to reproduce #469Michel Weststratebefore adoptione504e0b

lines 1475–1492why, one step back
1475 test("issue #469, state not frozen", () => {
1476 const project = produce(1477 {1478 id: 1,1479 tracks: [{id: 1}]1480 },1481 () => {}1482 )14831484 expect(() => {1485 project.id = 2 // Does not throw error1486 }).toThrowError("Cannot assign to read only property 'id'")14871488 expect(() => {1489 Object.assign(project, {id: 2}) // Uncaught TypeError: Cannot assign to read only property 'id' of object '#<Object>'1490 }).toThrowError("Cannot assign to read only property 'id'")1491 })1492

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 1493–1750why, one step back
1493 describe("recipe functions", () => {
1494 it("can return a new object", () => {1495 const base = {x: 3}1496 const res = produce(base, d => {1497 return {x: d.x + 1}1498 })1499 expect(res).not.toBe(base)1500 expect(res).toEqual({x: 4})1501 })15021503 it("can return the draft", () => {1504 const base = {x: 3}1505 const res = produce(base, d => {1506 d.x = 41507 return d1508 })1509 expect(res).not.toBe(base)1510 expect(res).toEqual({x: 4})1511 })15121513 it("can return an unmodified child draft", () => {1514 const base = {a: {}}1515 const res = produce(base, d => {1516 return d.a1517 })1518 expect(res).toBe(base.a)1519 })15201521 // TODO: Avoid throwing if only the child draft was modified.1522 it("cannot return a modified child draft", () => {1523 const base = {a: {}}1524 expect(() => {1525 produce(base, d => {1526 d.a.b = 11527 return d.a1528 })1529 }).toThrowErrorMatchingSnapshot()1530 })15311532 it("can return a frozen object", () => {1533 const res = deepFreeze([{x: 3}])1534 expect(produce({}, () => res)).toBe(res)1535 })15361537 it("can return an object with two references to another object", () => {1538 const next = produce({}, d => {1539 const obj = {}1540 return {obj, arr: [obj]}1541 })1542 expect(next.obj).toBe(next.arr[0])1543 })15441545 it("can return an object with two references to an unmodified draft", () => {1546 const base = {a: {}}1547 const next = produce(base, d => {1548 return [d.a, d.a]1549 })1550 expect(next[0]).toBe(base.a)1551 expect(next[0]).toBe(next[1])1552 })15531554 it("cannot return an object that references itself", () => {1555 const res = {}1556 res.self = res1557 expect(() => {1558 produce(res, () => res.self)1559 }).toThrowErrorMatchingSnapshot()1560 })1561 })15621563 describe("async recipe function", () => {1564 it("can modify the draft", () => {1565 const base = {a: 0, b: 0}1566 return produce(base, async d => {1567 d.a = 11568 await Promise.resolve()1569 d.b = 11570 }).then(res => {1571 expect(res).not.toBe(base)1572 expect(res).toEqual({a: 1, b: 1})1573 })1574 })15751576 it("works with rejected promises", () => {1577 let draft1578 const base = {a: 0, b: 0}1579 const err = new Error("passed")1580 return produce(base, d => {1581 draft = d1582 draft.b = 11583 return Promise.reject(err)1584 }).then(1585 () => {1586 throw "failed"1587 },1588 e => {1589 expect(e).toBe(err)1590 expect(() => draft.a).toThrowErrorMatchingSnapshot()1591 }1592 )1593 })15941595 it("supports recursive produce calls after await", () => {1596 const base = {obj: {k: 1}}1597 return produce(base, d => {1598 const obj = d.obj1599 delete d.obj1600 return Promise.resolve().then(() => {1601 d.a = produce({}, d => {1602 d.b = obj // Assign a draft owned by the parent scope.1603 })16041605 // Auto-freezing is prevented when an unowned draft exists.1606 expect(Object.isFrozen(d.a)).toBeFalsy()16071608 // Ensure `obj` is not revoked.1609 obj.c = 11610 })1611 }).then(res => {1612 expect(res).not.toBe(base)1613 expect(res).toEqual({1614 a: {b: {k: 1, c: 1}}1615 })1616 })1617 })1618 })16191620 it("throws when the draft is modified and another object is returned", () => {1621 const base = {x: 3}1622 expect(() => {1623 produce(base, draft => {1624 draft.x = 41625 return {x: 5}1626 })1627 }).toThrowErrorMatchingSnapshot()1628 })16291630 it("should fix #117 - 1", () => {1631 const reducer = (state, action) =>1632 produce(state, draft => {1633 switch (action.type) {1634 case "SET_STARTING_DOTS":1635 return draft.availableStartingDots.map(a => a)1636 default:1637 break1638 }1639 })1640 const base = {1641 availableStartingDots: [1642 {dots: 4, count: 1},1643 {dots: 3, count: 2},1644 {dots: 2, count: 3},1645 {dots: 1, count: 4}1646 ]1647 }1648 const next = reducer(base, {type: "SET_STARTING_DOTS"})1649 expect(next).toEqual(base.availableStartingDots)1650 expect(next).not.toBe(base.availableStartingDots)1651 })16521653 it("should fix #117 - 2", () => {1654 const reducer = (state, action) =>1655 produce(state, draft => {1656 switch (action.type) {1657 case "SET_STARTING_DOTS":1658 return {1659 dots: draft.availableStartingDots.map(a => a)1660 }1661 default:1662 break1663 }1664 })1665 const base = {1666 availableStartingDots: [1667 {dots: 4, count: 1},1668 {dots: 3, count: 2},1669 {dots: 2, count: 3},1670 {dots: 1, count: 4}1671 ]1672 }1673 const next = reducer(base, {type: "SET_STARTING_DOTS"})1674 expect(next).toEqual({dots: base.availableStartingDots})1675 })16761677 it("cannot always detect noop assignments - 0", () => {1678 const baseState = {x: {y: 3}}1679 const nextState = produce(baseState, d => {1680 const a = d.x1681 d.x = a1682 })1683 expect(nextState).toBe(baseState)1684 })16851686 it("cannot always detect noop assignments - 1", () => {1687 const baseState = {x: {y: 3}}1688 const nextState = produce(baseState, d => {1689 const a = d.x1690 d.x = 41691 d.x = a1692 })1693 // Ideally, this should actually be the same instances1694 // but this would be pretty expensive to detect,1695 // so we don't atm1696 expect(nextState).not.toBe(baseState)1697 })16981699 it("cannot always detect noop assignments - 2", () => {1700 const baseState = {x: {y: 3}}1701 const nextState = produce(baseState, d => {1702 const a = d.x1703 const stuff = a.y + 31704 d.x = 41705 d.x = a1706 })1707 // Ideally, this should actually be the same instances1708 // but this would be pretty expensive to detect,1709 // so we don't atm1710 expect(nextState).not.toBe(baseState)1711 })17121713 it("cannot always detect noop assignments - 3", () => {1714 const baseState = {x: 3}1715 const nextState = produce(baseState, d => {1716 d.x = 31717 })1718 expect(nextState).toBe(baseState)1719 })17201721 it("cannot always detect noop assignments - 4", () => {1722 const baseState = {x: 3}1723 const nextState = produce(baseState, d => {1724 d.x = 41725 d.x = 31726 })1727 // Ideally, this should actually be the same instances1728 // but this would be pretty expensive to detect,1729 // so we don't atm1730 expect(nextState).not.toBe(baseState)1731 })17321733 it("cannot produce undefined by returning undefined", () => {1734 const base = 31735 expect(produce(base, () => 4)).toBe(4)1736 expect(produce(base, () => null)).toBe(null)1737 expect(produce(base, () => undefined)).toBe(3)1738 expect(produce(base, () => {})).toBe(3)1739 expect(produce(base, () => nothing)).toBe(undefined)17401741 expect(produce({}, () => undefined)).toEqual({})1742 expect(produce({}, () => nothing)).toBe(undefined)1743 expect(produce(3, () => nothing)).toBe(undefined)17441745 expect(produce(() => undefined)({})).toEqual({})1746 expect(produce(() => nothing)({})).toBe(undefined)1747 expect(produce(() => nothing)(3)).toBe(undefined)1748 })17491750 describe("base state type", () => {

chore: verify build during testMichel Weststratebefore adoptionef8990b

line 1751why, one step back
1751 if (!global.USES_BUILD) testObjectTypes(produce)

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 1752–1764why, one step back
1752 testLiteralTypes(produce)
1753 })17541755 afterEach(() => {1756 expect(baseState).toBe(origBaseState)1757 expect(baseState).toEqual(createBaseState())1758 })17591760 class Foo {}1761 function createBaseState() {1762 const data = {1763 anInstance: new Foo(),1764 anArray: [3, 2, {c: 3}, 1],

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 1765–1776why, one step back
1765 aMap: new Map([
1766 ["jedi", {name: "Luke", skill: 10}],1767 ["jediTotal", 42],1768 ["force", "these aren't the droids you're looking for"]1769 ]),1770 aSet: new Set([1771 "Luke",1772 42,1773 {1774 jedi: "Yoda"1775 }1776 ]),

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 1777–1788why, one step back
1777 aProp: "hi",
1778 anObject: {1779 nested: {1780 yummie: true1781 },1782 coffee: false1783 }1784 }1785 return autoFreeze ? deepFreeze(data) : data1786 }1787 })1788

fix: Fixed issue where drafts from nested producers were frozen. Fixes #522, #524Michel Weststratebefore adoptionacc5e54

lines 1789–1882why, one step back
1789 it(`works with spread #524 - ${name}`, () => {
1790 const state = {1791 level1: {1792 level2: {1793 level3: "data"1794 }1795 }1796 }17971798 const nextState = produce(state, draft => {1799 return {...draft}1800 })1801 const nextState1 = produce(state, draft => {1802 const newLevel1 = produce(draft.level1, level1Draft => {1803 return {...level1Draft}1804 })1805 draft.level1 = newLevel11806 })18071808 const nextState2 = produce(state, draft => {1809 const newLevel1 = produce(draft.level1, level1Draft => {1810 return {...level1Draft}1811 })1812 return {1813 level1: newLevel11814 }1815 })18161817 const nextState3 = produce(state, draft => {1818 const newLevel1 = produce(draft.level1, level1Draft => {1819 return Object.assign({}, level1Draft)1820 })1821 return Object.assign(draft, {1822 level1: newLevel11823 })1824 })18251826 const expected = {level1: {level2: {level3: "data"}}}1827 expect(nextState1).toEqual(expected)1828 expect(nextState2).toEqual(expected)1829 expect(nextState3).toEqual(expected)1830 })18311832 it(`Something with nested producers #522 ${name}`, () => {1833 function initialStateFactory() {1834 return {1835 substate: {1836 array: [1837 {id: "id1", value: 0},1838 {id: "id2", value: 0}1839 ]1840 },1841 array: [1842 {id: "id1", value: 0},1843 {id: "id2", value: 0}1844 ]1845 }1846 }18471848 const globalProducer = produce(draft => {1849 draft.substate = subProducer(draft.substate)1850 draft.array = arrayProducer(draft.array)1851 }, initialStateFactory())18521853 const subProducer = produce(draftSubState => {1854 draftSubState.array = arrayProducer(draftSubState.array)1855 })18561857 const arrayProducer = produce(draftArray => {1858 draftArray[0].value += 51859 })18601861 {1862 const state = globalProducer(undefined)1863 expect(state.array[0].value).toBe(5)1864 expect(state.array.length).toBe(2)1865 expect(state.array[1]).toMatchObject({1866 id: "id2",1867 value: 01868 })1869 }18701871 {1872 const state = globalProducer(undefined)1873 expect(state.substate.array[0].value).toBe(5)1874 expect(state.substate.array.length).toBe(2)1875 expect(state.substate.array[1]).toMatchObject({1876 id: "id2",1877 value: 01878 })1879 expect(state.substate.array).toMatchObject(state.array)1880 }1881 })1882

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 1883–1920why, one step back
1883 describe(`isDraft - ${name}`, () => {
1884 it("returns true for object drafts", () => {1885 produce({}, state => {1886 expect(isDraft(state)).toBeTruthy()1887 })1888 })1889 it("returns true for array drafts", () => {1890 produce([], state => {1891 expect(isDraft(state)).toBeTruthy()1892 })1893 })1894 it("returns true for objects nested in object drafts", () => {1895 produce({a: {b: {}}}, state => {1896 expect(isDraft(state.a)).toBeTruthy()1897 expect(isDraft(state.a.b)).toBeTruthy()1898 })1899 })1900 it("returns false for new objects added to a draft", () => {1901 produce({}, state => {1902 state.a = {}1903 expect(isDraft(state.a)).toBeFalsy()1904 })1905 })1906 it("returns false for objects returned by the producer", () => {1907 const object = produce(null, Object.create)1908 expect(isDraft(object)).toBeFalsy()1909 })1910 it("returns false for arrays returned by the producer", () => {1911 const array = produce(null, _ => [])1912 expect(isDraft(array)).toBeFalsy()1913 })1914 it("returns false for object drafts returned by the producer", () => {1915 const object = produce({}, state => state)1916 expect(isDraft(object)).toBeFalsy()1917 })1918 it("returns false for array drafts returned by the producer", () => {1919 const array = produce([], state => state)1920 expect(isDraft(array)).toBeFalsy()

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 1921–1929why, one step back
1921 })
1922 })19231924 describe(`complex nesting map / set / object`, () => {1925 const a = {a: 1}1926 const b = {b: 2}1927 const c = {c: 3}1928 const set1 = new Set([a, b])1929 const set2 = new Set([c])

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 1930–1933why, one step back
1930 const map = new Map([
1931 ["set1", set1],1932 ["set2", set2]1933 ])

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 1934–1982why, one step back
1934 const base = {map}
19351936 function first(set) {1937 return Array.from(set.values())[0]1938 }19391940 function second(set) {1941 return Array.from(set.values())[1]1942 }19431944 test(`modify deep object`, () => {1945 const [res, patches] = produceWithPatches(base, draft => {1946 const v = first(draft.map.get("set1"))1947 expect(original(v)).toBe(a)1948 expect(v).toEqual(a)1949 expect(v).not.toBe(a)1950 v.a++1951 })1952 expect(res).toMatchSnapshot()1953 expect(patches).toMatchSnapshot()1954 expect(a.a).toBe(1)1955 expect(base.map.get("set1")).toBe(set1)1956 expect(first(base.map.get("set1"))).toBe(a)1957 expect(res).not.toBe(base)1958 expect(res.map).not.toBe(base.map)1959 expect(res.map.get("set1")).not.toBe(base.map.get("set1"))1960 expect(second(base.map.get("set1"))).toBe(b)1961 expect(base.map.get("set2")).toBe(set2)1962 expect(first(res.map.get("set1"))).toEqual({a: 2})1963 })19641965 test(`modify deep object - keep value`, () => {1966 const [res, patches] = produceWithPatches(base, draft => {1967 const v = first(draft.map.get("set1"))1968 expect(original(v)).toBe(a)1969 expect(v).toEqual(a)1970 expect(v).not.toBe(a)1971 v.a = 1 // same value1972 })1973 expect(a.a).toBe(1)1974 expect(base.map.get("set1")).toBe(set1)1975 expect(first(base.map.get("set1"))).toBe(a)1976 expect(res).toBe(base)1977 expect(res.map).toBe(base.map)1978 expect(res.map.get("set1")).toBe(base.map.get("set1"))1979 expect(first(res.map.get("set1"))).toBe(a)1980 expect(second(base.map.get("set1"))).toBe(b)1981 expect(base.map.get("set2")).toBe(set2)1982 expect(patches.length).toBe(0)

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 1983–1986why, one step back
1983 })
1984 })1985}1986

chore: improve more testsaleclarsonbefore adoption4f59b12

line 1987why, one step back
1987function testObjectTypes(produce) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 1988–2040why, one step back
1988 class Foo {
1989 constructor(foo) {1990 this.foo = foo1991 this[immerable] = true1992 }1993 }1994 const values = {1995 "empty object": {},1996 "plain object": {a: 1, b: 2},1997 "object (no prototype)": Object.create(null),1998 "empty array": [],1999 "plain array": [1, 2],2000 "class instance (draftable)": new Foo(1)

The first 2,000 of 2,103 lines.