Merge pull request #236 from mweststrate/issue-233
Before adoption: this commit came in with the history the project adopted, so no landing records it.
fix(produce): avoid throwing on unproxyable objects
authored by Michel Weststrate · committed by GitHub · parents 2f381f5, dbfaa02 · Browse files · 3 files·+95 added−9 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+95 added−9 removed
- tests:+93 added−3 removed
- other:+2 added−6 removed
Provenance
- Commit
f12a7a8- Tree
dddbda1- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
2f381f5- Parent 2
dbfaa02
3 files changed+95 added−9 removed
modified:__tests__/base.js+93 added
1279 unmodified lines1280 expect(produce(() => nothing)({})).toBe(undefined)1281 expect(produce(() => nothing)(3)).toBe(undefined)1282 })1283 1284+ // TODO: use fuzz testing1285+ {1286+ class Foo {}1287+ const primitives = {1288+ "falsy number": 0,1289+ "truthy number": 1,1290+ "negative number": -1,1291+ infinity: 1 / 0,1292+ true: true,1293+ false: false,1294+ "empty string": "",1295+ "truthy string": "1",1296+ null: null,1297+ undefined: undefined,1298+1299+ /**1300+ * These objects are treated as primitives because Immer1301+ * chooses not to make drafts for them.1302+ */1303+ "regexp object": /.+/g,1304+ "boxed number": new Number(0),1305+ "boxed string": new String(""),1306+ "boxed boolean": new Boolean(),1307+ "date object": new Date(),1308+ "class instance": new Foo()1309+ }1310+ for (const name in primitives) {1311+ describe("base state type - " + name, () => {1312+ const value = primitives[name]1313+ it("does not create a draft", () => {1314+ produce(value, draft => {1315+ expect(draft).toBe(value)1316+ })1317+ })1318+ it("returns the same value when the producer returns undefined", () => {1319+ expect(produce(value, () => {})).toBe(value)1320+ })1321+ if (value && typeof value == "object") {1322+ it("does not return a copy when the producer makes changes", () => {1323+ expect(1324+ produce(value, draft => {1325+ draft.foo = true1326+ })1327+ ).toBe(value)1328+ })1329+ }1330+ expect(1331+ produce(value, draft => {1332+ expect(draft).toBe(value)1333+ })1334+ ).toBe(value)1335+ })1336+ }1337+ const objects = {1338+ "empty object": {},1339+ "plain object": {a: 1, b: {c: 1}},1340+ "frozen object": Object.freeze({}),1341+ "null-prototype object": Object.create(null),1342+ "empty array": [],1343+ "plain array": [1, [2, [3, []]]],1344+ "frozen array": Object.freeze([])1345+ }1346+ for (const name in objects) {1347+ describe("base state type - " + name, () => {1348+ const value = objects[name]1349+ it("returns a copy when changes are made", () => {1350+ const random = Math.random()1351+ const result = produce(value, draft => {1352+ if (Array.isArray(value)) {1353+ draft.push(random)1354+ } else {1355+ draft[random] = true1356+ }1357+ })1358+ expect(result).not.toBe(value)1359+ expect(result.constructor).toBe(value.constructor)1360+ if (Array.isArray(value)) {1361+ expect(result[result.length - 1]).toBe(random)1362+ } else {1363+ expect(result[random]).toBe(true)1364+ }1365+ })1366+ it("should ", () => {})1367+ expect(1368+ produce(value, draft => {1369+ expect(draft).not.toBe(value)1370+ expect(value).toEqual(draft)1371+ })1372+ ).toBe(value)1373+ })1374+ }1375+ }1376+1377 afterEach(() => {1378 expect(baseState).toBe(origBaseState)1379 expect(baseState).toEqual(createBaseState())1380 })67 unmodified linesmodified:__tests__/curry.js-3 removed
8 unmodified lines91011121314151314151666 unmodified lines8 unmodified lines setUseProxies(useProxies)
it("should check arguments", () => { expect(() => produce()).toThrow(/produce expects 1 to 3 arguments/) expect(() => produce(new Buffer(""), () => {})).toThrow( /the first argument to an immer producer should be a primitive, plain object or array/ ) expect(() => produce({}, () => {}, [])).toThrow( /third argument of a producer/ ) expect(() => produce({}, {})).toThrow(/should be a function/)66 unmodified linesmodified:src/immer.js+2 added-6 removed
47 unmodified lines4849505152535253545556575859606162636061626312 unmodified lines47 unmodified lines if (typeof producer !== "function") throw new Error("if first argument is not a function, the second argument to produce should be a function") if (patchListener !== undefined && typeof patchListener !== "function") throw new Error("the third argument of a producer should not be set or a function") }
// if state is a primitive, don't bother proxying at all if (typeof baseState !== "object" || baseState === null) { // avoid proxying anything except plain objects and arrays if (!isProxyable(baseState)) { const returnValue = producer(baseState) return returnValue === undefined ? baseState : normalizeResult(returnValue) }
if (!isProxyable(baseState)) throw new Error( `the first argument to an immer producer should be a primitive, plain object or array, got ${typeof baseState}: "${baseState}"` ) return normalizeResult( getUseProxies() ? produceProxy(baseState, producer, patchListener) : produceEs5(baseState, producer, patchListener)12 unmodified lines