base.js
This file
Path
__tests__/base.js
1,731 lines · 64.6 KiB · JavaScript
Last changed by
feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5
History of this fileTake it
The walk stopped after 200 commits: lines older than 53e9ce8 point at it.
perf: proxy assignaleclarsonbefore adoption53e9ce8
line 1why, one step back1"use strict"feat: limited class supportaleclarsonbefore adoption9877d64
line 2why, one step back2import {Immer, nothing, original, isDraft, immerable} from "../src/index"fix: Map.prototype.set should return a draftAndrey Goncharovbefore adoption7001201
line 3why, one step back3import {each, shallowCopy, isEnumerable, DRAFT_STATE} from "../src/common"perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 4–9why, one step back4import deepFreeze from "deep-freeze"5import cloneDeep from "lodash.clonedeep"6import * as lodash from "lodash"78jest.setTimeout(1000)9chore: improve more testsaleclarsonbefore adoption4f59b12
lines 10–13why, one step back10test("immer should have no dependencies", () => {11 expect(require("../package.json").dependencies).toBeUndefined()12})13perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 14–20why, one step back14runBaseTest("proxy (no freeze)", true, false)15runBaseTest("proxy (autofreeze)", true, true)16runBaseTest("proxy (autofreeze)(patch listener)", true, true, true)17runBaseTest("es5 (no freeze)", false, false)18runBaseTest("es5 (autofreeze)", false, true)19runBaseTest("es5 (autofreeze)(patch listener)", false, true, true)20test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 21why, one step back21function runBaseTest(name, useProxies, autoFreeze, useListener) {test: patch the `produce` function for interweaved Immer instances tooaleclarsonbefore adoption31bbe64
lines 22–23why, one step back22 const listener = useListener ? function() {} : undefined23 const {produce} = createPatchedImmer({test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 24–27why, one step back24 useProxies,25 autoFreeze26 })27test: patch the `produce` function for interweaved Immer instances tooaleclarsonbefore adoption31bbe64
lines 28–31why, one step back28 // When `useListener` is true, append a function to the arguments of every29 // uncurried `produce` call in every test. This makes tests easier to read.30 function createPatchedImmer(options) {31 const immer = new Immer(options)test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 32why, one step back32test: patch the `produce` function for interweaved Immer instances tooaleclarsonbefore adoption31bbe64
lines 33–41why, one step back33 const {produce} = immer34 immer.produce = (...args) =>35 typeof args[1] === "function" && args.length < 336 ? produce(...args, listener)37 : produce(...args)3839 return immer40 }41perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 42–49why, one step back42 describe(`base functionality - ${name}`, () => {43 let baseState44 let origBaseState4546 beforeEach(() => {47 origBaseState = baseState = createBaseState()48 })49chore: improve some testsaleclarsonbefore adoptionefd47f6
line 50why, one step back50 it("returns the original state when no changes are made", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 51–54why, one step back51 const nextState = produce(baseState, s => {52 expect(s.aProp).toBe("hi")53 expect(s.anObject.nested).toMatchObject({yummie: true})54 })perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 55–57why, one step back55 expect(nextState).toBe(baseState)56 })57chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 58–59why, one step back58 it("does structural sharing", () => {59 const random = Math.random()test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 60why, one step back60 const nextState = produce(baseState, s => {chore: improve some testsaleclarsonbefore adoptionefd47f6
line 61why, one step back61 s.aProp = randomtest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 62why, one step back62 })perf: proxy assignaleclarsonbefore adoption53e9ce8
line 63why, one step back63 expect(nextState).not.toBe(baseState)chore: improve some testsaleclarsonbefore adoptionefd47f6
line 64why, one step back64 expect(nextState.aProp).toBe(random)perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 65–68why, one step back65 expect(nextState.nested).toBe(baseState.nested)66 })6768 it("deep change bubbles up", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 69–71why, one step back69 const nextState = produce(baseState, s => {70 s.anObject.nested.yummie = false71 })perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 72–79why, one step back72 expect(nextState).not.toBe(baseState)73 expect(nextState.anObject).not.toBe(baseState.anObject)74 expect(baseState.anObject.nested.yummie).toBe(true)75 expect(nextState.anObject.nested.yummie).toBe(false)76 expect(nextState.anArray).toBe(baseState.anArray)77 })7879 it("can add props", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 80–82why, one step back80 const nextState = produce(baseState, s => {81 s.anObject.cookie = {tasty: true}82 })perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 83–89why, one step back83 expect(nextState).not.toBe(baseState)84 expect(nextState.anObject).not.toBe(baseState.anObject)85 expect(nextState.anObject.nested).toBe(baseState.anObject.nested)86 expect(nextState.anObject.cookie).toEqual({tasty: true})87 })8889 it("can delete props", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 90–92why, one step back90 const nextState = produce(baseState, s => {91 delete s.anObject.nested92 })perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 93–97why, one step back93 expect(nextState).not.toBe(baseState)94 expect(nextState.anObject).not.toBe(baseState.anObject)95 expect(nextState.anObject.nested).toBe(undefined)96 })97test: deleting props added in the produceraleclarsonbefore adoption691e73b
lines 98–99why, one step back98 // Found by: https://github.com/mweststrate/immer/pull/26799 it("can delete props added in the producer", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 100–103why, one step back100 const nextState = produce(baseState, s => {101 s.anObject.test = true102 delete s.anObject.test103 })test: deleting props added in the produceraleclarsonbefore adoption691e73b
lines 104–112why, one step back104 if (useProxies) {105 expect(nextState).not.toBe(baseState)106 expect(nextState).toEqual(baseState)107 } else {108 // The copy is avoided in ES5.109 expect(nextState).toBe(baseState)110 }111 })112test: delete then set an existing propertyAlec Larsonbefore adoption5121d36
lines 113–138why, one step back113 // Found by: https://github.com/mweststrate/immer/issues/328114 it("can set a property that was just deleted", () => {115 const baseState = {a: 1}116 const nextState = produce(baseState, s => {117 delete s.a118 s.a = 2119 })120 expect(nextState.a).toBe(2)121 })122123 it("can set a property to its original value after deleting it", () => {124 const baseState = {a: {b: 1}}125 const nextState = produce(baseState, s => {126 const a = s.a127 delete s.a128 s.a = a129 })130 if (useProxies) {131 expect(nextState).not.toBe(baseState)132 expect(nextState).toEqual(baseState)133 } else {134 // The copy is avoided in ES5.135 expect(nextState).toBe(baseState)136 }137 })138chore: improve more testsaleclarsonbefore adoption4f59b12
lines 139–141why, one step back139 it("can get property descriptors", () => {140 const getDescriptor = Object.getOwnPropertyDescriptor141 const baseState = deepFreeze([{a: 1}])test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
lines 142–148why, one step back142 produce(baseState, arr => {143 const obj = arr[0]144 const desc = {145 configurable: true,146 enumerable: true,147 ...(useProxies && {writable: true})148 }perf: proxy assignaleclarsonbefore adoption53e9ce8
line 149why, one step back149chore: improve more testsaleclarsonbefore adoption4f59b12
line 150why, one step back150 // Known propertytest: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
lines 151–152why, one step back151 expect(getDescriptor(obj, "a")).toMatchObject(desc)152 expect(getDescriptor(arr, 0)).toMatchObject(desc)chore: improve more testsaleclarsonbefore adoption4f59b12
lines 153–155why, one step back153154 // Deleted property155 delete obj.atest: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
line 156why, one step back156 arr.pop()chore: improve more testsaleclarsonbefore adoption4f59b12
line 157why, one step back157 expect(getDescriptor(obj, "a")).toBeUndefined()test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
line 158why, one step back158 expect(getDescriptor(arr, 0)).toBeUndefined()chore: improve more testsaleclarsonbefore adoption4f59b12
lines 159–161why, one step back159160 // Unknown property161 expect(getDescriptor(obj, "b")).toBeUndefined()test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
line 162why, one step back162 expect(getDescriptor(arr, 100)).toBeUndefined()chore: improve more testsaleclarsonbefore adoption4f59b12
lines 163–165why, one step back163164 // Added property165 obj.b = 2test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
line 166why, one step back166 arr[100] = 1chore: improve more testsaleclarsonbefore adoption4f59b12
line 167why, one step back167 expect(getDescriptor(obj, "b")).toBeDefined()test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
line 168why, one step back168 expect(getDescriptor(arr, 100)).toBeDefined()test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 169why, one step back169 })perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 170–171why, one step back170 })171chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 172–178why, one step back172 describe("array drafts", () => {173 it("supports Array.isArray()", () => {174 const nextState = produce(baseState, s => {175 expect(Array.isArray(s.anArray)).toBeTruthy()176 s.anArray.push(1)177 })178 expect(Array.isArray(nextState.anArray)).toBeTruthy()perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 179–180why, one step back179 })180chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 181–186why, one step back181 it("supports index access", () => {182 const value = baseState.anArray[0]183 const nextState = produce(baseState, s => {184 expect(s.anArray[0]).toBe(value)185 })186 expect(nextState).toBe(baseState)test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 187why, one step back187 })chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 188–204why, one step back188189 it("supports iteration", () => {190 const base = [{id: 1, a: 1}, {id: 2, a: 1}]191 const findById = (collection, id) => {192 for (const item of collection) {193 if (item.id === id) return item194 }195 return null196 }197 const result = produce(base, draft => {198 const obj1 = findById(draft, 1)199 const obj2 = findById(draft, 2)200 obj1.a = 2201 obj2.a = 2202 })203 expect(result[0].a).toEqual(2)204 expect(result[1].a).toEqual(2)perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 205–206why, one step back205 })206chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 207–213why, one step back207 it("can assign an index via bracket notation", () => {208 const nextState = produce(baseState, s => {209 s.anArray[3] = true210 })211 expect(nextState).not.toBe(baseState)212 expect(nextState.anArray).not.toBe(baseState.anArray)213 expect(nextState.anArray[3]).toEqual(true)test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 214why, one step back214 })perf: proxy assignaleclarsonbefore adoption53e9ce8
line 215why, one step back215chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 216–222why, one step back216 it("can use splice() to both add and remove items", () => {217 const nextState = produce(baseState, s => {218 s.anArray.splice(1, 1, "a", "b")219 })220 expect(nextState.anArray).not.toBe(baseState.anArray)221 expect(nextState.anArray[1]).toBe("a")222 expect(nextState.anArray[2]).toBe("b")test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 223why, one step back223 })perf: proxy assignaleclarsonbefore adoption53e9ce8
line 224why, one step back224chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 225–231why, one step back225 it("can truncate via the length property", () => {226 const baseLength = baseState.anArray.length227 const nextState = produce(baseState, s => {228 s.anArray.length = baseLength - 1229 })230 expect(nextState.anArray).not.toBe(baseState.anArray)231 expect(nextState.anArray.length).toBe(baseLength - 1)test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 232why, one step back232 })perf: proxy assignaleclarsonbefore adoption53e9ce8
line 233why, one step back233chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 234–241why, one step back234 it("can extend via the length property", () => {235 const baseLength = baseState.anArray.length236 const nextState = produce(baseState, s => {237 s.anArray.length = baseLength + 1238 })239 expect(nextState.anArray).not.toBe(baseState.anArray)240 expect(nextState.anArray.length).toBe(baseLength + 1)241 })perf: proxy assignaleclarsonbefore adoption53e9ce8
line 242why, one step back242chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 243–249why, one step back243 // Reported here: https://github.com/mweststrate/immer/issues/116244 it("can pop then push", () => {245 const nextState = produce([1, 2, 3], s => {246 s.pop()247 s.push(100)248 })249 expect(nextState).toEqual([1, 2, 100])test: rename and move a testaleclarsonbefore adoption8e0325c
lines 250–251why, one step back250 })251chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 252–258why, one step back252 it("can be sorted", () => {253 const baseState = [3, 1, 2]254 const nextState = produce(baseState, s => {255 s.sort()256 })257 expect(nextState).not.toBe(baseState)258 expect(nextState).toEqual([1, 2, 3])test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 259why, one step back259 })perf: proxy assignaleclarsonbefore adoption53e9ce8
line 260why, one step back260chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 261–269why, one step back261 it("supports modifying nested objects", () => {262 const baseState = [{a: 1}, {}]263 const nextState = produce(baseState, s => {264 s[0].a++265 s[1].a = 0266 })267 expect(nextState).not.toBe(baseState)268 expect(nextState[0].a).toBe(2)269 expect(nextState[1].a).toBe(0)test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 270why, one step back270 })perf: proxy assignaleclarsonbefore adoption53e9ce8
line 271why, one step back271chore: improve more testsaleclarsonbefore adoption4f59b12
lines 272–276why, one step back272 it("never preserves non-numeric properties", () => {273 const baseState = []274 baseState.x = 7275 const nextState = produce(baseState, s => {276 s.push(3)test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 277why, one step back277 })chore: improve more testsaleclarsonbefore adoption4f59b12
line 278why, one step back278 expect("x" in nextState).toBeFalsy()test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 279why, one step back279 })chore: improve more testsaleclarsonbefore adoption4f59b12
lines 280–286why, one step back280281 if (useProxies) {282 it("throws when a non-numeric property is added", () => {283 expect(() => {284 produce([], d => {285 d.x = 3286 })test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91
line 287why, one step back287 }).toThrowErrorMatchingSnapshot()chore: improve more testsaleclarsonbefore adoption4f59b12
lines 288–296why, one step back288 })289290 it("throws when a non-numeric property is deleted", () => {291 expect(() => {292 const baseState = []293 baseState.x = 7294 produce(baseState, d => {295 delete d.x296 })test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91
line 297why, one step back297 }).toThrowErrorMatchingSnapshot()chore: improve more testsaleclarsonbefore adoption4f59b12
lines 298–299why, one step back298 })299 }perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 300–301why, one step back300 })301feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 302–311why, one step back302 if (useProxies) {303 describe("map drafts", () => {304 it("supports key access", () => {305 const value = baseState.aMap.get("jedi")306 const nextState = produce(baseState, s => {307 expect(s.aMap.get("jedi")).toEqual(value)308 })309 expect(nextState).toBe(baseState)310 })311feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5
lines 312–321why, one step back312 it("supports key access for non-primitive keys", () => {313 const key = {prop: "val"}314 const base = new Map([[key, {id: 1, a: 1}]])315 const value = base.get(key)316 const nextState = produce(base, s => {317 expect(s.get(key)).toEqual(value)318 })319 expect(nextState).toBe(base)320 })321feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 322–404why, one step back322 it("supports iteration", () => {323 const base = new Map([324 ["first", {id: 1, a: 1}],325 ["second", {id: 2, a: 1}]326 ])327 const findById = (map, id) => {328 for (const [, item] of map) {329 if (item.id === id) return item330 }331 return null332 }333 const result = produce(base, draft => {334 const obj1 = findById(draft, 1)335 const obj2 = findById(draft, 2)336 obj1.a = 2337 obj2.a = 2338 })339 expect(result).not.toBe(base)340 expect(result.get("first").a).toEqual(2)341 expect(result.get("second").a).toEqual(2)342 })343344 it("supports 'entries'", () => {345 const base = new Map([346 ["first", {id: 1, a: 1}],347 ["second", {id: 2, a: 1}]348 ])349 const findById = (map, id) => {350 for (const [, item] of map.entries()) {351 if (item.id === id) return item352 }353 return null354 }355 const result = produce(base, draft => {356 const obj1 = findById(draft, 1)357 const obj2 = findById(draft, 2)358 obj1.a = 2359 obj2.a = 2360 })361 expect(result).not.toBe(base)362 expect(result.get("first").a).toEqual(2)363 expect(result.get("second").a).toEqual(2)364 })365366 it("supports 'values'", () => {367 const base = new Map([368 ["first", {id: 1, a: 1}],369 ["second", {id: 2, a: 1}]370 ])371 const findById = (map, id) => {372 for (const item of map.values()) {373 if (item.id === id) return item374 }375 return null376 }377 const result = produce(base, draft => {378 const obj1 = findById(draft, 1)379 const obj2 = findById(draft, 2)380 obj1.a = 2381 obj2.a = 2382 })383 expect(result).not.toBe(base)384 expect(result.get("first").a).toEqual(2)385 expect(result.get("second").a).toEqual(2)386 })387388 it("supports 'keys", () => {389 const base = new Map([390 ["first", Symbol()],391 ["second", Symbol()]392 ])393 const result = produce(base, draft => {394 expect([...draft.keys()]).toEqual(["first", "second"])395 draft.set("third", Symbol())396 expect([...draft.keys()]).toEqual([397 "first",398 "second",399 "third"400 ])401 })402 })403404 it("supports forEach", () => {feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5
lines 405–406why, one step back405 const key1 = {prop: "val1"}406 const key2 = {prop: "val2"}feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 407–408why, one step back407 const base = new Map([408 ["first", {id: 1, a: 1}],feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5
lines 409–411why, one step back409 ["second", {id: 2, a: 1}],410 [key1, {id: 3, a: 1}],411 [key2, {id: 4, a: 1}]feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 412–417why, one step back412 ])413 const result = produce(base, draft => {414 let sum1 = 0415 draft.forEach(({a}) => {416 sum1 += a417 })feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5
line 418why, one step back418 expect(sum1).toBe(4)feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 419–421why, one step back419 let sum2 = 0420 draft.get("first").a = 10421 draft.get("second").a = 20feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5
lines 422–423why, one step back422 draft.get(key1).a = 30423 draft.get(key2).a = 40feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 424–426why, one step back424 draft.forEach(({a}) => {425 sum2 += a426 })feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5
line 427why, one step back427 expect(sum2).toBe(100)feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
line 428why, one step back428 })feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5
lines 429–437why, one step back429 expect(result).not.toBe(base)430 expect(base.get("first").a).toEqual(1)431 expect(base.get("second").a).toEqual(1)432 expect(base.get(key1).a).toEqual(1)433 expect(base.get(key2).a).toEqual(1)434 expect(result.get("first").a).toEqual(10)435 expect(result.get("second").a).toEqual(20)436 expect(result.get(key1).a).toEqual(30)437 expect(result.get(key2).a).toEqual(40)feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 438–456why, one step back438 })439440 it("supports forEach mutation", () => {441 const base = new Map([442 ["first", {id: 1, a: 1}],443 ["second", {id: 2, a: 1}]444 ])445 const result = produce(base, draft => {446 draft.forEach(item => {447 item.a = 100448 })449 })450 expect(result).not.toBe(base)451 expect(result.get("first").a).toEqual(100)452 expect(result.get("second").a).toEqual(100)453 })454455 it("can assign by key", () => {456 const nextState = produce(baseState, s => {fix: Map.prototype.set should return a draftAndrey Goncharovbefore adoption7001201
lines 457–459why, one step back457 // Map.prototype.set should return the Map itself458 const res = s.aMap.set("force", true)459 expect(res).toBe(s.aMap[DRAFT_STATE].draft)feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 460–463why, one step back460 })461 expect(nextState).not.toBe(baseState)462 expect(nextState.aMap).not.toBe(baseState.aMap)463 expect(nextState.aMap.get("force")).toEqual(true)feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5
lines 464–475why, one step back464 })465466 it("can assign by a non-primitive key", () => {467 const key = {prop: "val"}468 const value = {id: 1, a: 1}469 const base = new Map([[key, value]])470 const nextState = produce(base, s => {471 s.set(key, true)472 })473 expect(nextState).not.toBe(base)474 expect(base.get(key)).toEqual(value)475 expect(nextState.get(key)).toEqual(true)feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 476–477why, one step back476 })477feat: `Set` draftsAndrey Goncharovbefore adoption4355b63
lines 478–485why, one step back478 it("state stays the same if the the same item is assigned by key", () => {479 const nextState = produce(baseState, s => {480 s.aMap.set("jediTotal", 42)481 })482 expect(nextState).toBe(baseState)483 expect(nextState.aMap).toBe(baseState.aMap)484 })485feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 486–515why, one step back486 it("returns 'size'", () => {487 const nextState = produce(baseState, s => {488 s.aMap.set("newKey", true)489 expect(s.aMap.size).toBe(baseState.aMap.size + 1)490 })491 expect(nextState).not.toBe(baseState)492 expect(nextState.aMap).not.toBe(baseState.aMap)493 expect(nextState.aMap.get("newKey")).toEqual(true)494 expect(nextState.aMap.size).toEqual(baseState.aMap.size + 1)495 })496497 it("can use 'delete' to remove items", () => {498 const nextState = produce(baseState, s => {499 expect(s.aMap.has("jedi")).toBe(true)500 s.aMap.delete("jedi")501 expect(s.aMap.has("jedi")).toBe(false)502 })503 expect(nextState.aMap).not.toBe(baseState.aMap)504 expect(nextState.aMap.size).toBe(baseState.aMap.size - 1)505 expect(baseState.aMap.has("jedi")).toBe(true)506 expect(nextState.aMap.has("jedi")).toBe(false)507 })508509 it("can use 'clear' to remove items", () => {510 const nextState = produce(baseState, s => {511 expect(s.aMap.size).not.toBe(0)512 s.aMap.clear()513 expect(s.aMap.size).toBe(0)514 })515 expect(nextState.aMap).not.toBe(baseState.aMap)feat: `Set` draftsAndrey Goncharovbefore adoption4355b63
line 516why, one step back516 expect(baseState.aMap.size).not.toBe(0)feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 517–527why, one step back517 expect(nextState.aMap.size).toBe(0)518 })519520 it("support 'has'", () => {521 const nextState = produce(baseState, s => {522 expect(s.aMap.has("newKey")).toBe(false)523 s.aMap.set("newKey", true)524 expect(s.aMap.has("newKey")).toBe(true)525 })526 expect(nextState).not.toBe(baseState)527 expect(nextState.aMap).not.toBe(baseState.aMap)feat: `Set` draftsAndrey Goncharovbefore adoption4355b63
lines 528–529why, one step back528 expect(baseState.aMap.has("newKey")).toBe(false)529 expect(nextState.aMap.has("newKey")).toBe(true)feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 530–547why, one step back530 })531532 it("supports nested maps", () => {533 const base = new Map([534 ["first", new Map([["second", {prop: "test"}]])]535 ])536 const result = produce(base, draft => {537 draft.get("first").get("second").prop = "test1"538 })539 expect(result).not.toBe(base)540 expect(result.get("first")).not.toBe(base.get("first"))541 expect(result.get("first").get("second")).not.toBe(542 base.get("first").get("second")543 )544 expect(base.get("first").get("second").prop).toBe("test")545 expect(result.get("first").get("second").prop).toBe("test1")546 })547 })feat: `Set` draftsAndrey Goncharovbefore adoption4355b63
lines 548–744why, one step back548549 describe("set drafts", () => {550 it("supports iteration", () => {551 const base = new Set([{id: 1, a: 1}, {id: 2, a: 1}])552 const findById = (set, id) => {553 for (const item of set) {554 if (item.id === id) return item555 }556 return null557 }558 const result = produce(base, draft => {559 const obj1 = findById(draft, 1)560 const obj2 = findById(draft, 2)561 obj1.a = 2562 obj2.a = 2563 })564 expect(result).not.toBe(base)565 expect(base).toEqual(566 new Set([{id: 1, a: 1}, {id: 2, a: 1}])567 )568 expect(result).toEqual(569 new Set([{id: 1, a: 2}, {id: 2, a: 2}])570 )571 })572573 it("supports 'entries'", () => {574 const base = new Set([{id: 1, a: 1}, {id: 2, a: 1}])575 const findById = (set, id) => {576 for (const [item1, item2] of set.entries()) {577 expect(item1).toBe(item2)578 if (item1.id === id) return item1579 }580 return null581 }582 const result = produce(base, draft => {583 const obj1 = findById(draft, 1)584 const obj2 = findById(draft, 2)585 obj1.a = 2586 obj2.a = 2587 })588 expect(result).not.toBe(base)589 expect(base).toEqual(590 new Set([{id: 1, a: 1}, {id: 2, a: 1}])591 )592 expect(result).toEqual(593 new Set([{id: 1, a: 2}, {id: 2, a: 2}])594 )595 })596597 it("supports 'values'", () => {598 const base = new Set([{id: 1, a: 1}, {id: 2, a: 1}])599 const findById = (set, id) => {600 for (const item of set.values()) {601 if (item.id === id) return item602 }603 return null604 }605 const result = produce(base, draft => {606 const obj1 = findById(draft, 1)607 const obj2 = findById(draft, 2)608 obj1.a = 2609 obj2.a = 2610 })611 expect(result).not.toBe(base)612 expect(base).toEqual(613 new Set([{id: 1, a: 1}, {id: 2, a: 1}])614 )615 expect(result).toEqual(616 new Set([{id: 1, a: 2}, {id: 2, a: 2}])617 )618 })619620 it("supports 'keys'", () => {621 const base = new Set([{id: 1, a: 1}, {id: 2, a: 1}])622 const findById = (set, id) => {623 for (const item of set.keys()) {624 if (item.id === id) return item625 }626 return null627 }628 const result = produce(base, draft => {629 const obj1 = findById(draft, 1)630 const obj2 = findById(draft, 2)631 obj1.a = 2632 obj2.a = 2633 })634 expect(result).not.toBe(base)635 expect(base).toEqual(636 new Set([{id: 1, a: 1}, {id: 2, a: 1}])637 )638 expect(result).toEqual(639 new Set([{id: 1, a: 2}, {id: 2, a: 2}])640 )641 })642643 it("supports forEach with mutation after reads", () => {644 const base = new Set([{id: 1, a: 1}, {id: 2, a: 2}])645 const result = produce(base, draft => {646 let sum1 = 0647 draft.forEach(({a}) => {648 sum1 += a649 })650 expect(sum1).toBe(3)651 let sum2 = 0652 draft.forEach(item => {653 item.a += 10654 sum2 += item.a655 })656 expect(sum2).toBe(23)657 })658 expect(result).not.toBe(base)659 expect(base).toEqual(660 new Set([{id: 1, a: 1}, {id: 2, a: 2}])661 )662 expect(result).toEqual(663 new Set([{id: 1, a: 11}, {id: 2, a: 12}])664 )665 })666667 it("state stays the same if the same item is added", () => {668 const nextState = produce(baseState, s => {669 s.aSet.add("Luke")670 })671 expect(nextState).toBe(baseState)672 expect(nextState.aSet).toBe(baseState.aSet)673 })674675 it("can add new items", () => {676 const nextState = produce(baseState, s => {677 // Set.prototype.set should return the Set itself678 const res = s.aSet.add("force")679 expect(res).toBe(s.aSet[DRAFT_STATE].draft)680 })681 expect(nextState).not.toBe(baseState)682 expect(nextState.aSet).not.toBe(baseState.aSet)683 expect(baseState.aSet.has("force")).toBe(false)684 expect(nextState.aSet.has("force")).toBe(true)685 })686687 it("returns 'size'", () => {688 const nextState = produce(baseState, s => {689 s.aSet.add("newKey")690 expect(s.aSet.size).toBe(baseState.aSet.size + 1)691 })692 expect(nextState).not.toBe(baseState)693 expect(nextState.aSet).not.toBe(baseState.aSet)694 expect(nextState.aSet.has("newKey")).toBe(true)695 expect(nextState.aSet.size).toEqual(baseState.aSet.size + 1)696 })697698 it("can use 'delete' to remove items", () => {699 const nextState = produce(baseState, s => {700 expect(s.aSet.has("Luke")).toBe(true)701 s.aSet.delete("Luke")702 expect(s.aSet.has("Luke")).toBe(false)703 })704 expect(nextState.aSet).not.toBe(baseState.aSet)705 expect(nextState.aSet.size).toBe(baseState.aSet.size - 1)706 expect(baseState.aSet.has("Luke")).toBe(true)707 expect(nextState.aSet.has("Luke")).toBe(false)708 })709710 it("can use 'clear' to remove items", () => {711 const nextState = produce(baseState, s => {712 expect(s.aSet.size).not.toBe(0)713 s.aSet.clear()714 expect(s.aSet.size).toBe(0)715 })716 expect(nextState.aSet).not.toBe(baseState.aSet)717 expect(baseState.aSet.size).not.toBe(0)718 expect(nextState.aSet.size).toBe(0)719 })720721 it("supports 'has'", () => {722 const nextState = produce(baseState, s => {723 expect(s.aSet.has("newKey")).toBe(false)724 s.aSet.add("newKey")725 expect(s.aSet.has("newKey")).toBe(true)726 })727 expect(nextState).not.toBe(baseState)728 expect(nextState.aSet).not.toBe(baseState.aSet)729 expect(baseState.aSet.has("newKey")).toBe(false)730 expect(nextState.aSet.has("newKey")).toBe(true)731 })732733 it("supports nested sets", () => {734 const base = new Set([new Set(["Serenity"])])735 const result = produce(base, draft => {736 draft.forEach(nestedItem => nestedItem.add("Firefly"))737 })738 expect(result).not.toBe(base)739 expect(base).toEqual(new Set([new Set(["Serenity"])]))740 expect(result).toEqual(741 new Set([new Set(["Serenity", "Firefly"])])742 )743 })744 })feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 745–746why, one step back745 }746feat: limited class supportaleclarsonbefore adoption9877d64
lines 747–773why, one step back747 it("supports `immerable` symbol on constructor", () => {748 class One {}749 One[immerable] = true750 const baseState = new One()751 const nextState = produce(baseState, draft => {752 expect(draft).not.toBe(baseState)753 draft.foo = true754 })755 expect(nextState).not.toBe(baseState)756 expect(nextState.foo).toBeTruthy()757 })758759 it("preserves symbol properties", () => {760 const test = Symbol("test")761 const baseState = {[test]: true}762 const nextState = produce(baseState, s => {763 expect(s[test]).toBeTruthy()764 s.foo = true765 })766 expect(nextState).toEqual({767 [test]: true,768 foo: true769 })770 })771772 it("preserves non-enumerable properties", () => {773 const baseState = {}test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
line 774why, one step back774 // Non-enumerable object propertyfeat: limited class supportaleclarsonbefore adoption9877d64
line 775why, one step back775 Object.defineProperty(baseState, "foo", {test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
line 776why, one step back776 value: {a: 1},feat: limited class supportaleclarsonbefore adoption9877d64
lines 777–778why, one step back777 enumerable: false778 })test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
lines 779–783why, one step back779 // Non-enumerable primitive property780 Object.defineProperty(baseState, "bar", {781 value: 1,782 enumerable: false783 })feat: limited class supportaleclarsonbefore adoption9877d64
lines 784–786why, one step back784 const nextState = produce(baseState, s => {785 expect(s.foo).toBeTruthy()786 expect(isEnumerable(s, "foo")).toBeFalsy()test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
lines 787–790why, one step back787 s.bar++788 expect(isEnumerable(s, "foo")).toBeFalsy()789 s.foo.a++790 expect(isEnumerable(s, "foo")).toBeFalsy()feat: limited class supportaleclarsonbefore adoption9877d64
lines 791–808why, one step back791 })792 expect(nextState.foo).toBeTruthy()793 expect(isEnumerable(nextState, "foo")).toBeFalsy()794 })795796 it("throws on computed properties", () => {797 const baseState = {}798 Object.defineProperty(baseState, "foo", {799 get: () => {},800 enumerable: true801 })802 expect(() => {803 produce(baseState, s => {804 // Proxies only throw once a change is made.805 if (useProxies) {806 s.modified = true807 }808 })test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91
line 809why, one step back809 }).toThrowErrorMatchingSnapshot()feat: limited class supportaleclarsonbefore adoption9877d64
lines 810–830why, one step back810 })811812 it("allows inherited computed properties", () => {813 const proto = {}814 Object.defineProperty(proto, "foo", {815 get() {816 return this.bar817 },818 set(val) {819 this.bar = val820 }821 })822 const baseState = Object.create(proto)823 produce(baseState, s => {824 expect(s.bar).toBeUndefined()825 s.foo = {}826 expect(s.bar).toBeDefined()827 expect(s.foo).toBe(s.bar)828 })829 })830test: improve coveragealeclarsonbefore adoption14d7e3c
lines 831–852why, one step back831 it("supports a base state with multiple references to an object", () => {832 const obj = {}833 const res = produce({a: obj, b: obj}, d => {834 // Two drafts are created for each occurrence of an object in the base state.835 expect(d.a).not.toBe(d.b)836 d.a.z = true837 expect(d.b.z).toBeUndefined()838 })839 expect(res.b).toBe(obj)840 expect(res.a).not.toBe(res.b)841 expect(res.a.z).toBeTruthy()842 })843844 // NOTE: Except the root draft.845 it("supports multiple references to any modified draft", () => {846 const next = produce({a: {b: 1}}, d => {847 d.a.b++848 d.b = d.a849 })850 expect(next.a).toBe(next.b)851 })852perf: proxy assignaleclarsonbefore adoption53e9ce8
line 853why, one step back853 it("can rename nested objects (no changes)", () => {chore: improve more testsaleclarsonbefore adoption4f59b12
lines 854–856why, one step back854 const nextState = produce({obj: {}}, s => {855 s.foo = s.obj856 delete s.objtest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 857why, one step back857 })chore: improve more testsaleclarsonbefore adoption4f59b12
line 858why, one step back858 expect(nextState).toEqual({foo: {}})perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 859–863why, one step back859 })860861 // Very similar to the test before, but the reused object has one862 // property changed, one added, and one removed.863 it("can rename nested objects (with changes)", () => {chore: improve more testsaleclarsonbefore adoption4f59b12
lines 864–867why, one step back864 const nextState = produce({obj: {a: 1, b: 1}}, s => {865 s.obj.a = true // change866 delete s.obj.b // delete867 s.obj.c = true // addperf: proxy assignaleclarsonbefore adoption53e9ce8
line 868why, one step back868chore: improve more testsaleclarsonbefore adoption4f59b12
lines 869–870why, one step back869 s.foo = s.obj870 delete s.objtest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 871why, one step back871 })chore: improve more testsaleclarsonbefore adoption4f59b12
line 872why, one step back872 expect(nextState).toEqual({foo: {a: true, c: true}})perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 873–875why, one step back873 })874875 it("can nest a draft in a new object (no changes)", () => {chore: improve more testsaleclarsonbefore adoption4f59b12
line 876why, one step back876 const baseState = {obj: {}}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 877why, one step back877 const nextState = produce(baseState, s => {chore: improve more testsaleclarsonbefore adoption4f59b12
lines 878–879why, one step back878 s.foo = {bar: s.obj}879 delete s.objtest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 880why, one step back880 })chore: improve more testsaleclarsonbefore adoption4f59b12
line 881why, one step back881 expect(nextState.foo.bar).toBe(baseState.obj)perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 882–883why, one step back882 })883chore: improve more testsaleclarsonbefore adoption4f59b12
lines 884–888why, one step back884 it("can nest a modified draft in a new object", () => {885 const nextState = produce({obj: {a: 1, b: 1}}, s => {886 s.obj.a = true // change887 delete s.obj.b // delete888 s.obj.c = true // addperf: proxy assignaleclarsonbefore adoption53e9ce8
line 889why, one step back889chore: improve more testsaleclarsonbefore adoption4f59b12
lines 890–891why, one step back890 s.foo = {bar: s.obj}891 delete s.objtest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 892why, one step back892 })chore: improve more testsaleclarsonbefore adoption4f59b12
line 893why, one step back893 expect(nextState).toEqual({foo: {bar: {a: true, c: true}}})perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 894–895why, one step back894 })895chore: improve some testsaleclarsonbefore adoptionefd47f6
line 896why, one step back896 it("supports assigning undefined to an existing property", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 897–899why, one step back897 const nextState = produce(baseState, s => {898 s.aProp = undefined899 })perf: proxy assignaleclarsonbefore adoption53e9ce8
line 900why, one step back900 expect(nextState).not.toBe(baseState)chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 901–909why, one step back901 expect(nextState.aProp).toBe(undefined)902 })903904 it("supports assigning undefined to a new property", () => {905 const baseState = {}906 const nextState = produce(baseState, s => {907 s.aProp = undefined908 })909 expect(nextState).not.toBe(baseState)perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 910–912why, one step back910 expect(nextState.aProp).toBe(undefined)911 })912test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad
lines 913–915why, one step back913 // NOTE: ES5 drafts only protect existing properties when revoked.914 it("revokes the draft once produce returns", () => {915 const expectRevoked = (fn, shouldThrow = true) => {test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91
line 916why, one step back916 if (shouldThrow) expect(fn).toThrowErrorMatchingSnapshot()test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad
lines 917–918why, one step back917 else expect(fn).not.toThrow()918 }perf: proxy assignaleclarsonbefore adoption53e9ce8
line 919why, one step back919test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad
lines 920–925why, one step back920 // Test object drafts:921 let draft922 produce({a: 1, b: 1}, s => {923 draft = s924 delete s.b925 })perf: proxy assignaleclarsonbefore adoption53e9ce8
line 926why, one step back926test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad
lines 927–929why, one step back927 // Access known property on object draft.928 expectRevoked(() => {929 draft.aperf: proxy assignaleclarsonbefore adoption53e9ce8
lines 930–931why, one step back930 })931test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad
lines 932–934why, one step back932 // Assign known property on object draft.933 expectRevoked(() => {934 draft.a = truetest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 935why, one step back935 })test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad
lines 936–971why, one step back936937 // Access unknown property on object draft.938 expectRevoked(() => {939 draft.z940 }, useProxies)941942 // Assign unknown property on object draft.943 expectRevoked(() => {944 draft.z = true945 }, useProxies)946947 // Test array drafts:948 produce([1, 2], s => {949 draft = s950 s.pop()951 })952953 // Access known index of an array draft.954 expectRevoked(() => {955 draft[0]956 })957958 // Assign known index of an array draft.959 expectRevoked(() => {960 draft[0] = true961 })962963 // Access unknown index of an array draft.964 expectRevoked(() => {965 draft[1]966 }, useProxies)967968 // Assign unknown index of an array draft.969 expectRevoked(() => {970 draft[1] = true971 }, useProxies)perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 972–973why, one step back972 })973test: add missing edge casealeclarsonbefore adoptionc46af36
lines 974–981why, one step back974 it("can access a child draft that was created before the draft was modified", () => {975 produce({a: {}}, s => {976 const before = s.a977 s.b = 1978 expect(s.a).toBe(before)979 })980 })981perf: proxy assignaleclarsonbefore adoption53e9ce8
line 982why, one step back982 it("should reflect all changes made in the draft immediately", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 983–985why, one step back983 produce(baseState, draft => {984 draft.anArray[0] = 5985 draft.anArray.unshift("test")test: tiny improvementaleclarsonbefore adoptionee49da3
line 986why, one step back986 expect(enumerableOnly(draft.anArray)).toEqual([test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 987–995why, one step back987 "test",988 5,989 2,990 {c: 3},991 1992 ])993 draft.stuffz = "coffee"994 expect(draft.stuffz).toBe("coffee")995 })perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 996–997why, one step back996 })997chore: improve more testsaleclarsonbefore adoption4f59b12
lines 998–999why, one step back998 if (useProxies)999 it("throws when Object.defineProperty() is used on drafts", () => {perf: proxy assignaleclarsonbefore adoption53e9ce8
line 1000why, one step back1000 expect(() => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 1001–1007why, one step back1001 produce({}, draft => {1002 Object.defineProperty(draft, "xx", {1003 enumerable: true,1004 writeable: true,1005 value: 21006 })1007 })test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91
line 1008why, one step back1008 }).toThrowErrorMatchingSnapshot()perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1009–1011why, one step back1009 })10101011 it("should handle constructor correctly", () => {chore: improve more testsaleclarsonbefore adoption4f59b12
line 1012why, one step back1012 const baseState = {perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1013–1015why, one step back1013 arr: new Array(),1014 obj: new Object()1015 }chore: improve more testsaleclarsonbefore adoption4f59b12
line 1016why, one step back1016 const result = produce(baseState, draft => {perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1017–1024why, one step back1017 draft.arrConstructed = draft.arr.constructor(1)1018 draft.objConstructed = draft.obj.constructor(1)1019 })1020 expect(result.arrConstructed).toEqual(new Array().constructor(1))1021 expect(result.objConstructed).toEqual(new Object().constructor(1))1022 })10231024 it("should handle equality correctly - 1", () => {chore: improve more testsaleclarsonbefore adoption4f59b12
line 1025why, one step back1025 const baseState = {perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1026–1028why, one step back1026 y: 3 / 0,1027 z: NaN1028 }chore: improve more testsaleclarsonbefore adoption4f59b12
line 1029why, one step back1029 const nextState = produce(baseState, draft => {perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1030–1032why, one step back1030 draft.y = 4 / 01031 draft.z = NaN1032 })chore: improve more testsaleclarsonbefore adoption4f59b12
line 1033why, one step back1033 expect(nextState).toBe(baseState)perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1034–1036why, one step back1034 })10351036 it("should handle equality correctly - 2", () => {chore: improve more testsaleclarsonbefore adoption4f59b12
line 1037why, one step back1037 const baseState = {perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1038–1039why, one step back1038 x: -01039 }chore: improve more testsaleclarsonbefore adoption4f59b12
lines 1040–1041why, one step back1040 const nextState = produce(baseState, draft => {1041 draft.x = +0perf: proxy assignaleclarsonbefore adoption53e9ce8
line 1042why, one step back1042 })chore: improve more testsaleclarsonbefore adoption4f59b12
lines 1043–1044why, one step back1043 expect(nextState).not.toBe(baseState)1044 expect(nextState).not.toEqual(baseState)perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1045–1046why, one step back1045 })1046test: base state that contains a draftaleclarsonbefore adoptiond9b91d5
line 1047why, one step back1047 // AKA: recursive produce callschore: improve more testsaleclarsonbefore adoption4f59b12
line 1048why, one step back1048 describe("nested producers", () => {test: base state that contains a draftaleclarsonbefore adoptiond9b91d5
line 1049why, one step back1049 describe("when base state is not a draft", () => {perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1050–1052why, one step back1050 // This test ensures the global state used to manage proxies is1051 // never left in a corrupted state by a nested `produce` call.1052 it("never affects its parent producer implicitly", () => {test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736
lines 1053–1057why, one step back1053 const base = {obj: {a: 1}}1054 const next = produce(base, draft => {1055 // Notice how `base.obj` is passed, not `draft.obj`1056 const obj2 = produce(base.obj, draft2 => {1057 draft2.a = 0test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 1058why, one step back1058 })test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736
lines 1059–1060why, one step back1059 expect(obj2.a).toBe(0)1060 expect(draft.obj.a).toBe(1) // effects should not be visible outsidetest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 1061why, one step back1061 })test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736
line 1062why, one step back1062 expect(next).toBe(base)perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1063–1065why, one step back1063 })1064 })1065test: base state that contains a draftaleclarsonbefore adoptiond9b91d5
line 1066why, one step back1066 describe("when base state is a draft", () => {test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736
lines 1067–1072why, one step back1067 it("always wraps the draft in a new draft", () => {1068 produce({}, parent => {1069 produce(parent, child => {1070 expect(child).not.toBe(parent)1071 expect(isDraft(child)).toBeTruthy()1072 expect(original(child)).toBe(parent)test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 1073–1074why, one step back1073 })1074 })test: base state that contains a draftaleclarsonbefore adoptiond9b91d5
line 1075why, one step back1075 })test: more recursive produce testsAlec Larsonbefore adoption8adfb31
lines 1076–1101why, one step back10761077 // Reported by: https://github.com/mweststrate/immer/issues/3431078 it("ensures each property is drafted", () => {1079 produce({a: {}, b: {}}, parent => {1080 parent.a // Access "a" but not "b"1081 produce(parent, child => {1082 child.c = 11083 expect(isDraft(child.a)).toBeTruthy()1084 expect(isDraft(child.b)).toBeTruthy()1085 })1086 })1087 })10881089 it("preserves any pending changes", () => {1090 produce({a: 1, b: 1, d: 1}, parent => {1091 parent.b = 21092 parent.c = 21093 delete parent.d1094 produce(parent, child => {1095 expect(child.a).toBe(1) // unchanged1096 expect(child.b).toBe(2) // changed1097 expect(child.c).toBe(2) // added1098 expect(child.d).toBeUndefined() // deleted1099 })1100 })1101 })test: base state that contains a draftaleclarsonbefore adoptiond9b91d5
lines 1102–1109why, one step back1102 })11031104 describe("when base state contains a draft", () => {1105 it("wraps unowned draft with its own draft", () => {1106 produce({a: {}}, parent => {1107 produce({a: parent.a}, child => {1108 expect(child.a).not.toBe(parent.a)1109 expect(isDraft(child.a)).toBeTruthy()test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736
line 1110why, one step back1110 expect(original(child.a)).toBe(parent.a)test: base state that contains a draftaleclarsonbefore adoptiond9b91d5
lines 1111–1141why, one step back1111 })1112 })1113 })11141115 it("returns unowned draft if no changes were made", () => {1116 produce({a: {}}, parent => {1117 const result = produce({a: parent.a}, () => {})1118 expect(result.a).toBe(parent.a)1119 })1120 })11211122 it("clones the unowned draft when changes are made", () => {1123 produce({a: {}}, parent => {1124 const result = produce({a: parent.a}, child => {1125 child.a.b = 11126 })1127 expect(result.a).not.toBe(parent.a)1128 expect(result.a.b).toBe(1)1129 expect("b" in parent.a).toBeFalsy()1130 })1131 })11321133 // We cannot auto-freeze the result of a nested producer,1134 // because it may contain a draft from a parent producer.1135 it("never auto-freezes the result", () => {1136 produce({a: {}}, parent => {1137 const r = produce({a: parent.a}, child => {1138 child.b = 1 // Ensure a copy is returned.1139 })1140 expect(Object.isFrozen(r)).toBeFalsy()1141 })perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1142–1143why, one step back1142 })1143 })test: returning an upvalue draft from a nested produceraleclarsonbefore adoptionb053637
lines 1144–1161why, one step back11441145 // "Upvalues" are variables from a parent scope.1146 it("does not finalize upvalue drafts", () => {1147 produce({a: {}, b: {}}, parent => {1148 expect(produce({}, () => parent)).toBe(parent)1149 parent.x // Ensure proxy not revoked.11501151 expect(produce({}, () => [parent])[0]).toBe(parent)1152 parent.x // Ensure proxy not revoked.11531154 expect(produce({}, () => parent.a)).toBe(parent.a)1155 parent.a.x // Ensure proxy not revoked.11561157 // Modified parent test1158 parent.c = 11159 expect(produce({}, () => [parent.b])[0]).toBe(parent.b)1160 parent.b.x // Ensure proxy not revoked.1161 })test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5
lines 1162–1165why, one step back1162 })11631164 it("works with interweaved Immer instances", () => {1165 const options = {useProxies, autoFreeze}test: patch the `produce` function for interweaved Immer instances tooaleclarsonbefore adoption31bbe64
lines 1166–1167why, one step back1166 const one = createPatchedImmer(options)1167 const two = createPatchedImmer(options)test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5
lines 1168–1182why, one step back11681169 const base = {}1170 const result = one.produce(base, s1 =>1171 two.produce({s1}, s2 => {1172 expect(original(s2.s1)).toBe(s1)1173 s2.n = 11174 s2.s1 = one.produce({s2}, s3 => {1175 expect(original(s3.s2)).toBe(s2)1176 expect(original(s3.s2.s1)).toBe(s2.s1)1177 return s3.s2.s11178 })1179 })1180 )1181 expect(result.n).toBe(1)1182 expect(result.s1).toBe(base)test: returning an upvalue draft from a nested produceraleclarsonbefore adoptionb053637
line 1183why, one step back1183 })perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1184–1186why, one step back1184 })11851186 if (useProxies)chore: improve more testsaleclarsonbefore adoption4f59b12
line 1187why, one step back1187 it("throws when Object.setPrototypeOf() is used on a draft", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 1188why, one step back1188 produce({}, draft => {test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91
lines 1189–1191why, one step back1189 expect(() =>1190 Object.setPrototypeOf(draft, Array)1191 ).toThrowErrorMatchingSnapshot()test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 1192why, one step back1192 })perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1193–1194why, one step back1193 })1194chore: improve more testsaleclarsonbefore adoption4f59b12
lines 1195–1197why, one step back1195 it("supports the 'in' operator", () => {1196 produce(baseState, draft => {1197 // Known propertytest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 1198–1199why, one step back1198 expect("anArray" in draft).toBe(true)1199 expect(Reflect.has(draft, "anArray")).toBe(true)perf: proxy assignaleclarsonbefore adoption53e9ce8
line 1200why, one step back1200chore: improve more testsaleclarsonbefore adoption4f59b12
line 1201why, one step back1201 // Unknown propertytest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 1202–1203why, one step back1202 expect("bla" in draft).toBe(false)1203 expect(Reflect.has(draft, "bla")).toBe(false)perf: proxy assignaleclarsonbefore adoption53e9ce8
line 1204why, one step back1204chore: improve more testsaleclarsonbefore adoption4f59b12
line 1205why, one step back1205 // Known indextest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 1206–1209why, one step back1206 expect(0 in draft.anArray).toBe(true)1207 expect("0" in draft.anArray).toBe(true)1208 expect(Reflect.has(draft.anArray, 0)).toBe(true)1209 expect(Reflect.has(draft.anArray, "0")).toBe(true)perf: proxy assignaleclarsonbefore adoption53e9ce8
line 1210why, one step back1210chore: improve more testsaleclarsonbefore adoption4f59b12
line 1211why, one step back1211 // Unknown indextest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 1212–1216why, one step back1212 expect(17 in draft.anArray).toBe(false)1213 expect("17" in draft.anArray).toBe(false)1214 expect(Reflect.has(draft.anArray, 17)).toBe(false)1215 expect(Reflect.has(draft.anArray, "17")).toBe(false)1216 })perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1217–1218why, one step back1217 })1218feat: `this` in recipe is no longer the draft, but unboundMichel Weststratebefore adoption3f40298
line 1219why, one step back1219 it("'this' should not be bound anymore - 1", () => {perf: proxy assignaleclarsonbefore adoption53e9ce8
line 1220why, one step back1220 const base = {x: 3}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 1221why, one step back1221 const next1 = produce(base, function() {feat: `this` in recipe is no longer the draft, but unboundMichel Weststratebefore adoption3f40298
line 1222why, one step back1222 expect(this).toBe(undefined)test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 1223why, one step back1223 })perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1224–1225why, one step back1224 })1225feat: `this` in recipe is no longer the draft, but unboundMichel Weststratebefore adoption3f40298
line 1226why, one step back1226 it("'this' should not be bound anymore - 2", () => {perf: proxy assignaleclarsonbefore adoption53e9ce8
line 1227why, one step back1227 const incrementor = produce(function() {feat: `this` in recipe is no longer the draft, but unboundMichel Weststratebefore adoption3f40298
line 1228why, one step back1228 expect(this).toBe(undefined)perf: proxy assignaleclarsonbefore adoption53e9ce8
line 1229why, one step back1229 })feat: `this` in recipe is no longer the draft, but unboundMichel Weststratebefore adoption3f40298
lines 1230–1232why, one step back1230 incrementor()1231 })1232refactor(ts): simplify IProduce type testsAlec Larsonbefore adoptionbce282d
line 1233why, one step back1233 it("should be possible to use dynamic bound this", () => {feat: `this` in recipe is no longer the draft, but unboundMichel Weststratebefore adoption3f40298
lines 1234–1242why, one step back1234 const world = {1235 counter: {count: 1},1236 inc: produce(function(draft) {1237 expect(this).toBe(world)1238 draft.counter.count = this.counter.count + 11239 })1240 }12411242 expect(world.inc(world).counter.count).toBe(2)perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1243–1244why, one step back1243 })1244test: rename and expand a testaleclarsonbefore adoption733e16f
line 1245why, one step back1245 // See here: https://github.com/mweststrate/immer/issues/89chore: improve more testsaleclarsonbefore adoption4f59b12
line 1246why, one step back1246 it("supports the spread operator", () => {test: rename and expand a testaleclarsonbefore adoption733e16f
lines 1247–1250why, one step back1247 const base = {foo: {x: 0, y: 0}, bar: [0, 0]}1248 const result = produce(base, draft => {1249 draft.foo = {x: 1, ...draft.foo, y: 1}1250 draft.bar = [1, ...draft.bar, 1]test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 1251why, one step back1251 })test: rename and expand a testaleclarsonbefore adoption733e16f
lines 1252–1254why, one step back1252 expect(result).toEqual({1253 foo: {x: 0, y: 1},1254 bar: [1, 0, 0, 1]perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1255–1259why, one step back1255 })1256 })12571258 it("processes with lodash.set", () => {1259 const base = [{id: 1, a: 1}]test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 1260–1262why, one step back1260 const result = produce(base, draft => {1261 lodash.set(draft, "[0].a", 2)1262 })perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1263–1268why, one step back1263 expect(base[0].a).toEqual(1)1264 expect(result[0].a).toEqual(2)1265 })12661267 it("processes with lodash.find", () => {1268 const base = [{id: 1, a: 1}]test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 1269–1272why, one step back1269 const result = produce(base, draft => {1270 const obj1 = lodash.find(draft, {id: 1})1271 lodash.set(obj1, "a", 2)1272 })perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1273–1276why, one step back1273 expect(base[0].a).toEqual(1)1274 expect(result[0].a).toEqual(2)1275 })1276chore: improve more testsaleclarsonbefore adoption4f59b12
lines 1277–1279why, one step back1277 describe("recipe functions", () => {1278 it("can return a new object", () => {1279 const base = {x: 3}test: improve coveragealeclarsonbefore adoption14d7e3c
lines 1280–1281why, one step back1280 const res = produce(base, d => {1281 return {x: d.x + 1}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 1282why, one step back1282 })chore: improve more testsaleclarsonbefore adoption4f59b12
lines 1283–1284why, one step back1283 expect(res).not.toBe(base)1284 expect(res).toEqual({x: 4})test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 1285why, one step back1285 })perf: proxy assignaleclarsonbefore adoption53e9ce8
line 1286why, one step back1286chore: improve more testsaleclarsonbefore adoption4f59b12
lines 1287–1288why, one step back1287 it("can return the draft", () => {1288 const base = {x: 3}test: improve coveragealeclarsonbefore adoption14d7e3c
lines 1289–1291why, one step back1289 const res = produce(base, d => {1290 d.x = 41291 return dchore: improve more testsaleclarsonbefore adoption4f59b12
lines 1292–1294why, one step back1292 })1293 expect(res).not.toBe(base)1294 expect(res).toEqual({x: 4})test: improve coveragealeclarsonbefore adoption14d7e3c
lines 1295–1296why, one step back1295 })1296fix(es5): throw if a modified child draft is returnedaleclarsonbefore adoptioneabe9db
line 1297why, one step back1297 it("can return an unmodified child draft", () => {test: improve coveragealeclarsonbefore adoption14d7e3c
lines 1298–1302why, one step back1298 const base = {a: {}}1299 const res = produce(base, d => {1300 return d.a1301 })1302 expect(res).toBe(base.a)fix(es5): throw if a modified child draft is returnedaleclarsonbefore adoptioneabe9db
lines 1303–1312why, one step back1303 })13041305 // TODO: Avoid throwing if only the child draft was modified.1306 it("cannot return a modified child draft", () => {1307 const base = {a: {}}1308 expect(() => {1309 produce(base, d => {1310 d.a.b = 11311 return d.a1312 })test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91
line 1313why, one step back1313 }).toThrowErrorMatchingSnapshot()test: improve coveragealeclarsonbefore adoption14d7e3c
lines 1314–1328why, one step back1314 })13151316 it("can return a frozen object", () => {1317 const res = deepFreeze([{x: 3}])1318 expect(produce({}, () => res)).toBe(res)1319 })13201321 it("can return an object with two references to another object", () => {1322 const next = produce({}, d => {1323 const obj = {}1324 return {obj, arr: [obj]}1325 })1326 expect(next.obj).toBe(next.arr[0])1327 })1328Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
line 1329why, one step back1329 it("can return an object with two references to an unmodified draft", () => {test: improve coveragealeclarsonbefore adoption14d7e3c
lines 1330–1342why, one step back1330 const base = {a: {}}1331 const next = produce(base, d => {1332 return [d.a, d.a]1333 })1334 expect(next[0]).toBe(base.a)1335 expect(next[0]).toBe(next[1])1336 })13371338 it("cannot return an object that references itself", () => {1339 const res = {}1340 res.self = res1341 expect(() => {1342 produce(res, () => res.self)test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91
line 1343why, one step back1343 }).toThrowErrorMatchingSnapshot()Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
lines 1344–1349why, one step back1344 })1345 })13461347 describe("async recipe function", () => {1348 it("can modify the draft", () => {1349 const base = {a: 0, b: 0}feat: Dropped node 6 supportMichel Weststratebefore adoptioneecc773
line 1350why, one step back1350 return produce(base, async d => {Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
line 1351why, one step back1351 d.a = 1feat: Dropped node 6 supportMichel Weststratebefore adoptioneecc773
lines 1352–1353why, one step back1352 await Promise.resolve()1353 d.b = 1Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
lines 1354–1373why, one step back1354 }).then(res => {1355 expect(res).not.toBe(base)1356 expect(res).toEqual({a: 1, b: 1})1357 })1358 })13591360 it("works with rejected promises", () => {1361 let draft1362 const base = {a: 0, b: 0}1363 const err = new Error("passed")1364 return produce(base, d => {1365 draft = d1366 draft.b = 11367 return Promise.reject(err)1368 }).then(1369 () => {1370 throw "failed"1371 },1372 e => {1373 expect(e).toBe(err)test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91
line 1374why, one step back1374 expect(() => draft.a).toThrowErrorMatchingSnapshot()Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
lines 1375–1400why, one step back1375 }1376 )1377 })13781379 it("supports recursive produce calls after await", () => {1380 const base = {obj: {k: 1}}1381 return produce(base, d => {1382 const obj = d.obj1383 delete d.obj1384 return Promise.resolve().then(() => {1385 d.a = produce({}, d => {1386 d.b = obj // Assign a draft owned by the parent scope.1387 })13881389 // Auto-freezing is prevented when an unowned draft exists.1390 expect(Object.isFrozen(d.a)).toBeFalsy()13911392 // Ensure `obj` is not revoked.1393 obj.c = 11394 })1395 }).then(res => {1396 expect(res).not.toBe(base)1397 expect(res).toEqual({1398 a: {b: {k: 1, c: 1}}1399 })1400 })test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 1401why, one step back1401 })perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1402–1403why, one step back1402 })1403chore: improve more testsaleclarsonbefore adoption4f59b12
line 1404why, one step back1404 it("throws when the draft is modified and another object is returned", () => {perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1405–1406why, one step back1405 const base = {x: 3}1406 expect(() => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 1407–1410why, one step back1407 produce(base, draft => {1408 draft.x = 41409 return {x: 5}1410 })test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91
line 1411why, one step back1411 }).toThrowErrorMatchingSnapshot()perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1412–1413why, one step back1412 })1413chore: improve more testsaleclarsonbefore adoption4f59b12
line 1414why, one step back1414 it("should fix #117 - 1", () => {perf: proxy assignaleclarsonbefore adoption53e9ce8
line 1415why, one step back1415 const reducer = (state, action) =>test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 1416–1423why, one step back1416 produce(state, draft => {1417 switch (action.type) {1418 case "SET_STARTING_DOTS":1419 return draft.availableStartingDots.map(a => a)1420 default:1421 break1422 }1423 })perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1424–1438why, one step back1424 const base = {1425 availableStartingDots: [1426 {dots: 4, count: 1},1427 {dots: 3, count: 2},1428 {dots: 2, count: 3},1429 {dots: 1, count: 4}1430 ]1431 }1432 const next = reducer(base, {type: "SET_STARTING_DOTS"})1433 expect(next).toEqual(base.availableStartingDots)1434 expect(next).not.toBe(base.availableStartingDots)1435 })14361437 it("should fix #117 - 2", () => {1438 const reducer = (state, action) =>test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 1439–1448why, one step back1439 produce(state, draft => {1440 switch (action.type) {1441 case "SET_STARTING_DOTS":1442 return {1443 dots: draft.availableStartingDots.map(a => a)1444 }1445 default:1446 break1447 }1448 })perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1449–1460why, one step back1449 const base = {1450 availableStartingDots: [1451 {dots: 4, count: 1},1452 {dots: 3, count: 2},1453 {dots: 2, count: 3},1454 {dots: 1, count: 4}1455 ]1456 }1457 const next = reducer(base, {type: "SET_STARTING_DOTS"})1458 expect(next).toEqual({dots: base.availableStartingDots})1459 })1460chore: improve more testsaleclarsonbefore adoption4f59b12
line 1461why, one step back1461 it("cannot always detect noop assignments - 0", () => {perf: proxy assignaleclarsonbefore adoption53e9ce8
line 1462why, one step back1462 const baseState = {x: {y: 3}}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 1463–1466why, one step back1463 const nextState = produce(baseState, d => {1464 const a = d.x1465 d.x = a1466 })perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1467–1469why, one step back1467 expect(nextState).toBe(baseState)1468 })1469chore: improve more testsaleclarsonbefore adoption4f59b12
line 1470why, one step back1470 it("cannot always detect noop assignments - 1", () => {perf: proxy assignaleclarsonbefore adoption53e9ce8
line 1471why, one step back1471 const baseState = {x: {y: 3}}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 1472–1476why, one step back1472 const nextState = produce(baseState, d => {1473 const a = d.x1474 d.x = 41475 d.x = a1476 })perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1477–1482why, one step back1477 // Ideally, this should actually be the same instances1478 // but this would be pretty expensive to detect,1479 // so we don't atm1480 expect(nextState).not.toBe(baseState)1481 })1482chore: improve more testsaleclarsonbefore adoption4f59b12
line 1483why, one step back1483 it("cannot always detect noop assignments - 2", () => {perf: proxy assignaleclarsonbefore adoption53e9ce8
line 1484why, one step back1484 const baseState = {x: {y: 3}}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 1485–1490why, one step back1485 const nextState = produce(baseState, d => {1486 const a = d.x1487 const stuff = a.y + 31488 d.x = 41489 d.x = a1490 })perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1491–1496why, one step back1491 // Ideally, this should actually be the same instances1492 // but this would be pretty expensive to detect,1493 // so we don't atm1494 expect(nextState).not.toBe(baseState)1495 })1496chore: improve more testsaleclarsonbefore adoption4f59b12
line 1497why, one step back1497 it("cannot always detect noop assignments - 3", () => {perf: proxy assignaleclarsonbefore adoption53e9ce8
line 1498why, one step back1498 const baseState = {x: 3}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 1499–1501why, one step back1499 const nextState = produce(baseState, d => {1500 d.x = 31501 })perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1502–1504why, one step back1502 expect(nextState).toBe(baseState)1503 })1504chore: improve more testsaleclarsonbefore adoption4f59b12
line 1505why, one step back1505 it("cannot always detect noop assignments - 4", () => {perf: proxy assignaleclarsonbefore adoption53e9ce8
line 1506why, one step back1506 const baseState = {x: 3}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 1507–1510why, one step back1507 const nextState = produce(baseState, d => {1508 d.x = 41509 d.x = 31510 })perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1511–1516why, one step back1511 // Ideally, this should actually be the same instances1512 // but this would be pretty expensive to detect,1513 // so we don't atm1514 expect(nextState).not.toBe(baseState)1515 })1516chore: improve more testsaleclarsonbefore adoption4f59b12
line 1517why, one step back1517 it("cannot produce undefined by returning undefined", () => {perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1518–1533why, one step back1518 const base = 31519 expect(produce(base, () => 4)).toBe(4)1520 expect(produce(base, () => null)).toBe(null)1521 expect(produce(base, () => undefined)).toBe(3)1522 expect(produce(base, () => {})).toBe(3)1523 expect(produce(base, () => nothing)).toBe(undefined)15241525 expect(produce({}, () => undefined)).toEqual({})1526 expect(produce({}, () => nothing)).toBe(undefined)1527 expect(produce(3, () => nothing)).toBe(undefined)15281529 expect(produce(() => undefined)({})).toEqual({})1530 expect(produce(() => nothing)({})).toBe(undefined)1531 expect(produce(() => nothing)(3)).toBe(undefined)1532 })1533chore: improve more testsaleclarsonbefore adoption4f59b12
lines 1534–1537why, one step back1534 describe("base state type", () => {1535 testObjectTypes(produce)1536 testLiteralTypes(produce)1537 })perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1538–1543why, one step back15381539 afterEach(() => {1540 expect(baseState).toBe(origBaseState)1541 expect(baseState).toEqual(createBaseState())1542 })1543chore: improve more testsaleclarsonbefore adoption4f59b12
line 1544why, one step back1544 class Foo {}perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1545–1546why, one step back1545 function createBaseState() {1546 const data = {chore: improve more testsaleclarsonbefore adoption4f59b12
line 1547why, one step back1547 anInstance: new Foo(),perf: proxy assignaleclarsonbefore adoption53e9ce8
line 1548why, one step back1548 anArray: [3, 2, {c: 3}, 1],feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 1549–1552why, one step back1549 aMap: new Map([1550 ["jedi", {name: "Luke", skill: 10}],1551 ["jediTotal", 42],1552 ["force", "these aren't the droids you're looking for"]feat: `Set` draftsAndrey Goncharovbefore adoption4355b63
lines 1553–1559why, one step back1553 ]),1554 aSet: new Set([1555 "Luke",1556 42,1557 {1558 jedi: "Yoda"1559 }feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
line 1560why, one step back1560 ]),perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1561–1568why, one step back1561 aProp: "hi",1562 anObject: {1563 nested: {1564 yummie: true1565 },1566 coffee: false1567 }1568 }test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 1569why, one step back1569 return autoFreeze ? deepFreeze(data) : dataperf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1570–1612why, one step back1570 }1571 })15721573 describe(`isDraft - ${name}`, () => {1574 it("returns true for object drafts", () => {1575 produce({}, state => {1576 expect(isDraft(state)).toBeTruthy()1577 })1578 })1579 it("returns true for array drafts", () => {1580 produce([], state => {1581 expect(isDraft(state)).toBeTruthy()1582 })1583 })1584 it("returns true for objects nested in object drafts", () => {1585 produce({a: {b: {}}}, state => {1586 expect(isDraft(state.a)).toBeTruthy()1587 expect(isDraft(state.a.b)).toBeTruthy()1588 })1589 })1590 it("returns false for new objects added to a draft", () => {1591 produce({}, state => {1592 state.a = {}1593 expect(isDraft(state.a)).toBeFalsy()1594 })1595 })1596 it("returns false for objects returned by the producer", () => {1597 const object = produce(null, Object.create)1598 expect(isDraft(object)).toBeFalsy()1599 })1600 it("returns false for arrays returned by the producer", () => {1601 const array = produce(null, _ => [])1602 expect(isDraft(array)).toBeFalsy()1603 })1604 it("returns false for object drafts returned by the producer", () => {1605 const object = produce({}, state => state)1606 expect(isDraft(object)).toBeFalsy()1607 })1608 it("returns false for array drafts returned by the producer", () => {1609 const array = produce([], state => state)1610 expect(isDraft(array)).toBeFalsy()1611 })1612 })test: extract some tests into their own functionaleclarsonbefore adoption21846ab
lines 1613–1614why, one step back1613}1614chore: improve more testsaleclarsonbefore adoption4f59b12
line 1615why, one step back1615function testObjectTypes(produce) {feat: limited class supportaleclarsonbefore adoption9877d64
lines 1616–1621why, one step back1616 class Foo {1617 constructor(foo) {1618 this.foo = foo1619 this[immerable] = true1620 }1621 }chore: improve more testsaleclarsonbefore adoption4f59b12
lines 1622–1626why, one step back1622 const values = {1623 "empty object": {},1624 "plain object": {a: 1, b: 2},1625 "object (no prototype)": Object.create(null),1626 "empty array": [],feat: limited class supportaleclarsonbefore adoption9877d64
lines 1627–1628why, one step back1627 "plain array": [1, 2],1628 "class instance (draftable)": new Foo(1)chore: improve more testsaleclarsonbefore adoption4f59b12
lines 1629–1669why, one step back1629 }1630 for (const name in values) {1631 const value = values[name]1632 const copy = shallowCopy(value)1633 testObjectType(name, value)1634 testObjectType(name + " (frozen)", Object.freeze(copy))1635 }1636 function testObjectType(name, base) {1637 describe(name, () => {1638 it("creates a draft", () => {1639 produce(base, draft => {1640 expect(draft).not.toBe(base)1641 expect(shallowCopy(draft, true)).toEqual(base)1642 })1643 })16441645 it("preserves the prototype", () => {1646 const proto = Object.getPrototypeOf(base)1647 produce(base, draft => {1648 expect(Object.getPrototypeOf(draft)).toBe(proto)1649 })1650 })16511652 it("returns the base state when no changes are made", () => {1653 expect(produce(base, () => {})).toBe(base)1654 })16551656 it("returns a copy when changes are made", () => {1657 const random = Math.random()1658 const result = produce(base, draft => {1659 draft[0] = random1660 })1661 expect(result).not.toBe(base)1662 expect(result.constructor).toBe(base.constructor)1663 expect(result[0]).toBe(random)1664 })1665 })1666 }1667}16681669function testLiteralTypes(produce) {test: extract some tests into their own functionaleclarsonbefore adoption21846ab
line 1670why, one step back1670 class Foo {}chore: improve more testsaleclarsonbefore adoption4f59b12
line 1671why, one step back1671 const values = {test: extract some tests into their own functionaleclarsonbefore adoption21846ab
lines 1672–1674why, one step back1672 "falsy number": 0,1673 "truthy number": 1,1674 "negative number": -1,chore: improve more testsaleclarsonbefore adoption4f59b12
line 1675why, one step back1675 NaN: NaN,test: extract some tests into their own functionaleclarsonbefore adoption21846ab
lines 1676–1684why, one step back1676 infinity: 1 / 0,1677 true: true,1678 false: false,1679 "empty string": "",1680 "truthy string": "1",1681 null: null,1682 undefined: undefined,16831684 /**chore: improve more testsaleclarsonbefore adoption4f59b12
lines 1685–1686why, one step back1685 * These objects are treated as literals because Immer1686 * does not know how to draft them.test: extract some tests into their own functionaleclarsonbefore adoption21846ab
line 1687why, one step back1687 */chore: improve more testsaleclarsonbefore adoption4f59b12
line 1688why, one step back1688 function: () => {},test: extract some tests into their own functionaleclarsonbefore adoption21846ab
lines 1689–1693why, one step back1689 "regexp object": /.+/g,1690 "boxed number": new Number(0),1691 "boxed string": new String(""),1692 "boxed boolean": new Boolean(),1693 "date object": new Date(),feat: limited class supportaleclarsonbefore adoption9877d64
line 1694why, one step back1694 "class instance (not draftable)": new Foo()test: extract some tests into their own functionaleclarsonbefore adoption21846ab
line 1695why, one step back1695 }chore: improve more testsaleclarsonbefore adoption4f59b12
lines 1696–1699why, one step back1696 for (const name in values) {1697 describe(name, () => {1698 const value = values[name]1699test: extract some tests into their own functionaleclarsonbefore adoption21846ab
lines 1700–1704why, one step back1700 it("does not create a draft", () => {1701 produce(value, draft => {1702 expect(draft).toBe(value)1703 })1704 })chore: improve more testsaleclarsonbefore adoption4f59b12
lines 1705–1706why, one step back17051706 it("returns the base state when no changes are made", () => {test: extract some tests into their own functionaleclarsonbefore adoption21846ab
lines 1707–1708why, one step back1707 expect(produce(value, () => {})).toBe(value)1708 })chore: improve more testsaleclarsonbefore adoption4f59b12
line 1709why, one step back1709test: extract some tests into their own functionaleclarsonbefore adoption21846ab
line 1710why, one step back1710 if (value && typeof value == "object") {chore: improve more testsaleclarsonbefore adoption4f59b12
line 1711why, one step back1711 it("does not return a copy when changes are made", () => {test: extract some tests into their own functionaleclarsonbefore adoption21846ab
lines 1712–1720why, one step back1712 expect(1713 produce(value, draft => {1714 draft.foo = true1715 })1716 ).toBe(value)1717 })1718 }1719 })1720 }perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1721–1723why, one step back1721}17221723function enumerableOnly(x) {feat: limited class supportaleclarsonbefore adoption9877d64
lines 1724–1725why, one step back1724 const copy = Array.isArray(x) ? x.slice() : Object.assign({}, x)1725 each(copy, (prop, value) => {test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44
line 1726why, one step back1726 if (value && typeof value === "object") {feat: limited class supportaleclarsonbefore adoption9877d64
line 1727why, one step back1727 copy[prop] = enumerableOnly(value)test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44
line 1728why, one step back1728 }feat: limited class supportaleclarsonbefore adoption9877d64
line 1729why, one step back1729 })test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44
line 1730why, one step back1730 return copyperf: proxy assignaleclarsonbefore adoption53e9ce8
line 1731why, one step back1731}