Skip to content
Myr

flow.js.flow

This file

Path

__tests__/flow/flow.js.flow

187 lines · 3.4 KiB

Last changed by

fix: Move index.js.flow from dist/ -> dist/cjs/ to match index.js (#1038)Björgvin Ragnarssonbefore adoptiona3b5603

History of this file

Take it

1// @flow2import {3    produce,4    enableMapSet,5    setAutoFreeze,6    produce as produce2,7    applyPatches,8    Patch,9    original10} from "../../dist/cjs/index.js.flow"1112enableMapSet()13setAutoFreeze(true)1415// we really don't want this code to actually execute..16const result = produce({x: 3}, draft => {17    draft.x = 41819    // $ExpectError20    console.log(draft.y)21})2223console.log(result.x)2425const result2 = produce({x: 3}, draft => {26    return {x: 4}27})2829console.log(result2.x)3031const f = produce(32    (draft): {| x: number |} => {33        return {x: 4}34    }35)36f({x: 3})3738const f2 = produce((draft): void => {})39f2({x: 3})4041// $ExpectError42setAutoFreeze(3)4344// $ExpectError45console.log(result.y)4647// $ExpectError48produce()4950// $ExpectError51produce({x: 3})5253// $ExpectError54produce({x: 3}, [])5556{57    // curried & initial arg58    const f = produce(59        (state, increment: number): void => {60            state.x += increment61        },62        {x: 3}63    )6465    // $ExpectError Too few arguments66    f({x: 5})6768    // $ExpectError69    f({x: 5}, "test")7071    f({x: 5}, 3)7273    f(undefined, 3)74}7576{77    // curried f & no initial arg78    const f2 = produce(79        (state: {| x: number |}, increment: number): void => {80            state.x += increment81        }82    )8384    f2({x: 5}, 3)8586    // $ExpectError87    f2(undefined, 3)88}8990type State = {| user: {| age: number |} |}91{92    declare function setState(updater: (State) => $Shape<State>): void93    // curried use with setState, infer type of draft94    setState(produce(draft => {95        if (draft.user.age === 10) {96            return97        }98        draft.user.age += 199        // $ExpectError100        draft.user.name = "missing"101    }))102}103104{105    // $ExpectError106    produce2({x: 3})107108    const result = produce2({x: 3}, draft => {109        draft.x = 4110111        // $ExpectError112        console.log(draft.y)113    })114}115116{117    let p: Patch[] = []118    produce(119        {},120        d => {},121        (patches, inverse) => {122            p = patches123        }124    )125    applyPatches({}, p)126}127128{129    produce({x: 3}, draftState => {130        let a = original(draftState)131        if (a) {132            a.x133            // $ExpectError134            a.y135        }136    })137}138139{140    const arr: Array<number> = [1]141    produce(arr, draftState => {142        const a = original(draftState)143        if (a) {144            const b: number = a[0]145            // $ExpectError146            const c: string = a[0]147        }148    })149}150151function testMap(state: Map<number, string>) {152    // normal153    state = produce(state, draft => {154        if (draft.has(1)) {155            return156        }157        // $ExpectError158        draft.set(1, 2)159        draft.set(1, "tada")160    })161    // curried, no initial state162    const f1 = produce(draft=> { draft.set(2, "ok") })163    state = f1(state)164    // curried, with initial state165    const f2 = produce(draft=> { draft.set(2, "ok") }, state)166    state = f2(state)167    state = f2(undefined)168}169170function testSet(state: Set<number>) {171    // normal172    state = produce(state, draft => {173        if (draft.has(1)) {174            return175        }176        // $ExpectError177        draft.add("me")178        draft.add(100)179    })180    // curried, no initial state181    const f1 = produce(draft => { draft.add(draft.size) })182    state = f1(state)183    // curried, with initial state184    const f2 = produce(draft => { draft.add(draft.size) }, state)185    state = f2(state)186    state = f2(undefined)187}