base.js
This file
Path
__tests__/base.js
1,162 lines · 40.9 KiB · JavaScript
Last changed by
test: improve coveragealeclarsonbefore adoption14d7e3c
History of this fileTake it
The walk stopped after 200 commits: lines older than bbedb9f point at it.
Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 1why, one step back1"use strict"feat: limited class supportaleclarsonbefore adoption9877d64
lines 2–3why, one step back2import {Immer, nothing, original, isDraft, immerable} from "../src/index"3import {each, shallowCopy, isEnumerable} from "../src/common"Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
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})13Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 14–15why, one step back14runBaseTest("proxy (no freeze)", true, false)15runBaseTest("proxy (autofreeze)", true, true)Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 16why, one step back16runBaseTest("proxy (autofreeze)(patch listener)", true, true, true)Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 17–18why, one step back17runBaseTest("es5 (no freeze)", false, false)18runBaseTest("es5 (autofreeze)", false, true)Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 19why, one step back19runBaseTest("es5 (autofreeze)(patch listener)", false, true, true)Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 20why, one step back20test: 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 }41Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
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 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
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 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
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)Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 65–67why, one step back65 expect(nextState.nested).toBe(baseState.nested)66 })67Merge pull request #158 from stefanwille/base-test-uses-if-instead-of-itbenbraoubefore adoption0d04d62
line 68why, one step back68 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 })Merge pull request #158 from stefanwille/base-test-uses-if-instead-of-itbenbraoubefore adoption0d04d62
lines 72–77why, 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 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 78–79why, one step back7879 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 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
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 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
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 })112chore: improve more testsaleclarsonbefore adoption4f59b12
lines 113–117why, one step back113 it("can get property descriptors", () => {114 const getDescriptor = Object.getOwnPropertyDescriptor115 const baseState = deepFreeze([{a: 1}])116 produce(baseState, draft => {117 const obj = draft[0]Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 118why, one step back118chore: improve more testsaleclarsonbefore adoption4f59b12
lines 119–143why, one step back119 // Known property120 expect(getDescriptor(obj, "a")).toMatchObject({121 configurable: true,122 enumerable: true123 })124 expect(getDescriptor(draft, 0)).toMatchObject({125 configurable: true,126 enumerable: true127 })128129 // Deleted property130 delete obj.a131 draft.pop()132 expect(getDescriptor(obj, "a")).toBeUndefined()133 expect(getDescriptor(draft, 0)).toBeUndefined()134135 // Unknown property136 expect(getDescriptor(obj, "b")).toBeUndefined()137 expect(getDescriptor(draft, 100)).toBeUndefined()138139 // Added property140 obj.b = 2141 draft[100] = 1142 expect(getDescriptor(obj, "b")).toBeDefined()143 expect(getDescriptor(draft, 100)).toBeDefined()test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 144why, one step back144 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 145–146why, one step back145 })146chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 147–153why, one step back147 describe("array drafts", () => {148 it("supports Array.isArray()", () => {149 const nextState = produce(baseState, s => {150 expect(Array.isArray(s.anArray)).toBeTruthy()151 s.anArray.push(1)152 })153 expect(Array.isArray(nextState.anArray)).toBeTruthy()Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 154–155why, one step back154 })155chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 156–161why, one step back156 it("supports index access", () => {157 const value = baseState.anArray[0]158 const nextState = produce(baseState, s => {159 expect(s.anArray[0]).toBe(value)160 })161 expect(nextState).toBe(baseState)test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 162why, one step back162 })chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 163–179why, one step back163164 it("supports iteration", () => {165 const base = [{id: 1, a: 1}, {id: 2, a: 1}]166 const findById = (collection, id) => {167 for (const item of collection) {168 if (item.id === id) return item169 }170 return null171 }172 const result = produce(base, draft => {173 const obj1 = findById(draft, 1)174 const obj2 = findById(draft, 2)175 obj1.a = 2176 obj2.a = 2177 })178 expect(result[0].a).toEqual(2)179 expect(result[1].a).toEqual(2)Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 180–181why, one step back180 })181chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 182–188why, one step back182 it("can assign an index via bracket notation", () => {183 const nextState = produce(baseState, s => {184 s.anArray[3] = true185 })186 expect(nextState).not.toBe(baseState)187 expect(nextState.anArray).not.toBe(baseState.anArray)188 expect(nextState.anArray[3]).toEqual(true)test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 189why, one step back189 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 190why, one step back190chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 191–197why, one step back191 it("can use splice() to both add and remove items", () => {192 const nextState = produce(baseState, s => {193 s.anArray.splice(1, 1, "a", "b")194 })195 expect(nextState.anArray).not.toBe(baseState.anArray)196 expect(nextState.anArray[1]).toBe("a")197 expect(nextState.anArray[2]).toBe("b")test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 198why, one step back198 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 199why, one step back199chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 200–206why, one step back200 it("can truncate via the length property", () => {201 const baseLength = baseState.anArray.length202 const nextState = produce(baseState, s => {203 s.anArray.length = baseLength - 1204 })205 expect(nextState.anArray).not.toBe(baseState.anArray)206 expect(nextState.anArray.length).toBe(baseLength - 1)test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 207why, one step back207 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 208why, one step back208chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 209–216why, one step back209 it("can extend via the length property", () => {210 const baseLength = baseState.anArray.length211 const nextState = produce(baseState, s => {212 s.anArray.length = baseLength + 1213 })214 expect(nextState.anArray).not.toBe(baseState.anArray)215 expect(nextState.anArray.length).toBe(baseLength + 1)216 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 217why, one step back217chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 218–224why, one step back218 // Reported here: https://github.com/mweststrate/immer/issues/116219 it("can pop then push", () => {220 const nextState = produce([1, 2, 3], s => {221 s.pop()222 s.push(100)223 })224 expect(nextState).toEqual([1, 2, 100])test: rename and move a testaleclarsonbefore adoption8e0325c
lines 225–226why, one step back225 })226chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 227–233why, one step back227 it("can be sorted", () => {228 const baseState = [3, 1, 2]229 const nextState = produce(baseState, s => {230 s.sort()231 })232 expect(nextState).not.toBe(baseState)233 expect(nextState).toEqual([1, 2, 3])test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 234why, one step back234 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 235why, one step back235chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 236–244why, one step back236 it("supports modifying nested objects", () => {237 const baseState = [{a: 1}, {}]238 const nextState = produce(baseState, s => {239 s[0].a++240 s[1].a = 0241 })242 expect(nextState).not.toBe(baseState)243 expect(nextState[0].a).toBe(2)244 expect(nextState[1].a).toBe(0)test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 245why, one step back245 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 246why, one step back246chore: improve more testsaleclarsonbefore adoption4f59b12
lines 247–251why, one step back247 it("never preserves non-numeric properties", () => {248 const baseState = []249 baseState.x = 7250 const nextState = produce(baseState, s => {251 s.push(3)test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 252why, one step back252 })chore: improve more testsaleclarsonbefore adoption4f59b12
line 253why, one step back253 expect("x" in nextState).toBeFalsy()test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 254why, one step back254 })chore: improve more testsaleclarsonbefore adoption4f59b12
lines 255–261why, one step back255256 if (useProxies) {257 it("throws when a non-numeric property is added", () => {258 expect(() => {259 produce([], d => {260 d.x = 3261 })feat: limited class supportaleclarsonbefore adoption9877d64
lines 262–264why, one step back262 }).toThrow(263 "Immer only supports setting array indices and the 'length' property"264 )chore: improve more testsaleclarsonbefore adoption4f59b12
lines 265–273why, one step back265 })266267 it("throws when a non-numeric property is deleted", () => {268 expect(() => {269 const baseState = []270 baseState.x = 7271 produce(baseState, d => {272 delete d.x273 })feat: limited class supportaleclarsonbefore adoption9877d64
line 274why, one step back274 }).toThrow("Immer only supports deleting array indices")chore: improve more testsaleclarsonbefore adoption4f59b12
lines 275–276why, one step back275 })276 }Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 277–278why, one step back277 })278feat: limited class supportaleclarsonbefore adoption9877d64
lines 279–353why, one step back279 it("supports `immerable` symbol on constructor", () => {280 class One {}281 One[immerable] = true282 const baseState = new One()283 const nextState = produce(baseState, draft => {284 expect(draft).not.toBe(baseState)285 draft.foo = true286 })287 expect(nextState).not.toBe(baseState)288 expect(nextState.foo).toBeTruthy()289 })290291 it("preserves symbol properties", () => {292 const test = Symbol("test")293 const baseState = {[test]: true}294 const nextState = produce(baseState, s => {295 expect(s[test]).toBeTruthy()296 s.foo = true297 })298 expect(nextState).toEqual({299 [test]: true,300 foo: true301 })302 })303304 it("preserves non-enumerable properties", () => {305 const baseState = {}306 Object.defineProperty(baseState, "foo", {307 value: true,308 enumerable: false309 })310 const nextState = produce(baseState, s => {311 expect(s.foo).toBeTruthy()312 expect(isEnumerable(s, "foo")).toBeFalsy()313 s.bar = true314 })315 expect(nextState.foo).toBeTruthy()316 expect(isEnumerable(nextState, "foo")).toBeFalsy()317 })318319 it("throws on computed properties", () => {320 const baseState = {}321 Object.defineProperty(baseState, "foo", {322 get: () => {},323 enumerable: true324 })325 expect(() => {326 produce(baseState, s => {327 // Proxies only throw once a change is made.328 if (useProxies) {329 s.modified = true330 }331 })332 }).toThrowError("Immer drafts cannot have computed properties")333 })334335 it("allows inherited computed properties", () => {336 const proto = {}337 Object.defineProperty(proto, "foo", {338 get() {339 return this.bar340 },341 set(val) {342 this.bar = val343 }344 })345 const baseState = Object.create(proto)346 produce(baseState, s => {347 expect(s.bar).toBeUndefined()348 s.foo = {}349 expect(s.bar).toBeDefined()350 expect(s.foo).toBe(s.bar)351 })352 })353test: improve coveragealeclarsonbefore adoption14d7e3c
lines 354–375why, one step back354 it("supports a base state with multiple references to an object", () => {355 const obj = {}356 const res = produce({a: obj, b: obj}, d => {357 // Two drafts are created for each occurrence of an object in the base state.358 expect(d.a).not.toBe(d.b)359 d.a.z = true360 expect(d.b.z).toBeUndefined()361 })362 expect(res.b).toBe(obj)363 expect(res.a).not.toBe(res.b)364 expect(res.a.z).toBeTruthy()365 })366367 // NOTE: Except the root draft.368 it("supports multiple references to any modified draft", () => {369 const next = produce({a: {b: 1}}, d => {370 d.a.b++371 d.b = d.a372 })373 expect(next.a).toBe(next.b)374 })375test: renaming propertiesaleclarsonbefore adoptiond83f4b5
line 376why, one step back376 it("can rename nested objects (no changes)", () => {chore: improve more testsaleclarsonbefore adoption4f59b12
lines 377–379why, one step back377 const nextState = produce({obj: {}}, s => {378 s.foo = s.obj379 delete s.objtest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 380why, one step back380 })chore: improve more testsaleclarsonbefore adoption4f59b12
line 381why, one step back381 expect(nextState).toEqual({foo: {}})Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 382–383why, one step back382 })383test: renaming propertiesaleclarsonbefore adoptiond83f4b5
lines 384–386why, one step back384 // Very similar to the test before, but the reused object has one385 // property changed, one added, and one removed.386 it("can rename nested objects (with changes)", () => {chore: improve more testsaleclarsonbefore adoption4f59b12
lines 387–390why, one step back387 const nextState = produce({obj: {a: 1, b: 1}}, s => {388 s.obj.a = true // change389 delete s.obj.b // delete390 s.obj.c = true // addtest: renaming propertiesaleclarsonbefore adoptiond83f4b5
line 391why, one step back391chore: improve more testsaleclarsonbefore adoption4f59b12
lines 392–393why, one step back392 s.foo = s.obj393 delete s.objtest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 394why, one step back394 })chore: improve more testsaleclarsonbefore adoption4f59b12
line 395why, one step back395 expect(nextState).toEqual({foo: {a: true, c: true}})Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 396–397why, one step back396 })397test: draft nested in new objectaleclarsonbefore adoptionf46659b
line 398why, one step back398 it("can nest a draft in a new object (no changes)", () => {chore: improve more testsaleclarsonbefore adoption4f59b12
line 399why, one step back399 const baseState = {obj: {}}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 400why, one step back400 const nextState = produce(baseState, s => {chore: improve more testsaleclarsonbefore adoption4f59b12
lines 401–402why, one step back401 s.foo = {bar: s.obj}402 delete s.objtest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 403why, one step back403 })chore: improve more testsaleclarsonbefore adoption4f59b12
line 404why, one step back404 expect(nextState.foo.bar).toBe(baseState.obj)test: draft nested in new objectaleclarsonbefore adoptionf46659b
lines 405–406why, one step back405 })406chore: improve more testsaleclarsonbefore adoption4f59b12
lines 407–411why, one step back407 it("can nest a modified draft in a new object", () => {408 const nextState = produce({obj: {a: 1, b: 1}}, s => {409 s.obj.a = true // change410 delete s.obj.b // delete411 s.obj.c = true // addtest: draft nested in new objectaleclarsonbefore adoptionf46659b
line 412why, one step back412chore: improve more testsaleclarsonbefore adoption4f59b12
lines 413–414why, one step back413 s.foo = {bar: s.obj}414 delete s.objtest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 415why, one step back415 })chore: improve more testsaleclarsonbefore adoption4f59b12
line 416why, one step back416 expect(nextState).toEqual({foo: {bar: {a: true, c: true}}})test: draft nested in new objectaleclarsonbefore adoptionf46659b
lines 417–418why, one step back417 })418chore: improve some testsaleclarsonbefore adoptionefd47f6
line 419why, one step back419 it("supports assigning undefined to an existing property", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 420–422why, one step back420 const nextState = produce(baseState, s => {421 s.aProp = undefined422 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 423why, one step back423 expect(nextState).not.toBe(baseState)chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 424–432why, one step back424 expect(nextState.aProp).toBe(undefined)425 })426427 it("supports assigning undefined to a new property", () => {428 const baseState = {}429 const nextState = produce(baseState, s => {430 s.aProp = undefined431 })432 expect(nextState).not.toBe(baseState)Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 433–435why, one step back433 expect(nextState.aProp).toBe(undefined)434 })435test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad
lines 436–441why, one step back436 // NOTE: ES5 drafts only protect existing properties when revoked.437 it("revokes the draft once produce returns", () => {438 const expectRevoked = (fn, shouldThrow = true) => {439 if (shouldThrow) expect(fn).toThrowError(/revoked/)440 else expect(fn).not.toThrow()441 }Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 442why, one step back442test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad
lines 443–448why, one step back443 // Test object drafts:444 let draft445 produce({a: 1, b: 1}, s => {446 draft = s447 delete s.b448 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 449why, one step back449test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad
lines 450–452why, one step back450 // Access known property on object draft.451 expectRevoked(() => {452 draft.aMerge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 453–454why, one step back453 })454test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad
lines 455–457why, one step back455 // Assign known property on object draft.456 expectRevoked(() => {457 draft.a = truetest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 458why, one step back458 })test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad
lines 459–494why, one step back459460 // Access unknown property on object draft.461 expectRevoked(() => {462 draft.z463 }, useProxies)464465 // Assign unknown property on object draft.466 expectRevoked(() => {467 draft.z = true468 }, useProxies)469470 // Test array drafts:471 produce([1, 2], s => {472 draft = s473 s.pop()474 })475476 // Access known index of an array draft.477 expectRevoked(() => {478 draft[0]479 })480481 // Assign known index of an array draft.482 expectRevoked(() => {483 draft[0] = true484 })485486 // Access unknown index of an array draft.487 expectRevoked(() => {488 draft[1]489 }, useProxies)490491 // Assign unknown index of an array draft.492 expectRevoked(() => {493 draft[1] = true494 }, useProxies)Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 495–496why, one step back495 })496test: add missing edge casealeclarsonbefore adoptionc46af36
lines 497–504why, one step back497 it("can access a child draft that was created before the draft was modified", () => {498 produce({a: {}}, s => {499 const before = s.a500 s.b = 1501 expect(s.a).toBe(before)502 })503 })504Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 505why, one step back505 it("should reflect all changes made in the draft immediately", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 506–508why, one step back506 produce(baseState, draft => {507 draft.anArray[0] = 5508 draft.anArray.unshift("test")test: tiny improvementaleclarsonbefore adoptionee49da3
line 509why, one step back509 expect(enumerableOnly(draft.anArray)).toEqual([test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 510–518why, one step back510 "test",511 5,512 2,513 {c: 3},514 1515 ])516 draft.stuffz = "coffee"517 expect(draft.stuffz).toBe("coffee")518 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 519–520why, one step back519 })520chore: improve more testsaleclarsonbefore adoption4f59b12
lines 521–522why, one step back521 if (useProxies)522 it("throws when Object.defineProperty() is used on drafts", () => {Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 523why, one step back523 expect(() => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 524–530why, one step back524 produce({}, draft => {525 Object.defineProperty(draft, "xx", {526 enumerable: true,527 writeable: true,528 value: 2529 })530 })feat: limited class supportaleclarsonbefore adoption9877d64
lines 531–533why, one step back531 }).toThrow(532 "Object.defineProperty() cannot be used on an Immer draft"533 )Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 534why, one step back534 })Be explicit about not supporting arbitrary properties on arraysMichel Weststratebefore adoption0a80c2c
line 535why, one step back535Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 536why, one step back536 it("should handle constructor correctly", () => {chore: improve more testsaleclarsonbefore adoption4f59b12
line 537why, one step back537 const baseState = {Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 538–540why, one step back538 arr: new Array(),539 obj: new Object()540 }chore: improve more testsaleclarsonbefore adoption4f59b12
line 541why, one step back541 const result = produce(baseState, draft => {Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 542–549why, one step back542 draft.arrConstructed = draft.arr.constructor(1)543 draft.objConstructed = draft.obj.constructor(1)544 })545 expect(result.arrConstructed).toEqual(new Array().constructor(1))546 expect(result.objConstructed).toEqual(new Object().constructor(1))547 })548549 it("should handle equality correctly - 1", () => {chore: improve more testsaleclarsonbefore adoption4f59b12
line 550why, one step back550 const baseState = {Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 551–553why, one step back551 y: 3 / 0,552 z: NaN553 }chore: improve more testsaleclarsonbefore adoption4f59b12
line 554why, one step back554 const nextState = produce(baseState, draft => {Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 555–557why, one step back555 draft.y = 4 / 0556 draft.z = NaN557 })chore: improve more testsaleclarsonbefore adoption4f59b12
line 558why, one step back558 expect(nextState).toBe(baseState)Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 559–561why, one step back559 })560561 it("should handle equality correctly - 2", () => {chore: improve more testsaleclarsonbefore adoption4f59b12
line 562why, one step back562 const baseState = {Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 563–564why, one step back563 x: -0564 }chore: improve more testsaleclarsonbefore adoption4f59b12
lines 565–566why, one step back565 const nextState = produce(baseState, draft => {566 draft.x = +0Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 567why, one step back567 })chore: improve more testsaleclarsonbefore adoption4f59b12
lines 568–569why, one step back568 expect(nextState).not.toBe(baseState)569 expect(nextState).not.toEqual(baseState)Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 570–571why, one step back570 })571test: base state that contains a draftaleclarsonbefore adoptiond9b91d5
line 572why, one step back572 // AKA: recursive produce callschore: improve more testsaleclarsonbefore adoption4f59b12
line 573why, one step back573 describe("nested producers", () => {test: base state that contains a draftaleclarsonbefore adoptiond9b91d5
line 574why, one step back574 describe("when base state is not a draft", () => {Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371
lines 575–578why, one step back575 // This test ensures the global state used to manage proxies is576 // never left in a corrupted state by a nested `produce` call.577 it("never affects its parent producer implicitly", () => {578 const bear = {paw: {honey: true}}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 579–585why, one step back579 const next = produce(bear, draft => {580 const paw2 = produce(bear.paw, draft => {581 draft.honey = false582 })583 expect(paw2.honey).toBe(false)584 expect(draft.paw.honey).toBe(true) // effects should not be visible outside585 })Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371
lines 586–588why, one step back586 expect(next.paw.honey).toBe(true)587 expect(next).toBe(bear)588 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 589why, one step back589Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371
lines 590–591why, one step back590 it("returns a normal object", () => {591 const bear = {paw: {honey: true}}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 592–600why, one step back592 const next = produce(bear, draft => {593 const paw2 = produce(bear.paw, draft => {594 draft.honey = false595 })596 expect(paw2.honey).toBe(false)597 expect(draft.paw.honey).toBe(true)598 draft.paw = paw2599 expect(draft.paw.honey).toBe(false)600 })Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371
lines 601–605why, one step back601 expect(next.paw.honey).toBe(false)602 expect(next).not.toBe(bear)603 })604 })605test: base state that contains a draftaleclarsonbefore adoptiond9b91d5
line 606why, one step back606 describe("when base state is a draft", () => {Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371
lines 607–608why, one step back607 it("always reuses the draft", () => {608 const bear = {paw: {honey: true}}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 609–617why, one step back609 const next = produce(bear, bear => {610 const paw2 = produce(bear.paw, paw => {611 expect(paw).toBe(bear.paw)612 paw.honey = false613 })614 expect(paw2).toBe(bear.paw)615 expect(paw2.honey).toBe(false)616 expect(bear.paw.honey).toBe(false)617 })Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371
lines 618–619why, one step back618 expect(next.paw.honey).toBe(false)619 expect(next).not.toBe(bear)test: base state that contains a draftaleclarsonbefore adoptiond9b91d5
lines 620–659why, one step back620 })621 })622623 describe("when base state contains a draft", () => {624 it("wraps unowned draft with its own draft", () => {625 produce({a: {}}, parent => {626 produce({a: parent.a}, child => {627 expect(child.a).not.toBe(parent.a)628 expect(isDraft(child.a)).toBeTruthy()629 })630 })631 })632633 it("returns unowned draft if no changes were made", () => {634 produce({a: {}}, parent => {635 const result = produce({a: parent.a}, () => {})636 expect(result.a).toBe(parent.a)637 })638 })639640 it("clones the unowned draft when changes are made", () => {641 produce({a: {}}, parent => {642 const result = produce({a: parent.a}, child => {643 child.a.b = 1644 })645 expect(result.a).not.toBe(parent.a)646 expect(result.a.b).toBe(1)647 expect("b" in parent.a).toBeFalsy()648 })649 })650651 // We cannot auto-freeze the result of a nested producer,652 // because it may contain a draft from a parent producer.653 it("never auto-freezes the result", () => {654 produce({a: {}}, parent => {655 const r = produce({a: parent.a}, child => {656 child.b = 1 // Ensure a copy is returned.657 })658 expect(Object.isFrozen(r)).toBeFalsy()659 })Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371
lines 660–661why, one step back660 })661 })test: returning an upvalue draft from a nested produceraleclarsonbefore adoptionb053637
lines 662–679why, one step back662663 // "Upvalues" are variables from a parent scope.664 it("does not finalize upvalue drafts", () => {665 produce({a: {}, b: {}}, parent => {666 expect(produce({}, () => parent)).toBe(parent)667 parent.x // Ensure proxy not revoked.668669 expect(produce({}, () => [parent])[0]).toBe(parent)670 parent.x // Ensure proxy not revoked.671672 expect(produce({}, () => parent.a)).toBe(parent.a)673 parent.a.x // Ensure proxy not revoked.674675 // Modified parent test676 parent.c = 1677 expect(produce({}, () => [parent.b])[0]).toBe(parent.b)678 parent.b.x // Ensure proxy not revoked.679 })test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5
lines 680–683why, one step back680 })681682 it("works with interweaved Immer instances", () => {683 const options = {useProxies, autoFreeze}test: patch the `produce` function for interweaved Immer instances tooaleclarsonbefore adoption31bbe64
lines 684–685why, one step back684 const one = createPatchedImmer(options)685 const two = createPatchedImmer(options)test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5
lines 686–700why, one step back686687 const base = {}688 const result = one.produce(base, s1 =>689 two.produce({s1}, s2 => {690 expect(original(s2.s1)).toBe(s1)691 s2.n = 1692 s2.s1 = one.produce({s2}, s3 => {693 expect(original(s3.s2)).toBe(s2)694 expect(original(s3.s2.s1)).toBe(s2.s1)695 return s3.s2.s1696 })697 })698 )699 expect(result.n).toBe(1)700 expect(result.s1).toBe(base)test: returning an upvalue draft from a nested produceraleclarsonbefore adoptionb053637
line 701why, one step back701 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 702–704why, one step back702 })703704 if (useProxies)chore: improve more testsaleclarsonbefore adoption4f59b12
line 705why, one step back705 it("throws when Object.setPrototypeOf() is used on a draft", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 706–707why, one step back706 produce({}, draft => {707 expect(() => Object.setPrototypeOf(draft, Array)).toThrow(feat: limited class supportaleclarsonbefore adoption9877d64
line 708why, one step back708 "Object.setPrototypeOf() cannot be used on an Immer draft"test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 709–710why, one step back709 )710 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 711–712why, one step back711 })712chore: improve more testsaleclarsonbefore adoption4f59b12
lines 713–715why, one step back713 it("supports the 'in' operator", () => {714 produce(baseState, draft => {715 // Known propertytest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 716–717why, one step back716 expect("anArray" in draft).toBe(true)717 expect(Reflect.has(draft, "anArray")).toBe(true)Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 718why, one step back718chore: improve more testsaleclarsonbefore adoption4f59b12
line 719why, one step back719 // Unknown propertytest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 720–721why, one step back720 expect("bla" in draft).toBe(false)721 expect(Reflect.has(draft, "bla")).toBe(false)Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 722why, one step back722chore: improve more testsaleclarsonbefore adoption4f59b12
line 723why, one step back723 // Known indextest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 724–727why, one step back724 expect(0 in draft.anArray).toBe(true)725 expect("0" in draft.anArray).toBe(true)726 expect(Reflect.has(draft.anArray, 0)).toBe(true)727 expect(Reflect.has(draft.anArray, "0")).toBe(true)Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 728why, one step back728chore: improve more testsaleclarsonbefore adoption4f59b12
line 729why, one step back729 // Unknown indextest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 730–734why, one step back730 expect(17 in draft.anArray).toBe(false)731 expect("17" in draft.anArray).toBe(false)732 expect(Reflect.has(draft.anArray, 17)).toBe(false)733 expect(Reflect.has(draft.anArray, "17")).toBe(false)734 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 735–738why, one step back735 })736737 it("'this' should work - 1", () => {738 const base = {x: 3}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 739–741why, one step back739 const next1 = produce(base, function() {740 this.x = 4741 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 742–745why, one step back742 expect(next1).not.toBe(base)743 expect(next1.x).toBe(4)744 })745chore: improve more testsaleclarsonbefore adoption4f59b12
line 746why, one step back746 it("'this' should work - 2", () => {Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 747–755why, one step back747 const base = {x: 3}748 const incrementor = produce(function() {749 this.x = 4750 })751 const next1 = incrementor(base)752 expect(next1).not.toBe(base)753 expect(next1.x).toBe(4)754 })755test: rename and expand a testaleclarsonbefore adoption733e16f
line 756why, one step back756 // See here: https://github.com/mweststrate/immer/issues/89chore: improve more testsaleclarsonbefore adoption4f59b12
line 757why, one step back757 it("supports the spread operator", () => {test: rename and expand a testaleclarsonbefore adoption733e16f
lines 758–761why, one step back758 const base = {foo: {x: 0, y: 0}, bar: [0, 0]}759 const result = produce(base, draft => {760 draft.foo = {x: 1, ...draft.foo, y: 1}761 draft.bar = [1, ...draft.bar, 1]test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 762why, one step back762 })test: rename and expand a testaleclarsonbefore adoption733e16f
lines 763–765why, one step back763 expect(result).toEqual({764 foo: {x: 0, y: 1},765 bar: [1, 0, 0, 1]Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 766–770why, one step back766 })767 })768769 it("processes with lodash.set", () => {770 const base = [{id: 1, a: 1}]test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 771–773why, one step back771 const result = produce(base, draft => {772 lodash.set(draft, "[0].a", 2)773 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 774–779why, one step back774 expect(base[0].a).toEqual(1)775 expect(result[0].a).toEqual(2)776 })777778 it("processes with lodash.find", () => {779 const base = [{id: 1, a: 1}]test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 780–783why, one step back780 const result = produce(base, draft => {781 const obj1 = lodash.find(draft, {id: 1})782 lodash.set(obj1, "a", 2)783 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 784–787why, one step back784 expect(base[0].a).toEqual(1)785 expect(result[0].a).toEqual(2)786 })787chore: improve more testsaleclarsonbefore adoption4f59b12
lines 788–790why, one step back788 describe("recipe functions", () => {789 it("can return a new object", () => {790 const base = {x: 3}test: improve coveragealeclarsonbefore adoption14d7e3c
lines 791–792why, one step back791 const res = produce(base, d => {792 return {x: d.x + 1}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 793why, one step back793 })chore: improve more testsaleclarsonbefore adoption4f59b12
lines 794–795why, one step back794 expect(res).not.toBe(base)795 expect(res).toEqual({x: 4})test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 796why, one step back796 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 797why, one step back797chore: improve more testsaleclarsonbefore adoption4f59b12
lines 798–799why, one step back798 it("can return the draft", () => {799 const base = {x: 3}test: improve coveragealeclarsonbefore adoption14d7e3c
lines 800–802why, one step back800 const res = produce(base, d => {801 d.x = 4802 return dchore: improve more testsaleclarsonbefore adoption4f59b12
lines 803–805why, one step back803 })804 expect(res).not.toBe(base)805 expect(res).toEqual({x: 4})test: improve coveragealeclarsonbefore adoption14d7e3c
lines 806–843why, one step back806 })807808 it("can return a child draft", () => {809 const base = {a: {}}810 const res = produce(base, d => {811 return d.a812 })813 expect(res).toBe(base.a)814 })815816 it("can return a frozen object", () => {817 const res = deepFreeze([{x: 3}])818 expect(produce({}, () => res)).toBe(res)819 })820821 it("can return an object with two references to another object", () => {822 const next = produce({}, d => {823 const obj = {}824 return {obj, arr: [obj]}825 })826 expect(next.obj).toBe(next.arr[0])827 })828829 it("can return an object with two references to any pristine draft", () => {830 const base = {a: {}}831 const next = produce(base, d => {832 return [d.a, d.a]833 })834 expect(next[0]).toBe(base.a)835 expect(next[0]).toBe(next[1])836 })837838 it("cannot return an object that references itself", () => {839 const res = {}840 res.self = res841 expect(() => {842 produce(res, () => res.self)843 }).toThrow("Immer forbids circular references")test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 844why, one step back844 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 845–846why, one step back845 })846chore: improve more testsaleclarsonbefore adoption4f59b12
line 847why, one step back847 it("throws when the draft is modified and another object is returned", () => {Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 848–849why, one step back848 const base = {x: 3}849 expect(() => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 850–853why, one step back850 produce(base, draft => {851 draft.x = 4852 return {x: 5}853 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 854–856why, one step back854 }).toThrow(/An immer producer returned a new value/)855 })856chore: improve more testsaleclarsonbefore adoption4f59b12
line 857why, one step back857 it("should fix #117 - 1", () => {Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 858why, one step back858 const reducer = (state, action) =>test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 859–866why, one step back859 produce(state, draft => {860 switch (action.type) {861 case "SET_STARTING_DOTS":862 return draft.availableStartingDots.map(a => a)863 default:864 break865 }866 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 867–881why, one step back867 const base = {868 availableStartingDots: [869 {dots: 4, count: 1},870 {dots: 3, count: 2},871 {dots: 2, count: 3},872 {dots: 1, count: 4}873 ]874 }875 const next = reducer(base, {type: "SET_STARTING_DOTS"})876 expect(next).toEqual(base.availableStartingDots)877 expect(next).not.toBe(base.availableStartingDots)878 })879880 it("should fix #117 - 2", () => {881 const reducer = (state, action) =>test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 882–891why, one step back882 produce(state, draft => {883 switch (action.type) {884 case "SET_STARTING_DOTS":885 return {886 dots: draft.availableStartingDots.map(a => a)887 }888 default:889 break890 }891 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 892–903why, one step back892 const base = {893 availableStartingDots: [894 {dots: 4, count: 1},895 {dots: 3, count: 2},896 {dots: 2, count: 3},897 {dots: 1, count: 4}898 ]899 }900 const next = reducer(base, {type: "SET_STARTING_DOTS"})901 expect(next).toEqual({dots: base.availableStartingDots})902 })903chore: improve more testsaleclarsonbefore adoption4f59b12
line 904why, one step back904 it("cannot always detect noop assignments - 0", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 905why, one step back905 const baseState = {x: {y: 3}}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 906–909why, one step back906 const nextState = produce(baseState, d => {907 const a = d.x908 d.x = a909 })Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 910–912why, one step back910 expect(nextState).toBe(baseState)911 })912chore: improve more testsaleclarsonbefore adoption4f59b12
line 913why, one step back913 it("cannot always detect noop assignments - 1", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 914why, one step back914 const baseState = {x: {y: 3}}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 915–919why, one step back915 const nextState = produce(baseState, d => {916 const a = d.x917 d.x = 4918 d.x = a919 })Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 920–925why, one step back920 // Ideally, this should actually be the same instances921 // but this would be pretty expensive to detect,922 // so we don't atm923 expect(nextState).not.toBe(baseState)924 })925chore: improve more testsaleclarsonbefore adoption4f59b12
line 926why, one step back926 it("cannot always detect noop assignments - 2", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 927why, one step back927 const baseState = {x: {y: 3}}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 928–933why, one step back928 const nextState = produce(baseState, d => {929 const a = d.x930 const stuff = a.y + 3931 d.x = 4932 d.x = a933 })Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 934–939why, one step back934 // Ideally, this should actually be the same instances935 // but this would be pretty expensive to detect,936 // so we don't atm937 expect(nextState).not.toBe(baseState)938 })939chore: improve more testsaleclarsonbefore adoption4f59b12
line 940why, one step back940 it("cannot always detect noop assignments - 3", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 941why, one step back941 const baseState = {x: 3}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 942–944why, one step back942 const nextState = produce(baseState, d => {943 d.x = 3944 })Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 945–947why, one step back945 expect(nextState).toBe(baseState)946 })947chore: improve more testsaleclarsonbefore adoption4f59b12
line 948why, one step back948 it("cannot always detect noop assignments - 4", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 949why, one step back949 const baseState = {x: 3}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 950–953why, one step back950 const nextState = produce(baseState, d => {951 d.x = 4952 d.x = 3953 })Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 954–957why, one step back954 // Ideally, this should actually be the same instances955 // but this would be pretty expensive to detect,956 // so we don't atm957 expect(nextState).not.toBe(baseState)Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
lines 958–959why, one step back958 })959chore: improve more testsaleclarsonbefore adoption4f59b12
line 960why, one step back960 it("cannot produce undefined by returning undefined", () => {Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
lines 961–974why, one step back961 const base = 3962 expect(produce(base, () => 4)).toBe(4)963 expect(produce(base, () => null)).toBe(null)964 expect(produce(base, () => undefined)).toBe(3)965 expect(produce(base, () => {})).toBe(3)966 expect(produce(base, () => nothing)).toBe(undefined)967968 expect(produce({}, () => undefined)).toEqual({})969 expect(produce({}, () => nothing)).toBe(undefined)970 expect(produce(3, () => nothing)).toBe(undefined)971972 expect(produce(() => undefined)({})).toEqual({})973 expect(produce(() => nothing)({})).toBe(undefined)974 expect(produce(() => nothing)(3)).toBe(undefined)Tried to reproduce #195Michel Weststratebefore adoption4f27a93
lines 975–976why, one step back975 })976chore: improve more testsaleclarsonbefore adoption4f59b12
lines 977–980why, one step back977 describe("base state type", () => {978 testObjectTypes(produce)979 testLiteralTypes(produce)980 })Merge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417
line 981why, one step back981Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 982–986why, one step back982 afterEach(() => {983 expect(baseState).toBe(origBaseState)984 expect(baseState).toEqual(createBaseState())985 })986chore: improve more testsaleclarsonbefore adoption4f59b12
line 987why, one step back987 class Foo {}Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 988–989why, one step back988 function createBaseState() {989 const data = {chore: improve more testsaleclarsonbefore adoption4f59b12
line 990why, one step back990 anInstance: new Foo(),Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 991–999why, one step back991 anArray: [3, 2, {c: 3}, 1],992 aProp: "hi",993 anObject: {994 nested: {995 yummie: true996 },997 coffee: false998 }999 }test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 1000why, one step back1000 return autoFreeze ? deepFreeze(data) : dataMerge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 1001why, one step back1001 }Merge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417
lines 1002–1042why, one step back1002 })10031004 describe(`isDraft - ${name}`, () => {1005 it("returns true for object drafts", () => {1006 produce({}, state => {1007 expect(isDraft(state)).toBeTruthy()1008 })1009 })1010 it("returns true for array drafts", () => {1011 produce([], state => {1012 expect(isDraft(state)).toBeTruthy()1013 })1014 })1015 it("returns true for objects nested in object drafts", () => {1016 produce({a: {b: {}}}, state => {1017 expect(isDraft(state.a)).toBeTruthy()1018 expect(isDraft(state.a.b)).toBeTruthy()1019 })1020 })1021 it("returns false for new objects added to a draft", () => {1022 produce({}, state => {1023 state.a = {}1024 expect(isDraft(state.a)).toBeFalsy()1025 })1026 })1027 it("returns false for objects returned by the producer", () => {1028 const object = produce(null, Object.create)1029 expect(isDraft(object)).toBeFalsy()1030 })1031 it("returns false for arrays returned by the producer", () => {1032 const array = produce(null, _ => [])1033 expect(isDraft(array)).toBeFalsy()1034 })1035 it("returns false for object drafts returned by the producer", () => {1036 const object = produce({}, state => state)1037 expect(isDraft(object)).toBeFalsy()1038 })1039 it("returns false for array drafts returned by the producer", () => {1040 const array = produce([], state => state)1041 expect(isDraft(array)).toBeFalsy()1042 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 1043why, one step back1043 })test: extract some tests into their own functionaleclarsonbefore adoption21846ab
lines 1044–1045why, one step back1044}1045chore: improve more testsaleclarsonbefore adoption4f59b12
line 1046why, one step back1046function testObjectTypes(produce) {feat: limited class supportaleclarsonbefore adoption9877d64
lines 1047–1052why, one step back1047 class Foo {1048 constructor(foo) {1049 this.foo = foo1050 this[immerable] = true1051 }1052 }chore: improve more testsaleclarsonbefore adoption4f59b12
lines 1053–1057why, one step back1053 const values = {1054 "empty object": {},1055 "plain object": {a: 1, b: 2},1056 "object (no prototype)": Object.create(null),1057 "empty array": [],feat: limited class supportaleclarsonbefore adoption9877d64
lines 1058–1059why, one step back1058 "plain array": [1, 2],1059 "class instance (draftable)": new Foo(1)chore: improve more testsaleclarsonbefore adoption4f59b12
lines 1060–1100why, one step back1060 }1061 for (const name in values) {1062 const value = values[name]1063 const copy = shallowCopy(value)1064 testObjectType(name, value)1065 testObjectType(name + " (frozen)", Object.freeze(copy))1066 }1067 function testObjectType(name, base) {1068 describe(name, () => {1069 it("creates a draft", () => {1070 produce(base, draft => {1071 expect(draft).not.toBe(base)1072 expect(shallowCopy(draft, true)).toEqual(base)1073 })1074 })10751076 it("preserves the prototype", () => {1077 const proto = Object.getPrototypeOf(base)1078 produce(base, draft => {1079 expect(Object.getPrototypeOf(draft)).toBe(proto)1080 })1081 })10821083 it("returns the base state when no changes are made", () => {1084 expect(produce(base, () => {})).toBe(base)1085 })10861087 it("returns a copy when changes are made", () => {1088 const random = Math.random()1089 const result = produce(base, draft => {1090 draft[0] = random1091 })1092 expect(result).not.toBe(base)1093 expect(result.constructor).toBe(base.constructor)1094 expect(result[0]).toBe(random)1095 })1096 })1097 }1098}10991100function testLiteralTypes(produce) {test: extract some tests into their own functionaleclarsonbefore adoption21846ab
line 1101why, one step back1101 class Foo {}chore: improve more testsaleclarsonbefore adoption4f59b12
line 1102why, one step back1102 const values = {test: extract some tests into their own functionaleclarsonbefore adoption21846ab
lines 1103–1105why, one step back1103 "falsy number": 0,1104 "truthy number": 1,1105 "negative number": -1,chore: improve more testsaleclarsonbefore adoption4f59b12
line 1106why, one step back1106 NaN: NaN,test: extract some tests into their own functionaleclarsonbefore adoption21846ab
lines 1107–1115why, one step back1107 infinity: 1 / 0,1108 true: true,1109 false: false,1110 "empty string": "",1111 "truthy string": "1",1112 null: null,1113 undefined: undefined,11141115 /**chore: improve more testsaleclarsonbefore adoption4f59b12
lines 1116–1117why, one step back1116 * These objects are treated as literals because Immer1117 * does not know how to draft them.test: extract some tests into their own functionaleclarsonbefore adoption21846ab
line 1118why, one step back1118 */chore: improve more testsaleclarsonbefore adoption4f59b12
line 1119why, one step back1119 function: () => {},test: extract some tests into their own functionaleclarsonbefore adoption21846ab
lines 1120–1124why, one step back1120 "regexp object": /.+/g,1121 "boxed number": new Number(0),1122 "boxed string": new String(""),1123 "boxed boolean": new Boolean(),1124 "date object": new Date(),feat: limited class supportaleclarsonbefore adoption9877d64
line 1125why, one step back1125 "class instance (not draftable)": new Foo()test: extract some tests into their own functionaleclarsonbefore adoption21846ab
line 1126why, one step back1126 }chore: improve more testsaleclarsonbefore adoption4f59b12
lines 1127–1130why, one step back1127 for (const name in values) {1128 describe(name, () => {1129 const value = values[name]1130test: extract some tests into their own functionaleclarsonbefore adoption21846ab
lines 1131–1135why, one step back1131 it("does not create a draft", () => {1132 produce(value, draft => {1133 expect(draft).toBe(value)1134 })1135 })chore: improve more testsaleclarsonbefore adoption4f59b12
lines 1136–1137why, one step back11361137 it("returns the base state when no changes are made", () => {test: extract some tests into their own functionaleclarsonbefore adoption21846ab
lines 1138–1139why, one step back1138 expect(produce(value, () => {})).toBe(value)1139 })chore: improve more testsaleclarsonbefore adoption4f59b12
line 1140why, one step back1140test: extract some tests into their own functionaleclarsonbefore adoption21846ab
line 1141why, one step back1141 if (value && typeof value == "object") {chore: improve more testsaleclarsonbefore adoption4f59b12
line 1142why, one step back1142 it("does not return a copy when changes are made", () => {test: extract some tests into their own functionaleclarsonbefore adoption21846ab
lines 1143–1151why, one step back1143 expect(1144 produce(value, draft => {1145 draft.foo = true1146 })1147 ).toBe(value)1148 })1149 }1150 })1151 }Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 1152–1154why, one step back1152}11531154function enumerableOnly(x) {feat: limited class supportaleclarsonbefore adoption9877d64
lines 1155–1156why, one step back1155 const copy = Array.isArray(x) ? x.slice() : Object.assign({}, x)1156 each(copy, (prop, value) => {test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44
line 1157why, one step back1157 if (value && typeof value === "object") {feat: limited class supportaleclarsonbefore adoption9877d64
line 1158why, one step back1158 copy[prop] = enumerableOnly(value)test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44
line 1159why, one step back1159 }feat: limited class supportaleclarsonbefore adoption9877d64
line 1160why, one step back1160 })test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44
line 1161why, one step back1161 return copyMerge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 1162why, one step back1162}