MCPcopy Create free account
hub / github.com/TanStack/query / TestComponent

Class TestComponent

packages/angular-query/src/__tests__/inject-query.test.ts:104–245  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

102 const key = createKey()
103
104 @Component({
105 selector: 'app-test',
106 template: '',
107 changeDetection: ChangeDetectionStrategy.OnPush,
108 })
109 class TestComponent {
110 // unspecified query function should default to unknown
111 noQueryFn = injectQuery(() => ({
112 queryKey: key,
113 }))
114
115 // it should infer the result type from the query function
116 fromQueryFn = injectQuery(() => ({
117 queryKey: key,
118 queryFn: () => 'test',
119 }))
120
121 // it should be possible to specify the result type
122 withResult = injectQuery<string>(() => ({
123 queryKey: key,
124 queryFn: () => 'test',
125 }))
126
127 // it should be possible to specify the error type
128 withError = injectQuery<string, { message: string }>(() => ({
129 queryKey: key,
130 queryFn: () => 'test',
131 }))
132
133 // it should infer the result type from the configuration
134 withResultInfer = injectQuery(() => ({
135 queryKey: key,
136 queryFn: () => true,
137 }))
138
139 // it should be possible to specify a union type as result type
140 unionTypeSync = injectQuery(() => ({
141 queryKey: key,
142 queryFn: () => (Math.random() > 0.5 ? ('a' as const) : ('b' as const)),
143 }))
144
145 unionTypeAsync = injectQuery<'a' | 'b'>(() => ({
146 queryKey: key,
147 queryFn: () => Promise.resolve(Math.random() > 0.5 ? 'a' : 'b'),
148 }))
149
150 // it should infer the result type from a generic query function
151 fromGenericQueryFn = (() => {
152 function queryFn<T = string>(): Promise<T> {
153 return Promise.resolve({} as T)
154 }
155 return injectQuery(() => ({
156 queryKey: key,
157 queryFn: () => queryFn(),
158 }))
159 })()
160
161 // todo use query options?

Callers

nothing calls this directly

Calls 7

sleepFunction · 0.90
injectQueryFunction · 0.85
createWrappedQueryFunction · 0.85
getMethod · 0.80
ComponentFunction · 0.50
queryFnFunction · 0.50

Tested by

no test coverage detected