Skip to content
Myr

wip

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 d2a47b6 · Browse files · 5 files+74 added−15 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+74 added−15 removed

  • tests:+66 added−9 removed
  • other:+8 added−6 removed

5 files+74 added−15 removed

modified:__tests__/base.js+66 added-9 removed
18 unmodified lines
19test("immer should have no dependencies", () => {20 expect(require("../package.json").dependencies).toBeUndefined()21})2223runBaseTest("proxy (no freeze)", true, false)24runBaseTest("proxy (autofreeze)", true, true)25runBaseTest("proxy (patch listener)", true, false, true)26runBaseTest("proxy (autofreeze)(patch listener)", true, true, true)23// runBaseTest("proxy (no freeze)", true, false)24// runBaseTest("proxy (autofreeze)", true, true)25// runBaseTest("proxy (patch listener)", true, false, true)26// runBaseTest("proxy (autofreeze)(patch listener)", true, true, true)2728runBaseTest("es5 (no freeze)", false, false)29runBaseTest("es5 (autofreeze)", false, true)30runBaseTest("es5 (patch listener)", false, false, true)31runBaseTest("es5 (autofreeze)(patch listener)", false, true, true)29// runBaseTest("es5 (autofreeze)", false, true)30// runBaseTest("es5 (patch listener)", false, false, true)31// runBaseTest("es5 (autofreeze)(patch listener)", false, true, true)3233function runBaseTest(name, useProxies, autoFreeze, useListener) {34 const listener = useListener ? function() {} : undefined35 const {produce, produceWithPatches} = createPatchedImmer({
896 unmodified lines
932 expect(nextState.foo).toBeTruthy()933 expect(isEnumerable(nextState, "foo")).toBeFalsy()934 })935936 it("throws on computed properties", () => {936 it("does not throws on computed properties", () => {937 const baseState = {}938 Object.defineProperty(baseState, "foo", {939 get: () => {},940 enumerable: true
4 unmodified lines
945 if (useProxies) {946 s.modified = true947 }948 })949 }).toThrowErrorMatchingSnapshot()949 }).not.toThrow()950 })951952 it("can work with computed props", () => {953 const baseState = {954 x: 1,955 set y(v) {956 this.x = v;957 },958 get y() {959 return this.x960 }961 }962963 debugger;964 const nextState = produce(baseState, d => {965 expect(d.y).toBe(1)966 d.y = 2;967 expect(d.x).toBe(2)968 expect(d.y).toBe(2)969 })970 expect(baseState.x).toBe(1)971 expect(baseState.y).toBe(1)972973 expect(nextState.x).toBe(2)974 expect(nextState.y).toBe(2)975 })976977 it("can work with class with computed props", () => {978 class State {979 [immerable]= true980981 x= 1,982983 set y(v) {984 this.x = v;985 },986987 get y() {988 return this.x989 }990 }991992 const baseState = new State();993994 const nextState = produce(baseState, d => {995 expect(d.y).toBe(1)996 d.y = 2;997 expect(d.x).toBe(2)998 expect(d.y).toBe(2)999 expect(Object.hasOwnProperty(d, "y")).toBeNull()1000 })1001 expect(baseState.x).toBe(1)1002 expect(baseState.y).toBe(1)10031004 expect(nextState.x).toBe(2)1005 expect(nextState.y).toBe(2)1006 expect(Object.hasOwnProperty(nextState, "y")).toBeNull()1007 })10081009 it("allows inherited computed properties", () => {1010 const proto = {}1011 Object.defineProperty(proto, "foo", {1012 get() {
1257 unmodified lines
modified:src/core/immerClass.ts+1 added
114 unmodified lines
115 }116 usePatchesInScope(scope, patchListener)117 return processResult(result, scope)118 } else {119 // TODO: simply throw!120 result = recipe(base)121 if (result === NOTHING) return undefined122 if (result === undefined) result = base123 if (this.autoFreeze_) freeze(result, true)
113 unmodified lines
modified:src/core/proxy.ts-4 removed
135 unmodified lines
136 // @ts-ignore137 drafts = state.copy_138 }139140 // const desc = Object.getOwnPropertyDescriptor(latest(state), prop)141 // Don't create and store proxies for getters (they might return drafts though)142 // See #558143 // if (!desc || desc.get) return value140 return (drafts![prop as any] = createProxy(141 state.scope_.immer_,142 value,143 state
124 unmodified lines
modified:src/utils/common.ts+4 added-1 removed
modified:src/utils/errors.ts+3 added-1 removed
27 unmodified lines
28 },29 18(plugin: string) {30 return `The plugin for '${plugin}' has not been loaded into Immer. To enable the plugin, import and call \`enable${plugin}()\` when initializing your application.`31 },32 19: "plugin not loaded",32 19(plugin: string) {33 return `Plugin not loaded: ${plugin}`34 },35 20: "Cannot use proxies if Proxy, Proxy.revocable or Reflect are not available"36} as const3738export function die(error: keyof typeof errors, ...args: any[]): never {
15 unmodified lines