Making several improvements at once
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Michel Weststrate · committed by Michel Weststrate · parent 11967a5 · Browse files · 3 files·+100 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+100 added−10 removed
- tests:+96 added−7 removed
- other:+4 added−3 removed
Provenance
- Commit
1116ccf- Tree
30ecbb0- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
11967a5
3 files changed+100 added−10 removed
modified:__tests__/base.js+96 added-7 removed
1 unmodified line2 import * as immerProxy from "../src/immer"3 import * as immerEs5 from "../src/es5"4 import deepFreeze from "deep-freeze"5 6+jest.setTimeout(1000)7+8 runBaseTest("proxy (no freeze)", immerProxy, false)9 runBaseTest("proxy (autofreeze)", immerProxy, true)10 runBaseTest("es5 (no freeze)", immerEs5, false)11 runBaseTest("es5 (autofreeze)", immerEs5, true)454 unmodified lines466 count: 3467 }468 ]469 })468- expect(result.newArr[0]).toBe(base.arr[2]) // isn't this weird? not sure469- expect(result.arr[2]).toBe(base.arr[2]) // isn't this weird? not sure470+ expect(result.newArr[0]).toBe(base.arr[2])471+ expect(result.arr[2]).toBe(base.arr[2])472 })473 474 it("should not throw error, see #53 - 3", () => {475 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}7 unmodified lines483 })484 expect(result.newArr[0]).toBe(base.arr[2])485 })486 487+ it("should not throw error, see #53 - 4", () => {488+ debugger489+ const base = {bear: {age: 10}}490+ const result = produce(base, draft => {491+ draft.bear.legs = 4492+ draft.room = {elephant: {kiddo: draft.bear}}493+ })494+ expect(result).toEqual({495+ bear: {age: 10, legs: 4},496+ room: {elephant: {kiddo: {age: 10, legs: 4}}}497+ })498+499+ const result2 = produce(result, draft => {500+ draft.bear.age = 11501+ draft.room.elephant.kiddo.legs = 5502+ })503+ expect(result2).toEqual({504+ bear: {age: 11, legs: 4},505+ room: {elephant: {kiddo: {age: 10, legs: 5}}}506+ })507+ })508+509+ it("should handle dates correctly", () => {510+ const data = {date: new Date()}511+ const next = produce(data, draft => {512+ draft.x = true513+ })514+ expect(next).toEqual({x: true, date: data.date})515+ expect(next.date).toBe(data.date)516+ const next2 = produce(next, draft => {517+ draft.date.setYear(2015)518+ })519+ // This still holds; because produce won't proxy Date objects520+ // and the original is actually modified!521+ expect(next2).toEqual({x: true, date: data.date})522+ expect(next2.date).toBe(next.date)523+ expect(next2.date).toBe(data.date)524+ expect(next2).toBe(next)525+ })526+527+ it("should handle equality correctly - 1", () => {528+ const base = {529+ y: 3 / 0,530+ z: NaN531+ }532+ const next = produce(base, draft => {533+ draft.y = 4 / 0534+ draft.z = NaN535+ })536+ expect(next).toEqual(base)537+ expect(next).toBe(base)538+ })539+540+ it("should handle equality correctly - 2", () => {541+ const base = {542+ x: -0543+ }544+ const next = produce(base, draft => {545+ draft.x = +1546+ })547+ expect(next).not.toEqual(base)548+ expect(next).not.toBe(base)549+ })550+551+ it("should handle nested immer calls correctly - 1", () => {552+ const bear = {paw: {honey: true}}553+ const next = produce(bear, draft => {554+ const paw2 = produce(bear.paw, draft => {555+ draft.honey = false556+ })557+ expect(paw2.honey).toBe(false)558+ expect(draft.paw.honey).toBe(true) // effects should not be visible outside559+ })560+ expect(next.paw.honey).toBe(true)561+ expect(next).toBe(bear)562+ })563+564+ it("should handle nested immer calls correctly - 2", () => {565+ const bear = {paw: {honey: true}}566+ const next = produce(bear, draft => {567+ const paw2 = produce(bear.paw, draft => {568+ draft.honey = false569+ })570+ expect(paw2.honey).toBe(false)571+ expect(draft.paw.honey).toBe(true)572+ draft.paw = paw2573+ expect(draft.paw.honey).toBe(false)574+ })575+ expect(next.paw.honey).toBe(false)576+ expect(next).not.toBe(bear)577+ })578+579 afterEach(() => {580 expect(baseState).toBe(origBaseState)581 expect(baseState).toEqual(createBaseState())582 })17 unmodified lines600 function enumerableOnly(x) {601 // this can be done better...602 return JSON.parse(JSON.stringify(x))603 }510-511-// TODO: test problem scenarios512-// nesting immmer513-// non-trees514-// complex objects / functionsmodified:src/es5.js+1 added-1 removed
67 unmodified lines68 69 set(prop, value) {70 assertUnfinished()71 if (!this.modified) {72- if (this.source[prop] === value) return72+ if (Object.is(this.source[prop], value)) return73 this.markChanged()74 }75 this.prepareCopy()76 this.copy[prop] = value220 unmodified linesmodified:src/immer.js+3 added-2 removed
4 unmodified lines5 throw new Error(6 "Immer requires `Proxy` to be available, but it seems to be not available on your platform. Consider requiring immer '\"immer/es5\"' instead."7 )8 9-const PROXY_STATE = Symbol("immer-proxy-state") // TODO: create per closure, to avoid sharing proxies between multiple immer version9+const PROXY_STATE = Symbol("immer-proxy-state")10 let autoFreeze = true11 12 const objectTraps = {13 get(target, prop) {120 unmodified lines134 135 set(prop, value) {136 if (!this.modified) {137 if (138- (prop in this.base && this.base[prop] === value) ||138+ (prop in this.base && Object.is(this.base[prop], value)) ||139 (prop in this.proxies && this.proxies[prop] === value)140 )141 return142 this.markChanged()57 unmodified lines200 } else return state.base201 } else if (base !== null && typeof base === "object") {202 // If finalize is called on an object that was not a proxy, it means that it is an object that was not there in the original203 // tree and it could contain proxies at arbitrarily places. Let's find and finalize them as well204+ // TODO: optimize this; walk the tree without writing to find proxies205 if (Array.isArray(base)) {206 for (let i = 0; i < base.length; i++)207 base[i] = finalize(base[i])208 return freeze(base)78 unmodified lines