Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { TestBed } from '@angular/core/testing'
3import { Component, input, inputBinding, signal } from '@angular/core'
4import { render } from '@testing-library/angular'
5import { queryKey, sleep } from '@tanstack/query-test-utils'
6import {
7 QueryClient,
8 injectIsMutating,
9 injectMutation,
10 provideTanStackQuery,
11} from '..'
12import { provideAngularQueryChangeDetection } from './test-utils'
13β
14describe('injectIsMutating', () => {
15 let queryClient: QueryClient
16β
17 beforeEach(() => {
18 vi.useFakeTimers()
19 queryClient = new QueryClient()
20β
21 TestBed.configureTestingModule({
22 providers: [
23 provideAngularQueryChangeDetection(),
24 provideTanStackQuery(() => queryClient),
25 ],
26 })
27 })
28β
29 afterEach(() => {
30 vi.useRealTimers()
31 })
32β
33 it('should properly return isMutating state', async () => {
34 const key = queryKey()
35β
36 @Component({
37 template: `<div>mutating: {{ isMutating() }}</div>`,
38 })
39 class Page {
40 readonly mutation = injectMutation(() => ({
41 mutationKey: key,
42 mutationFn: (params: { par1: string }) => sleep(10).then(() => params),
43 }))
44 readonly isMutating = injectIsMutating()
45 }
46β
47 const rendered = await render(Page)
48β
49 expect(rendered.getByText('mutating: 0')).toBeInTheDocument()
50β
51 rendered.fixture.componentInstance.mutation.mutate({ par1: 'par1' })
52 await vi.advanceTimersByTimeAsync(0)
53 rendered.fixture.detectChanges()
54 expect(rendered.getByText('mutating: 1')).toBeInTheDocument()
55 await vi.advanceTimersByTimeAsync(11)
56 rendered.fixture.detectChanges()
57 expect(rendered.getByText('mutating: 0')).toBeInTheDocument()
58 })
59β
60 it('should be able to filter by mutationKey', async () => {
61 const key1 = queryKey()
62 const key2 = queryKey()
63β
64 @Component({
65 template: `<div>mutating: {{ isMutating() }}</div>`,
66 })
67 class Page {
68 readonly mutation1 = injectMutation(() => ({
69 mutationKey: key1,
70 mutationFn: () => sleep(10).then(() => 'data1'),
71 }))
72 readonly mutation2 = injectMutation(() => ({
73 mutationKey: key2,
74 mutationFn: () => sleep(100).then(() => 'data2'),
75 }))
76 readonly isMutating = injectIsMutating(() => ({ mutationKey: key1 }))
77 }
78β
79 const rendered = await render(Page)
80β
81 rendered.fixture.componentInstance.mutation1.mutate()
82 rendered.fixture.componentInstance.mutation2.mutate()
83 await vi.advanceTimersByTimeAsync(0)
84 rendered.fixture.detectChanges()
85 expect(rendered.getByText('mutating: 1')).toBeInTheDocument()
86 await vi.advanceTimersByTimeAsync(11)
87 rendered.fixture.detectChanges()
88 expect(rendered.getByText('mutating: 0')).toBeInTheDocument()
89 })
90β
91 it('should support signal reads in filter predicates', async () => {
92 const mutation = queryClient.getMutationCache().build(queryClient, {
93 mutationFn: () => sleep(100).then(() => 'data'),
94 })
95 void mutation.execute(undefined)
96β
97 @Component({
98 template: `<div>mutating: {{ isMutating() }}</div>`,
99 })
100 class Page {
101 readonly includeMutation = input.required<boolean>()
102 readonly isMutating = injectIsMutating(() => ({
103 predicate: () => this.includeMutation(),
104 }))
105 }
106β
107 const subscribe = vi.spyOn(queryClient.getMutationCache(), 'subscribe')
108 const includeMutation = signal(true)
109 const rendered = await render(Page, {
110 bindings: [inputBinding('includeMutation', includeMutation)],
111 })
112β
113 expect(rendered.getByText('mutating: 1')).toBeInTheDocument()
114β
115 includeMutation.set(false)
116 rendered.fixture.detectChanges()
117 expect(subscribe).toHaveBeenCalledTimes(1)
118 expect(rendered.getByText('mutating: 0')).toBeInTheDocument()
119 })
120β
121 describe('injection context', () => {
122 it('should throw NG0203 with descriptive error outside injection context', () => {
123 expect(() => {
124 injectIsMutating()
125 }).toThrow(/NG0203(.*?)injectIsMutating/)
126 })
127 })
128})