Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

pending-tasks-ssr.test.ts

128 lines3.1 KB

1import {
2 ChangeDetectionStrategy,
3 Component,
4 destroyPlatform,
5 provideZonelessChangeDetection,
6} from '@angular/core'
7import { bootstrapApplication } from '@angular/platform-browser'
8import {
9 provideServerRendering,
10 renderApplication,
11} from '@angular/platform-server'
12import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
13import { sleep } from '@tanstack/query-test-utils'
14import {
15 QueryClient,
16 injectQueries,
17 injectQuery,
18 provideTanStackQuery,
19} from '..'
20​
21describe('PendingTasks SSR', () => {
22 beforeEach(() => {
23 destroyPlatform()
24 vi.useFakeTimers()
25 })
26​
27 afterEach(() => {
28 vi.useRealTimers()
29 })
30​
31 @Component({
32 selector: 'app-root',
33 template: '{{ query.data() }}',
34 changeDetection: ChangeDetectionStrategy.OnPush,
35 })
36 class TestInjectQueryComponent {
37 query = injectQuery(() => ({
38 queryKey: ['ssr-test'],
39 queryFn: async () => {
40 await sleep(1000)
41 return 'data-fetched-on-ssr'
42 },
43 }))
44 }
45​
46 @Component({
47 selector: 'app-queries-root',
48 template: '{{ queries()[0].data() }}',
49 changeDetection: ChangeDetectionStrategy.OnPush,
50 })
51 class TestInjectQueriesComponent {
52 queries = injectQueries(() => ({
53 queries: [
54 {
55 queryKey: ['ssr-queries-test'],
56 queryFn: async () => {
57 await sleep(1000)
58 return 'queries-data-fetched-on-ssr'
59 },
60 },
61 ],
62 }))
63 }
64​
65 it('should wait for stability of injectQuery', async () => {
66 const htmlPromise = renderApplication(
67 (context) =>
68 bootstrapApplication(
69 TestInjectQueryComponent,
70 {
71 providers: [
72 provideServerRendering(),
73 provideZonelessChangeDetection(),
74 provideTanStackQuery(
75 () =>
76 new QueryClient({
77 defaultOptions: { queries: { retry: false } },
78 }),
79 ),
80 ],
81 },
82 context,
83 ),
84 {
85 url: '/',
86 document:
87 '<!doctype html><html><body><app-root></app-root></body></html>',
88 },
89 )
90​
91 await vi.runAllTimersAsync()
92 const html = await htmlPromise
93​
94 expect(html).toContain('data-fetched-on-ssr')
95 })
96​
97 it('should wait for stability of injectQueries', async () => {
98 const htmlPromise = renderApplication(
99 (context) =>
100 bootstrapApplication(
101 TestInjectQueriesComponent,
102 {
103 providers: [
104 provideServerRendering(),
105 provideZonelessChangeDetection(),
106 provideTanStackQuery(
107 () =>
108 new QueryClient({
109 defaultOptions: { queries: { retry: false } },
110 }),
111 ),
112 ],
113 },
114 context,
115 ),
116 {
117 url: '/',
118 document:
119 '<!doctype html><html><body><app-queries-root></app-queries-root></body></html>',
120 },
121 )
122​
123 await vi.runAllTimersAsync()
124 const html = await htmlPromise
125​
126 expect(html).toContain('queries-data-fetched-on-ssr')
127 })
128})