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

inject-queries.test.ts

71 lines2.0 KB

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