refactor(ts): simplify IProduce type tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Alec Larson · committed by Alec Larson · parent 1282f04 · Browse files · 2 files·+37 added−97 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+37 added−97 removed
- tests:+37 added−97 removed
Provenance
- Commit
bce282d- Tree
3ca0ca5- Author
Alec Larson <alec.stanford.larson@gmail.com>- Committer
Alec Larson <alec.stanford.larson@gmail.com>- Parent
1282f04
2 files changed+37 added−97 removed
modified:__tests__/base.js+1 added-1 removed
783 unmodified lines784 })785 incrementor()786 })787 788- it("it should be possible to use dynamic bound this", () => {788+ it("should be possible to use dynamic bound this", () => {789 const world = {790 counter: {count: 1},791 inc: produce(function(draft) {792 expect(this).toBe(world)482 unmodified linesmodified:__tests__/produce.ts+36 added-96 removed
69 unmodified lines70 71 // NOTE: only when the function type is inferred72 it("can infer state type from default state", () => {73 type State = {readonly a: number}74- type Recipe = (base?: State | boolean) => State74+ type Recipe = <S extends State>(state?: S | undefined) => S75 76- let foo = produce((x: any) => {}, {} as State)77-78- type Arg0 = Parameters<typeof foo>[0]79- type L = Parameters<typeof foo>["length"]80- type Return = ReturnType<typeof foo>81- exactType({} as Arg0, {} as (State | undefined))82- exactType(({} as any) as L, 0 as (0 | 1))83- exactType({} as Return, {} as State)76+ let foo = produce((_: any) => {}, {} as State)77+ exactType(foo, {} as Recipe)78 })79 80 it("can infer state type from recipe function", () => {81 type State = {readonly a: string} | {readonly b: string}82+ type Recipe = <S extends State>(state: S) => S83 89- let foo = produce((draft: Draft<State>) => {})90-91- type Arg0 = Parameters<typeof foo>[0]92- type L = Parameters<typeof foo>["length"]93- type Return = ReturnType<typeof foo>94- exactType({} as Arg0, {} as State)95- exactType(({} as any) as L, 1 as const)96- exactType({} as Return, {} as State)84+ let foo = produce((_: Draft<State>) => {})85+ exactType(foo, {} as Recipe)86 })87 88 it("can infer state type from recipe function with arguments", () => {89 type State = {readonly a: string} | {readonly b: string}101- type Recipe = (base: State, x: number) => State90+ type Recipe = <S extends State>(state: S, x: number) => S91 92 let foo = produce((draft: Draft<State>, x: number) => {})104-105- type Arg0 = Parameters<typeof foo>[0]106- type Arg1 = Parameters<typeof foo>[1]107- type L = Parameters<typeof foo>["length"]108- type Return = ReturnType<typeof foo>109- exactType({} as Arg0, {} as State)110- exactType({} as Arg1, {} as number)111- exactType(({} as any) as L, 2 as const)112- exactType({} as Return, {} as State)93+ exactType(foo, {} as Recipe)94 })95 96 it("can infer state type from recipe function with arguments and initial state", () => {97 type State = {readonly a: string} | {readonly b: string}98+ type Recipe = <S extends State>(state: S | undefined, x: number) => S99 100 let foo = produce((draft: Draft<State>, x: number) => {}, {} as State)119-120- type Arg0 = Parameters<typeof foo>[0]121- type Arg1 = Parameters<typeof foo>[1]122- type L = Parameters<typeof foo>["length"]123- type Return = ReturnType<typeof foo>124- exactType({} as Arg0, {} as (State | undefined))125- exactType({} as Arg1, {} as number)126- exactType(({} as any) as L, 2 as const)127- exactType({} as Return, {} as State)101+ exactType(foo, {} as Recipe)102 })103 104 it("cannot infer state type when the function type and default state are missing", () => {105+ type Recipe = <S extends any>(state: S) => S106 const foo = produce((_: any) => {})132-133- type Arg0 = Parameters<typeof foo>[0]134- type L = Parameters<typeof foo>["length"]135- type Return = ReturnType<typeof foo>136- exactType({} as Arg0, {} as any)137- exactType(({} as any) as L, 1 as const)138- exactType({} as Return, {} as any)107+ exactType(foo, {} as Recipe)108 })109 110 it("can update readonly state via curried api", () => {111 const newState = produce((draft: Draft<State>) => {45 unmodified lines157 describe("curried producer", () => {158 it("supports rest parameters", () => {159 type State = {readonly a: 1}160 161+ // No initial state:162 {193- // No initial state:163+ type Recipe = <S extends State>(state: S, a: number, b: number) => S164 let foo = produce((s: State, a: number, b: number) => {})195-196- type Arg0 = Parameters<typeof foo>[0]197- type Arg1 = Parameters<typeof foo>[1]198- type L = Parameters<typeof foo>["length"]199- type Return = ReturnType<typeof foo>200- exactType({} as Arg0, {} as (State | undefined))201- exactType({} as Arg1, {} as number)202- exactType(({} as any) as L, 3 as const)203- exactType({} as Return, {} as State)204-165+ exactType(foo, {} as Recipe)166 foo({} as State, 1, 2)167 }168 169+ // Using argument parameters:170 {209- // Using argument parameters171+ type Recipe = <S extends State>(state: S, ...rest: number[]) => S172 let woo = produce((state: Draft<State>, ...args: number[]) => {})173+ exactType(woo, {} as Recipe)174 woo({} as State, 1, 2)212-213- type Args = Parameters<typeof woo>214- type L = Parameters<typeof woo>["length"]215- type Return = ReturnType<typeof woo>216- exactType({} as Args, {} as [State, ...number[]])217- exactType(({} as any) as L, 3 as number)218- exactType({} as Return, {} as State)175 }176 177+ // With initial state:178 {222- // With initial state:179+ type Recipe = <S extends State>(180+ state?: S | undefined,181+ ...rest: number[]182+ ) => S183 let bar = produce(184 (state: Draft<State>, ...args: number[]) => {},185 {} as State186 )187+ exactType(bar, {} as Recipe)188 bar({} as State, 1, 2)189 bar({} as State)190 bar()230-231- type Args = Parameters<typeof bar>232- type L = Parameters<typeof bar>["length"]233- type Return = ReturnType<typeof bar>234- exactType({} as Args, {} as [State?, ...number[]])235- exactType(({} as any) as L, 3 as number)236- exactType({} as Return, {} as State)191 }192 193+ // When args is a tuple:194 {240- // When args is a tuple:195+ type Recipe = <S extends State>(196+ state: S | undefined,197+ first: string,198+ ...rest: number[]199+ ) => S200 let tup = produce(201 (state: Draft<State>, ...args: [string, ...number[]]) => {},202 {} as State203 )204+ exactType(tup, {} as Recipe)205 tup({a: 1}, "", 2)206 tup(undefined, "", 2)247-248- type Args = Parameters<typeof tup>249- type L = Parameters<typeof tup>["length"]250- type Return = ReturnType<typeof tup>251- exactType(252- {} as Args,253- {} as [State | undefined, string, ...number[]]254- )255- exactType(({} as any) as L, 3 as number)256- exactType({} as Return, {} as State)207 }208 })209 210 it("can be passed a readonly array", () => {211 // No initial state:212 {213 let foo = produce((state: string[]) => {})214+ exactType(foo, {} as <S extends readonly string[]>(state: S) => S)215 foo([] as ReadonlyArray<string>)265-266- type Args = Parameters<typeof foo>267- type L = Parameters<typeof foo>["length"]268- type Return = ReturnType<typeof foo>269- exactType({} as Args, {} as [readonly string[]])270- exactType(({} as any) as L, 3 as 1)271- exactType({} as Return, {} as readonly string[])216 }217 218+ // With initial state:219 {275- // With initial state:220 let bar = produce(() => {}, [] as ReadonlyArray<string>)221+ exactType(bar, {} as <S extends readonly string[]>(222+ state?: S | undefined223+ ) => S)224 bar([] as ReadonlyArray<string>)225 bar(undefined)226 bar()280-281- type Args = Parameters<typeof bar>282- type L = Parameters<typeof bar>["length"]283- type Return = ReturnType<typeof bar>284- exactType({} as Args, {} as [((readonly string[]) | undefined)?])285- exactType(({} as any) as L, 3 as (0 | 1))286- exactType({} as Return, {} as readonly string[])227 }228 })229 })230 183 unmodified lines