Skip to content
Myr

test: return types of produce()

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

authored by aleclarson · committed by aleclarson · parent 3aa1c4a · Browse files · 1 file+37 added−5 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+37 added−5 removed

  • tests:+37 added−5 removed

1 file+37 added−5 removed

modified:__tests__/produce.ts+37 added-5 removed
61 unmodified lines
62 draft.arr.push({value: "asf"})63 draft.arr2[0].value = "foo"64 draft.arr2.push({value: "asf"})65 })66 expect(newState).not.toBe(state)67 expect(newState).toEqual(expectedState)66 exactType(newState, state)67})6869// NOTE: only when the function type is inferred70it("can infer state type from default state", () => {
86 unmodified lines
157 bar([] as ReadonlyArray<any>)158 })159})160162it("always returns an immutable type", () => {163 let result = produce([] as any[], () => {})164 exactType(result, {} as ReadonlyArray<any>)161it("works with return type of: number", () => {162 let base = {} as {a: number}163 let result = produce(base, () => 1)164 exactType(result, {} as number)165})166167it("works with return type of: number | undefined", () => {168 let base = {} as {a: number}169 let result = produce(base, draft => {170 return draft.a < 0 ? 0 : undefined171 })172 exactType(result, {} as {a: number} | number)173})174175it("can return an object type that is identical to the base type", () => {176 let base = {} as {a: number}177 let result = produce(base, draft => {178 return draft.a < 0 ? {a: 0} : undefined179 })180 // TODO: Can we resolve the weird union of identical object types?181 exactType(result, {} as {a: number} | {a: number})182})183184it("can return an object type that is _not_ assignable to the base type", () => {185 let base = {} as {a: number}186 let result = produce(base, draft => {187 return draft.a < 0 ? {a: true} : undefined188 })189 exactType(result, {} as {a: number} | {a: boolean})190})191192it("does not enforce immutability at the type level", () => {193 let result = produce([] as any[], draft => {194 draft.push(1)195 })196 exactType(result, {} as any[])197})198199it("can produce nothing", () => {200 let val: undefined = produce({}, s => nothing)201})202
19 unmodified lines