Open History to see the last commit
1import 'zone.js'
2import {
3 ChangeDetectionStrategy,
4 Component,
5 provideZoneChangeDetection,
6} from '@angular/core'
7import { TestBed } from '@angular/core/testing'
8import { sleep } from '@tanstack/query-test-utils'
9import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
10import {
11 QueryClient,
12 injectMutation,
13 injectQuery,
14 provideTanStackQuery,
15} from '..'
16β
17describe('adapter with Zone.js', () => {
18 let queryClient: QueryClient
19β
20 beforeEach(() => {
21 vi.useFakeTimers({ shouldAdvanceTime: true })
22 queryClient = new QueryClient()
23β
24 TestBed.resetTestingModule()
25 TestBed.configureTestingModule({
26 providers: [
27 provideZoneChangeDetection(),
28 provideTanStackQuery(queryClient),
29 ],
30 })
31 })
32β
33 afterEach(() => {
34 vi.useRealTimers()
35 })
36β
37 it('supports injectQuery in a Zone.js app', async () => {
38 @Component({
39 selector: 'zone-query-test',
40 template: '',
41 changeDetection: ChangeDetectionStrategy.OnPush,
42 })
43 class ZoneQueryTestComponent {
44 query = injectQuery(() => ({
45 queryKey: ['zone-query'],
46 queryFn: async () => {
47 await sleep(10)
48 return 'query-data'
49 },
50 }))
51 }
52β
53 const fixture = TestBed.createComponent(ZoneQueryTestComponent)
54 fixture.detectChanges()
55β
56 const query = fixture.componentInstance.query
57 expect(query.status()).toBe('pending')
58β
59 const stablePromise = fixture.whenStable()
60 await vi.advanceTimersByTimeAsync(20)
61 await stablePromise
62β
63 expect(query.status()).toBe('success')
64 expect(query.data()).toBe('query-data')
65 })
66β
67 it('supports injectMutation in a Zone.js app', async () => {
68 @Component({
69 selector: 'zone-mutation-test',
70 template: '',
71 changeDetection: ChangeDetectionStrategy.OnPush,
72 })
73 class ZoneMutationTestComponent {
74 mutation = injectMutation(() => ({
75 mutationKey: ['zone-mutation'],
76 mutationFn: async (value: string) => {
77 await sleep(10)
78 return `mutated-${value}`
79 },
80 }))
81 }
82β
83 const fixture = TestBed.createComponent(ZoneMutationTestComponent)
84 fixture.detectChanges()
85β
86 const mutation = fixture.componentInstance.mutation
87 mutation.mutate('value')
88β
89 const stablePromise = fixture.whenStable()
90 await vi.advanceTimersByTimeAsync(20)
91 await stablePromise
92β
93 expect(mutation.status()).toBe('success')
94 expect(mutation.data()).toBe('mutated-value')
95 })
96})