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

inject-queries.test.ts

79 lines2.1 KB

1import { beforeEach, describe, expect, it } from 'vitest'
2import { render } from '@testing-library/angular'
3import {
4 Component,
5 effect,
6 provideZonelessChangeDetection,
7} from '@angular/core'
8import { TestBed } from '@angular/core/testing'
9import { queryKey } from '@tanstack/query-test-utils'
10import { QueryClient, provideTanStackQuery } from '..'
11import { injectQueries } from '../inject-queries'
12​
13let queryClient: QueryClient
14​
15beforeEach(() => {
16 queryClient = new QueryClient()
17 TestBed.configureTestingModule({
18 providers: [
19 provideZonelessChangeDetection(),
20 provideTanStackQuery(queryClient),
21 ],
22 })
23})
24​
25describe('injectQueries', () => {
26 it('should return the correct states', async () => {
27 const key1 = queryKey()
28 const key2 = queryKey()
29 const results: Array<Array<Record<string, any>>> = []
30​
31 @Component({
32 template: `
33 <div>
34 <div>
35 data1: {{ result()[0].data() ?? 'null' }}, data2:
36 {{ result()[1].data() ?? 'null' }}
37 </div>
38 </div>
39 `,
40 })
41 class Page {
42 toString(val: any) {
43 return String(val)
44 }
45 result = injectQueries(() => ({
46 queries: [
47 {
48 queryKey: key1,
49 queryFn: async () => {
50 await new Promise((r) => setTimeout(r, 10))
51 return 1
52 },
53 },
54 {
55 queryKey: key2,
56 queryFn: async () => {
57 await new Promise((r) => setTimeout(r, 100))
58 return 2
59 },
60 },
61 ],
62 }))
63​
64 _pushResults = effect(() => {
65 const snapshot = this.result().map((q) => ({ data: q.data() }))
66 results.push(snapshot)
67 })
68 }
69​
70 const rendered = await render(Page)
71​
72 await rendered.findByText('data1: 1, data2: 2')
73​
74 expect(results.length).toBe(3)
75 expect(results[0]).toMatchObject([{ data: undefined }, { data: undefined }])
76 expect(results[1]).toMatchObject([{ data: 1 }, { data: undefined }])
77 expect(results[2]).toMatchObject([{ data: 1 }, { data: 2 }])
78 })
79})