test(ts): some more test cases
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Michel Weststrate · committed by Alec Larson · parent acf8a57 · Browse files · 1 file·+72 added−2 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+72 added−2 removed
- tests:+72 added−2 removed
Provenance
- Commit
6624aa8- Tree
9ca8fc8- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Alec Larson <alec.stanford.larson@gmail.com>- Parent
acf8a57
1 file changed+72 added−2 removed
modified:__tests__/produce.ts+72 added-2 removed
77 unmodified lines78 })79 80 it("can infer state type from recipe function", () => {81 type State = {readonly a: string} | {readonly b: string}82- type Recipe = (base: State) => State82+ type Recipe = (base: State, n: number) => State83 84- let foo = produce((draft: Draft<State>) => {})84+ let foo = produce((draft: Draft<State>, number: number) => {})85 exactType(foo, {} as Recipe)86+ const state: State = foo({} as State, 3)87 })88 89 it("can infer state type from recipe function with arguments", () => {90 type State = {readonly a: string} | {readonly b: string}218 unmodified lines309 let val: {readonly a: ReadonlyArray<number>} = 0 as any310 let arr: ReadonlyArray<typeof val> = 0 as any311 insert(arr, 0, val)312 })313+314+it("can work with non-readonly base types", () => {315+ const state = {316+ price: 10,317+ todos: [318+ {319+ title: "test",320+ done: false321+ }322+ ]323+ }324+ type State = typeof state325+326+ const newState: State = produce(state, draft => {327+ draft.price += 5328+ draft.todos.push({329+ title: "hi",330+ done: true331+ })332+ })333+334+ const reducer = (draft: State) => {335+ draft.price += 5336+ draft.todos.push({337+ title: "hi",338+ done: true339+ })340+ }341+ const newState2: Immutable<State> = produce(reducer)(state)342+ const newState3: Immutable<State> = produce(reducer, state)()343+})344+345+it("can work with readonly base types", () => {346+ type State = {347+ readonly price: number348+ readonly todos: readonly {349+ readonly title: string350+ readonly done: boolean351+ }[]352+ }353+354+ const state: State = {355+ price: 10,356+ todos: [357+ {358+ title: "test",359+ done: false360+ }361+ ]362+ }363+364+ const newState: State = produce(state, draft => {365+ draft.price + 5366+ draft.todos.push({367+ title: "hi",368+ done: true369+ })370+ })371+372+ const reducer = (draft: Draft<State>) => {373+ draft.price += 5374+ draft.todos.push({375+ title: "hi",376+ done: true377+ })378+ }379+ const newState2: State = produce(reducer)(state)380+ const newState3: State = produce(reducer, state)()381+})