Skip to content

Commit

Permalink
fix: Flow: Add tests for Map and Set and fix base type
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Jun 22, 2020
2 parents 4a1bd65 + 95c926c commit 9022672
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
42 changes: 41 additions & 1 deletion __tests__/flow/flow.js.flow
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow
import produce, {
enableMapSet,
setAutoFreeze,
setUseProxies,
produce as produce2,
Expand All @@ -8,6 +9,7 @@ import produce, {
original
} from "../../dist/index.js.flow"

enableMapSet()
setAutoFreeze(true)
setUseProxies(true)

Expand Down Expand Up @@ -136,7 +138,7 @@ type State = {| user: {| age: number |} |}
}

{
const arr: Array<number> = [1];
const arr: Array<number> = [1]
produce(arr, draftState => {
const a = original(draftState)
if (a) {
Expand All @@ -146,3 +148,41 @@ type State = {| user: {| age: number |} |}
}
})
}

function testMap(state: Map<number, string>) {
// normal
state = produce(state, draft => {
if (draft.has(1)) {
return
}
// $ExpectError
draft.set(1, 2)
draft.set(1, "tada")
})
// curried, no initial state
const f1 = produce(draft=> { draft.set(2, "ok") })
state = f1(state)
// curried, with initial state
const f2 = produce(draft=> { draft.set(2, "ok") }, state)
state = f2(state)
state = f2(undefined)
}

function testSet(state: Set<number>) {
// normal
state = produce(state, draft => {
if (draft.has(1)) {
return
}
// $ExpectError
draft.add("me")
draft.add(100)
})
// curried, no initial state
const f1 = produce(draft => { draft.add(draft.size) })
state = f1(state)
// curried, with initial state
const f2 = produce(draft => { draft.add(draft.size) }, state)
state = f2(state)
state = f2(undefined)
}
2 changes: 1 addition & 1 deletion src/types/index.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface Patch {

export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void

type Base = {...} | Array<any> | Map<any> | Set<any>
type Base = {...} | Array<any>
interface IProduce {
/**
* Immer takes a state, and runs a function against it.
Expand Down

0 comments on commit 9022672

Please sign in to comment.