-
Notifications
You must be signed in to change notification settings - Fork 6
/
task.test.ts
458 lines (379 loc) · 10.6 KB
/
task.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
import { reaction, action, observable } from 'mobx'
import { task } from '../index'
import { memoize } from 'lodash'
import autobind from 'autobind-decorator'
import { throws } from 'smid'
import { defer } from './defer'
// tslint:disable:await-promise no-floating-promises
test('goes from pending -> resolved', async () => {
const d = defer()
const base = jest.fn((_a: number, _b: number, _c: number) => d.promise)
const fn = task(base)
expect(fn.state === 'pending').toBe(true)
expect(fn.pending === true).toBe(true)
const p = fn(1, 2, 3)
expect(fn.state === 'pending').toBe(true)
expect(fn.pending === true).toBe(true)
d.resolve(1337)
const result = await p
expect(result).toBe(1337)
expect(fn.pending).toBe(false)
expect(fn.rejected).toBe(false)
expect(fn.resolved).toBe(true)
expect(fn.result).toBe(1337)
expect(fn.state).toBe('resolved')
})
test('goes from pending -> rejected on error', async () => {
const d = defer()
const base = jest.fn((_a: number, _b: number, _c: number) => d.promise)
const fn = task(base)
expect(fn.state === 'pending').toBe(true)
expect(fn.pending === true).toBe(true)
const p = fn(1, 2, 3)
expect(fn.state === 'pending').toBe(true)
expect(fn.pending === true).toBe(true)
const err = new Error('hah')
d.reject(err)
const result = await throws(p)
expect(result).toBe(err)
expect(fn.pending).toBe(false)
expect(fn.rejected).toBe(true)
expect(fn.resolved).toBe(false)
expect(fn.result).toBe(undefined)
expect(fn.error).toBe(err)
expect(fn.state).toBe('rejected')
})
test('goes from resolved -> pending -> resolved when state is set', async () => {
const d = defer()
const fn = task(() => d.promise, { state: 'resolved' })
expect(fn.resolved).toBe(true)
const p = fn()
expect(fn.pending).toBe(true)
d.resolve(1337)
await p
expect(fn.resolved).toBe(true)
})
test('state is reactive', () => {
const fn = task(() => 1337)
const isPending = jest.fn()
const isResolved = jest.fn()
const isRejected = jest.fn()
reaction(() => fn.pending, isPending)
reaction(() => fn.resolved, isResolved)
reaction(() => fn.rejected, isRejected)
fn.setState({ state: 'resolved' })
fn.setState({ state: 'rejected' })
fn.setState({ state: 'pending' })
// these goes from false to true, and from true to false
expect(isPending).toHaveBeenCalledTimes(2)
expect(isResolved).toHaveBeenCalledTimes(2)
expect(isRejected).toHaveBeenCalledTimes(2)
})
test('setState lets me modify the internal state', () => {
const fn = task(() => 1337)
expect(fn.pending).toBe(true)
fn.setState({ state: 'resolved', result: 123 })
expect(fn.result).toBe(123)
expect(fn.resolved).toBe(true)
})
test('there are shortcuts to check the state.', () => {
const fn = task(() => 1337)
expect(fn.state).toBe('pending')
expect(fn.pending).toBe(true)
expect(fn.resolved).toBe(false)
expect(fn.rejected).toBe(false)
fn.setState({ state: 'resolved', result: 123 })
expect(fn.state).toBe('resolved')
expect(fn.pending).toBe(false)
expect(fn.resolved).toBe(true)
expect(fn.rejected).toBe(false)
fn.setState({ state: 'rejected' })
expect(fn.state).toBe('rejected')
expect(fn.pending).toBe(false)
expect(fn.resolved).toBe(false)
expect(fn.rejected).toBe(true)
})
test('task.resolved shorthand sets state: resolved', () => {
const fn = task.resolved(() => 1337)
expect(fn.resolved).toBe(true)
})
test('swallow: true does not throw exceptions', async () => {
const err = new Error('haha shiit')
const fn = task((_a: number) => Promise.reject(err), { swallow: true })
const result = await fn(123)
expect(result).toBe(undefined)
expect(fn.result).toBe(undefined)
expect(fn.error).toBe(err)
})
test('regular decorator', async () => {
const d = defer()
class Test {
@task
fn(arg: any) {
return d.promise.then(() => arg)
}
}
const test = new Test()
expect((test.fn as any).pending).toBe(true)
const p = test.fn(123)
expect((test.fn as any).pending).toBe(true)
d.resolve(1337)
const result = await p
expect(result).toBe(123)
expect((test.fn as any).resolved).toBe(true)
expect((test.fn as any).result).toBe(123)
})
test('decorator factory', () => {
class Test {
@task({ state: 'resolved', result: 1337 })
fn(_arg: any) {
/**/
}
}
const test = new Test()
expect((test.fn as any).resolved).toBe(true)
expect((test.fn as any).result).toBe(1337)
})
test('preconfigured decorator', () => {
const error = new Error('hah')
class Test {
@task.resolved
fnResolved() {
/**/
}
@task.rejected({ error })
fnRejected() {
/**/
}
}
const test = new Test()
expect((test.fnResolved as any).resolved).toBe(true)
expect((test.fnRejected as any).rejected).toBe(true)
expect((test.fnRejected as any).error).toBe(error)
})
test('bind returns a task function', async () => {
const fn = task(function(this: any, arg1: number) {
return [this, arg1]
})
const that = {}
const bound = fn.bind(that, 1)
const boundResult = await (bound as any)(1337)
expect(boundResult[0]).toBe(that)
expect(boundResult[1]).toBe(1)
})
test('wrap returns a task function', async () => {
const fn = task(() => 42).wrap(f => {
const inner = () => {
inner.callCount += 1
return f()
}
inner.callCount = 0
return inner
})
expect(await fn()).toBe(42)
expect((fn as any).callCount).toBe(1)
expect(fn.resolved).toBe(true)
})
test('can be memoized', async () => {
let i = 1
const fn = task(() => i++).wrap(memoize)
expect(await fn()).toBe(1)
expect(await fn()).toBe(1)
expect(fn.resolved).toBe(true)
})
test('can decorate an already decorated method', async () => {
/**
* For this to work the task decorator has
* to be the last decorator to run (declared first)
*/
class Test {
@task
@action.bound
method() {
return this
}
}
const test = new Test()
expect((test.method as any).pending).toBe(true)
const method = test.method
expect(await method()).toBe(test)
// twice to test caching
expect(await method()).toBe(test)
})
test('can be tacked onto an observable', async () => {
const store = observable({
todos: [],
fetchTodos: task(async () => {
return [{ text: 'install mobx-task' }]
})
})
expect(store.fetchTodos.pending).toBe(true)
await store.fetchTodos()
expect(store.fetchTodos.resolved).toBe(true)
})
test('match returns the case for the current state', () => {
const fn = task((): number | undefined => undefined)
const run = () =>
fn.match({
pending: () => 1,
resolved: value => value,
rejected: (err: any) => err.message
})
expect(run()).toBe(1)
fn.setState({ state: 'resolved', result: 42 })
expect(run()).toBe(42)
fn.setState({ state: 'rejected', error: new Error('hah') })
expect(run()).toBe('hah')
})
test('match returns undefined if there is no case', () => {
const fn = task((): number | undefined => undefined)
const run = () =>
fn.match({
resolved: value => value,
rejected: (err: any) => err.message
})
expect(run()).toBe(undefined)
fn.setState({ state: 'resolved', result: 42 })
expect(run()).toBe(42)
fn.setState({ state: 'rejected', error: new Error('hah') })
expect(run()).toBe('hah')
})
test('match passes arguments to pending', () => {
const fn = task((_a: number, _b: number) => undefined)
let calledWith = null
fn(1, 2)
fn.match({
pending: (arg1, arg2) => {
calledWith = [arg1, arg2]
}
})
expect(calledWith).toEqual([1, 2])
})
test('calling the function multiple times will only trigger setState:resolved once', async () => {
const fn = task(d => d.promise)
const d1 = defer()
const d2 = defer()
const p1 = fn(d1)
const p2 = fn(d2)
await new Promise(resolve => setTimeout(resolve, 20))
d1.resolve(1)
const r1 = await p1
expect(fn.pending).toBe(true)
d2.resolve(2)
const r2 = await p2
expect(fn.resolved).toBe(true)
expect(r1).toBe(1)
expect(r2).toBe(2)
})
test('calling the function multiple times will not trigger setState:rejected if not the last call', async () => {
const fn = task(d => d.promise)
const d1 = defer()
const d2 = defer()
const p1 = fn(d1)
const p2 = fn(d2)
await new Promise(resolve => setTimeout(resolve, 20))
d1.reject(new Error('Oh shit'))
const r1 = await throws(() => p1)
expect(fn.pending).toBe(true)
d2.resolve(2)
const r2 = await p2
expect(fn.resolved).toBe(true)
expect(r1.message).toBe('Oh shit')
expect(r2).toBe(2)
})
test('catches sync errors', async () => {
const fn = task(() => {
throw new Error('hah')
})
await throws(() => fn())
expect(fn.rejected).toBe(true)
expect((fn.error as any).message).toBe('hah')
})
test('reset() resets the state to resolved with result', async () => {
const fn = task(
() => {
return 1337
},
{ state: 'resolved', result: 42 }
)
expect(fn.result).toBe(42)
const result = await fn()
expect(result).toBe(1337)
expect(fn.state).toBe('resolved')
const fromReset = fn.reset()
expect(fromReset).toBe(fn)
expect(fn.result).toBe(42)
})
test('reset() resets the state to pending', async () => {
const fn = task(() => {
return 1337
})
expect(fn.result).toBe(undefined)
const result = await fn()
expect(result).toBe(1337)
expect(fn.state).toBe('resolved')
fn.reset()
expect(fn.result).toBe(undefined)
expect(fn.state).toBe('pending')
})
test('autobind works', async () => {
@autobind
class Test {
value: number
constructor() {
this.value = 42
}
@task
@autobind
func() {
return this.value
}
}
const sub = new Test()
const fn = sub.func
const result = await fn()
expect(result).toBe(42)
expect((fn as any).state).toBe('resolved')
expect((sub.func as any).state).toBe('resolved')
})
test('can reassign decorated method', async () => {
class Test {
@task
method() {
/**/
}
}
const sub = new Test()
sub.method = 123 as any
expect(sub.method).toBe(123)
})
test('decorator value is cached', async () => {
class Test {
@task
method() {
return 42
}
}
const sub = new Test()
expect(await sub.method()).toBe(42)
expect(await sub.method()).toBe(42)
})
test('decorator supports fields with functions', async () => {
class Test {
@task method = function() {
return 42
}
}
const sub = new Test()
expect(await sub.method()).toBe(42)
})
test('decorator supports fields with arrow functions', async () => {
class Test {
getValue() {
return 42
}
@task method = () => this.getValue()
}
const sub = new Test()
expect(await sub.method()).toBe(42)
})