Skip to content
Myr

test: improve "nested producer" tests

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 a84eea5 · Browse files · 1 file+15 added−34 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+15 added−34 removed

  • tests:+15 added−34 removed

1 file+15 added−34 removed

modified:__tests__/base.js+15 added-34 removed
581 unmodified lines
582 describe("when base state is not a draft", () => {583 // This test ensures the global state used to manage proxies is584 // never left in a corrupted state by a nested `produce` call.585 it("never affects its parent producer implicitly", () => {586 const bear = {paw: {honey: true}}587 const next = produce(bear, draft => {588 const paw2 = produce(bear.paw, draft => {589 draft.honey = false586 const base = {obj: {a: 1}}587 const next = produce(base, draft => {588 // Notice how `base.obj` is passed, not `draft.obj`589 const obj2 = produce(base.obj, draft2 => {590 draft2.a = 0591 })591 expect(paw2.honey).toBe(false)592 expect(draft.paw.honey).toBe(true) // effects should not be visible outside592 expect(obj2.a).toBe(0)593 expect(draft.obj.a).toBe(1) // effects should not be visible outside594 })594 expect(next.paw.honey).toBe(true)595 expect(next).toBe(bear)595 expect(next).toBe(base)596 })597598 it("returns a normal object", () => {599 const bear = {paw: {honey: true}}600 const next = produce(bear, draft => {601 const paw2 = produce(bear.paw, draft => {602 draft.honey = false603 })604 expect(paw2.honey).toBe(false)605 expect(draft.paw.honey).toBe(true)606 draft.paw = paw2607 expect(draft.paw.honey).toBe(false)608 })609 expect(next.paw.honey).toBe(false)610 expect(next).not.toBe(bear)611 })597 })598599 describe("when base state is a draft", () => {615 it("always reuses the draft", () => {616 const bear = {paw: {honey: true}}617 const next = produce(bear, bear => {618 const paw2 = produce(bear.paw, paw => {619 expect(paw).toBe(bear.paw)620 paw.honey = false600 it("always wraps the draft in a new draft", () => {601 produce({}, parent => {602 produce(parent, child => {603 expect(child).not.toBe(parent)604 expect(isDraft(child)).toBeTruthy()605 expect(original(child)).toBe(parent)606 })622 expect(paw2).toBe(bear.paw)623 expect(paw2.honey).toBe(false)624 expect(bear.paw.honey).toBe(false)607 })626 expect(next.paw.honey).toBe(false)627 expect(next).not.toBe(bear)608 })609 })610611 describe("when base state contains a draft", () => {
1 unmodified line
613 produce({a: {}}, parent => {614 produce({a: parent.a}, child => {615 expect(child.a).not.toBe(parent.a)616 expect(isDraft(child.a)).toBeTruthy()617 expect(original(child.a)).toBe(parent.a)618 })619 })620 })621
530 unmodified lines