test: improve coverage
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by aleclarson · committed by Alec Larson · parent 48b371c · Browse files · 2 files·+66 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+66 added−6 removed
- tests:+65 added−5 removed
- other:+1 added−1 removed
Provenance
- Commit
14d7e3c- Tree
358dfa6- Author
aleclarson <alec.stanford.larson@gmail.com>- Committer
Alec Larson <alec.stanford.larson@gmail.com>- Parent
48b371c
2 files changed+66 added−6 removed
modified:__tests__/base.js+65 added-5 removed
349 unmodified lines350 expect(s.foo).toBe(s.bar)351 })352 })353 354+ it("supports a base state with multiple references to an object", () => {355+ const obj = {}356+ const res = produce({a: obj, b: obj}, d => {357+ // Two drafts are created for each occurrence of an object in the base state.358+ expect(d.a).not.toBe(d.b)359+ d.a.z = true360+ expect(d.b.z).toBeUndefined()361+ })362+ expect(res.b).toBe(obj)363+ expect(res.a).not.toBe(res.b)364+ expect(res.a.z).toBeTruthy()365+ })366+367+ // NOTE: Except the root draft.368+ it("supports multiple references to any modified draft", () => {369+ const next = produce({a: {b: 1}}, d => {370+ d.a.b++371+ d.b = d.a372+ })373+ expect(next.a).toBe(next.b)374+ })375+376 it("can rename nested objects (no changes)", () => {377 const nextState = produce({obj: {}}, s => {378 s.foo = s.obj379 delete s.obj407 unmodified lines787 788 describe("recipe functions", () => {789 it("can return a new object", () => {790 const base = {x: 3}769- const res = produce(base, draft => {770- return {x: draft.x + 1}791+ const res = produce(base, d => {792+ return {x: d.x + 1}793 })794 expect(res).not.toBe(base)795 expect(res).toEqual({x: 4})796 })797 798 it("can return the draft", () => {799 const base = {x: 3}778- const res = produce(base, draft => {779- draft.x = 4780- return draft800+ const res = produce(base, d => {801+ d.x = 4802+ return d803 })804 expect(res).not.toBe(base)805 expect(res).toEqual({x: 4})806 })807+808+ it("can return a child draft", () => {809+ const base = {a: {}}810+ const res = produce(base, d => {811+ return d.a812+ })813+ expect(res).toBe(base.a)814+ })815+816+ it("can return a frozen object", () => {817+ const res = deepFreeze([{x: 3}])818+ expect(produce({}, () => res)).toBe(res)819+ })820+821+ it("can return an object with two references to another object", () => {822+ const next = produce({}, d => {823+ const obj = {}824+ return {obj, arr: [obj]}825+ })826+ expect(next.obj).toBe(next.arr[0])827+ })828+829+ it("can return an object with two references to any pristine draft", () => {830+ const base = {a: {}}831+ const next = produce(base, d => {832+ return [d.a, d.a]833+ })834+ expect(next[0]).toBe(base.a)835+ expect(next[0]).toBe(next[1])836+ })837+838+ it("cannot return an object that references itself", () => {839+ const res = {}840+ res.self = res841+ expect(() => {842+ produce(res, () => res.self)843+ }).toThrow("Immer forbids circular references")844+ })845 })846 847 it("throws when the draft is modified and another object is returned", () => {848 const base = {x: 3}314 unmodified linesmodified:src/es5.js+1 added-1 removed
129 unmodified lines130 function assertUnrevoked(state) {131 if (state.revoked === true)132 throw new Error(133 "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " +134- JSON.stringify(state.copy || state.base)134+ JSON.stringify(source(state))135 )136 }137 138 // This looks expensive, but only proxies are visited, and only objects without known changes are scanned.97 unmodified lines