Skip to content
Myr

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

3 files+95 added−9 removed

modified:__tests__/base.js+93 added
1279 unmodified lines
1280 expect(produce(() => nothing)({})).toBe(undefined)1281 expect(produce(() => nothing)(3)).toBe(undefined)1282 })12831284 // 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,12981299 /**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 }13761377 afterEach(() => {1378 expect(baseState).toBe(origBaseState)1379 expect(baseState).toEqual(createBaseState())1380 })
67 unmodified lines
modified:__tests__/curry.js-3 removed
modified:src/immer.js+2 added-6 removed