forked from posva/pinia-colada
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssr.spec.ts
80 lines (73 loc) · 2.1 KB
/
ssr.spec.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
/**
* @vitest-environment node
*/
import { describe, it, expect, vi } from 'vitest'
import { createSSRApp, defineComponent, onErrorCaptured } from 'vue'
import { renderToString, ssrRenderComponent, ssrRenderSuspense } from '@vue/server-renderer'
import type { UseQueryOptions } from './query-options'
import { isSpy } from '../test/utils'
import { useQuery } from './use-query'
import { PiniaColada } from './pinia-colada'
import { createPinia } from 'pinia'
describe('SSR', () => {
function renderApp<TResult = number, TError = Error>({
options = {},
appSetup,
}: {
options?: Partial<UseQueryOptions<TResult>>
appSetup?: () => void
} = {}) {
const query = options.query ? (isSpy(options.query) ? options.query : vi.fn(options.query)) : vi.fn(async () => 42)
const InnerComp = defineComponent({
render: () => null,
setup() {
const useQueryResult = useQuery<TResult, TError>({
key: ['key'],
...options,
// @ts-expect-error: generic unmatched but types work
query,
})
return {
...useQueryResult,
}
},
})
const App = defineComponent({
ssrRender(_ctx: any, push: any, _parent: any) {
ssrRenderSuspense(push, {
default: () => {
push(ssrRenderComponent(InnerComp, null, null, _parent))
},
// @ts-expect-error: Vue type error?
_: 1 /* STABLE */,
})
},
setup() {
appSetup?.()
return {}
},
})
const app = createSSRApp(App)
const pinia = createPinia()
app.use(pinia)
app.use(PiniaColada, {})
return {
app,
query,
pinia,
}
}
it('propagates query errors', async () => {
// return false to stop the error from propagating
const spy = vi.fn(() => false)
const { app, query } = renderApp({
appSetup() {
onErrorCaptured(spy)
},
})
query.mockRejectedValueOnce(new Error('ko'))
expect(await renderToString(app)).toMatchInlineSnapshot(`"<!---->"`)
expect(spy).toHaveBeenCalledTimes(1)
})
// it('can avoid ')
})