-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* @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('works', async () => { | ||
const spy = vi.fn() | ||
const { app, query } = renderApp({ | ||
appSetup() { | ||
onErrorCaptured(spy) | ||
}, | ||
}) | ||
query.mockRejectedValueOnce(new Error('ko')) | ||
expect(await renderToString(app)).toMatchInlineSnapshot(`"<!---->"`) | ||
expect(spy).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
// it('can avoid ') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters