Skip to content
Myr

draft.ts

This file

Path

__tests__/draft.ts

287 lines · 6.0 KiB · TypeScript

Last changed by

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

History of this file

Take it

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

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

refactor(ts): Simpler draft / immutable implementationMichel Weststratebefore adoption7e971b6

line 2why, one step back
2import {Draft} from "../dist/immer.js"

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 3–7why, one step back
3
4// For checking if a type is assignable to its draft type (and vice versa)5declare const toDraft: <T>(value: T) => Draft<T>6declare const fromDraft: <T>(draft: Draft<T>) => T7

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 8–10why, one step back
8// DraftArray<T>
9{10 // NOTE: As of 3.2.2, everything fails without "extends any"

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

line 11why, one step back
11 ;<Value extends any>(val: ReadonlyArray<Value>) => {

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 12–16why, one step back
12 val = _ as Draft<typeof val>
13 let elem: Value = _ as Draft<Value>14 }15}16

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 17–19why, one step back
17// Tuple
18{19 let val: [1, 2] = _

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

lines 20–21why, one step back
20 assert(toDraft(val), val)
21 assert(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 22–26why, one step back
22}
2324// Tuple (nested in a tuple)25{26 let val: [[1, 2]] = _

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

lines 27–28why, one step back
27 assert(toDraft(val), val)
28 assert(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 29–30why, one step back
29}
30

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 31–34why, one step back
31// Tuple (nested in two mutable arrays)
32{33 let val: [1, 2][][] = _34 let draft: typeof val = _

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

lines 35–36why, one step back
35 val = assert(toDraft(val), draft)
36 assert(fromDraft(draft), val)

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 37–38why, one step back
37}
38

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 39–42why, one step back
39// Tuple (nested in two readonly arrays)
40{41 let val: ReadonlyArray<ReadonlyArray<[1, 2]>> = _42 let draft: [1, 2][][] = _

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

line 43why, one step back
43 val = assert(toDraft(val), draft)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 44–45why, one step back
44}
45

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 46–51why, one step back
46// Readonly tuple
47{48 // TODO: Uncomment this when readonly tuples are supported.49 // More info: https://stackoverflow.com/a/53822074/222855950 // let val: Readonly<[1, 2]> = _51 // let draft: [1, 2] = _

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

line 52why, one step back
52 // draft = assert(toDraft(val), draft)

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 53–55why, one step back
53 // val = fromDraft(draft)
54}55

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 56–58why, one step back
56// Mutable array
57{58 let val: string[] = _

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

lines 59–60why, one step back
59 assert(toDraft(val), val)
60 assert(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 61–65why, one step back
61}
6263// Mutable array (nested in tuple)64{65 let val: [string[]] = _

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

lines 66–67why, one step back
66 assert(toDraft(val), val)
67 assert(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 68–73why, one step back
68}
6970// Readonly array71{72 let val: ReadonlyArray<string> = _73 let draft: string[] = _

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

line 74why, one step back
74 val = assert(toDraft(val), draft)

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

line 75why, one step back
75 fromDraft(draft)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 76–81why, one step back
76}
7778// Readonly array (nested in readonly object)79{80 let val: {readonly a: ReadonlyArray<string>} = _81 let draft: {a: string[]} = _

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

line 82why, one step back
82 val = assert(toDraft(val), draft)

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

line 83why, one step back
83 fromDraft(draft)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 84–88why, one step back
84}
8586// Mutable object87{88 let val: {a: 1} = _

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

lines 89–90why, one step back
89 assert(toDraft(val), val)
90 assert(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 91–95why, one step back
91}
9293// Mutable object (nested in mutable object)94{95 let val: {a: {b: 1}} = _

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

lines 96–97why, one step back
96 assert(toDraft(val), val)
97 assert(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 98–105why, one step back
98}
99100// Interface101{102 interface Foo {103 a: {b: number}104 }105 let val: Foo = _

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

lines 106–107why, one step back
106 assert(toDraft(val), val)
107 assert(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 108–118why, one step back
108}
109110// Interface (nested in interface)111{112 interface Foo {113 a: {b: number}114 }115 interface Bar {116 foo: Foo117 }118 let val: Bar = _

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

lines 119–120why, one step back
119 assert(toDraft(val), val)
120 assert(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 121–124why, one step back
121}
122123// Readonly object124{

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

line 125why, one step back
125 let val: {readonly a: 1} = _

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

line 126why, one step back
126 let draft: {a: 1} = _

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

line 127why, one step back
127 val = assert(toDraft(val), draft)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 128–131why, one step back
128}
129130// Readonly object (nested in tuple)131{

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 132–133why, one step back
132 let val: [{readonly a: 1}] = _
133 let draft: [{a: 1}] = _

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

line 134why, one step back
134 val = assert(toDraft(val), draft)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 135–139why, one step back
135}
136137// Loose function138{139 let val: Function = _

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

lines 140–141why, one step back
140 assert(toDraft(val), val)
141 assert(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 142–146why, one step back
142}
143144// Strict function145{146 let val: () => void = _

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

lines 147–148why, one step back
147 assert(toDraft(val), val)
148 assert(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 149–150why, one step back
149}
150

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 151–159why, one step back
151// Class type (mutable)
152{153 class Foo {154 private test: any155 constructor(public bar: string) {}156 }157 let val: Foo = _158 // TODO: Uncomment this when plain object types can be distinguished from class types.159 // More info here: https://github.com/Microsoft/TypeScript/issues/29063

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

lines 160–161why, one step back
160 // assert(toDraft(val), val)
161 // assert(fromDraft(toDraft(val)), val)

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 162–172why, one step back
162}
163164// Class type (readonly)165{166 class Foo {167 private test: any168 constructor(readonly bar: string) {}169 }170 let val: Foo = _171 // TODO: Uncomment this when plain object types can be distinguished from class types.172 // More info here: https://github.com/Microsoft/TypeScript/issues/29063

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

lines 173–174why, one step back
173 // assert(toDraft(val), val)
174 // assert(fromDraft(toDraft(val)), val)

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 175–176why, one step back
175}
176

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 177–179why, one step back
177// Map instance
178{179 let val: Map<any, any> = _

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

lines 180–181why, one step back
180 assert(toDraft(val), val)
181 assert(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 182–184why, one step back
182
183 // Weak maps184 let weak: WeakMap<any, any> = _

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

lines 185–186why, one step back
185 assert(toDraft(weak), weak)
186 assert(fromDraft(toDraft(weak)), weak)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 187–191why, one step back
187}
188189// Set instance190{191 let val: Set<any> = _

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

lines 192–193why, one step back
192 assert(toDraft(val), val)
193 assert(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 194–196why, one step back
194
195 // Weak sets196 let weak: WeakSet<any> = _

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

lines 197–198why, one step back
197 assert(toDraft(weak), weak)
198 assert(fromDraft(toDraft(weak)), weak)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 199–203why, one step back
199}
200201// Promise object202{203 let val: Promise<any> = _

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

lines 204–205why, one step back
204 assert(toDraft(val), val)
205 assert(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 206–210why, one step back
206}
207208// Date instance209{210 let val: Date = _

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

lines 211–212why, one step back
211 assert(toDraft(val), val)
212 assert(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 213–217why, one step back
213}
214215// RegExp instance216{217 let val: RegExp = _

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

lines 218–219why, one step back
218 assert(toDraft(val), val)
219 assert(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 220–224why, one step back
220}
221222// Boxed primitive223{224 let val: Boolean = _

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

lines 225–226why, one step back
225 assert(toDraft(val), val)
226 assert(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 227–231why, one step back
227}
228229// String literal230{231 let val: string = _

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

lines 232–233why, one step back
232 assert(toDraft(val), val)
233 assert(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 234–238why, one step back
234}
235236// Any237{238 let val: any = _

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

lines 239–240why, one step back
239 assert(toDraft(val), val)
240 assert(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 241–245why, one step back
241}
242243// Never244{245 let val: never = _ as never

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

lines 246–247why, one step back
246 assert(toDraft(val), val)
247 assert(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 248–252why, one step back
248}
249250// Unknown251{252 let val: unknown = _

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

lines 253–254why, one step back
253 assert(toDraft(val), val)
254 assert(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 255–259why, one step back
255}
256257// Numeral258{259 let val: 1 = _

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

lines 260–261why, one step back
260 assert(toDraft(val), val)
261 assert(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 262–266why, one step back
262}
263264// Union of numerals265{266 let val: 1 | 2 | 3 = _

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

lines 267–268why, one step back
267 assert(toDraft(val), val)
268 assert(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 269–273why, one step back
269}
270271// Union of tuple, array, object272{273 let val: [0] | ReadonlyArray<string> | Readonly<{a: 1}> = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

line 274why, one step back
274 let draft: [0] | string[] | {a: 1} = _

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

line 275why, one step back
275 val = assert(toDraft(val), draft)

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 276–282why, one step back
276}
277278// Generic type279{280 // NOTE: "extends any" only helps a little.281 const $ = <T extends any>(val: ReadonlyArray<T>) => {282 let draft: Draft<typeof val> = _

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

line 283why, one step back
283 val = assert(toDraft(val), draft)

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

line 284why, one step back
284 // $ExpectError: [ts] Argument of type 'DraftArray<T>' is not assignable to parameter of type 'Draft<T>'. [2345]

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

line 285why, one step back
285 // assert(fromDraft(draft), draft)

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

line 286why, one step back
286 }

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

line 287why, one step back
287}