base.js
This file
Path
__tests__/base.js
1,210 lines · 42.6 KiB · JavaScript
Last changed by
test: avoid async/await for node 6aleclarsonbefore adoptione9f274e
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–115why, one step back113 it("can get property descriptors", () => {114 const getDescriptor = Object.getOwnPropertyDescriptor115 const baseState = deepFreeze([{a: 1}])test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
lines 116–122why, one step back116 produce(baseState, arr => {117 const obj = arr[0]118 const desc = {119 configurable: true,120 enumerable: true,121 ...(useProxies && {writable: true})122 }Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 123why, one step back123chore: improve more testsaleclarsonbefore adoption4f59b12
line 124why, one step back124 // Known propertytest: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
lines 125–126why, one step back125 expect(getDescriptor(obj, "a")).toMatchObject(desc)126 expect(getDescriptor(arr, 0)).toMatchObject(desc)chore: improve more testsaleclarsonbefore adoption4f59b12
lines 127–129why, one step back127128 // Deleted property129 delete obj.atest: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
line 130why, one step back130 arr.pop()chore: improve more testsaleclarsonbefore adoption4f59b12
line 131why, one step back131 expect(getDescriptor(obj, "a")).toBeUndefined()test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
line 132why, one step back132 expect(getDescriptor(arr, 0)).toBeUndefined()chore: improve more testsaleclarsonbefore adoption4f59b12
lines 133–135why, one step back133134 // Unknown property135 expect(getDescriptor(obj, "b")).toBeUndefined()test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
line 136why, one step back136 expect(getDescriptor(arr, 100)).toBeUndefined()chore: improve more testsaleclarsonbefore adoption4f59b12
lines 137–139why, one step back137138 // Added property139 obj.b = 2test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
line 140why, one step back140 arr[100] = 1chore: improve more testsaleclarsonbefore adoption4f59b12
line 141why, one step back141 expect(getDescriptor(obj, "b")).toBeDefined()test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
line 142why, one step back142 expect(getDescriptor(arr, 100)).toBeDefined()test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 143why, one step back143 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 144–145why, one step back144 })145chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 146–152why, one step back146 describe("array drafts", () => {147 it("supports Array.isArray()", () => {148 const nextState = produce(baseState, s => {149 expect(Array.isArray(s.anArray)).toBeTruthy()150 s.anArray.push(1)151 })152 expect(Array.isArray(nextState.anArray)).toBeTruthy()Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 153–154why, one step back153 })154chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 155–160why, one step back155 it("supports index access", () => {156 const value = baseState.anArray[0]157 const nextState = produce(baseState, s => {158 expect(s.anArray[0]).toBe(value)159 })160 expect(nextState).toBe(baseState)test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 161why, one step back161 })chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 162–178why, one step back162163 it("supports iteration", () => {164 const base = [{id: 1, a: 1}, {id: 2, a: 1}]165 const findById = (collection, id) => {166 for (const item of collection) {167 if (item.id === id) return item168 }169 return null170 }171 const result = produce(base, draft => {172 const obj1 = findById(draft, 1)173 const obj2 = findById(draft, 2)174 obj1.a = 2175 obj2.a = 2176 })177 expect(result[0].a).toEqual(2)178 expect(result[1].a).toEqual(2)Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 179–180why, one step back179 })180chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 181–187why, one step back181 it("can assign an index via bracket notation", () => {182 const nextState = produce(baseState, s => {183 s.anArray[3] = true184 })185 expect(nextState).not.toBe(baseState)186 expect(nextState.anArray).not.toBe(baseState.anArray)187 expect(nextState.anArray[3]).toEqual(true)test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 188why, one step back188 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 189why, one step back189chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 190–196why, one step back190 it("can use splice() to both add and remove items", () => {191 const nextState = produce(baseState, s => {192 s.anArray.splice(1, 1, "a", "b")193 })194 expect(nextState.anArray).not.toBe(baseState.anArray)195 expect(nextState.anArray[1]).toBe("a")196 expect(nextState.anArray[2]).toBe("b")test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 197why, one step back197 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 198why, one step back198chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 199–205why, one step back199 it("can truncate via the length property", () => {200 const baseLength = baseState.anArray.length201 const nextState = produce(baseState, s => {202 s.anArray.length = baseLength - 1203 })204 expect(nextState.anArray).not.toBe(baseState.anArray)205 expect(nextState.anArray.length).toBe(baseLength - 1)test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 206why, one step back206 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 207why, one step back207chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 208–215why, one step back208 it("can extend via the length property", () => {209 const baseLength = baseState.anArray.length210 const nextState = produce(baseState, s => {211 s.anArray.length = baseLength + 1212 })213 expect(nextState.anArray).not.toBe(baseState.anArray)214 expect(nextState.anArray.length).toBe(baseLength + 1)215 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 216why, one step back216chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 217–223why, one step back217 // Reported here: https://github.com/mweststrate/immer/issues/116218 it("can pop then push", () => {219 const nextState = produce([1, 2, 3], s => {220 s.pop()221 s.push(100)222 })223 expect(nextState).toEqual([1, 2, 100])test: rename and move a testaleclarsonbefore adoption8e0325c
lines 224–225why, one step back224 })225chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 226–232why, one step back226 it("can be sorted", () => {227 const baseState = [3, 1, 2]228 const nextState = produce(baseState, s => {229 s.sort()230 })231 expect(nextState).not.toBe(baseState)232 expect(nextState).toEqual([1, 2, 3])test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 233why, one step back233 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 234why, one step back234chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 235–243why, one step back235 it("supports modifying nested objects", () => {236 const baseState = [{a: 1}, {}]237 const nextState = produce(baseState, s => {238 s[0].a++239 s[1].a = 0240 })241 expect(nextState).not.toBe(baseState)242 expect(nextState[0].a).toBe(2)243 expect(nextState[1].a).toBe(0)test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 244why, one step back244 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 245why, one step back245chore: improve more testsaleclarsonbefore adoption4f59b12
lines 246–250why, one step back246 it("never preserves non-numeric properties", () => {247 const baseState = []248 baseState.x = 7249 const nextState = produce(baseState, s => {250 s.push(3)test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 251why, one step back251 })chore: improve more testsaleclarsonbefore adoption4f59b12
line 252why, one step back252 expect("x" in nextState).toBeFalsy()test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 253why, one step back253 })chore: improve more testsaleclarsonbefore adoption4f59b12
lines 254–260why, one step back254255 if (useProxies) {256 it("throws when a non-numeric property is added", () => {257 expect(() => {258 produce([], d => {259 d.x = 3260 })feat: limited class supportaleclarsonbefore adoption9877d64
lines 261–263why, one step back261 }).toThrow(262 "Immer only supports setting array indices and the 'length' property"263 )chore: improve more testsaleclarsonbefore adoption4f59b12
lines 264–272why, one step back264 })265266 it("throws when a non-numeric property is deleted", () => {267 expect(() => {268 const baseState = []269 baseState.x = 7270 produce(baseState, d => {271 delete d.x272 })feat: limited class supportaleclarsonbefore adoption9877d64
line 273why, one step back273 }).toThrow("Immer only supports deleting array indices")chore: improve more testsaleclarsonbefore adoption4f59b12
lines 274–275why, one step back274 })275 }Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 276–277why, one step back276 })277feat: limited class supportaleclarsonbefore adoption9877d64
lines 278–304why, one step back278 it("supports `immerable` symbol on constructor", () => {279 class One {}280 One[immerable] = true281 const baseState = new One()282 const nextState = produce(baseState, draft => {283 expect(draft).not.toBe(baseState)284 draft.foo = true285 })286 expect(nextState).not.toBe(baseState)287 expect(nextState.foo).toBeTruthy()288 })289290 it("preserves symbol properties", () => {291 const test = Symbol("test")292 const baseState = {[test]: true}293 const nextState = produce(baseState, s => {294 expect(s[test]).toBeTruthy()295 s.foo = true296 })297 expect(nextState).toEqual({298 [test]: true,299 foo: true300 })301 })302303 it("preserves non-enumerable properties", () => {304 const baseState = {}test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
line 305why, one step back305 // Non-enumerable object propertyfeat: limited class supportaleclarsonbefore adoption9877d64
line 306why, one step back306 Object.defineProperty(baseState, "foo", {test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
line 307why, one step back307 value: {a: 1},feat: limited class supportaleclarsonbefore adoption9877d64
lines 308–309why, one step back308 enumerable: false309 })test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
lines 310–314why, one step back310 // Non-enumerable primitive property311 Object.defineProperty(baseState, "bar", {312 value: 1,313 enumerable: false314 })feat: limited class supportaleclarsonbefore adoption9877d64
lines 315–317why, one step back315 const nextState = produce(baseState, s => {316 expect(s.foo).toBeTruthy()317 expect(isEnumerable(s, "foo")).toBeFalsy()test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
lines 318–321why, one step back318 s.bar++319 expect(isEnumerable(s, "foo")).toBeFalsy()320 s.foo.a++321 expect(isEnumerable(s, "foo")).toBeFalsy()feat: limited class supportaleclarsonbefore adoption9877d64
lines 322–361why, one step back322 })323 expect(nextState.foo).toBeTruthy()324 expect(isEnumerable(nextState, "foo")).toBeFalsy()325 })326327 it("throws on computed properties", () => {328 const baseState = {}329 Object.defineProperty(baseState, "foo", {330 get: () => {},331 enumerable: true332 })333 expect(() => {334 produce(baseState, s => {335 // Proxies only throw once a change is made.336 if (useProxies) {337 s.modified = true338 }339 })340 }).toThrowError("Immer drafts cannot have computed properties")341 })342343 it("allows inherited computed properties", () => {344 const proto = {}345 Object.defineProperty(proto, "foo", {346 get() {347 return this.bar348 },349 set(val) {350 this.bar = val351 }352 })353 const baseState = Object.create(proto)354 produce(baseState, s => {355 expect(s.bar).toBeUndefined()356 s.foo = {}357 expect(s.bar).toBeDefined()358 expect(s.foo).toBe(s.bar)359 })360 })361test: improve coveragealeclarsonbefore adoption14d7e3c
lines 362–383why, one step back362 it("supports a base state with multiple references to an object", () => {363 const obj = {}364 const res = produce({a: obj, b: obj}, d => {365 // Two drafts are created for each occurrence of an object in the base state.366 expect(d.a).not.toBe(d.b)367 d.a.z = true368 expect(d.b.z).toBeUndefined()369 })370 expect(res.b).toBe(obj)371 expect(res.a).not.toBe(res.b)372 expect(res.a.z).toBeTruthy()373 })374375 // NOTE: Except the root draft.376 it("supports multiple references to any modified draft", () => {377 const next = produce({a: {b: 1}}, d => {378 d.a.b++379 d.b = d.a380 })381 expect(next.a).toBe(next.b)382 })383test: renaming propertiesaleclarsonbefore adoptiond83f4b5
line 384why, one step back384 it("can rename nested objects (no changes)", () => {chore: improve more testsaleclarsonbefore adoption4f59b12
lines 385–387why, one step back385 const nextState = produce({obj: {}}, s => {386 s.foo = s.obj387 delete s.objtest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 388why, one step back388 })chore: improve more testsaleclarsonbefore adoption4f59b12
line 389why, one step back389 expect(nextState).toEqual({foo: {}})Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 390–391why, one step back390 })391test: renaming propertiesaleclarsonbefore adoptiond83f4b5
lines 392–394why, one step back392 // Very similar to the test before, but the reused object has one393 // property changed, one added, and one removed.394 it("can rename nested objects (with changes)", () => {chore: improve more testsaleclarsonbefore adoption4f59b12
lines 395–398why, one step back395 const nextState = produce({obj: {a: 1, b: 1}}, s => {396 s.obj.a = true // change397 delete s.obj.b // delete398 s.obj.c = true // addtest: renaming propertiesaleclarsonbefore adoptiond83f4b5
line 399why, one step back399chore: improve more testsaleclarsonbefore adoption4f59b12
lines 400–401why, one step back400 s.foo = s.obj401 delete s.objtest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 402why, one step back402 })chore: improve more testsaleclarsonbefore adoption4f59b12
line 403why, one step back403 expect(nextState).toEqual({foo: {a: true, c: true}})Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 404–405why, one step back404 })405test: draft nested in new objectaleclarsonbefore adoptionf46659b
line 406why, one step back406 it("can nest a draft in a new object (no changes)", () => {chore: improve more testsaleclarsonbefore adoption4f59b12
line 407why, one step back407 const baseState = {obj: {}}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 408why, one step back408 const nextState = produce(baseState, s => {chore: improve more testsaleclarsonbefore adoption4f59b12
lines 409–410why, one step back409 s.foo = {bar: s.obj}410 delete s.objtest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 411why, one step back411 })chore: improve more testsaleclarsonbefore adoption4f59b12
line 412why, one step back412 expect(nextState.foo.bar).toBe(baseState.obj)test: draft nested in new objectaleclarsonbefore adoptionf46659b
lines 413–414why, one step back413 })414chore: improve more testsaleclarsonbefore adoption4f59b12
lines 415–419why, one step back415 it("can nest a modified draft in a new object", () => {416 const nextState = produce({obj: {a: 1, b: 1}}, s => {417 s.obj.a = true // change418 delete s.obj.b // delete419 s.obj.c = true // addtest: draft nested in new objectaleclarsonbefore adoptionf46659b
line 420why, one step back420chore: improve more testsaleclarsonbefore adoption4f59b12
lines 421–422why, one step back421 s.foo = {bar: s.obj}422 delete s.objtest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 423why, one step back423 })chore: improve more testsaleclarsonbefore adoption4f59b12
line 424why, one step back424 expect(nextState).toEqual({foo: {bar: {a: true, c: true}}})test: draft nested in new objectaleclarsonbefore adoptionf46659b
lines 425–426why, one step back425 })426chore: improve some testsaleclarsonbefore adoptionefd47f6
line 427why, one step back427 it("supports assigning undefined to an existing property", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 428–430why, one step back428 const nextState = produce(baseState, s => {429 s.aProp = undefined430 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 431why, one step back431 expect(nextState).not.toBe(baseState)chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 432–440why, one step back432 expect(nextState.aProp).toBe(undefined)433 })434435 it("supports assigning undefined to a new property", () => {436 const baseState = {}437 const nextState = produce(baseState, s => {438 s.aProp = undefined439 })440 expect(nextState).not.toBe(baseState)Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 441–443why, one step back441 expect(nextState.aProp).toBe(undefined)442 })443test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad
lines 444–449why, one step back444 // NOTE: ES5 drafts only protect existing properties when revoked.445 it("revokes the draft once produce returns", () => {446 const expectRevoked = (fn, shouldThrow = true) => {447 if (shouldThrow) expect(fn).toThrowError(/revoked/)448 else expect(fn).not.toThrow()449 }Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 450why, one step back450test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad
lines 451–456why, one step back451 // Test object drafts:452 let draft453 produce({a: 1, b: 1}, s => {454 draft = s455 delete s.b456 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 457why, one step back457test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad
lines 458–460why, one step back458 // Access known property on object draft.459 expectRevoked(() => {460 draft.aMerge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 461–462why, one step back461 })462test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad
lines 463–465why, one step back463 // Assign known property on object draft.464 expectRevoked(() => {465 draft.a = truetest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 466why, one step back466 })test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad
lines 467–502why, one step back467468 // Access unknown property on object draft.469 expectRevoked(() => {470 draft.z471 }, useProxies)472473 // Assign unknown property on object draft.474 expectRevoked(() => {475 draft.z = true476 }, useProxies)477478 // Test array drafts:479 produce([1, 2], s => {480 draft = s481 s.pop()482 })483484 // Access known index of an array draft.485 expectRevoked(() => {486 draft[0]487 })488489 // Assign known index of an array draft.490 expectRevoked(() => {491 draft[0] = true492 })493494 // Access unknown index of an array draft.495 expectRevoked(() => {496 draft[1]497 }, useProxies)498499 // Assign unknown index of an array draft.500 expectRevoked(() => {501 draft[1] = true502 }, useProxies)Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 503–504why, one step back503 })504test: add missing edge casealeclarsonbefore adoptionc46af36
lines 505–512why, one step back505 it("can access a child draft that was created before the draft was modified", () => {506 produce({a: {}}, s => {507 const before = s.a508 s.b = 1509 expect(s.a).toBe(before)510 })511 })512Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 513why, one step back513 it("should reflect all changes made in the draft immediately", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 514–516why, one step back514 produce(baseState, draft => {515 draft.anArray[0] = 5516 draft.anArray.unshift("test")test: tiny improvementaleclarsonbefore adoptionee49da3
line 517why, one step back517 expect(enumerableOnly(draft.anArray)).toEqual([test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 518–526why, one step back518 "test",519 5,520 2,521 {c: 3},522 1523 ])524 draft.stuffz = "coffee"525 expect(draft.stuffz).toBe("coffee")526 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 527–528why, one step back527 })528chore: improve more testsaleclarsonbefore adoption4f59b12
lines 529–530why, one step back529 if (useProxies)530 it("throws when Object.defineProperty() is used on drafts", () => {Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 531why, one step back531 expect(() => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 532–538why, one step back532 produce({}, draft => {533 Object.defineProperty(draft, "xx", {534 enumerable: true,535 writeable: true,536 value: 2537 })538 })feat: limited class supportaleclarsonbefore adoption9877d64
lines 539–541why, one step back539 }).toThrow(540 "Object.defineProperty() cannot be used on an Immer draft"541 )Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 542why, one step back542 })Be explicit about not supporting arbitrary properties on arraysMichel Weststratebefore adoption0a80c2c
line 543why, one step back543Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 544why, one step back544 it("should handle constructor correctly", () => {chore: improve more testsaleclarsonbefore adoption4f59b12
line 545why, one step back545 const baseState = {Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 546–548why, one step back546 arr: new Array(),547 obj: new Object()548 }chore: improve more testsaleclarsonbefore adoption4f59b12
line 549why, one step back549 const result = produce(baseState, draft => {Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 550–557why, one step back550 draft.arrConstructed = draft.arr.constructor(1)551 draft.objConstructed = draft.obj.constructor(1)552 })553 expect(result.arrConstructed).toEqual(new Array().constructor(1))554 expect(result.objConstructed).toEqual(new Object().constructor(1))555 })556557 it("should handle equality correctly - 1", () => {chore: improve more testsaleclarsonbefore adoption4f59b12
line 558why, one step back558 const baseState = {Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 559–561why, one step back559 y: 3 / 0,560 z: NaN561 }chore: improve more testsaleclarsonbefore adoption4f59b12
line 562why, one step back562 const nextState = produce(baseState, draft => {Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 563–565why, one step back563 draft.y = 4 / 0564 draft.z = NaN565 })chore: improve more testsaleclarsonbefore adoption4f59b12
line 566why, one step back566 expect(nextState).toBe(baseState)Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 567–569why, one step back567 })568569 it("should handle equality correctly - 2", () => {chore: improve more testsaleclarsonbefore adoption4f59b12
line 570why, one step back570 const baseState = {Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 571–572why, one step back571 x: -0572 }chore: improve more testsaleclarsonbefore adoption4f59b12
lines 573–574why, one step back573 const nextState = produce(baseState, draft => {574 draft.x = +0Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 575why, one step back575 })chore: improve more testsaleclarsonbefore adoption4f59b12
lines 576–577why, one step back576 expect(nextState).not.toBe(baseState)577 expect(nextState).not.toEqual(baseState)Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 578–579why, one step back578 })579test: base state that contains a draftaleclarsonbefore adoptiond9b91d5
line 580why, one step back580 // AKA: recursive produce callschore: improve more testsaleclarsonbefore adoption4f59b12
line 581why, one step back581 describe("nested producers", () => {test: base state that contains a draftaleclarsonbefore adoptiond9b91d5
line 582why, one step back582 describe("when base state is not a draft", () => {Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371
lines 583–585why, one step back583 // This test ensures the global state used to manage proxies is584 // never left in a corrupted state by a nested `produce` call.585 it("never affects its parent producer implicitly", () => {test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736
lines 586–590why, one step back586 const base = {obj: {a: 1}}587 const next = produce(base, draft => {588 // Notice how `base.obj` is passed, not `draft.obj`589 const obj2 = produce(base.obj, draft2 => {590 draft2.a = 0test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 591why, one step back591 })test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736
lines 592–593why, one step back592 expect(obj2.a).toBe(0)593 expect(draft.obj.a).toBe(1) // effects should not be visible outsidetest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 594why, one step back594 })test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736
line 595why, one step back595 expect(next).toBe(base)Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371
lines 596–598why, one step back596 })597 })598test: base state that contains a draftaleclarsonbefore adoptiond9b91d5
line 599why, one step back599 describe("when base state is a draft", () => {test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736
lines 600–605why, one step back600 it("always wraps the draft in a new draft", () => {601 produce({}, parent => {602 produce(parent, child => {603 expect(child).not.toBe(parent)604 expect(isDraft(child)).toBeTruthy()605 expect(original(child)).toBe(parent)test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 606–607why, one step back606 })607 })test: base state that contains a draftaleclarsonbefore adoptiond9b91d5
lines 608–616why, one step back608 })609 })610611 describe("when base state contains a draft", () => {612 it("wraps unowned draft with its own draft", () => {613 produce({a: {}}, parent => {614 produce({a: parent.a}, child => {615 expect(child.a).not.toBe(parent.a)616 expect(isDraft(child.a)).toBeTruthy()test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736
line 617why, one step back617 expect(original(child.a)).toBe(parent.a)test: base state that contains a draftaleclarsonbefore adoptiond9b91d5
lines 618–648why, one step back618 })619 })620 })621622 it("returns unowned draft if no changes were made", () => {623 produce({a: {}}, parent => {624 const result = produce({a: parent.a}, () => {})625 expect(result.a).toBe(parent.a)626 })627 })628629 it("clones the unowned draft when changes are made", () => {630 produce({a: {}}, parent => {631 const result = produce({a: parent.a}, child => {632 child.a.b = 1633 })634 expect(result.a).not.toBe(parent.a)635 expect(result.a.b).toBe(1)636 expect("b" in parent.a).toBeFalsy()637 })638 })639640 // We cannot auto-freeze the result of a nested producer,641 // because it may contain a draft from a parent producer.642 it("never auto-freezes the result", () => {643 produce({a: {}}, parent => {644 const r = produce({a: parent.a}, child => {645 child.b = 1 // Ensure a copy is returned.646 })647 expect(Object.isFrozen(r)).toBeFalsy()648 })Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371
lines 649–650why, one step back649 })650 })test: returning an upvalue draft from a nested produceraleclarsonbefore adoptionb053637
lines 651–668why, one step back651652 // "Upvalues" are variables from a parent scope.653 it("does not finalize upvalue drafts", () => {654 produce({a: {}, b: {}}, parent => {655 expect(produce({}, () => parent)).toBe(parent)656 parent.x // Ensure proxy not revoked.657658 expect(produce({}, () => [parent])[0]).toBe(parent)659 parent.x // Ensure proxy not revoked.660661 expect(produce({}, () => parent.a)).toBe(parent.a)662 parent.a.x // Ensure proxy not revoked.663664 // Modified parent test665 parent.c = 1666 expect(produce({}, () => [parent.b])[0]).toBe(parent.b)667 parent.b.x // Ensure proxy not revoked.668 })test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5
lines 669–672why, one step back669 })670671 it("works with interweaved Immer instances", () => {672 const options = {useProxies, autoFreeze}test: patch the `produce` function for interweaved Immer instances tooaleclarsonbefore adoption31bbe64
lines 673–674why, one step back673 const one = createPatchedImmer(options)674 const two = createPatchedImmer(options)test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5
lines 675–689why, one step back675676 const base = {}677 const result = one.produce(base, s1 =>678 two.produce({s1}, s2 => {679 expect(original(s2.s1)).toBe(s1)680 s2.n = 1681 s2.s1 = one.produce({s2}, s3 => {682 expect(original(s3.s2)).toBe(s2)683 expect(original(s3.s2.s1)).toBe(s2.s1)684 return s3.s2.s1685 })686 })687 )688 expect(result.n).toBe(1)689 expect(result.s1).toBe(base)test: returning an upvalue draft from a nested produceraleclarsonbefore adoptionb053637
line 690why, one step back690 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 691–693why, one step back691 })692693 if (useProxies)chore: improve more testsaleclarsonbefore adoption4f59b12
line 694why, one step back694 it("throws when Object.setPrototypeOf() is used on a draft", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 695–696why, one step back695 produce({}, draft => {696 expect(() => Object.setPrototypeOf(draft, Array)).toThrow(feat: limited class supportaleclarsonbefore adoption9877d64
line 697why, one step back697 "Object.setPrototypeOf() cannot be used on an Immer draft"test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 698–699why, one step back698 )699 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 700–701why, one step back700 })701chore: improve more testsaleclarsonbefore adoption4f59b12
lines 702–704why, one step back702 it("supports the 'in' operator", () => {703 produce(baseState, draft => {704 // Known propertytest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 705–706why, one step back705 expect("anArray" in draft).toBe(true)706 expect(Reflect.has(draft, "anArray")).toBe(true)Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 707why, one step back707chore: improve more testsaleclarsonbefore adoption4f59b12
line 708why, one step back708 // Unknown propertytest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 709–710why, one step back709 expect("bla" in draft).toBe(false)710 expect(Reflect.has(draft, "bla")).toBe(false)Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 711why, one step back711chore: improve more testsaleclarsonbefore adoption4f59b12
line 712why, one step back712 // Known indextest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 713–716why, one step back713 expect(0 in draft.anArray).toBe(true)714 expect("0" in draft.anArray).toBe(true)715 expect(Reflect.has(draft.anArray, 0)).toBe(true)716 expect(Reflect.has(draft.anArray, "0")).toBe(true)Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 717why, one step back717chore: improve more testsaleclarsonbefore adoption4f59b12
line 718why, one step back718 // Unknown indextest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 719–723why, one step back719 expect(17 in draft.anArray).toBe(false)720 expect("17" in draft.anArray).toBe(false)721 expect(Reflect.has(draft.anArray, 17)).toBe(false)722 expect(Reflect.has(draft.anArray, "17")).toBe(false)723 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 724–727why, one step back724 })725726 it("'this' should work - 1", () => {727 const base = {x: 3}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 728–730why, one step back728 const next1 = produce(base, function() {729 this.x = 4730 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 731–734why, one step back731 expect(next1).not.toBe(base)732 expect(next1.x).toBe(4)733 })734chore: improve more testsaleclarsonbefore adoption4f59b12
line 735why, one step back735 it("'this' should work - 2", () => {Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 736–744why, one step back736 const base = {x: 3}737 const incrementor = produce(function() {738 this.x = 4739 })740 const next1 = incrementor(base)741 expect(next1).not.toBe(base)742 expect(next1.x).toBe(4)743 })744test: rename and expand a testaleclarsonbefore adoption733e16f
line 745why, one step back745 // See here: https://github.com/mweststrate/immer/issues/89chore: improve more testsaleclarsonbefore adoption4f59b12
line 746why, one step back746 it("supports the spread operator", () => {test: rename and expand a testaleclarsonbefore adoption733e16f
lines 747–750why, one step back747 const base = {foo: {x: 0, y: 0}, bar: [0, 0]}748 const result = produce(base, draft => {749 draft.foo = {x: 1, ...draft.foo, y: 1}750 draft.bar = [1, ...draft.bar, 1]test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 751why, one step back751 })test: rename and expand a testaleclarsonbefore adoption733e16f
lines 752–754why, one step back752 expect(result).toEqual({753 foo: {x: 0, y: 1},754 bar: [1, 0, 0, 1]Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 755–759why, one step back755 })756 })757758 it("processes with lodash.set", () => {759 const base = [{id: 1, a: 1}]test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 760–762why, one step back760 const result = produce(base, draft => {761 lodash.set(draft, "[0].a", 2)762 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 763–768why, one step back763 expect(base[0].a).toEqual(1)764 expect(result[0].a).toEqual(2)765 })766767 it("processes with lodash.find", () => {768 const base = [{id: 1, a: 1}]test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 769–772why, one step back769 const result = produce(base, draft => {770 const obj1 = lodash.find(draft, {id: 1})771 lodash.set(obj1, "a", 2)772 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 773–776why, one step back773 expect(base[0].a).toEqual(1)774 expect(result[0].a).toEqual(2)775 })776chore: improve more testsaleclarsonbefore adoption4f59b12
lines 777–779why, one step back777 describe("recipe functions", () => {778 it("can return a new object", () => {779 const base = {x: 3}test: improve coveragealeclarsonbefore adoption14d7e3c
lines 780–781why, one step back780 const res = produce(base, d => {781 return {x: d.x + 1}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 782why, one step back782 })chore: improve more testsaleclarsonbefore adoption4f59b12
lines 783–784why, one step back783 expect(res).not.toBe(base)784 expect(res).toEqual({x: 4})test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 785why, one step back785 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 786why, one step back786chore: improve more testsaleclarsonbefore adoption4f59b12
lines 787–788why, one step back787 it("can return the draft", () => {788 const base = {x: 3}test: improve coveragealeclarsonbefore adoption14d7e3c
lines 789–791why, one step back789 const res = produce(base, d => {790 d.x = 4791 return dchore: improve more testsaleclarsonbefore adoption4f59b12
lines 792–794why, one step back792 })793 expect(res).not.toBe(base)794 expect(res).toEqual({x: 4})test: improve coveragealeclarsonbefore adoption14d7e3c
lines 795–817why, one step back795 })796797 it("can return a child draft", () => {798 const base = {a: {}}799 const res = produce(base, d => {800 return d.a801 })802 expect(res).toBe(base.a)803 })804805 it("can return a frozen object", () => {806 const res = deepFreeze([{x: 3}])807 expect(produce({}, () => res)).toBe(res)808 })809810 it("can return an object with two references to another object", () => {811 const next = produce({}, d => {812 const obj = {}813 return {obj, arr: [obj]}814 })815 expect(next.obj).toBe(next.arr[0])816 })817feat: support async recipesaleclarsonbefore adoption5fee518
line 818why, one step back818 it("can return an object with two references to an unmodified draft", () => {test: improve coveragealeclarsonbefore adoption14d7e3c
lines 819–832why, one step back819 const base = {a: {}}820 const next = produce(base, d => {821 return [d.a, d.a]822 })823 expect(next[0]).toBe(base.a)824 expect(next[0]).toBe(next[1])825 })826827 it("cannot return an object that references itself", () => {828 const res = {}829 res.self = res830 expect(() => {831 produce(res, () => res.self)832 }).toThrow("Immer forbids circular references")feat: support async recipesaleclarsonbefore adoption5fee518
lines 833–835why, one step back833 })834 })835test: avoid async/await for node 6aleclarsonbefore adoptione9f274e
line 836why, one step back836 // TODO: rewrite tests with async/await once node 6 support is droppedfeat: support async recipesaleclarsonbefore adoption5fee518
line 837why, one step back837 describe("async recipe function", () => {test: avoid async/await for node 6aleclarsonbefore adoptione9f274e
line 838why, one step back838 it("can modify the draft", () => {feat: support async recipesaleclarsonbefore adoption5fee518
line 839why, one step back839 const base = {a: 0, b: 0}test: avoid async/await for node 6aleclarsonbefore adoptione9f274e
line 840why, one step back840 return produce(base, d => {feat: support async recipesaleclarsonbefore adoption5fee518
line 841why, one step back841 d.a = 1test: avoid async/await for node 6aleclarsonbefore adoptione9f274e
lines 842–847why, one step back842 return Promise.resolve().then(() => {843 d.b = 1844 })845 }).then(res => {846 expect(res).not.toBe(base)847 expect(res).toEqual({a: 1, b: 1})feat: support async recipesaleclarsonbefore adoption5fee518
lines 848–850why, one step back848 })849 })850test: avoid async/await for node 6aleclarsonbefore adoptione9f274e
line 851why, one step back851 it("works with rejected promises", () => {feat: support async recipesaleclarsonbefore adoption5fee518
lines 852–854why, one step back852 let draft853 const base = {a: 0, b: 0}854 const err = new Error("passed")test: avoid async/await for node 6aleclarsonbefore adoptione9f274e
lines 855–867why, one step back855 return produce(base, d => {856 draft = d857 draft.b = 1858 return Promise.reject(err)859 }).then(860 () => {861 throw "failed"862 },863 e => {864 expect(e).toBe(err)865 expect(() => draft.a).toThrowError(/revoked/)866 }867 )test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 868why, one step back868 })test: recursive produce call in async recipealeclarsonbefore adoptione266f4b
line 869why, one step back869test: avoid async/await for node 6aleclarsonbefore adoptione9f274e
line 870why, one step back870 it("supports recursive produce calls after await", () => {test: recursive produce call in async recipealeclarsonbefore adoptione266f4b
line 871why, one step back871 const base = {obj: {k: 1}}test: avoid async/await for node 6aleclarsonbefore adoptione9f274e
line 872why, one step back872 return produce(base, d => {test: recursive produce call in async recipealeclarsonbefore adoptione266f4b
lines 873–874why, one step back873 const obj = d.obj874 delete d.objtest: avoid async/await for node 6aleclarsonbefore adoptione9f274e
lines 875–878why, one step back875 return Promise.resolve().then(() => {876 d.a = produce({}, d => {877 d.b = obj // Assign a draft owned by the parent scope.878 })test: recursive produce call in async recipealeclarsonbefore adoptione266f4b
line 879why, one step back879test: avoid async/await for node 6aleclarsonbefore adoptione9f274e
lines 880–881why, one step back880 // Auto-freezing is prevented when an unowned draft exists.881 expect(Object.isFrozen(d.a)).toBeFalsy()test: recursive produce call in async recipealeclarsonbefore adoptione266f4b
line 882why, one step back882test: avoid async/await for node 6aleclarsonbefore adoptione9f274e
lines 883–884why, one step back883 // Ensure `obj` is not revoked.884 obj.c = 1test: recursive produce call in async recipealeclarsonbefore adoptione266f4b
line 885why, one step back885 })test: avoid async/await for node 6aleclarsonbefore adoptione9f274e
lines 886–890why, one step back886 }).then(res => {887 expect(res).not.toBe(base)888 expect(res).toEqual({889 a: {b: {k: 1, c: 1}}890 })test: recursive produce call in async recipealeclarsonbefore adoptione266f4b
lines 891–892why, one step back891 })892 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 893–894why, one step back893 })894chore: improve more testsaleclarsonbefore adoption4f59b12
line 895why, one step back895 it("throws when the draft is modified and another object is returned", () => {Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 896–897why, one step back896 const base = {x: 3}897 expect(() => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 898–901why, one step back898 produce(base, draft => {899 draft.x = 4900 return {x: 5}901 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 902–904why, one step back902 }).toThrow(/An immer producer returned a new value/)903 })904chore: improve more testsaleclarsonbefore adoption4f59b12
line 905why, one step back905 it("should fix #117 - 1", () => {Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 906why, one step back906 const reducer = (state, action) =>test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 907–914why, one step back907 produce(state, draft => {908 switch (action.type) {909 case "SET_STARTING_DOTS":910 return draft.availableStartingDots.map(a => a)911 default:912 break913 }914 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 915–929why, one step back915 const base = {916 availableStartingDots: [917 {dots: 4, count: 1},918 {dots: 3, count: 2},919 {dots: 2, count: 3},920 {dots: 1, count: 4}921 ]922 }923 const next = reducer(base, {type: "SET_STARTING_DOTS"})924 expect(next).toEqual(base.availableStartingDots)925 expect(next).not.toBe(base.availableStartingDots)926 })927928 it("should fix #117 - 2", () => {929 const reducer = (state, action) =>test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 930–939why, one step back930 produce(state, draft => {931 switch (action.type) {932 case "SET_STARTING_DOTS":933 return {934 dots: draft.availableStartingDots.map(a => a)935 }936 default:937 break938 }939 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 940–951why, one step back940 const base = {941 availableStartingDots: [942 {dots: 4, count: 1},943 {dots: 3, count: 2},944 {dots: 2, count: 3},945 {dots: 1, count: 4}946 ]947 }948 const next = reducer(base, {type: "SET_STARTING_DOTS"})949 expect(next).toEqual({dots: base.availableStartingDots})950 })951chore: improve more testsaleclarsonbefore adoption4f59b12
line 952why, one step back952 it("cannot always detect noop assignments - 0", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 953why, one step back953 const baseState = {x: {y: 3}}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 954–957why, one step back954 const nextState = produce(baseState, d => {955 const a = d.x956 d.x = a957 })Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 958–960why, one step back958 expect(nextState).toBe(baseState)959 })960chore: improve more testsaleclarsonbefore adoption4f59b12
line 961why, one step back961 it("cannot always detect noop assignments - 1", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 962why, one step back962 const baseState = {x: {y: 3}}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 963–967why, one step back963 const nextState = produce(baseState, d => {964 const a = d.x965 d.x = 4966 d.x = a967 })Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 968–973why, one step back968 // Ideally, this should actually be the same instances969 // but this would be pretty expensive to detect,970 // so we don't atm971 expect(nextState).not.toBe(baseState)972 })973chore: improve more testsaleclarsonbefore adoption4f59b12
line 974why, one step back974 it("cannot always detect noop assignments - 2", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 975why, one step back975 const baseState = {x: {y: 3}}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 976–981why, one step back976 const nextState = produce(baseState, d => {977 const a = d.x978 const stuff = a.y + 3979 d.x = 4980 d.x = a981 })Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 982–987why, one step back982 // Ideally, this should actually be the same instances983 // but this would be pretty expensive to detect,984 // so we don't atm985 expect(nextState).not.toBe(baseState)986 })987chore: improve more testsaleclarsonbefore adoption4f59b12
line 988why, one step back988 it("cannot always detect noop assignments - 3", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 989why, one step back989 const baseState = {x: 3}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 990–992why, one step back990 const nextState = produce(baseState, d => {991 d.x = 3992 })Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 993–995why, one step back993 expect(nextState).toBe(baseState)994 })995chore: improve more testsaleclarsonbefore adoption4f59b12
line 996why, one step back996 it("cannot always detect noop assignments - 4", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 997why, one step back997 const baseState = {x: 3}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 998–1001why, one step back998 const nextState = produce(baseState, d => {999 d.x = 41000 d.x = 31001 })Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 1002–1005why, one step back1002 // Ideally, this should actually be the same instances1003 // but this would be pretty expensive to detect,1004 // so we don't atm1005 expect(nextState).not.toBe(baseState)Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
lines 1006–1007why, one step back1006 })1007chore: improve more testsaleclarsonbefore adoption4f59b12
line 1008why, one step back1008 it("cannot produce undefined by returning undefined", () => {Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
lines 1009–1022why, one step back1009 const base = 31010 expect(produce(base, () => 4)).toBe(4)1011 expect(produce(base, () => null)).toBe(null)1012 expect(produce(base, () => undefined)).toBe(3)1013 expect(produce(base, () => {})).toBe(3)1014 expect(produce(base, () => nothing)).toBe(undefined)10151016 expect(produce({}, () => undefined)).toEqual({})1017 expect(produce({}, () => nothing)).toBe(undefined)1018 expect(produce(3, () => nothing)).toBe(undefined)10191020 expect(produce(() => undefined)({})).toEqual({})1021 expect(produce(() => nothing)({})).toBe(undefined)1022 expect(produce(() => nothing)(3)).toBe(undefined)Tried to reproduce #195Michel Weststratebefore adoption4f27a93
lines 1023–1024why, one step back1023 })1024chore: improve more testsaleclarsonbefore adoption4f59b12
lines 1025–1028why, one step back1025 describe("base state type", () => {1026 testObjectTypes(produce)1027 testLiteralTypes(produce)1028 })Merge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417
line 1029why, one step back1029Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 1030–1034why, one step back1030 afterEach(() => {1031 expect(baseState).toBe(origBaseState)1032 expect(baseState).toEqual(createBaseState())1033 })1034chore: improve more testsaleclarsonbefore adoption4f59b12
line 1035why, one step back1035 class Foo {}Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 1036–1037why, one step back1036 function createBaseState() {1037 const data = {chore: improve more testsaleclarsonbefore adoption4f59b12
line 1038why, one step back1038 anInstance: new Foo(),Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 1039–1047why, one step back1039 anArray: [3, 2, {c: 3}, 1],1040 aProp: "hi",1041 anObject: {1042 nested: {1043 yummie: true1044 },1045 coffee: false1046 }1047 }test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 1048why, one step back1048 return autoFreeze ? deepFreeze(data) : dataMerge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 1049why, one step back1049 }Merge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417
lines 1050–1090why, one step back1050 })10511052 describe(`isDraft - ${name}`, () => {1053 it("returns true for object drafts", () => {1054 produce({}, state => {1055 expect(isDraft(state)).toBeTruthy()1056 })1057 })1058 it("returns true for array drafts", () => {1059 produce([], state => {1060 expect(isDraft(state)).toBeTruthy()1061 })1062 })1063 it("returns true for objects nested in object drafts", () => {1064 produce({a: {b: {}}}, state => {1065 expect(isDraft(state.a)).toBeTruthy()1066 expect(isDraft(state.a.b)).toBeTruthy()1067 })1068 })1069 it("returns false for new objects added to a draft", () => {1070 produce({}, state => {1071 state.a = {}1072 expect(isDraft(state.a)).toBeFalsy()1073 })1074 })1075 it("returns false for objects returned by the producer", () => {1076 const object = produce(null, Object.create)1077 expect(isDraft(object)).toBeFalsy()1078 })1079 it("returns false for arrays returned by the producer", () => {1080 const array = produce(null, _ => [])1081 expect(isDraft(array)).toBeFalsy()1082 })1083 it("returns false for object drafts returned by the producer", () => {1084 const object = produce({}, state => state)1085 expect(isDraft(object)).toBeFalsy()1086 })1087 it("returns false for array drafts returned by the producer", () => {1088 const array = produce([], state => state)1089 expect(isDraft(array)).toBeFalsy()1090 })Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 1091why, one step back1091 })test: extract some tests into their own functionaleclarsonbefore adoption21846ab
lines 1092–1093why, one step back1092}1093chore: improve more testsaleclarsonbefore adoption4f59b12
line 1094why, one step back1094function testObjectTypes(produce) {feat: limited class supportaleclarsonbefore adoption9877d64
lines 1095–1100why, one step back1095 class Foo {1096 constructor(foo) {1097 this.foo = foo1098 this[immerable] = true1099 }1100 }chore: improve more testsaleclarsonbefore adoption4f59b12
lines 1101–1105why, one step back1101 const values = {1102 "empty object": {},1103 "plain object": {a: 1, b: 2},1104 "object (no prototype)": Object.create(null),1105 "empty array": [],feat: limited class supportaleclarsonbefore adoption9877d64
lines 1106–1107why, one step back1106 "plain array": [1, 2],1107 "class instance (draftable)": new Foo(1)chore: improve more testsaleclarsonbefore adoption4f59b12
lines 1108–1148why, one step back1108 }1109 for (const name in values) {1110 const value = values[name]1111 const copy = shallowCopy(value)1112 testObjectType(name, value)1113 testObjectType(name + " (frozen)", Object.freeze(copy))1114 }1115 function testObjectType(name, base) {1116 describe(name, () => {1117 it("creates a draft", () => {1118 produce(base, draft => {1119 expect(draft).not.toBe(base)1120 expect(shallowCopy(draft, true)).toEqual(base)1121 })1122 })11231124 it("preserves the prototype", () => {1125 const proto = Object.getPrototypeOf(base)1126 produce(base, draft => {1127 expect(Object.getPrototypeOf(draft)).toBe(proto)1128 })1129 })11301131 it("returns the base state when no changes are made", () => {1132 expect(produce(base, () => {})).toBe(base)1133 })11341135 it("returns a copy when changes are made", () => {1136 const random = Math.random()1137 const result = produce(base, draft => {1138 draft[0] = random1139 })1140 expect(result).not.toBe(base)1141 expect(result.constructor).toBe(base.constructor)1142 expect(result[0]).toBe(random)1143 })1144 })1145 }1146}11471148function testLiteralTypes(produce) {test: extract some tests into their own functionaleclarsonbefore adoption21846ab
line 1149why, one step back1149 class Foo {}chore: improve more testsaleclarsonbefore adoption4f59b12
line 1150why, one step back1150 const values = {test: extract some tests into their own functionaleclarsonbefore adoption21846ab
lines 1151–1153why, one step back1151 "falsy number": 0,1152 "truthy number": 1,1153 "negative number": -1,chore: improve more testsaleclarsonbefore adoption4f59b12
line 1154why, one step back1154 NaN: NaN,test: extract some tests into their own functionaleclarsonbefore adoption21846ab
lines 1155–1163why, one step back1155 infinity: 1 / 0,1156 true: true,1157 false: false,1158 "empty string": "",1159 "truthy string": "1",1160 null: null,1161 undefined: undefined,11621163 /**chore: improve more testsaleclarsonbefore adoption4f59b12
lines 1164–1165why, one step back1164 * These objects are treated as literals because Immer1165 * does not know how to draft them.test: extract some tests into their own functionaleclarsonbefore adoption21846ab
line 1166why, one step back1166 */chore: improve more testsaleclarsonbefore adoption4f59b12
line 1167why, one step back1167 function: () => {},test: extract some tests into their own functionaleclarsonbefore adoption21846ab
lines 1168–1172why, one step back1168 "regexp object": /.+/g,1169 "boxed number": new Number(0),1170 "boxed string": new String(""),1171 "boxed boolean": new Boolean(),1172 "date object": new Date(),feat: limited class supportaleclarsonbefore adoption9877d64
line 1173why, one step back1173 "class instance (not draftable)": new Foo()test: extract some tests into their own functionaleclarsonbefore adoption21846ab
line 1174why, one step back1174 }chore: improve more testsaleclarsonbefore adoption4f59b12
lines 1175–1178why, one step back1175 for (const name in values) {1176 describe(name, () => {1177 const value = values[name]1178test: extract some tests into their own functionaleclarsonbefore adoption21846ab
lines 1179–1183why, one step back1179 it("does not create a draft", () => {1180 produce(value, draft => {1181 expect(draft).toBe(value)1182 })1183 })chore: improve more testsaleclarsonbefore adoption4f59b12
lines 1184–1185why, one step back11841185 it("returns the base state when no changes are made", () => {test: extract some tests into their own functionaleclarsonbefore adoption21846ab
lines 1186–1187why, one step back1186 expect(produce(value, () => {})).toBe(value)1187 })chore: improve more testsaleclarsonbefore adoption4f59b12
line 1188why, one step back1188test: extract some tests into their own functionaleclarsonbefore adoption21846ab
line 1189why, one step back1189 if (value && typeof value == "object") {chore: improve more testsaleclarsonbefore adoption4f59b12
line 1190why, one step back1190 it("does not return a copy when changes are made", () => {test: extract some tests into their own functionaleclarsonbefore adoption21846ab
lines 1191–1199why, one step back1191 expect(1192 produce(value, draft => {1193 draft.foo = true1194 })1195 ).toBe(value)1196 })1197 }1198 })1199 }Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
lines 1200–1202why, one step back1200}12011202function enumerableOnly(x) {feat: limited class supportaleclarsonbefore adoption9877d64
lines 1203–1204why, one step back1203 const copy = Array.isArray(x) ? x.slice() : Object.assign({}, x)1204 each(copy, (prop, value) => {test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44
line 1205why, one step back1205 if (value && typeof value === "object") {feat: limited class supportaleclarsonbefore adoption9877d64
line 1206why, one step back1206 copy[prop] = enumerableOnly(value)test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44
line 1207why, one step back1207 }feat: limited class supportaleclarsonbefore adoption9877d64
line 1208why, one step back1208 })test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44
line 1209why, one step back1209 return copyMerge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f
line 1210why, one step back1210}