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