Skip to content
Myr

More tests, also fixed binding of produceWithPatches

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 24658bc · Browse files · 3 files+346 added−3 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+346 added−3 removed

  • tests:+345 added−3 removed
  • other:+1 added−0 removed

3 files+346 added−3 removed

modified:__tests__/__snapshots__/base.js.snap+282 added
153 unmodified lines
154155exports[`base functionality - proxy (no freeze) throws when Object.setPrototypeOf() is used on a draft 1`] = `"Object.setPrototypeOf() cannot be used on an Immer draft"`;156157exports[`base functionality - proxy (no freeze) throws when the draft is modified and another object is returned 1`] = `"An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft."`;158159exports[`complex nesting map / set / object modify deep object 1`] = `160Object {161 "map": Map {162 "set1" => Set {163 Object {164 "a": 2,165 },166 Object {167 "b": 2,168 },169 },170 "set2" => Set {171 Object {172 "c": 3,173 },174 },175 },176}177`;178179exports[`complex nesting map / set / object modify deep object 2`] = `180Array [181 Object {182 "op": "remove",183 "path": Array [184 "map",185 "set1",186 0,187 ],188 "value": Object {189 "a": 1,190 },191 },192 Object {193 "op": "add",194 "path": Array [195 "map",196 "set1",197 0,198 ],199 "value": Object {200 "a": 2,201 },202 },203]204`;205206exports[`complex nesting map / set / object modify deep object 3`] = `207Object {208 "map": Map {209 "set1" => Set {210 Object {211 "a": 2,212 },213 Object {214 "b": 2,215 },216 },217 "set2" => Set {218 Object {219 "c": 3,220 },221 },222 },223}224`;225226exports[`complex nesting map / set / object modify deep object 4`] = `227Array [228 Object {229 "op": "remove",230 "path": Array [231 "map",232 "set1",233 0,234 ],235 "value": Object {236 "a": 1,237 },238 },239 Object {240 "op": "add",241 "path": Array [242 "map",243 "set1",244 0,245 ],246 "value": Object {247 "a": 2,248 },249 },250]251`;252253exports[`complex nesting map / set / object modify deep object 5`] = `254Object {255 "map": Map {256 "set1" => Set {257 Object {258 "a": 2,259 },260 Object {261 "b": 2,262 },263 },264 "set2" => Set {265 Object {266 "c": 3,267 },268 },269 },270}271`;272273exports[`complex nesting map / set / object modify deep object 6`] = `274Array [275 Object {276 "op": "remove",277 "path": Array [278 "map",279 "set1",280 0,281 ],282 "value": Object {283 "a": 1,284 },285 },286 Object {287 "op": "add",288 "path": Array [289 "map",290 "set1",291 0,292 ],293 "value": Object {294 "a": 2,295 },296 },297]298`;299300exports[`complex nesting map / set / object modify deep object 7`] = `301Object {302 "map": Map {303 "set1" => Set {304 Object {305 "a": 2,306 },307 Object {308 "b": 2,309 },310 },311 "set2" => Set {312 Object {313 "c": 3,314 },315 },316 },317}318`;319320exports[`complex nesting map / set / object modify deep object 8`] = `321Array [322 Object {323 "op": "remove",324 "path": Array [325 "map",326 "set1",327 0,328 ],329 "value": Object {330 "a": 1,331 },332 },333 Object {334 "op": "add",335 "path": Array [336 "map",337 "set1",338 0,339 ],340 "value": Object {341 "a": 2,342 },343 },344]345`;346347exports[`complex nesting map / set / object modify deep object 9`] = `348Object {349 "map": Map {350 "set1" => Set {351 Object {352 "a": 2,353 },354 Object {355 "b": 2,356 },357 },358 "set2" => Set {359 Object {360 "c": 3,361 },362 },363 },364}365`;366367exports[`complex nesting map / set / object modify deep object 10`] = `368Array [369 Object {370 "op": "remove",371 "path": Array [372 "map",373 "set1",374 0,375 ],376 "value": Object {377 "a": 1,378 },379 },380 Object {381 "op": "add",382 "path": Array [383 "map",384 "set1",385 0,386 ],387 "value": Object {388 "a": 2,389 },390 },391]392`;393394exports[`complex nesting map / set / object modify deep object 11`] = `395Object {396 "map": Map {397 "set1" => Set {398 Object {399 "a": 2,400 },401 Object {402 "b": 2,403 },404 },405 "set2" => Set {406 Object {407 "c": 3,408 },409 },410 },411}412`;413414exports[`complex nesting map / set / object modify deep object 12`] = `415Array [416 Object {417 "op": "remove",418 "path": Array [419 "map",420 "set1",421 0,422 ],423 "value": Object {424 "a": 1,425 },426 },427 Object {428 "op": "add",429 "path": Array [430 "map",431 "set1",432 0,433 ],434 "value": Object {435 "a": 2,436 },437 },438]439`;
modified:__tests__/base.js+63 added-3 removed
18 unmodified lines
19runBaseTest("es5 (autofreeze)(patch listener)", false, true, true)2021function runBaseTest(name, useProxies, autoFreeze, useListener) {22 const listener = useListener ? function() {} : undefined23 const {produce} = createPatchedImmer({23 const {produce, produceWithPatches} = createPatchedImmer({24 useProxies,25 autoFreeze26 })27
2 unmodified lines
30 function createPatchedImmer(options) {31 const immer = new Immer(options)3233 const {produce} = immer34 immer.produce = (...args) =>35 typeof args[1] === "function" && args.length < 334 immer.produce = function(...args) {35 return typeof args[1] === "function" && args.length < 336 ? produce(...args, listener)37 : produce(...args)38 }3940 return immer41 }42
1528 unmodified lines
1571 const array = produce([], state => state)1572 expect(isDraft(array)).toBeFalsy()1573 })1574 })15751576 describe(`complex nesting map / set / object`, () => {1577 const a = {a: 1}1578 const b = {b: 2}1579 const c = {c: 3}1580 const set1 = new Set([a, b])1581 const set2 = new Set([c])1582 const map = new Map([["set1", set1], ["set2", set2]])1583 const base = {map}15841585 function first(set) {1586 return Array.from(set.values())[0]1587 }15881589 function second(set) {1590 return Array.from(set.values())[1]1591 }15921593 test(`modify deep object`, () => {1594 const [res, patches] = produceWithPatches(base, draft => {1595 const v = first(draft.map.get("set1"))1596 expect(original(v)).toBe(a)1597 expect(v).toEqual(a)1598 expect(v).not.toBe(a)1599 v.a++1600 })1601 expect(res).toMatchSnapshot()1602 expect(patches).toMatchSnapshot()1603 expect(a.a).toBe(1)1604 expect(base.map.get("set1")).toBe(set1)1605 expect(first(base.map.get("set1"))).toBe(a)1606 expect(res).not.toBe(base)1607 expect(res.map).not.toBe(base.map)1608 expect(res.map.get("set1")).not.toBe(base.map.get("set1"))1609 expect(second(base.map.get("set1"))).toBe(b)1610 expect(base.map.get("set2")).toBe(set2)1611 expect(first(res.map.get("set1"))).toEqual({a: 2})1612 })16131614 test(`modify deep object - keep value`, () => {1615 const [res, patches] = produceWithPatches(base, draft => {1616 const v = first(draft.map.get("set1"))1617 expect(original(v)).toBe(a)1618 expect(v).toEqual(a)1619 expect(v).not.toBe(a)1620 v.a = 1 // same value1621 })1622 expect(a.a).toBe(1)1623 expect(base.map.get("set1")).toBe(set1)1624 expect(first(base.map.get("set1"))).toBe(a)1625 expect(res).toBe(base)1626 expect(res.map).toBe(base.map)1627 expect(res.map.get("set1")).toBe(base.map.get("set1"))1628 expect(first(res.map.get("set1"))).toBe(a)1629 expect(second(base.map.get("set1"))).toBe(b)1630 expect(base.map.get("set2")).toBe(set2)1631 expect(patches.length).toBe(0)1632 })1633 })1634}16351636function testObjectTypes(produce) {1637 class Foo {
115 unmodified lines
modified:src/immer.js+1 added
38 unmodified lines
39 constructor(config) {40 assign(this, configDefaults, config)41 this.setUseProxies(this.useProxies)42 this.produce = this.produce.bind(this)43 this.produceWithPatches = this.produceWithPatches.bind(this)44 }45 produce(base, recipe, patchListener) {46 // curried invocation47 if (typeof base === "function" && typeof recipe !== "function") {
303 unmodified lines