Skip to content
Myr

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

2 files+66 added−6 removed

modified:__tests__/base.js+65 added-5 removed
349 unmodified lines
350 expect(s.foo).toBe(s.bar)351 })352 })353354 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 })366367 // 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 })375376 it("can rename nested objects (no changes)", () => {377 const nextState = produce({obj: {}}, s => {378 s.foo = s.obj379 delete s.obj
407 unmodified lines
787788 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 })797798 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 })807808 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 })815816 it("can return a frozen object", () => {817 const res = deepFreeze([{x: 3}])818 expect(produce({}, () => res)).toBe(res)819 })820821 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 })828829 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 })837838 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 })846847 it("throws when the draft is modified and another object is returned", () => {848 const base = {x: 3}
314 unmodified lines
modified:src/es5.js+1 added-1 removed
129 unmodified lines
130function 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}137138// This looks expensive, but only proxies are visited, and only objects without known changes are scanned.
97 unmodified lines