-
Notifications
You must be signed in to change notification settings - Fork 74
/
use-swrv.spec.tsx
623 lines (516 loc) · 16.8 KB
/
use-swrv.spec.tsx
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
import Vue from 'vue/dist/vue.common.js'
import VueCompositionApi, { watch, defineComponent, ref } from '@vue/composition-api'
import useSWRV, { mutate } from '@/use-swrv'
Vue.use(VueCompositionApi)
jest.useFakeTimers()
const timeout: Function = milliseconds => jest.advanceTimersByTime(milliseconds)
const tick: Function = async (vm, times) => {
for (let _ in [...Array(times).keys()]) {
await vm.$nextTick()
}
}
describe('useSWRV', () => {
it('should return data on hydration when fetch is not a promise', done => {
const fetch = () => 'SWR'
const vm = new Vue({
template: `<div>hello, {{ data }}</div>`,
setup () {
return useSWRV('cache-key-not-a-promise', fetch)
}
}).$mount()
expect(vm.data).toBe('SWR')
done()
})
it('should return `undefined` on hydration', done => {
const fetch = () => new Promise(res => setTimeout(() => res('SWR'), 1))
const vm = new Vue({
template: `<div>hello, {{ data }}</div>`,
setup () {
return useSWRV('cache-key-1', fetch)
}
}).$mount()
expect(vm.data).toBe(undefined)
done()
})
it('should return data after hydration', async done => {
const vm = new Vue({
template: `<div>hello, {{ data }}</div>`,
setup () {
return useSWRV('cache-key-2', () => 'SWR')
}
}).$mount()
await tick(vm, 4)
expect(vm.$el.textContent).toBe('hello, SWR')
done()
})
it('should return data from a promise', async done => {
const vm = new Vue({
template: `<div>hello, {{ data }}</div>`,
setup () {
return useSWRV('cache-key-promise', () => new Promise(resolve => resolve('SWR')))
}
}).$mount()
expect(vm.$el.textContent).toBe('hello, ')
await tick(vm, 2)
expect(vm.$el.textContent).toEqual('hello, SWR')
done()
})
it('should allow functions as key and reuse the cache', async done => {
const vm = new Vue({
template: `<div>hello, {{ data }}</div>`,
setup () {
return useSWRV(() => 'cache-key-2', () => 'SWR')
}
}).$mount()
// immediately available via cache without waiting for $nextTick
expect(vm.$el.textContent).toBe('hello, SWR')
done()
})
it('should allow async fetcher functions', async done => {
const vm = new Vue({
template: `<div>hello, {{ data }}</div>`,
setup () {
return useSWRV('cache-key-3', () =>
new Promise(res => setTimeout(() => res('SWR'), 200))
)
}
}).$mount()
expect(vm.$el.textContent).toBe('hello, ')
timeout(200)
await tick(vm, 2)
expect(vm.$el.textContent).toBe('hello, SWR')
done()
})
it('should dedupe requests by default', async done => {
let count = 0
const fetch = () => {
count++
return new Promise(res => setTimeout(() => res('SWR'), 200))
}
const vm = new Vue({
template: `<div>{{v1}}, {{v2}}</div>`,
setup () {
const { data: v1 } = useSWRV('cache-key-4', fetch)
const { data: v2 } = useSWRV('cache-key-4', fetch)
return { v1, v2 }
}
}).$mount()
expect(vm.$el.textContent).toBe(', ')
timeout(200)
await tick(vm, 2)
expect(vm.$el.textContent).toBe('SWR, SWR')
// only fetches once
expect(count).toEqual(1)
done()
})
it('should fetch dependently', async done => {
let count = 0
const loadUser = (): Promise<{ id: number }> => {
return new Promise(res => setTimeout(() => {
count++
res({ id: 123 })
}, 1000))
}
const loadProfile = endpoint => {
return new Promise((res) => setTimeout(() => {
count++
endpoint && res({
userId: 123,
age: 20
})
}, 200))
}
const vm = new Vue({
template: `<div>d1:{{ data1 && data1.id }} e1:{{ error1 }} d2:{{ data2 && data2.userId }} e2:{{ error2 }}</div>`,
setup () {
const { data: data1, error: error1 } = useSWRV('/api/user', loadUser)
// TODO: checking truthiness of data1.value to avoid watcher warning
// https://github.com/vuejs/composition-api/issues/242
const { data: data2, error: error2 } = useSWRV(() => data1.value && `/api/profile?id=` + data1.value.id, loadProfile)
return { data1, error1, data2, error2 }
}
}).$mount()
expect(vm.$el.textContent).toBe('d1: e1: d2: e2:')
timeout(100)
await tick(vm, 2)
expect(vm.$el.textContent).toBe('d1: e1: d2: e2:')
expect(count).toEqual(0) // Promises still in flight
timeout(900)
await tick(vm, 2)
expect(vm.$el.textContent).toBe('d1:123 e1: d2: e2:')
expect(count).toEqual(2)
timeout(200)
await tick(vm, 2)
expect(vm.$el.textContent).toBe('d1:123 e1: d2:123 e2:')
expect(count).toEqual(3)
done()
})
// From #24
it('should only update refs of current cache key', async done => {
const fetcher = (key) => new Promise(res => setTimeout(() => res(key), 1000))
const vm = new Vue({
template: `<div>Page: {{ data }}</div>`,
setup () {
const page = ref('1')
const { data, error } = useSWRV(() => {
return page.value
}, fetcher)
let interval = setInterval(() => {
const nextPage: number = parseInt(page.value) + 1
page.value = String(nextPage)
nextPage > 2 && clearInterval(interval)
}, 500)
return { data, error, page }
}
}).$mount()
// initially page is empty, but fetcher has fired with page=1
expect(vm.$el.textContent).toBe('Page: ')
await tick(vm, 2)
expect(vm.$data.page).toBe('1')
expect(vm.$el.textContent).toBe('Page: ')
// page has now updated to page=2, fetcher1 has not yet resolved, fetcher
// for page=2 has now fired
timeout(500)
await tick(vm, 2)
expect(vm.$data.page).toBe('2')
expect(vm.$el.textContent).toBe('Page: ')
// fetcher for page=1 has resolved, but the cache key is not equal to the
// current page, so the data ref does not update. fetcher for page=3 has
// now fired
timeout(500)
await tick(vm, 2)
expect(vm.$data.page).toBe('3')
expect(vm.$el.textContent).toBe('Page: ')
// cache key is no longer updating and the fetcher for page=3 has resolved
// so the data ref now updates.
timeout(1000)
await tick(vm, 2)
expect(vm.$data.page).toBe('3')
expect(vm.$el.textContent).toBe('Page: 3')
done()
})
})
describe('useSWRV - loading', () => {
const loadData = () => new Promise(res => setTimeout(() => res('data'), 100))
it('should return loading state via undefined data', async done => {
let renderCount = 0
const vm = new Vue({
render: h => h(defineComponent({
setup () {
const { data } = useSWRV('is-validating-1', loadData)
return () => {
renderCount++
return <div>hello, {!data.value ? 'loading' : data.value}</div>
}
}
}))
}).$mount()
expect(renderCount).toEqual(1)
expect(vm.$el.textContent).toBe('hello, loading')
timeout(100)
await tick(vm, 2)
expect(vm.$el.textContent).toBe('hello, data')
expect(renderCount).toEqual(2)
done()
})
it('should return loading state via isValidating', async done => {
// Prime the cache
const vm = new Vue({
render: h => h(defineComponent({
setup () {
const { data, isValidating } = useSWRV('is-validating-2', loadData, {
refreshInterval: 1000
})
return () => <div>hello, {data.value}, {isValidating.value ? 'loading' : 'ready'}</div>
}
}))
}).$mount()
expect(vm.$el.textContent).toBe('hello, , loading')
timeout(100)
await tick(vm, 2)
expect(vm.$el.textContent).toBe('hello, data, ready')
// Reactive to future refreshes
timeout(900)
await tick(vm, 2)
expect(vm.$el.textContent).toBe('hello, data, loading')
timeout(100)
await tick(vm, 2)
expect(vm.$el.textContent).toBe('hello, data, ready')
done()
})
})
describe('useSWRV - mutate', () => {
it('prefetches via mutate', done => {
// Prime the cache
const loadData = key => new Promise(res => setTimeout(() => res(key), 100))
mutate('is-prefetched-1', loadData('is-prefetched-1')).then(() => {
const vm = new Vue({
render: h => h(defineComponent({
setup () {
const { data: dataFromCache } = useSWRV('is-prefetched-1', loadData)
const { data: dataNotFromCache } = useSWRV('is-prefetched-2', loadData)
const msg1 = !dataFromCache.value ? 'loading' : dataFromCache.value
const msg2 = !dataNotFromCache.value ? 'loading' : dataNotFromCache.value
return () => <div>hello, {msg1} and {msg2}</div>
}
}))
}).$mount()
expect(vm.$el.textContent).toBe('hello, is-prefetched-1 and loading')
done()
})
timeout(100)
})
test.todo('mutate triggers revalidations')
// it('mutate triggers revalidations', done => {
// const loadData = key => new Promise(res => setTimeout(() => res(key + Date.now()), 100))
// mutate('mutate-is-revalidated-1', loadData('mutate-is-revalidated-1')).then(async () => {
// const vm = new Vue({
// template: `<div>hello, {{ data }}</div>`,
// setup () {
// setTimeout(() => {
// mutate('mutate-is-revalidated-1', new Promise((resolve) => resolve('hey')))
// }, 50)
// return useSWRV('mutate-is-revalidated-1', loadData)
// }
// }).$mount()
// tick(vm, 4)
// expect(vm.$el.textContent).toContain('hello, mutate-is-revalidated-1')
// timeout(100)
// tick(vm, 4)
// expect(vm.$el.textContent).toContain('hello, mutate-is-revalidated-1')
// timeout(50)
// tick(vm, 4)
//
// // TODO: understand why revalidation isn't working here
// expect(vm.$el.textContent).toBe('hello, hey')
// done()
// })
// timeout(100)
// })
})
describe('useSWRV - listeners', () => {
it('tears down listeners', async done => {
let revalidate
const f1 = jest.fn()
const f2 = jest.fn()
const f3 = jest.fn()
const f4 = jest.fn()
document.addEventListener = f1
document.removeEventListener = f2
window.addEventListener = f3
window.removeEventListener = f4
const vm = new Vue({
template: `<div>hello, {{ data }}</div>`,
setup () {
const refs = useSWRV('cache-key-1', () => 'SWR')
revalidate = refs.revalidate
return refs
}
}).$mount()
await vm.$nextTick()
vm.$destroy()
expect(f1).toHaveBeenLastCalledWith('visibilitychange', revalidate, false)
expect(f2).toHaveBeenLastCalledWith('visibilitychange', revalidate, false)
expect(f3).toHaveBeenLastCalledWith('focus', revalidate, false)
expect(f4).toHaveBeenLastCalledWith('focus', revalidate, false)
done()
})
})
describe('useSWRV - refresh', () => {
it('should rerender automatically on interval', async done => {
let count = 0
const vm = new Vue({
template: `<div>count: {{ data }}</div>`,
setup () {
return useSWRV('dynamic-1', () => count++, {
refreshInterval: 200,
dedupingInterval: 100
})
}
}).$mount()
await tick(vm, 2)
expect(vm.$el.textContent).toEqual('count: 0')
timeout(210)
await tick(vm, 2)
expect(vm.$el.textContent).toEqual('count: 1')
timeout(50)
await tick(vm, 2)
expect(vm.$el.textContent).toEqual('count: 1')
timeout(150)
await tick(vm, 2)
expect(vm.$el.textContent).toEqual('count: 2')
done()
})
it('should dedupe requests combined with intervals - promises', async done => {
/**
* TODO: right now, only promises get deduped, so if the fetcherFn is a
* regular function then it will keep refreshing.
*/
let count = 0
const loadData = () => new Promise(res => setTimeout(() => {
res(count++)
}, 10)) // Resolves quickly, but gets de-duplicated during refresh intervals
const vm = new Vue({
template: `<div>count: {{ data }}</div>`,
setup () {
return useSWRV('dynamic-2', loadData, {
refreshInterval: 200,
dedupingInterval: 300
})
}
}).$mount()
expect(vm.$el.textContent).toBe('count: ')
timeout(100)
await tick(vm, 2)
expect(vm.$el.textContent).toBe('count: 0')
/**
* check inside promises cache within deduping interval so even though
* promise resolves quickly, it will grab the promise out of the cache
* instead and not increment the count
*/
timeout(100)
await tick(vm, 1)
expect(vm.$el.textContent).toBe('count: 0')
timeout(100) // update
await tick(vm, 2)
expect(vm.$el.textContent).toBe('count: 1')
timeout(200) // no update (deduped)
await tick(vm, 2)
expect(vm.$el.textContent).toBe('count: 1')
timeout(150) // update
await tick(vm, 2)
expect(vm.$el.textContent).toBe('count: 2')
done()
})
})
describe('useSWRV - error', () => {
it('should handle errors', async done => {
const vm = new Vue({
template: `<div>
<div v-if="data">hello, {{ data }}</div>
<div v-if="error">{{error.message}}</div>
</div>`,
setup () {
return useSWRV(() => 'error-1', () => new Promise((_, reject) => {
reject(new Error('error!'))
}))
}
}).$mount()
await tick(vm, 2)
expect(vm.$el.textContent.trim()).toBe('error!')
done()
})
it('should be able to watch errors - similar to onError callback', async done => {
let erroredSWR = null
const vm = new Vue({
template: `<div>
<div>hello, {{ data }}</div>
</div>`,
setup () {
const { data, error } = useSWRV(() => 'error-2', () => new Promise((_, rej) =>
setTimeout(() => rej(new Error('error!')), 200)
))
watch(error, error1 => {
erroredSWR = error1 && error1.message
})
return {
data, error
}
}
}).$mount()
expect(vm.$el.textContent).toBe('hello, ')
timeout(200)
await tick(vm, 2)
expect(erroredSWR).toEqual('error!')
done()
})
it('should serve stale-if-error', async done => {
let count = 0
const loadData = () => new Promise((resolve, reject) => setTimeout(() => {
count++
count > 2 ? reject(new Error('uh oh!')) : resolve(count)
}, 100))
const vm = new Vue({
template: `<div>count: {{ data }} {{ error }}</div>`,
setup () {
return useSWRV('error-3', loadData, {
refreshInterval: 200
})
}
}).$mount()
timeout(300) // 200 refresh + 100 timeout
await tick(vm, 3)
expect(vm.$el.textContent).toBe('count: 1 ')
timeout(300)
await tick(vm, 3)
expect(vm.$el.textContent).toBe('count: 2 ')
timeout(300)
await tick(vm, 2)
// stale data sticks around even when error exists
expect(vm.$el.textContent).toBe('count: 2 Error: uh oh!')
done()
})
})
describe('useSWRV - window events', () => {
const toggleVisibility = state => Object.defineProperty(document, 'visibilityState', {
configurable: true,
get: function () { return state }
})
const toggleOnline = state => Object.defineProperty(navigator, 'onLine', {
configurable: true,
get: function () { return state }
})
it('should not rerender when document is not visible', async done => {
let count = 0
const vm = new Vue({
template: `<div>count: {{ data }}</div>`,
setup () {
return useSWRV('dynamic-5', () => count++, {
refreshInterval: 200
})
}
}).$mount()
await tick(vm, 1)
expect(vm.$el.textContent).toBe('count: 0')
toggleVisibility(undefined)
timeout(200)
await tick(vm, 1)
// should still update even though visibilityState is undefined
expect(vm.$el.textContent).toBe('count: 1')
toggleVisibility('hidden')
timeout(200)
await tick(vm, 1)
// should not rerender because document is hidden e.g. switched tabs
expect(vm.$el.textContent).toBe('count: 1')
vm.$destroy()
// put it back to visible for other tests
toggleVisibility('visible')
done()
})
it('should not rerender when offline', async done => {
let count = 0
const vm = new Vue({
template: `<div>count: {{ data }}</div>`,
setup () {
return useSWRV('dynamic-6', () => count++, {
refreshInterval: 200,
dedupingInterval: 10
})
}
}).$mount()
await tick(vm, 1)
expect(vm.$el.textContent).toBe('count: 0')
toggleOnline(undefined)
timeout(200)
await tick(vm, 1)
// should rerender since we're AMERICA ONLINE
expect(vm.$el.textContent).toBe('count: 1')
// connection drops... your mom picked up the phone while you were 🏄♂️ the 🕸
toggleOnline(false)
timeout(200)
await tick(vm, 1)
// should not rerender cuz offline
expect(vm.$el.textContent).toBe('count: 1')
done()
})
})