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

inject-is-mutating.test.ts

132 lines3.9 KB

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​
53 await vi.advanceTimersByTimeAsync(0)
54 rendered.fixture.detectChanges()
55 expect(rendered.getByText('mutating: 1')).toBeInTheDocument()
56​
57 await vi.advanceTimersByTimeAsync(11)
58 rendered.fixture.detectChanges()
59 expect(rendered.getByText('mutating: 0')).toBeInTheDocument()
60 })
61​
62 it('should be able to filter by mutationKey', async () => {
63 const key1 = queryKey()
64 const key2 = queryKey()
65​
66 @Component({
67 template: `<div>mutating: {{ isMutating() }}</div>`,
68 })
69 class Page {
70 readonly mutation1 = injectMutation(() => ({
71 mutationKey: key1,
72 mutationFn: () => sleep(10).then(() => 'data1'),
73 }))
74 readonly mutation2 = injectMutation(() => ({
75 mutationKey: key2,
76 mutationFn: () => sleep(100).then(() => 'data2'),
77 }))
78 readonly isMutating = injectIsMutating(() => ({ mutationKey: key1 }))
79 }
80​
81 const rendered = await render(Page)
82​
83 rendered.fixture.componentInstance.mutation1.mutate()
84 rendered.fixture.componentInstance.mutation2.mutate()
85​
86 await vi.advanceTimersByTimeAsync(0)
87 rendered.fixture.detectChanges()
88 expect(rendered.getByText('mutating: 1')).toBeInTheDocument()
89​
90 await vi.advanceTimersByTimeAsync(11)
91 rendered.fixture.detectChanges()
92 expect(rendered.getByText('mutating: 0')).toBeInTheDocument()
93 })
94​
95 it('should support signal reads in filter predicates', async () => {
96 const mutation = queryClient.getMutationCache().build(queryClient, {
97 mutationFn: () => sleep(100).then(() => 'data'),
98 })
99 void mutation.execute(undefined)
100​
101 @Component({
102 template: `<div>mutating: {{ isMutating() }}</div>`,
103 })
104 class Page {
105 readonly includeMutation = input.required<boolean>()
106 readonly isMutating = injectIsMutating(() => ({
107 predicate: () => this.includeMutation(),
108 }))
109 }
110​
111 const subscribe = vi.spyOn(queryClient.getMutationCache(), 'subscribe')
112 const includeMutation = signal(true)
113 const rendered = await render(Page, {
114 bindings: [inputBinding('includeMutation', includeMutation)],
115 })
116​
117 expect(rendered.getByText('mutating: 1')).toBeInTheDocument()
118​
119 includeMutation.set(false)
120 rendered.fixture.detectChanges()
121 expect(subscribe).toHaveBeenCalledTimes(1)
122 expect(rendered.getByText('mutating: 0')).toBeInTheDocument()
123 })
124​
125 describe('injection context', () => {
126 it('should throw NG0203 with descriptive error outside injection context', () => {
127 expect(() => {
128 injectIsMutating()
129 }).toThrow(/NG0203(.*?)injectIsMutating/)
130 })
131 })
132})