Skip to content
Myr

flow.js.flow

This file

Path

__tests__/flow/flow.js.flow

188 lines · 3.4 KiB

Last changed by

fix: Flow: Add tests for Map and Set and fix base typeMichel Weststratebefore adoption9022672

History of this file

Take it

1// @flow2import produce, {3    enableMapSet,4    setAutoFreeze,5    setUseProxies,6    produce as produce2,7    applyPatches,8    Patch,9    original10} from "../../dist/index.js.flow"1112enableMapSet()13setAutoFreeze(true)14setUseProxies(true)1516// we really don't want this code to actually execute..17const result = produce({x: 3}, draft => {18    draft.x = 41920    // $ExpectError21    console.log(draft.y)22})2324console.log(result.x)2526const result2 = produce({x: 3}, draft => {27    return {x: 4}28})2930console.log(result2.x)3132const f = produce(33    (draft): {| x: number |} => {34        return {x: 4}35    }36)37f({x: 3})3839const f2 = produce((draft): void => {})40f2({x: 3})4142// $ExpectError43setAutoFreeze(3)4445// $ExpectError46console.log(result.y)4748// $ExpectError49produce()5051// $ExpectError52produce({x: 3})5354// $ExpectError55produce({x: 3}, [])5657{58    // curried & initial arg59    const f = produce(60        (state, increment: number): void => {61            state.x += increment62        },63        {x: 3}64    )6566    // $ExpectError Too few arguments67    f({x: 5})6869    // $ExpectError70    f({x: 5}, "test")7172    f({x: 5}, 3)7374    f(undefined, 3)75}7677{78    // curried f & no initial arg79    const f2 = produce(80        (state: {| x: number |}, increment: number): void => {81            state.x += increment82        }83    )8485    f2({x: 5}, 3)8687    // $ExpectError88    f2(undefined, 3)89}9091type State = {| user: {| age: number |} |}92{93    declare function setState(updater: (State) => $Shape<State>): void94    // curried use with setState, infer type of draft95    setState(produce(draft => {96        if (draft.user.age === 10) {97            return98        }99        draft.user.age += 1100        // $ExpectError101        draft.user.name = "missing"102    }))103}104105{106    // $ExpectError107    produce2({x: 3})108109    const result = produce2({x: 3}, draft => {110        draft.x = 4111112        // $ExpectError113        console.log(draft.y)114    })115}116117{118    let p: Patch[] = []119    produce(120        {},121        d => {},122        (patches, inverse) => {123            p = patches124        }125    )126    applyPatches({}, p)127}128129{130    produce({x: 3}, draftState => {131        let a = original(draftState)132        if (a) {133            a.x134            // $ExpectError135            a.y136        }137    })138}139140{141    const arr: Array<number> = [1]142    produce(arr, draftState => {143        const a = original(draftState)144        if (a) {145            const b: number = a[0]146            // $ExpectError147            const c: string = a[0]148        }149    })150}151152function testMap(state: Map<number, string>) {153    // normal154    state = produce(state, draft => {155        if (draft.has(1)) {156            return157        }158        // $ExpectError159        draft.set(1, 2)160        draft.set(1, "tada")161    })162    // curried, no initial state163    const f1 = produce(draft=> { draft.set(2, "ok") })164    state = f1(state)165    // curried, with initial state166    const f2 = produce(draft=> { draft.set(2, "ok") }, state)167    state = f2(state)168    state = f2(undefined)169}170171function testSet(state: Set<number>) {172    // normal173    state = produce(state, draft => {174        if (draft.has(1)) {175            return176        }177        // $ExpectError178        draft.add("me")179        draft.add(100)180    })181    // curried, no initial state182    const f1 = produce(draft => { draft.add(draft.size) })183    state = f1(state)184    // curried, with initial state185    const f2 = produce(draft => { draft.add(draft.size) }, state)186    state = f2(state)187    state = f2(undefined)188}