base.js
This file
Path
__tests__/base.js
1,274 lines · 44.8 KiB · JavaScript
Last changed by
refactor(ts): simplify IProduce type testsAlec Larsonbefore adoptionbce282d
History of this fileTake it
The walk stopped after 200 commits: lines older than 0d72d13 point at it.
Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
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 #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
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 #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 14–20why, one step back14runBaseTest("proxy (no freeze)", true, false)15runBaseTest("proxy (autofreeze)", true, true)16runBaseTest("proxy (autofreeze)(patch listener)", true, true, true)17runBaseTest("es5 (no freeze)", false, false)18runBaseTest("es5 (autofreeze)", false, true)19runBaseTest("es5 (autofreeze)(patch listener)", false, true, true)20test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 21why, one step back21function runBaseTest(name, useProxies, autoFreeze, useListener) {test: patch the `produce` function for interweaved Immer instances tooaleclarsonbefore adoption31bbe64
lines 22–23why, one step back22 const listener = useListener ? function() {} : undefined23 const {produce} = createPatchedImmer({test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 24–27why, one step back24 useProxies,25 autoFreeze26 })27test: patch the `produce` function for interweaved Immer instances tooaleclarsonbefore adoption31bbe64
lines 28–31why, one step back28 // When `useListener` is true, append a function to the arguments of every29 // uncurried `produce` call in every test. This makes tests easier to read.30 function createPatchedImmer(options) {31 const immer = new Immer(options)test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 32why, one step back32test: patch the `produce` function for interweaved Immer instances tooaleclarsonbefore adoption31bbe64
lines 33–41why, one step back33 const {produce} = immer34 immer.produce = (...args) =>35 typeof args[1] === "function" && args.length < 336 ? produce(...args, listener)37 : produce(...args)3839 return immer40 }41Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
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 #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
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 #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
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 #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 65–68why, one step back65 expect(nextState.nested).toBe(baseState.nested)66 })6768 it("deep change bubbles up", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 69–71why, one step back69 const nextState = produce(baseState, s => {70 s.anObject.nested.yummie = false71 })Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 72–79why, one step back72 expect(nextState).not.toBe(baseState)73 expect(nextState.anObject).not.toBe(baseState.anObject)74 expect(baseState.anObject.nested.yummie).toBe(true)75 expect(nextState.anObject.nested.yummie).toBe(false)76 expect(nextState.anArray).toBe(baseState.anArray)77 })7879 it("can add props", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 80–82why, one step back80 const nextState = produce(baseState, s => {81 s.anObject.cookie = {tasty: true}82 })Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
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 #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 93–97why, one step back93 expect(nextState).not.toBe(baseState)94 expect(nextState.anObject).not.toBe(baseState.anObject)95 expect(nextState.anObject.nested).toBe(undefined)96 })97test: deleting props added in the produceraleclarsonbefore adoption691e73b
lines 98–99why, one step back98 // Found by: https://github.com/mweststrate/immer/pull/26799 it("can delete props added in the producer", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 100–103why, one step back100 const nextState = produce(baseState, s => {101 s.anObject.test = true102 delete s.anObject.test103 })test: deleting props added in the produceraleclarsonbefore adoption691e73b
lines 104–112why, one step back104 if (useProxies) {105 expect(nextState).not.toBe(baseState)106 expect(nextState).toEqual(baseState)107 } else {108 // The copy is avoided in ES5.109 expect(nextState).toBe(baseState)110 }111 })112test: delete then set an existing propertyAlec Larsonbefore adoption5121d36
lines 113–138why, one step back113 // Found by: https://github.com/mweststrate/immer/issues/328114 it("can set a property that was just deleted", () => {115 const baseState = {a: 1}116 const nextState = produce(baseState, s => {117 delete s.a118 s.a = 2119 })120 expect(nextState.a).toBe(2)121 })122123 it("can set a property to its original value after deleting it", () => {124 const baseState = {a: {b: 1}}125 const nextState = produce(baseState, s => {126 const a = s.a127 delete s.a128 s.a = a129 })130 if (useProxies) {131 expect(nextState).not.toBe(baseState)132 expect(nextState).toEqual(baseState)133 } else {134 // The copy is avoided in ES5.135 expect(nextState).toBe(baseState)136 }137 })138chore: improve more testsaleclarsonbefore adoption4f59b12
lines 139–141why, one step back139 it("can get property descriptors", () => {140 const getDescriptor = Object.getOwnPropertyDescriptor141 const baseState = deepFreeze([{a: 1}])test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
lines 142–148why, one step back142 produce(baseState, arr => {143 const obj = arr[0]144 const desc = {145 configurable: true,146 enumerable: true,147 ...(useProxies && {writable: true})148 }Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
line 149why, one step back149chore: improve more testsaleclarsonbefore adoption4f59b12
line 150why, one step back150 // Known propertytest: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
lines 151–152why, one step back151 expect(getDescriptor(obj, "a")).toMatchObject(desc)152 expect(getDescriptor(arr, 0)).toMatchObject(desc)chore: improve more testsaleclarsonbefore adoption4f59b12
lines 153–155why, one step back153154 // Deleted property155 delete obj.atest: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
line 156why, one step back156 arr.pop()chore: improve more testsaleclarsonbefore adoption4f59b12
line 157why, one step back157 expect(getDescriptor(obj, "a")).toBeUndefined()test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
line 158why, one step back158 expect(getDescriptor(arr, 0)).toBeUndefined()chore: improve more testsaleclarsonbefore adoption4f59b12
lines 159–161why, one step back159160 // Unknown property161 expect(getDescriptor(obj, "b")).toBeUndefined()test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
line 162why, one step back162 expect(getDescriptor(arr, 100)).toBeUndefined()chore: improve more testsaleclarsonbefore adoption4f59b12
lines 163–165why, one step back163164 // Added property165 obj.b = 2test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
line 166why, one step back166 arr[100] = 1chore: improve more testsaleclarsonbefore adoption4f59b12
line 167why, one step back167 expect(getDescriptor(obj, "b")).toBeDefined()test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
line 168why, one step back168 expect(getDescriptor(arr, 100)).toBeDefined()test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 169why, one step back169 })Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 170–171why, one step back170 })171chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 172–178why, one step back172 describe("array drafts", () => {173 it("supports Array.isArray()", () => {174 const nextState = produce(baseState, s => {175 expect(Array.isArray(s.anArray)).toBeTruthy()176 s.anArray.push(1)177 })178 expect(Array.isArray(nextState.anArray)).toBeTruthy()Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 179–180why, one step back179 })180chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 181–186why, one step back181 it("supports index access", () => {182 const value = baseState.anArray[0]183 const nextState = produce(baseState, s => {184 expect(s.anArray[0]).toBe(value)185 })186 expect(nextState).toBe(baseState)test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 187why, one step back187 })chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 188–204why, one step back188189 it("supports iteration", () => {190 const base = [{id: 1, a: 1}, {id: 2, a: 1}]191 const findById = (collection, id) => {192 for (const item of collection) {193 if (item.id === id) return item194 }195 return null196 }197 const result = produce(base, draft => {198 const obj1 = findById(draft, 1)199 const obj2 = findById(draft, 2)200 obj1.a = 2201 obj2.a = 2202 })203 expect(result[0].a).toEqual(2)204 expect(result[1].a).toEqual(2)Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 205–206why, one step back205 })206chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 207–213why, one step back207 it("can assign an index via bracket notation", () => {208 const nextState = produce(baseState, s => {209 s.anArray[3] = true210 })211 expect(nextState).not.toBe(baseState)212 expect(nextState.anArray).not.toBe(baseState.anArray)213 expect(nextState.anArray[3]).toEqual(true)test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 214why, one step back214 })Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
line 215why, one step back215chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 216–222why, one step back216 it("can use splice() to both add and remove items", () => {217 const nextState = produce(baseState, s => {218 s.anArray.splice(1, 1, "a", "b")219 })220 expect(nextState.anArray).not.toBe(baseState.anArray)221 expect(nextState.anArray[1]).toBe("a")222 expect(nextState.anArray[2]).toBe("b")test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 223why, one step back223 })Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
line 224why, one step back224chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 225–231why, one step back225 it("can truncate via the length property", () => {226 const baseLength = baseState.anArray.length227 const nextState = produce(baseState, s => {228 s.anArray.length = baseLength - 1229 })230 expect(nextState.anArray).not.toBe(baseState.anArray)231 expect(nextState.anArray.length).toBe(baseLength - 1)test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 232why, one step back232 })Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
line 233why, one step back233chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 234–241why, one step back234 it("can extend via the length property", () => {235 const baseLength = baseState.anArray.length236 const nextState = produce(baseState, s => {237 s.anArray.length = baseLength + 1238 })239 expect(nextState.anArray).not.toBe(baseState.anArray)240 expect(nextState.anArray.length).toBe(baseLength + 1)241 })Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
line 242why, one step back242chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 243–249why, one step back243 // Reported here: https://github.com/mweststrate/immer/issues/116244 it("can pop then push", () => {245 const nextState = produce([1, 2, 3], s => {246 s.pop()247 s.push(100)248 })249 expect(nextState).toEqual([1, 2, 100])test: rename and move a testaleclarsonbefore adoption8e0325c
lines 250–251why, one step back250 })251chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 252–258why, one step back252 it("can be sorted", () => {253 const baseState = [3, 1, 2]254 const nextState = produce(baseState, s => {255 s.sort()256 })257 expect(nextState).not.toBe(baseState)258 expect(nextState).toEqual([1, 2, 3])test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 259why, one step back259 })Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
line 260why, one step back260chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 261–269why, one step back261 it("supports modifying nested objects", () => {262 const baseState = [{a: 1}, {}]263 const nextState = produce(baseState, s => {264 s[0].a++265 s[1].a = 0266 })267 expect(nextState).not.toBe(baseState)268 expect(nextState[0].a).toBe(2)269 expect(nextState[1].a).toBe(0)test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 270why, one step back270 })Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
line 271why, one step back271chore: improve more testsaleclarsonbefore adoption4f59b12
lines 272–276why, one step back272 it("never preserves non-numeric properties", () => {273 const baseState = []274 baseState.x = 7275 const nextState = produce(baseState, s => {276 s.push(3)test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 277why, one step back277 })chore: improve more testsaleclarsonbefore adoption4f59b12
line 278why, one step back278 expect("x" in nextState).toBeFalsy()test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 279why, one step back279 })chore: improve more testsaleclarsonbefore adoption4f59b12
lines 280–286why, one step back280281 if (useProxies) {282 it("throws when a non-numeric property is added", () => {283 expect(() => {284 produce([], d => {285 d.x = 3286 })test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91
line 287why, one step back287 }).toThrowErrorMatchingSnapshot()chore: improve more testsaleclarsonbefore adoption4f59b12
lines 288–296why, one step back288 })289290 it("throws when a non-numeric property is deleted", () => {291 expect(() => {292 const baseState = []293 baseState.x = 7294 produce(baseState, d => {295 delete d.x296 })test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91
line 297why, one step back297 }).toThrowErrorMatchingSnapshot()chore: improve more testsaleclarsonbefore adoption4f59b12
lines 298–299why, one step back298 })299 }Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 300–301why, one step back300 })301feat: limited class supportaleclarsonbefore adoption9877d64
lines 302–328why, one step back302 it("supports `immerable` symbol on constructor", () => {303 class One {}304 One[immerable] = true305 const baseState = new One()306 const nextState = produce(baseState, draft => {307 expect(draft).not.toBe(baseState)308 draft.foo = true309 })310 expect(nextState).not.toBe(baseState)311 expect(nextState.foo).toBeTruthy()312 })313314 it("preserves symbol properties", () => {315 const test = Symbol("test")316 const baseState = {[test]: true}317 const nextState = produce(baseState, s => {318 expect(s[test]).toBeTruthy()319 s.foo = true320 })321 expect(nextState).toEqual({322 [test]: true,323 foo: true324 })325 })326327 it("preserves non-enumerable properties", () => {328 const baseState = {}test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
line 329why, one step back329 // Non-enumerable object propertyfeat: limited class supportaleclarsonbefore adoption9877d64
line 330why, one step back330 Object.defineProperty(baseState, "foo", {test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
line 331why, one step back331 value: {a: 1},feat: limited class supportaleclarsonbefore adoption9877d64
lines 332–333why, one step back332 enumerable: false333 })test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
lines 334–338why, one step back334 // Non-enumerable primitive property335 Object.defineProperty(baseState, "bar", {336 value: 1,337 enumerable: false338 })feat: limited class supportaleclarsonbefore adoption9877d64
lines 339–341why, one step back339 const nextState = produce(baseState, s => {340 expect(s.foo).toBeTruthy()341 expect(isEnumerable(s, "foo")).toBeFalsy()test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5
lines 342–345why, one step back342 s.bar++343 expect(isEnumerable(s, "foo")).toBeFalsy()344 s.foo.a++345 expect(isEnumerable(s, "foo")).toBeFalsy()feat: limited class supportaleclarsonbefore adoption9877d64
lines 346–363why, one step back346 })347 expect(nextState.foo).toBeTruthy()348 expect(isEnumerable(nextState, "foo")).toBeFalsy()349 })350351 it("throws on computed properties", () => {352 const baseState = {}353 Object.defineProperty(baseState, "foo", {354 get: () => {},355 enumerable: true356 })357 expect(() => {358 produce(baseState, s => {359 // Proxies only throw once a change is made.360 if (useProxies) {361 s.modified = true362 }363 })test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91
line 364why, one step back364 }).toThrowErrorMatchingSnapshot()feat: limited class supportaleclarsonbefore adoption9877d64
lines 365–385why, one step back365 })366367 it("allows inherited computed properties", () => {368 const proto = {}369 Object.defineProperty(proto, "foo", {370 get() {371 return this.bar372 },373 set(val) {374 this.bar = val375 }376 })377 const baseState = Object.create(proto)378 produce(baseState, s => {379 expect(s.bar).toBeUndefined()380 s.foo = {}381 expect(s.bar).toBeDefined()382 expect(s.foo).toBe(s.bar)383 })384 })385test: improve coveragealeclarsonbefore adoption14d7e3c
lines 386–407why, one step back386 it("supports a base state with multiple references to an object", () => {387 const obj = {}388 const res = produce({a: obj, b: obj}, d => {389 // Two drafts are created for each occurrence of an object in the base state.390 expect(d.a).not.toBe(d.b)391 d.a.z = true392 expect(d.b.z).toBeUndefined()393 })394 expect(res.b).toBe(obj)395 expect(res.a).not.toBe(res.b)396 expect(res.a.z).toBeTruthy()397 })398399 // NOTE: Except the root draft.400 it("supports multiple references to any modified draft", () => {401 const next = produce({a: {b: 1}}, d => {402 d.a.b++403 d.b = d.a404 })405 expect(next.a).toBe(next.b)406 })407test: renaming propertiesaleclarsonbefore adoptiond83f4b5
line 408why, one step back408 it("can rename nested objects (no changes)", () => {chore: improve more testsaleclarsonbefore adoption4f59b12
lines 409–411why, one step back409 const nextState = produce({obj: {}}, s => {410 s.foo = s.obj411 delete s.objtest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 412why, one step back412 })chore: improve more testsaleclarsonbefore adoption4f59b12
line 413why, one step back413 expect(nextState).toEqual({foo: {}})Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 414–415why, one step back414 })415test: renaming propertiesaleclarsonbefore adoptiond83f4b5
lines 416–418why, one step back416 // Very similar to the test before, but the reused object has one417 // property changed, one added, and one removed.418 it("can rename nested objects (with changes)", () => {chore: improve more testsaleclarsonbefore adoption4f59b12
lines 419–422why, one step back419 const nextState = produce({obj: {a: 1, b: 1}}, s => {420 s.obj.a = true // change421 delete s.obj.b // delete422 s.obj.c = true // addtest: renaming propertiesaleclarsonbefore adoptiond83f4b5
line 423why, one step back423chore: improve more testsaleclarsonbefore adoption4f59b12
lines 424–425why, one step back424 s.foo = s.obj425 delete s.objtest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 426why, one step back426 })chore: improve more testsaleclarsonbefore adoption4f59b12
line 427why, one step back427 expect(nextState).toEqual({foo: {a: true, c: true}})Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 428–429why, one step back428 })429test: draft nested in new objectaleclarsonbefore adoptionf46659b
line 430why, one step back430 it("can nest a draft in a new object (no changes)", () => {chore: improve more testsaleclarsonbefore adoption4f59b12
line 431why, one step back431 const baseState = {obj: {}}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 432why, one step back432 const nextState = produce(baseState, s => {chore: improve more testsaleclarsonbefore adoption4f59b12
lines 433–434why, one step back433 s.foo = {bar: s.obj}434 delete s.objtest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 435why, one step back435 })chore: improve more testsaleclarsonbefore adoption4f59b12
line 436why, one step back436 expect(nextState.foo.bar).toBe(baseState.obj)test: draft nested in new objectaleclarsonbefore adoptionf46659b
lines 437–438why, one step back437 })438chore: improve more testsaleclarsonbefore adoption4f59b12
lines 439–443why, one step back439 it("can nest a modified draft in a new object", () => {440 const nextState = produce({obj: {a: 1, b: 1}}, s => {441 s.obj.a = true // change442 delete s.obj.b // delete443 s.obj.c = true // addtest: draft nested in new objectaleclarsonbefore adoptionf46659b
line 444why, one step back444chore: improve more testsaleclarsonbefore adoption4f59b12
lines 445–446why, one step back445 s.foo = {bar: s.obj}446 delete s.objtest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 447why, one step back447 })chore: improve more testsaleclarsonbefore adoption4f59b12
line 448why, one step back448 expect(nextState).toEqual({foo: {bar: {a: true, c: true}}})test: draft nested in new objectaleclarsonbefore adoptionf46659b
lines 449–450why, one step back449 })450chore: improve some testsaleclarsonbefore adoptionefd47f6
line 451why, one step back451 it("supports assigning undefined to an existing property", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 452–454why, one step back452 const nextState = produce(baseState, s => {453 s.aProp = undefined454 })Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
line 455why, one step back455 expect(nextState).not.toBe(baseState)chore: improve some testsaleclarsonbefore adoptionefd47f6
lines 456–464why, one step back456 expect(nextState.aProp).toBe(undefined)457 })458459 it("supports assigning undefined to a new property", () => {460 const baseState = {}461 const nextState = produce(baseState, s => {462 s.aProp = undefined463 })464 expect(nextState).not.toBe(baseState)Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 465–467why, one step back465 expect(nextState.aProp).toBe(undefined)466 })467test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad
lines 468–470why, one step back468 // NOTE: ES5 drafts only protect existing properties when revoked.469 it("revokes the draft once produce returns", () => {470 const expectRevoked = (fn, shouldThrow = true) => {test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91
line 471why, one step back471 if (shouldThrow) expect(fn).toThrowErrorMatchingSnapshot()test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad
lines 472–473why, one step back472 else expect(fn).not.toThrow()473 }Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
line 474why, one step back474test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad
lines 475–480why, one step back475 // Test object drafts:476 let draft477 produce({a: 1, b: 1}, s => {478 draft = s479 delete s.b480 })Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
line 481why, one step back481test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad
lines 482–484why, one step back482 // Access known property on object draft.483 expectRevoked(() => {484 draft.aMerge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 485–486why, one step back485 })486test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad
lines 487–489why, one step back487 // Assign known property on object draft.488 expectRevoked(() => {489 draft.a = truetest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 490why, one step back490 })test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad
lines 491–526why, one step back491492 // Access unknown property on object draft.493 expectRevoked(() => {494 draft.z495 }, useProxies)496497 // Assign unknown property on object draft.498 expectRevoked(() => {499 draft.z = true500 }, useProxies)501502 // Test array drafts:503 produce([1, 2], s => {504 draft = s505 s.pop()506 })507508 // Access known index of an array draft.509 expectRevoked(() => {510 draft[0]511 })512513 // Assign known index of an array draft.514 expectRevoked(() => {515 draft[0] = true516 })517518 // Access unknown index of an array draft.519 expectRevoked(() => {520 draft[1]521 }, useProxies)522523 // Assign unknown index of an array draft.524 expectRevoked(() => {525 draft[1] = true526 }, useProxies)Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 527–528why, one step back527 })528test: add missing edge casealeclarsonbefore adoptionc46af36
lines 529–536why, one step back529 it("can access a child draft that was created before the draft was modified", () => {530 produce({a: {}}, s => {531 const before = s.a532 s.b = 1533 expect(s.a).toBe(before)534 })535 })536Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
line 537why, one step back537 it("should reflect all changes made in the draft immediately", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 538–540why, one step back538 produce(baseState, draft => {539 draft.anArray[0] = 5540 draft.anArray.unshift("test")test: tiny improvementaleclarsonbefore adoptionee49da3
line 541why, one step back541 expect(enumerableOnly(draft.anArray)).toEqual([test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 542–550why, one step back542 "test",543 5,544 2,545 {c: 3},546 1547 ])548 draft.stuffz = "coffee"549 expect(draft.stuffz).toBe("coffee")550 })Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 551–552why, one step back551 })552chore: improve more testsaleclarsonbefore adoption4f59b12
lines 553–554why, one step back553 if (useProxies)554 it("throws when Object.defineProperty() is used on drafts", () => {Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
line 555why, one step back555 expect(() => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 556–562why, one step back556 produce({}, draft => {557 Object.defineProperty(draft, "xx", {558 enumerable: true,559 writeable: true,560 value: 2561 })562 })test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91
line 563why, one step back563 }).toThrowErrorMatchingSnapshot()Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 564–566why, one step back564 })565566 it("should handle constructor correctly", () => {chore: improve more testsaleclarsonbefore adoption4f59b12
line 567why, one step back567 const baseState = {Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 568–570why, one step back568 arr: new Array(),569 obj: new Object()570 }chore: improve more testsaleclarsonbefore adoption4f59b12
line 571why, one step back571 const result = produce(baseState, draft => {Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 572–579why, one step back572 draft.arrConstructed = draft.arr.constructor(1)573 draft.objConstructed = draft.obj.constructor(1)574 })575 expect(result.arrConstructed).toEqual(new Array().constructor(1))576 expect(result.objConstructed).toEqual(new Object().constructor(1))577 })578579 it("should handle equality correctly - 1", () => {chore: improve more testsaleclarsonbefore adoption4f59b12
line 580why, one step back580 const baseState = {Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 581–583why, one step back581 y: 3 / 0,582 z: NaN583 }chore: improve more testsaleclarsonbefore adoption4f59b12
line 584why, one step back584 const nextState = produce(baseState, draft => {Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 585–587why, one step back585 draft.y = 4 / 0586 draft.z = NaN587 })chore: improve more testsaleclarsonbefore adoption4f59b12
line 588why, one step back588 expect(nextState).toBe(baseState)Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 589–591why, one step back589 })590591 it("should handle equality correctly - 2", () => {chore: improve more testsaleclarsonbefore adoption4f59b12
line 592why, one step back592 const baseState = {Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 593–594why, one step back593 x: -0594 }chore: improve more testsaleclarsonbefore adoption4f59b12
lines 595–596why, one step back595 const nextState = produce(baseState, draft => {596 draft.x = +0Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
line 597why, one step back597 })chore: improve more testsaleclarsonbefore adoption4f59b12
lines 598–599why, one step back598 expect(nextState).not.toBe(baseState)599 expect(nextState).not.toEqual(baseState)Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 600–601why, one step back600 })601test: base state that contains a draftaleclarsonbefore adoptiond9b91d5
line 602why, one step back602 // AKA: recursive produce callschore: improve more testsaleclarsonbefore adoption4f59b12
line 603why, one step back603 describe("nested producers", () => {test: base state that contains a draftaleclarsonbefore adoptiond9b91d5
line 604why, one step back604 describe("when base state is not a draft", () => {Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371
lines 605–607why, one step back605 // This test ensures the global state used to manage proxies is606 // never left in a corrupted state by a nested `produce` call.607 it("never affects its parent producer implicitly", () => {test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736
lines 608–612why, one step back608 const base = {obj: {a: 1}}609 const next = produce(base, draft => {610 // Notice how `base.obj` is passed, not `draft.obj`611 const obj2 = produce(base.obj, draft2 => {612 draft2.a = 0test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 613why, one step back613 })test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736
lines 614–615why, one step back614 expect(obj2.a).toBe(0)615 expect(draft.obj.a).toBe(1) // effects should not be visible outsidetest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 616why, one step back616 })test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736
line 617why, one step back617 expect(next).toBe(base)Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371
lines 618–620why, one step back618 })619 })620test: base state that contains a draftaleclarsonbefore adoptiond9b91d5
line 621why, one step back621 describe("when base state is a draft", () => {test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736
lines 622–627why, one step back622 it("always wraps the draft in a new draft", () => {623 produce({}, parent => {624 produce(parent, child => {625 expect(child).not.toBe(parent)626 expect(isDraft(child)).toBeTruthy()627 expect(original(child)).toBe(parent)test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 628–629why, one step back628 })629 })test: base state that contains a draftaleclarsonbefore adoptiond9b91d5
line 630why, one step back630 })test: more recursive produce testsAlec Larsonbefore adoption8adfb31
lines 631–656why, one step back631632 // Reported by: https://github.com/mweststrate/immer/issues/343633 it("ensures each property is drafted", () => {634 produce({a: {}, b: {}}, parent => {635 parent.a // Access "a" but not "b"636 produce(parent, child => {637 child.c = 1638 expect(isDraft(child.a)).toBeTruthy()639 expect(isDraft(child.b)).toBeTruthy()640 })641 })642 })643644 it("preserves any pending changes", () => {645 produce({a: 1, b: 1, d: 1}, parent => {646 parent.b = 2647 parent.c = 2648 delete parent.d649 produce(parent, child => {650 expect(child.a).toBe(1) // unchanged651 expect(child.b).toBe(2) // changed652 expect(child.c).toBe(2) // added653 expect(child.d).toBeUndefined() // deleted654 })655 })656 })test: base state that contains a draftaleclarsonbefore adoptiond9b91d5
lines 657–664why, one step back657 })658659 describe("when base state contains a draft", () => {660 it("wraps unowned draft with its own draft", () => {661 produce({a: {}}, parent => {662 produce({a: parent.a}, child => {663 expect(child.a).not.toBe(parent.a)664 expect(isDraft(child.a)).toBeTruthy()test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736
line 665why, one step back665 expect(original(child.a)).toBe(parent.a)test: base state that contains a draftaleclarsonbefore adoptiond9b91d5
lines 666–696why, one step back666 })667 })668 })669670 it("returns unowned draft if no changes were made", () => {671 produce({a: {}}, parent => {672 const result = produce({a: parent.a}, () => {})673 expect(result.a).toBe(parent.a)674 })675 })676677 it("clones the unowned draft when changes are made", () => {678 produce({a: {}}, parent => {679 const result = produce({a: parent.a}, child => {680 child.a.b = 1681 })682 expect(result.a).not.toBe(parent.a)683 expect(result.a.b).toBe(1)684 expect("b" in parent.a).toBeFalsy()685 })686 })687688 // We cannot auto-freeze the result of a nested producer,689 // because it may contain a draft from a parent producer.690 it("never auto-freezes the result", () => {691 produce({a: {}}, parent => {692 const r = produce({a: parent.a}, child => {693 child.b = 1 // Ensure a copy is returned.694 })695 expect(Object.isFrozen(r)).toBeFalsy()696 })Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371
lines 697–698why, one step back697 })698 })test: returning an upvalue draft from a nested produceraleclarsonbefore adoptionb053637
lines 699–716why, one step back699700 // "Upvalues" are variables from a parent scope.701 it("does not finalize upvalue drafts", () => {702 produce({a: {}, b: {}}, parent => {703 expect(produce({}, () => parent)).toBe(parent)704 parent.x // Ensure proxy not revoked.705706 expect(produce({}, () => [parent])[0]).toBe(parent)707 parent.x // Ensure proxy not revoked.708709 expect(produce({}, () => parent.a)).toBe(parent.a)710 parent.a.x // Ensure proxy not revoked.711712 // Modified parent test713 parent.c = 1714 expect(produce({}, () => [parent.b])[0]).toBe(parent.b)715 parent.b.x // Ensure proxy not revoked.716 })test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5
lines 717–720why, one step back717 })718719 it("works with interweaved Immer instances", () => {720 const options = {useProxies, autoFreeze}test: patch the `produce` function for interweaved Immer instances tooaleclarsonbefore adoption31bbe64
lines 721–722why, one step back721 const one = createPatchedImmer(options)722 const two = createPatchedImmer(options)test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5
lines 723–737why, one step back723724 const base = {}725 const result = one.produce(base, s1 =>726 two.produce({s1}, s2 => {727 expect(original(s2.s1)).toBe(s1)728 s2.n = 1729 s2.s1 = one.produce({s2}, s3 => {730 expect(original(s3.s2)).toBe(s2)731 expect(original(s3.s2.s1)).toBe(s2.s1)732 return s3.s2.s1733 })734 })735 )736 expect(result.n).toBe(1)737 expect(result.s1).toBe(base)test: returning an upvalue draft from a nested produceraleclarsonbefore adoptionb053637
line 738why, one step back738 })Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 739–741why, one step back739 })740741 if (useProxies)chore: improve more testsaleclarsonbefore adoption4f59b12
line 742why, one step back742 it("throws when Object.setPrototypeOf() is used on a draft", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 743why, one step back743 produce({}, draft => {test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91
lines 744–746why, one step back744 expect(() =>745 Object.setPrototypeOf(draft, Array)746 ).toThrowErrorMatchingSnapshot()test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 747why, one step back747 })Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 748–749why, one step back748 })749chore: improve more testsaleclarsonbefore adoption4f59b12
lines 750–752why, one step back750 it("supports the 'in' operator", () => {751 produce(baseState, draft => {752 // Known propertytest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 753–754why, one step back753 expect("anArray" in draft).toBe(true)754 expect(Reflect.has(draft, "anArray")).toBe(true)Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
line 755why, one step back755chore: improve more testsaleclarsonbefore adoption4f59b12
line 756why, one step back756 // Unknown propertytest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 757–758why, one step back757 expect("bla" in draft).toBe(false)758 expect(Reflect.has(draft, "bla")).toBe(false)Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
line 759why, one step back759chore: improve more testsaleclarsonbefore adoption4f59b12
line 760why, one step back760 // Known indextest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 761–764why, one step back761 expect(0 in draft.anArray).toBe(true)762 expect("0" in draft.anArray).toBe(true)763 expect(Reflect.has(draft.anArray, 0)).toBe(true)764 expect(Reflect.has(draft.anArray, "0")).toBe(true)Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
line 765why, one step back765chore: improve more testsaleclarsonbefore adoption4f59b12
line 766why, one step back766 // Unknown indextest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 767–771why, one step back767 expect(17 in draft.anArray).toBe(false)768 expect("17" in draft.anArray).toBe(false)769 expect(Reflect.has(draft.anArray, 17)).toBe(false)770 expect(Reflect.has(draft.anArray, "17")).toBe(false)771 })Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 772–773why, one step back772 })773feat: `this` in recipe is no longer the draft, but unboundMichel Weststratebefore adoption3f40298
line 774why, one step back774 it("'this' should not be bound anymore - 1", () => {Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
line 775why, one step back775 const base = {x: 3}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 776why, one step back776 const next1 = produce(base, function() {feat: `this` in recipe is no longer the draft, but unboundMichel Weststratebefore adoption3f40298
line 777why, one step back777 expect(this).toBe(undefined)test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 778why, one step back778 })Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 779–780why, one step back779 })780feat: `this` in recipe is no longer the draft, but unboundMichel Weststratebefore adoption3f40298
line 781why, one step back781 it("'this' should not be bound anymore - 2", () => {Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
line 782why, one step back782 const incrementor = produce(function() {feat: `this` in recipe is no longer the draft, but unboundMichel Weststratebefore adoption3f40298
line 783why, one step back783 expect(this).toBe(undefined)Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
line 784why, one step back784 })feat: `this` in recipe is no longer the draft, but unboundMichel Weststratebefore adoption3f40298
lines 785–787why, one step back785 incrementor()786 })787refactor(ts): simplify IProduce type testsAlec Larsonbefore adoptionbce282d
line 788why, one step back788 it("should be possible to use dynamic bound this", () => {feat: `this` in recipe is no longer the draft, but unboundMichel Weststratebefore adoption3f40298
lines 789–797why, one step back789 const world = {790 counter: {count: 1},791 inc: produce(function(draft) {792 expect(this).toBe(world)793 draft.counter.count = this.counter.count + 1794 })795 }796797 expect(world.inc(world).counter.count).toBe(2)Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 798–799why, one step back798 })799test: rename and expand a testaleclarsonbefore adoption733e16f
line 800why, one step back800 // See here: https://github.com/mweststrate/immer/issues/89chore: improve more testsaleclarsonbefore adoption4f59b12
line 801why, one step back801 it("supports the spread operator", () => {test: rename and expand a testaleclarsonbefore adoption733e16f
lines 802–805why, one step back802 const base = {foo: {x: 0, y: 0}, bar: [0, 0]}803 const result = produce(base, draft => {804 draft.foo = {x: 1, ...draft.foo, y: 1}805 draft.bar = [1, ...draft.bar, 1]test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 806why, one step back806 })test: rename and expand a testaleclarsonbefore adoption733e16f
lines 807–809why, one step back807 expect(result).toEqual({808 foo: {x: 0, y: 1},809 bar: [1, 0, 0, 1]Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 810–814why, one step back810 })811 })812813 it("processes with lodash.set", () => {814 const base = [{id: 1, a: 1}]test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 815–817why, one step back815 const result = produce(base, draft => {816 lodash.set(draft, "[0].a", 2)817 })Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 818–823why, one step back818 expect(base[0].a).toEqual(1)819 expect(result[0].a).toEqual(2)820 })821822 it("processes with lodash.find", () => {823 const base = [{id: 1, a: 1}]test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 824–827why, one step back824 const result = produce(base, draft => {825 const obj1 = lodash.find(draft, {id: 1})826 lodash.set(obj1, "a", 2)827 })Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 828–831why, one step back828 expect(base[0].a).toEqual(1)829 expect(result[0].a).toEqual(2)830 })831chore: improve more testsaleclarsonbefore adoption4f59b12
lines 832–834why, one step back832 describe("recipe functions", () => {833 it("can return a new object", () => {834 const base = {x: 3}test: improve coveragealeclarsonbefore adoption14d7e3c
lines 835–836why, one step back835 const res = produce(base, d => {836 return {x: d.x + 1}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 837why, one step back837 })chore: improve more testsaleclarsonbefore adoption4f59b12
lines 838–839why, one step back838 expect(res).not.toBe(base)839 expect(res).toEqual({x: 4})test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 840why, one step back840 })Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
line 841why, one step back841chore: improve more testsaleclarsonbefore adoption4f59b12
lines 842–843why, one step back842 it("can return the draft", () => {843 const base = {x: 3}test: improve coveragealeclarsonbefore adoption14d7e3c
lines 844–846why, one step back844 const res = produce(base, d => {845 d.x = 4846 return dchore: improve more testsaleclarsonbefore adoption4f59b12
lines 847–849why, one step back847 })848 expect(res).not.toBe(base)849 expect(res).toEqual({x: 4})test: improve coveragealeclarsonbefore adoption14d7e3c
lines 850–851why, one step back850 })851fix(es5): throw if a modified child draft is returnedaleclarsonbefore adoptioneabe9db
line 852why, one step back852 it("can return an unmodified child draft", () => {test: improve coveragealeclarsonbefore adoption14d7e3c
lines 853–857why, one step back853 const base = {a: {}}854 const res = produce(base, d => {855 return d.a856 })857 expect(res).toBe(base.a)fix(es5): throw if a modified child draft is returnedaleclarsonbefore adoptioneabe9db
lines 858–867why, one step back858 })859860 // TODO: Avoid throwing if only the child draft was modified.861 it("cannot return a modified child draft", () => {862 const base = {a: {}}863 expect(() => {864 produce(base, d => {865 d.a.b = 1866 return d.a867 })test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91
line 868why, one step back868 }).toThrowErrorMatchingSnapshot()test: improve coveragealeclarsonbefore adoption14d7e3c
lines 869–883why, one step back869 })870871 it("can return a frozen object", () => {872 const res = deepFreeze([{x: 3}])873 expect(produce({}, () => res)).toBe(res)874 })875876 it("can return an object with two references to another object", () => {877 const next = produce({}, d => {878 const obj = {}879 return {obj, arr: [obj]}880 })881 expect(next.obj).toBe(next.arr[0])882 })883Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
line 884why, one step back884 it("can return an object with two references to an unmodified draft", () => {test: improve coveragealeclarsonbefore adoption14d7e3c
lines 885–897why, one step back885 const base = {a: {}}886 const next = produce(base, d => {887 return [d.a, d.a]888 })889 expect(next[0]).toBe(base.a)890 expect(next[0]).toBe(next[1])891 })892893 it("cannot return an object that references itself", () => {894 const res = {}895 res.self = res896 expect(() => {897 produce(res, () => res.self)test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91
line 898why, one step back898 }).toThrowErrorMatchingSnapshot()Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
lines 899–904why, one step back899 })900 })901902 describe("async recipe function", () => {903 it("can modify the draft", () => {904 const base = {a: 0, b: 0}feat: Dropped node 6 supportMichel Weststratebefore adoptioneecc773
line 905why, one step back905 return produce(base, async d => {Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
line 906why, one step back906 d.a = 1feat: Dropped node 6 supportMichel Weststratebefore adoptioneecc773
lines 907–908why, one step back907 await Promise.resolve()908 d.b = 1Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
lines 909–928why, one step back909 }).then(res => {910 expect(res).not.toBe(base)911 expect(res).toEqual({a: 1, b: 1})912 })913 })914915 it("works with rejected promises", () => {916 let draft917 const base = {a: 0, b: 0}918 const err = new Error("passed")919 return produce(base, d => {920 draft = d921 draft.b = 1922 return Promise.reject(err)923 }).then(924 () => {925 throw "failed"926 },927 e => {928 expect(e).toBe(err)test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91
line 929why, one step back929 expect(() => draft.a).toThrowErrorMatchingSnapshot()Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
lines 930–955why, one step back930 }931 )932 })933934 it("supports recursive produce calls after await", () => {935 const base = {obj: {k: 1}}936 return produce(base, d => {937 const obj = d.obj938 delete d.obj939 return Promise.resolve().then(() => {940 d.a = produce({}, d => {941 d.b = obj // Assign a draft owned by the parent scope.942 })943944 // Auto-freezing is prevented when an unowned draft exists.945 expect(Object.isFrozen(d.a)).toBeFalsy()946947 // Ensure `obj` is not revoked.948 obj.c = 1949 })950 }).then(res => {951 expect(res).not.toBe(base)952 expect(res).toEqual({953 a: {b: {k: 1, c: 1}}954 })955 })test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 956why, one step back956 })Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 957–958why, one step back957 })958chore: improve more testsaleclarsonbefore adoption4f59b12
line 959why, one step back959 it("throws when the draft is modified and another object is returned", () => {Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 960–961why, one step back960 const base = {x: 3}961 expect(() => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 962–965why, one step back962 produce(base, draft => {963 draft.x = 4964 return {x: 5}965 })test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91
line 966why, one step back966 }).toThrowErrorMatchingSnapshot()Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 967–968why, one step back967 })968chore: improve more testsaleclarsonbefore adoption4f59b12
line 969why, one step back969 it("should fix #117 - 1", () => {Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
line 970why, one step back970 const reducer = (state, action) =>test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 971–978why, one step back971 produce(state, draft => {972 switch (action.type) {973 case "SET_STARTING_DOTS":974 return draft.availableStartingDots.map(a => a)975 default:976 break977 }978 })Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 979–993why, one step back979 const base = {980 availableStartingDots: [981 {dots: 4, count: 1},982 {dots: 3, count: 2},983 {dots: 2, count: 3},984 {dots: 1, count: 4}985 ]986 }987 const next = reducer(base, {type: "SET_STARTING_DOTS"})988 expect(next).toEqual(base.availableStartingDots)989 expect(next).not.toBe(base.availableStartingDots)990 })991992 it("should fix #117 - 2", () => {993 const reducer = (state, action) =>test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 994–1003why, one step back994 produce(state, draft => {995 switch (action.type) {996 case "SET_STARTING_DOTS":997 return {998 dots: draft.availableStartingDots.map(a => a)999 }1000 default:1001 break1002 }1003 })Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 1004–1015why, one step back1004 const base = {1005 availableStartingDots: [1006 {dots: 4, count: 1},1007 {dots: 3, count: 2},1008 {dots: 2, count: 3},1009 {dots: 1, count: 4}1010 ]1011 }1012 const next = reducer(base, {type: "SET_STARTING_DOTS"})1013 expect(next).toEqual({dots: base.availableStartingDots})1014 })1015chore: improve more testsaleclarsonbefore adoption4f59b12
line 1016why, one step back1016 it("cannot always detect noop assignments - 0", () => {Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
line 1017why, one step back1017 const baseState = {x: {y: 3}}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 1018–1021why, one step back1018 const nextState = produce(baseState, d => {1019 const a = d.x1020 d.x = a1021 })Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 1022–1024why, one step back1022 expect(nextState).toBe(baseState)1023 })1024chore: improve more testsaleclarsonbefore adoption4f59b12
line 1025why, one step back1025 it("cannot always detect noop assignments - 1", () => {Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
line 1026why, one step back1026 const baseState = {x: {y: 3}}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 1027–1031why, one step back1027 const nextState = produce(baseState, d => {1028 const a = d.x1029 d.x = 41030 d.x = a1031 })Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 1032–1037why, one step back1032 // Ideally, this should actually be the same instances1033 // but this would be pretty expensive to detect,1034 // so we don't atm1035 expect(nextState).not.toBe(baseState)1036 })1037chore: improve more testsaleclarsonbefore adoption4f59b12
line 1038why, one step back1038 it("cannot always detect noop assignments - 2", () => {Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
line 1039why, one step back1039 const baseState = {x: {y: 3}}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 1040–1045why, one step back1040 const nextState = produce(baseState, d => {1041 const a = d.x1042 const stuff = a.y + 31043 d.x = 41044 d.x = a1045 })Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 1046–1051why, one step back1046 // Ideally, this should actually be the same instances1047 // but this would be pretty expensive to detect,1048 // so we don't atm1049 expect(nextState).not.toBe(baseState)1050 })1051chore: improve more testsaleclarsonbefore adoption4f59b12
line 1052why, one step back1052 it("cannot always detect noop assignments - 3", () => {Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
line 1053why, one step back1053 const baseState = {x: 3}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 1054–1056why, one step back1054 const nextState = produce(baseState, d => {1055 d.x = 31056 })Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 1057–1059why, one step back1057 expect(nextState).toBe(baseState)1058 })1059chore: improve more testsaleclarsonbefore adoption4f59b12
line 1060why, one step back1060 it("cannot always detect noop assignments - 4", () => {Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
line 1061why, one step back1061 const baseState = {x: 3}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 1062–1065why, one step back1062 const nextState = produce(baseState, d => {1063 d.x = 41064 d.x = 31065 })Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 1066–1071why, one step back1066 // Ideally, this should actually be the same instances1067 // but this would be pretty expensive to detect,1068 // so we don't atm1069 expect(nextState).not.toBe(baseState)1070 })1071chore: improve more testsaleclarsonbefore adoption4f59b12
line 1072why, one step back1072 it("cannot produce undefined by returning undefined", () => {Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 1073–1088why, one step back1073 const base = 31074 expect(produce(base, () => 4)).toBe(4)1075 expect(produce(base, () => null)).toBe(null)1076 expect(produce(base, () => undefined)).toBe(3)1077 expect(produce(base, () => {})).toBe(3)1078 expect(produce(base, () => nothing)).toBe(undefined)10791080 expect(produce({}, () => undefined)).toEqual({})1081 expect(produce({}, () => nothing)).toBe(undefined)1082 expect(produce(3, () => nothing)).toBe(undefined)10831084 expect(produce(() => undefined)({})).toEqual({})1085 expect(produce(() => nothing)({})).toBe(undefined)1086 expect(produce(() => nothing)(3)).toBe(undefined)1087 })1088chore: improve more testsaleclarsonbefore adoption4f59b12
lines 1089–1092why, one step back1089 describe("base state type", () => {1090 testObjectTypes(produce)1091 testLiteralTypes(produce)1092 })Merge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417
line 1093why, one step back1093Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 1094–1098why, one step back1094 afterEach(() => {1095 expect(baseState).toBe(origBaseState)1096 expect(baseState).toEqual(createBaseState())1097 })1098chore: improve more testsaleclarsonbefore adoption4f59b12
line 1099why, one step back1099 class Foo {}Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 1100–1101why, one step back1100 function createBaseState() {1101 const data = {chore: improve more testsaleclarsonbefore adoption4f59b12
line 1102why, one step back1102 anInstance: new Foo(),Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 1103–1111why, one step back1103 anArray: [3, 2, {c: 3}, 1],1104 aProp: "hi",1105 anObject: {1106 nested: {1107 yummie: true1108 },1109 coffee: false1110 }1111 }test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 1112why, one step back1112 return autoFreeze ? deepFreeze(data) : dataMerge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
line 1113why, one step back1113 }Merge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417
lines 1114–1154why, one step back1114 })11151116 describe(`isDraft - ${name}`, () => {1117 it("returns true for object drafts", () => {1118 produce({}, state => {1119 expect(isDraft(state)).toBeTruthy()1120 })1121 })1122 it("returns true for array drafts", () => {1123 produce([], state => {1124 expect(isDraft(state)).toBeTruthy()1125 })1126 })1127 it("returns true for objects nested in object drafts", () => {1128 produce({a: {b: {}}}, state => {1129 expect(isDraft(state.a)).toBeTruthy()1130 expect(isDraft(state.a.b)).toBeTruthy()1131 })1132 })1133 it("returns false for new objects added to a draft", () => {1134 produce({}, state => {1135 state.a = {}1136 expect(isDraft(state.a)).toBeFalsy()1137 })1138 })1139 it("returns false for objects returned by the producer", () => {1140 const object = produce(null, Object.create)1141 expect(isDraft(object)).toBeFalsy()1142 })1143 it("returns false for arrays returned by the producer", () => {1144 const array = produce(null, _ => [])1145 expect(isDraft(array)).toBeFalsy()1146 })1147 it("returns false for object drafts returned by the producer", () => {1148 const object = produce({}, state => state)1149 expect(isDraft(object)).toBeFalsy()1150 })1151 it("returns false for array drafts returned by the producer", () => {1152 const array = produce([], state => state)1153 expect(isDraft(array)).toBeFalsy()1154 })Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
line 1155why, one step back1155 })test: extract some tests into their own functionaleclarsonbefore adoption21846ab
lines 1156–1157why, one step back1156}1157chore: improve more testsaleclarsonbefore adoption4f59b12
line 1158why, one step back1158function testObjectTypes(produce) {feat: limited class supportaleclarsonbefore adoption9877d64
lines 1159–1164why, one step back1159 class Foo {1160 constructor(foo) {1161 this.foo = foo1162 this[immerable] = true1163 }1164 }chore: improve more testsaleclarsonbefore adoption4f59b12
lines 1165–1169why, one step back1165 const values = {1166 "empty object": {},1167 "plain object": {a: 1, b: 2},1168 "object (no prototype)": Object.create(null),1169 "empty array": [],feat: limited class supportaleclarsonbefore adoption9877d64
lines 1170–1171why, one step back1170 "plain array": [1, 2],1171 "class instance (draftable)": new Foo(1)chore: improve more testsaleclarsonbefore adoption4f59b12
lines 1172–1212why, one step back1172 }1173 for (const name in values) {1174 const value = values[name]1175 const copy = shallowCopy(value)1176 testObjectType(name, value)1177 testObjectType(name + " (frozen)", Object.freeze(copy))1178 }1179 function testObjectType(name, base) {1180 describe(name, () => {1181 it("creates a draft", () => {1182 produce(base, draft => {1183 expect(draft).not.toBe(base)1184 expect(shallowCopy(draft, true)).toEqual(base)1185 })1186 })11871188 it("preserves the prototype", () => {1189 const proto = Object.getPrototypeOf(base)1190 produce(base, draft => {1191 expect(Object.getPrototypeOf(draft)).toBe(proto)1192 })1193 })11941195 it("returns the base state when no changes are made", () => {1196 expect(produce(base, () => {})).toBe(base)1197 })11981199 it("returns a copy when changes are made", () => {1200 const random = Math.random()1201 const result = produce(base, draft => {1202 draft[0] = random1203 })1204 expect(result).not.toBe(base)1205 expect(result.constructor).toBe(base.constructor)1206 expect(result[0]).toBe(random)1207 })1208 })1209 }1210}12111212function testLiteralTypes(produce) {test: extract some tests into their own functionaleclarsonbefore adoption21846ab
line 1213why, one step back1213 class Foo {}chore: improve more testsaleclarsonbefore adoption4f59b12
line 1214why, one step back1214 const values = {test: extract some tests into their own functionaleclarsonbefore adoption21846ab
lines 1215–1217why, one step back1215 "falsy number": 0,1216 "truthy number": 1,1217 "negative number": -1,chore: improve more testsaleclarsonbefore adoption4f59b12
line 1218why, one step back1218 NaN: NaN,test: extract some tests into their own functionaleclarsonbefore adoption21846ab
lines 1219–1227why, one step back1219 infinity: 1 / 0,1220 true: true,1221 false: false,1222 "empty string": "",1223 "truthy string": "1",1224 null: null,1225 undefined: undefined,12261227 /**chore: improve more testsaleclarsonbefore adoption4f59b12
lines 1228–1229why, one step back1228 * These objects are treated as literals because Immer1229 * does not know how to draft them.test: extract some tests into their own functionaleclarsonbefore adoption21846ab
line 1230why, one step back1230 */chore: improve more testsaleclarsonbefore adoption4f59b12
line 1231why, one step back1231 function: () => {},test: extract some tests into their own functionaleclarsonbefore adoption21846ab
lines 1232–1236why, one step back1232 "regexp object": /.+/g,1233 "boxed number": new Number(0),1234 "boxed string": new String(""),1235 "boxed boolean": new Boolean(),1236 "date object": new Date(),feat: limited class supportaleclarsonbefore adoption9877d64
line 1237why, one step back1237 "class instance (not draftable)": new Foo()test: extract some tests into their own functionaleclarsonbefore adoption21846ab
line 1238why, one step back1238 }chore: improve more testsaleclarsonbefore adoption4f59b12
lines 1239–1242why, one step back1239 for (const name in values) {1240 describe(name, () => {1241 const value = values[name]1242test: extract some tests into their own functionaleclarsonbefore adoption21846ab
lines 1243–1247why, one step back1243 it("does not create a draft", () => {1244 produce(value, draft => {1245 expect(draft).toBe(value)1246 })1247 })chore: improve more testsaleclarsonbefore adoption4f59b12
lines 1248–1249why, one step back12481249 it("returns the base state when no changes are made", () => {test: extract some tests into their own functionaleclarsonbefore adoption21846ab
lines 1250–1251why, one step back1250 expect(produce(value, () => {})).toBe(value)1251 })chore: improve more testsaleclarsonbefore adoption4f59b12
line 1252why, one step back1252test: extract some tests into their own functionaleclarsonbefore adoption21846ab
line 1253why, one step back1253 if (value && typeof value == "object") {chore: improve more testsaleclarsonbefore adoption4f59b12
line 1254why, one step back1254 it("does not return a copy when changes are made", () => {test: extract some tests into their own functionaleclarsonbefore adoption21846ab
lines 1255–1263why, one step back1255 expect(1256 produce(value, draft => {1257 draft.foo = true1258 })1259 ).toBe(value)1260 })1261 }1262 })1263 }Merge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
lines 1264–1266why, one step back1264}12651266function enumerableOnly(x) {feat: limited class supportaleclarsonbefore adoption9877d64
lines 1267–1268why, one step back1267 const copy = Array.isArray(x) ? x.slice() : Object.assign({}, x)1268 each(copy, (prop, value) => {test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44
line 1269why, one step back1269 if (value && typeof value === "object") {feat: limited class supportaleclarsonbefore adoption9877d64
line 1270why, one step back1270 copy[prop] = enumerableOnly(value)test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44
line 1271why, one step back1271 }feat: limited class supportaleclarsonbefore adoption9877d64
line 1272why, one step back1272 })test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44
line 1273why, one step back1273 return copyMerge pull request #199 from stefanwille/fix-tests-for-node-v10.10Michel Weststratebefore adoption0d72d13
line 1274why, one step back1274}