99import { TestBed } from '@angular/core/testing'
1010import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest'
1111import { By } from '@angular/platform-browser'
12+ import { render } from '@testing-library/angular'
1213import { queryKey , sleep } from '@tanstack/query-test-utils'
1314import { QueryClient , injectMutation , provideTanStackQuery } from '..'
1415import { expectSignals , setFixtureSignalInputs } from './test-utils'
@@ -94,24 +95,36 @@ describe('injectMutation', () => {
9495
9596 it ( 'should return data when request succeeds' , async ( ) => {
9697 const result = 'Mock data'
97- const mutation = TestBed . runInInjectionContext ( ( ) => {
98- return injectMutation ( ( ) => ( {
98+
99+ @Component ( {
100+ template : `
101+ <div>isIdle: {{ mutation.isIdle() }}</div>
102+ <div>isPending: {{ mutation.isPending() }}</div>
103+ <div>isError: {{ mutation.isError() }}</div>
104+ <div>isSuccess: {{ mutation.isSuccess() }}</div>
105+ <div>data: {{ mutation.data() ?? 'none' }}</div>
106+ <div>error: {{ mutation.error()?.message ?? 'none' }}</div>
107+ ` ,
108+ } )
109+ class Page {
110+ readonly mutation = injectMutation ( ( ) => ( {
99111 mutationFn : ( params : string ) => sleep ( 10 ) . then ( ( ) => params ) ,
100112 } ) )
101- } )
113+ }
102114
103- mutation . mutate ( result )
115+ const rendered = await render ( Page )
104116
105- await vi . advanceTimersByTimeAsync ( 11 )
117+ rendered . fixture . componentInstance . mutation . mutate ( result )
106118
107- expectSignals ( mutation , {
108- isIdle : false ,
109- isPending : false ,
110- isError : false ,
111- isSuccess : true ,
112- data : result ,
113- error : null ,
114- } )
119+ await vi . advanceTimersByTimeAsync ( 11 )
120+ rendered . fixture . detectChanges ( )
121+
122+ expect ( rendered . getByText ( 'isIdle: false' ) ) . toBeInTheDocument ( )
123+ expect ( rendered . getByText ( 'isPending: false' ) ) . toBeInTheDocument ( )
124+ expect ( rendered . getByText ( 'isError: false' ) ) . toBeInTheDocument ( )
125+ expect ( rendered . getByText ( 'isSuccess: true' ) ) . toBeInTheDocument ( )
126+ expect ( rendered . getByText ( `data: ${ result } ` ) ) . toBeInTheDocument ( )
127+ expect ( rendered . getByText ( 'error: none' ) ) . toBeInTheDocument ( )
115128 } )
116129
117130 it ( 'should update mutation when reactive options change' , ( ) => {
@@ -138,31 +151,44 @@ describe('injectMutation', () => {
138151 } )
139152
140153 it ( 'should reset state after invoking mutation.reset' , async ( ) => {
141- const mutation = TestBed . runInInjectionContext ( ( ) => {
142- return injectMutation ( ( ) => ( {
154+ @Component ( {
155+ template : `
156+ <div>isIdle: {{ mutation.isIdle() }}</div>
157+ <div>isPending: {{ mutation.isPending() }}</div>
158+ <div>isError: {{ mutation.isError() }}</div>
159+ <div>isSuccess: {{ mutation.isSuccess() }}</div>
160+ <div>data: {{ mutation.data() ?? 'none' }}</div>
161+ <div>error: {{ mutation.error()?.message ?? 'none' }}</div>
162+ ` ,
163+ } )
164+ class Page {
165+ readonly mutation = injectMutation ( ( ) => ( {
143166 mutationFn : ( ) =>
144167 sleep ( 10 ) . then ( ( ) => Promise . reject ( new Error ( 'Some error' ) ) ) ,
145168 } ) )
146- } )
169+ }
147170
148- mutation . mutate ( )
171+ const rendered = await render ( Page )
172+
173+ rendered . fixture . componentInstance . mutation . mutate ( )
149174
150175 await vi . advanceTimersByTimeAsync ( 11 )
176+ rendered . fixture . detectChanges ( )
151177
152- expect ( mutation . isError ( ) ) . toBe ( true )
178+ expect ( rendered . getByText ( 'isError: true' ) ) . toBeInTheDocument ( )
179+ expect ( rendered . getByText ( 'error: Some error' ) ) . toBeInTheDocument ( )
153180
154- mutation . reset ( )
181+ rendered . fixture . componentInstance . mutation . reset ( )
155182
156183 await vi . advanceTimersByTimeAsync ( 0 )
157-
158- expectSignals ( mutation , {
159- isIdle : true ,
160- isPending : false ,
161- isError : false ,
162- isSuccess : false ,
163- data : undefined ,
164- error : null ,
165- } )
184+ rendered . fixture . detectChanges ( )
185+
186+ expect ( rendered . getByText ( 'isIdle: true' ) ) . toBeInTheDocument ( )
187+ expect ( rendered . getByText ( 'isPending: false' ) ) . toBeInTheDocument ( )
188+ expect ( rendered . getByText ( 'isError: false' ) ) . toBeInTheDocument ( )
189+ expect ( rendered . getByText ( 'isSuccess: false' ) ) . toBeInTheDocument ( )
190+ expect ( rendered . getByText ( 'data: none' ) ) . toBeInTheDocument ( )
191+ expect ( rendered . getByText ( 'error: none' ) ) . toBeInTheDocument ( )
166192 } )
167193
168194 describe ( 'side effects' , ( ) => {
0 commit comments