Skip to content
Myr

flow.js.flow

This file

Path

__tests__/flow/flow.js.flow

193 lines · 2.9 KiB

Last changed by

chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f

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((draft): {|x: number|} => {32	return {x: 4}33})34f({x: 3})3536const f2 = produce((draft): void => {})37f2({x: 3})3839// $ExpectError40setAutoFreeze(3)4142// $ExpectError43console.log(result.y)4445// $ExpectError46produce()4748// $ExpectError49produce({x: 3})5051// $ExpectError52produce({x: 3}, [])5354{55	// curried & initial arg56	const f = produce(57		(state, increment: number): void => {58			state.x += increment59		},60		{x: 3}61	)6263	// $ExpectError Too few arguments64	f({x: 5})6566	// $ExpectError67	f({x: 5}, "test")6869	f({x: 5}, 3)7071	f(undefined, 3)72}7374{75	// curried f & no initial arg76	const f2 = produce((state: {|x: number|}, increment: number): void => {77		state.x += increment78	})7980	f2({x: 5}, 3)8182	// $ExpectError83	f2(undefined, 3)84}8586type State = {|user: {|age: number|}|}87{88	declare function setState(updater: (State) => $Shape<State>): void89	// curried use with setState, infer type of draft90	setState(91		produce(draft => {92			if (draft.user.age === 10) {93				return94			}95			draft.user.age += 196			// $ExpectError97			draft.user.name = "missing"98		})99	)100}101102{103	// $ExpectError104	produce2({x: 3})105106	const result = produce2({x: 3}, draft => {107		draft.x = 4108109		// $ExpectError110		console.log(draft.y)111	})112}113114{115	let p: Patch[] = []116	produce(117		{},118		d => {},119		(patches, inverse) => {120			p = patches121		}122	)123	applyPatches({}, p)124}125126{127	produce({x: 3}, draftState => {128		let a = original(draftState)129		if (a) {130			a.x131			// $ExpectError132			a.y133		}134	})135}136137{138	const arr: Array<number> = [1]139	produce(arr, draftState => {140		const a = original(draftState)141		if (a) {142			const b: number = a[0]143			// $ExpectError144			const c: string = a[0]145		}146	})147}148149function testMap(state: Map<number, string>) {150	// normal151	state = produce(state, draft => {152		if (draft.has(1)) {153			return154		}155		// $ExpectError156		draft.set(1, 2)157		draft.set(1, "tada")158	})159	// curried, no initial state160	const f1 = produce(draft => {161		draft.set(2, "ok")162	})163	state = f1(state)164	// curried, with initial state165	const f2 = produce(draft => {166		draft.set(2, "ok")167	}, state)168	state = f2(state)169	state = f2(undefined)170}171172function testSet(state: Set<number>) {173	// normal174	state = produce(state, draft => {175		if (draft.has(1)) {176			return177		}178		// $ExpectError179		draft.add("me")180		draft.add(100)181	})182	// curried, no initial state183	const f1 = produce(draft => {184		draft.add(draft.size)185	})186	state = f1(state)187	// curried, with initial state188	const f2 = produce(draft => {189		draft.add(draft.size)190	}, state)191	state = f2(state)192	state = f2(undefined)193}