Skip to content

Commit

Permalink
test: return types of produce()
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Jan 14, 2019
1 parent 3aa1c4a commit 883a258
Showing 1 changed file with 37 additions and 5 deletions.
42 changes: 37 additions & 5 deletions __tests__/produce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ it("can update readonly state via standard api", () => {
draft.arr2[0].value = "foo"
draft.arr2.push({value: "asf"})
})
expect(newState).not.toBe(state)
expect(newState).toEqual(expectedState)
exactType(newState, state)
})

// NOTE: only when the function type is inferred
Expand Down Expand Up @@ -159,9 +158,42 @@ describe("curried producer", () => {
})
})

it("always returns an immutable type", () => {
let result = produce([] as any[], () => {})
exactType(result, {} as ReadonlyArray<any>)
it("works with return type of: number", () => {
let base = {} as {a: number}
let result = produce(base, () => 1)
exactType(result, {} as number)
})

it("works with return type of: number | undefined", () => {
let base = {} as {a: number}
let result = produce(base, draft => {
return draft.a < 0 ? 0 : undefined
})
exactType(result, {} as {a: number} | number)
})

it("can return an object type that is identical to the base type", () => {
let base = {} as {a: number}
let result = produce(base, draft => {
return draft.a < 0 ? {a: 0} : undefined
})
// TODO: Can we resolve the weird union of identical object types?
exactType(result, {} as {a: number} | {a: number})
})

it("can return an object type that is _not_ assignable to the base type", () => {
let base = {} as {a: number}
let result = produce(base, draft => {
return draft.a < 0 ? {a: true} : undefined
})
exactType(result, {} as {a: number} | {a: boolean})
})

it("does not enforce immutability at the type level", () => {
let result = produce([] as any[], draft => {
draft.push(1)
})
exactType(result, {} as any[])
})

it("can produce nothing", () => {
Expand Down

0 comments on commit 883a258

Please sign in to comment.