Skip to content
Myr

draft.ts

This file

Path

__tests__/draft.ts

344 lines · 6.8 KiB · TypeScript

Last changed by

New build set. Current statsMichel Weststratebefore adoption9aec0fe

History of this file

Take it

The walk stopped after 200 commits: lines older than 21846ab point at it.

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

line 1why, one step back
1import {assert, _} from "spec.ts"

New build set. Current statsMichel Weststratebefore adoption9aec0fe

lines 2–9why, one step back
2import produce, {
3 Draft,4 castDraft,5 original,6 enableAllPlugins7} from "../src/immer"89enableAllPlugins()

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 10–11why, one step back
10
11// For checking if a type is assignable to its draft type (and vice versa)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 12–13why, one step back
12const toDraft: <T>(value: T) => Draft<T> = x => x as any
13const fromDraft: <T>(draft: Draft<T>) => T = x => x as any

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 14why, one step back
14

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 15–22why, one step back
15test("draft.ts", () => {
16 // DraftArray<T>17 {18 // NOTE: As of 3.2.2, everything fails without "extends any"19 ;<Value extends any>(val: ReadonlyArray<Value>) => {20 val = _ as Draft<typeof val>21 let elem: Value = _ as Draft<Value>22 }

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 23why, one step back
23 }

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 24why, one step back
24

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 25–30why, one step back
25 // Tuple
26 {27 let val: [1, 2] = _28 assert(toDraft(val), val)29 assert(fromDraft(toDraft(val)), val)30 }

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 31why, one step back
31

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 32–37why, one step back
32 // Tuple (nested in a tuple)
33 {34 let val: [[1, 2]] = _35 assert(toDraft(val), val)36 assert(fromDraft(toDraft(val)), val)37 }

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 38why, one step back
38

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 39–45why, one step back
39 // Tuple (nested in two mutable arrays)
40 {41 let val: [1, 2][][] = _42 let draft: typeof val = _43 val = assert(toDraft(val), draft)44 assert(fromDraft(draft), val)45 }

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 46why, one step back
46

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 47–52why, one step back
47 // Tuple (nested in two readonly arrays)
48 {49 let val: ReadonlyArray<ReadonlyArray<[1, 2]>> = _50 let draft: [1, 2][][] = _51 val = assert(toDraft(val), draft)52 }

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 53why, one step back
53

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 54–62why, one step back
54 // Readonly tuple
55 {56 // TODO: Uncomment this when readonly tuples are supported.57 // More info: https://stackoverflow.com/a/53822074/222855958 // let val: Readonly<[1, 2]> = _59 // let draft: [1, 2] = _60 // draft = assert(toDraft(val), draft)61 // val = fromDraft(draft)62 }

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 63why, one step back
63

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 64–69why, one step back
64 // Mutable array
65 {66 let val: string[] = _67 assert(toDraft(val), val)68 assert(fromDraft(toDraft(val)), val)69 }

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 70why, one step back
70

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 71–76why, one step back
71 // Mutable array (nested in tuple)
72 {73 let val: [string[]] = _74 assert(toDraft(val), val)75 assert(fromDraft(toDraft(val)), val)76 }

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 77why, one step back
77

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 78–84why, one step back
78 // Readonly array
79 {80 let val: ReadonlyArray<string> = _81 let draft: string[] = _82 val = assert(toDraft(val), draft)83 fromDraft(draft)84 }

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 85why, one step back
85

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 86–92why, one step back
86 // Readonly array (nested in readonly object)
87 {88 let val: {readonly a: ReadonlyArray<string>} = _89 let draft: {a: string[]} = _90 val = assert(toDraft(val), draft)91 fromDraft(draft)92 }

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 93why, one step back
93

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 94–99why, one step back
94 // Mutable object
95 {96 let val: {a: 1} = _97 assert(toDraft(val), val)98 assert(fromDraft(toDraft(val)), val)99 }

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 100why, one step back
100

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 101–106why, one step back
101 // Mutable object (nested in mutable object)
102 {103 let val: {a: {b: 1}} = _104 assert(toDraft(val), val)105 assert(fromDraft(toDraft(val)), val)106 }

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 107why, one step back
107

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 108–115why, one step back
108 // Interface
109 {110 interface Foo {111 a: {b: number}112 }113 let val: Foo = _114 assert(toDraft(val), val)115 assert(fromDraft(toDraft(val)), val)

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 116why, one step back
116 }

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 117why, one step back
117

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 118–128why, one step back
118 // Interface (nested in interface)
119 {120 interface Foo {121 a: {b: number}122 }123 interface Bar {124 foo: Foo125 }126 let val: Bar = _127 assert(toDraft(val), val)128 assert(fromDraft(toDraft(val)), val)

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 129why, one step back
129 }

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 130–135why, one step back
130
131 // Readonly object132 {133 let val: {readonly a: 1} = _134 let draft: {a: 1} = _135 val = assert(toDraft(val), draft)

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 136why, one step back
136 }

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 137why, one step back
137

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 138–143why, one step back
138 // Readonly object (nested in tuple)
139 {140 let val: [{readonly a: 1}] = _141 let draft: [{a: 1}] = _142 val = assert(toDraft(val), draft)143 }

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 144why, one step back
144

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 145–150why, one step back
145 // Loose function
146 {147 let val: Function = _148 assert(toDraft(val), val)149 assert(fromDraft(toDraft(val)), val)150 }

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 151why, one step back
151

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 152–157why, one step back
152 // Strict function
153 {154 let val: () => void = _155 assert(toDraft(val), val)156 assert(fromDraft(toDraft(val)), val)157 }

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 158why, one step back
158

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 159–169why, one step back
159 // Class type (mutable)
160 {161 class Foo {162 private test: any163 constructor(public bar: string) {}164 }165 let val: Foo = _166 // TODO: Uncomment this when plain object types can be distinguished from class types.167 // More info here: https://github.com/Microsoft/TypeScript/issues/29063168 // assert(toDraft(val), val)169 // assert(fromDraft(toDraft(val)), val)

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 170why, one step back
170 }

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 171why, one step back
171

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 172–182why, one step back
172 // Class type (readonly)
173 {174 class Foo {175 private test: any176 constructor(readonly bar: string) {}177 }178 let val: Foo = _179 // TODO: Uncomment this when plain object types can be distinguished from class types.180 // More info here: https://github.com/Microsoft/TypeScript/issues/29063181 // assert(toDraft(val), val)182 // assert(fromDraft(toDraft(val)), val)

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 183why, one step back
183 }

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 184why, one step back
184

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 185–189why, one step back
185 // Map instance
186 {187 let val: Map<any, any> = _188 assert(toDraft(val), val)189 assert(fromDraft(toDraft(val)), val)

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 190why, one step back
190

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 191–195why, one step back
191 // Weak maps
192 let weak: WeakMap<any, any> = _193 assert(toDraft(weak), weak)194 assert(fromDraft(toDraft(weak)), weak)195 }

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 196why, one step back
196

feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1

lines 197–203why, one step back
197 // ReadonlyMap instance
198 {199 let val: ReadonlyMap<any, any> = _200 let draft: Map<any, any> = _201 assert(toDraft(val), draft)202 }203

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 204–208why, one step back
204 // Set instance
205 {206 let val: Set<any> = _207 assert(toDraft(val), val)208 assert(fromDraft(toDraft(val)), val)

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 209why, one step back
209

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 210–214why, one step back
210 // Weak sets
211 let weak: WeakSet<any> = _212 assert(toDraft(weak), weak)213 assert(fromDraft(toDraft(weak)), weak)214 }

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 215why, one step back
215

feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1

lines 216–222why, one step back
216 // ReadonlySet instance
217 {218 let val: ReadonlySet<any> = _219 let draft: Set<any> = _220 assert(toDraft(val), draft)221 }222

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 223–228why, one step back
223 // Promise object
224 {225 let val: Promise<any> = _226 assert(toDraft(val), val)227 assert(fromDraft(toDraft(val)), val)228 }

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 229why, one step back
229

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 230–235why, one step back
230 // Date instance
231 {232 let val: Date = _233 assert(toDraft(val), val)234 assert(fromDraft(toDraft(val)), val)235 }

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 236why, one step back
236

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 237–242why, one step back
237 // RegExp instance
238 {239 let val: RegExp = _240 assert(toDraft(val), val)241 assert(fromDraft(toDraft(val)), val)242 }

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 243why, one step back
243

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 244–249why, one step back
244 // Boxed primitive
245 {246 let val: Boolean = _247 assert(toDraft(val), val)248 assert(fromDraft(toDraft(val)), val)249 }

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 250why, one step back
250

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 251–256why, one step back
251 // String literal
252 {253 let val: string = _254 assert(toDraft(val), val)255 assert(fromDraft(toDraft(val)), val)256 }

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 257why, one step back
257

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 258–263why, one step back
258 // Any
259 {260 let val: any = _261 assert(toDraft(val), val)262 assert(fromDraft(toDraft(val)), val)263 }

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 264why, one step back
264

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 265–270why, one step back
265 // Never
266 {267 let val: never = _ as never268 assert(toDraft(val), val)269 assert(fromDraft(toDraft(val)), val)270 }

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 271why, one step back
271

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 272–277why, one step back
272 // Unknown
273 {274 let val: unknown = _275 assert(toDraft(val), val)276 assert(fromDraft(toDraft(val)), val)277 }

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 278why, one step back
278

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 279–284why, one step back
279 // Numeral
280 {281 let val: 1 = _282 assert(toDraft(val), val)283 assert(fromDraft(toDraft(val)), val)284 }

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 285why, one step back
285

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 286–291why, one step back
286 // Union of numerals
287 {288 let val: 1 | 2 | 3 = _289 assert(toDraft(val), val)290 assert(fromDraft(toDraft(val)), val)291 }

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 292why, one step back
292

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 293–296why, one step back
293 // Union of tuple, array, object
294 {295 let val: [0] | ReadonlyArray<string> | Readonly<{a: 1}> = _296 let draft: [0] | string[] | {a: 1} = _

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 297–298why, one step back
297 val = assert(toDraft(val), draft)
298 }

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 299–311why, one step back
299
300 // Generic type301 {302 // NOTE: "extends any" only helps a little.303 const $ = <T extends any>(val: ReadonlyArray<T>) => {304 let draft: Draft<typeof val> = _305 val = assert(toDraft(val), draft)306 // $ExpectError: [ts] Argument of type 'DraftArray<T>' is not assignable to parameter of type 'Draft<T>'. [2345]307 // assert(fromDraft(draft), draft)308 }309 }310311 expect(true).toBe(true)

feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1

lines 312–313why, one step back
312})
313

Merge pull request #531 from denis-sokolov/as-draft-namingMichel Weststratebefore adoptionc7e9de8

line 314why, one step back
314test("castDraft", () => {

feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1

lines 315–335why, one step back
315 type Todo = {readonly done: boolean}
316317 type State = {318 readonly finishedTodos: ReadonlyArray<Todo>319 readonly unfinishedTodos: ReadonlyArray<Todo>320 }321322 function markAllFinished(state: State) {323 produce(state, draft => {324 draft.finishedTodos = castDraft(state.unfinishedTodos)325 })326 }327})328329test("#505 original", () => {330 const baseState = {users: [{name: "Richie"}] as const}331 const nextState = produce(baseState, draftState => {332 original(draftState.users) === baseState.users333 })334})335

Merge pull request #531 from denis-sokolov/as-draft-namingMichel Weststratebefore adoptionc7e9de8

line 336why, one step back
336test("castDraft preserves a value", () => {

feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1

lines 337–338why, one step back
337 const x = {}
338 expect(castDraft(x)).toBe(x)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 339why, one step back
339})

fix: createDraft should return `Draft` instead of `Drafted`. Fixes #512Michel Weststratebefore adoption3f15d49

lines 340–344why, one step back
340
341test("#512 createDraft creates a draft", () => {342 const x = {y: 1}343 assert(x, _ as Draft<{y: number}>)344})