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 fileTake 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 back1import {assert, _} from "spec.ts"New build set. Current statsMichel Weststratebefore adoption9aec0fe
lines 2–9why, one step back2import 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 back1011// 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 back12const toDraft: <T>(value: T) => Draft<T> = x => x as any13const fromDraft: <T>(draft: Draft<T>) => T = x => x as anytest: extract some tests into their own functionaleclarsonbefore adoption21846ab
line 14why, one step back14feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 15–22why, one step back15test("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 back23 }test: extract some tests into their own functionaleclarsonbefore adoption21846ab
line 24why, one step back24feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 25–30why, one step back25 // Tuple26 {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 back31feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 32–37why, one step back32 // 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 back38feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 39–45why, one step back39 // 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 back46feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 47–52why, one step back47 // 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 back53feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 54–62why, one step back54 // Readonly tuple55 {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 back63feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 64–69why, one step back64 // Mutable array65 {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 back70feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 71–76why, one step back71 // 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 back77feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 78–84why, one step back78 // Readonly array79 {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 back85feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 86–92why, one step back86 // 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 back93feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 94–99why, one step back94 // Mutable object95 {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 back100feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 101–106why, one step back101 // 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 back107feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 108–115why, one step back108 // Interface109 {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 back116 }test: extract some tests into their own functionaleclarsonbefore adoption21846ab
line 117why, one step back117feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 118–128why, one step back118 // 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 back129 }feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 130–135why, one step back130131 // 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 back136 }test: extract some tests into their own functionaleclarsonbefore adoption21846ab
line 137why, one step back137feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 138–143why, one step back138 // 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 back144feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 145–150why, one step back145 // Loose function146 {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 back151feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 152–157why, one step back152 // Strict function153 {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 back158feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 159–169why, one step back159 // 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 back170 }test: extract some tests into their own functionaleclarsonbefore adoption21846ab
line 171why, one step back171feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 172–182why, one step back172 // 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 back183 }test: extract some tests into their own functionaleclarsonbefore adoption21846ab
line 184why, one step back184feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 185–189why, one step back185 // Map instance186 {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 back190feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 191–195why, one step back191 // Weak maps192 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 back196feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1
lines 197–203why, one step back197 // ReadonlyMap instance198 {199 let val: ReadonlyMap<any, any> = _200 let draft: Map<any, any> = _201 assert(toDraft(val), draft)202 }203feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 204–208why, one step back204 // Set instance205 {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 back209feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 210–214why, one step back210 // Weak sets211 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 back215feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1
lines 216–222why, one step back216 // ReadonlySet instance217 {218 let val: ReadonlySet<any> = _219 let draft: Set<any> = _220 assert(toDraft(val), draft)221 }222feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 223–228why, one step back223 // Promise object224 {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 back229feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 230–235why, one step back230 // Date instance231 {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 back236feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 237–242why, one step back237 // RegExp instance238 {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 back243feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 244–249why, one step back244 // Boxed primitive245 {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 back250feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 251–256why, one step back251 // String literal252 {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 back257feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 258–263why, one step back258 // Any259 {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 back264feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 265–270why, one step back265 // Never266 {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 back271feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 272–277why, one step back272 // Unknown273 {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 back278feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 279–284why, one step back279 // Numeral280 {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 back285feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 286–291why, one step back286 // Union of numerals287 {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 back292feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 293–296why, one step back293 // Union of tuple, array, object294 {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 back297 val = assert(toDraft(val), draft)298 }feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 299–311why, one step back299300 // 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 back312})313Merge pull request #531 from denis-sokolov/as-draft-namingMichel Weststratebefore adoptionc7e9de8
line 314why, one step back314test("castDraft", () => {feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1
lines 315–335why, one step back315 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})335Merge pull request #531 from denis-sokolov/as-draft-namingMichel Weststratebefore adoptionc7e9de8
line 336why, one step back336test("castDraft preserves a value", () => {feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1
lines 337–338why, one step back337 const x = {}338 expect(castDraft(x)).toBe(x)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 339why, one step back339})fix: createDraft should return `Draft` instead of `Drafted`. Fixes #512Michel Weststratebefore adoption3f15d49
lines 340–344why, one step back340341test("#512 createDraft creates a draft", () => {342 const x = {y: 1}343 assert(x, _ as Draft<{y: number}>)344})