Skip to content

Commit

Permalink
updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Sep 10, 2019
1 parent f2a79c7 commit 8aa4da3
Showing 1 changed file with 44 additions and 39 deletions.
83 changes: 44 additions & 39 deletions __tests__/frozen.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,72 +50,77 @@ function runTests(name, useProxies) {
expect(isFrozen(next)).toBeTruthy()
expect(isFrozen(next.a)).toBeTruthy()
})
})

describe("the result is never auto-frozen when", () => {
it("the producer is a no-op", () => {
const base = {}
const next = produce(base, () => {})
expect(next).toBe(base)
expect(isFrozen(next)).toBeFalsy()
})

it("the root draft is returned", () => {
const base = {}
const next = produce(base, draft => draft)
expect(next).toBe(base)
expect(isFrozen(next)).toBeFalsy()
})

it("a nested draft is returned", () => {
const base = {a: {}}
const next = produce(base, draft => draft.a)
expect(next).toBe(base.a)
expect(isFrozen(next)).toBeFalsy()
})

it("the base state is returned", () => {
const base = {}
const next = produce(base, () => base)
expect(next).toBe(base)
expect(isFrozen(next)).toBeFalsy()
})

it("a new object replaces a primitive base", () => {
const obj = {}
const obj = {a: {}}
const next = produce(null, () => obj)
expect(next).toBe(obj)
expect(isFrozen(next)).toBeFalsy()
expect(isFrozen(next)).toBeTruthy()
expect(isFrozen(next.a)).toBeTruthy()
})

it("a new object replaces the entire draft", () => {
const obj = {a: {b: {}}}
const next = produce({}, () => obj)
expect(next).toBe(obj)
expect(isFrozen(next)).toBeFalsy()
expect(isFrozen(next.a)).toBeFalsy()
expect(isFrozen(next.a.b)).toBeFalsy()
expect(isFrozen(next)).toBeTruthy()
expect(isFrozen(next.a)).toBeTruthy()
expect(isFrozen(next.a.b)).toBeTruthy()
})

it("a new object is added to the root draft", () => {
const base = {}
const next = produce(base, draft => {
draft.a = {}
draft.a = {b: []}
})
expect(next).not.toBe(base)
expect(isFrozen(next)).toBeTruthy()
expect(isFrozen(next.a)).toBeFalsy()
expect(isFrozen(next.a)).toBeTruthy()
expect(isFrozen(next.b)).toBeTruthy()
})

it("a new object is added to a nested draft", () => {
const base = {a: {}}
const next = produce(base, draft => {
draft.a.b = {}
draft.a.b = {c: {}}
})
expect(next).not.toBe(base)
expect(isFrozen(next)).toBeTruthy()
expect(isFrozen(next.a)).toBeTruthy()
expect(isFrozen(next.a.b)).toBeFalsy()
expect(isFrozen(next.a.b)).toBeTruthy()
expect(isFrozen(next.a.b.c)).toBeTruthy()
})
})

describe("the result is never auto-frozen when", () => {
it("the producer is a no-op", () => {
const base = {a: {}}
const next = produce(base, () => {})
expect(next).toBe(base)
expect(isFrozen(next)).toBeFalsy()
expect(isFrozen(next.a)).toBeFalsy()
})

it("the root draft is returned", () => {
const base = {a: {}}
const next = produce(base, draft => draft)
expect(next).toBe(base)
expect(isFrozen(next)).toBeFalsy()
expect(isFrozen(next.a)).toBeFalsy()
})

it("a nested draft is returned", () => {
const base = {a: {}}
const next = produce(base, draft => draft.a)
expect(next).toBe(base.a)
expect(isFrozen(next)).toBeFalsy()
})

it("the base state is returned", () => {
const base = {}
const next = produce(base, () => base)
expect(next).toBe(base)
expect(isFrozen(next)).toBeFalsy()
})
})

Expand Down

0 comments on commit 8aa4da3

Please sign in to comment.