@@ -28,6 +28,12 @@ async function buildCtx(genFilter?: (e: { name: string }) => boolean): Promise<G
2828 } ;
2929}
3030
31+ const pick = ( files : { path : string ; content : string } [ ] , suffix : string ) => {
32+ const f = files . find ( ( x ) => x . path . endsWith ( suffix ) ) ;
33+ if ( ! f ) throw new Error ( `no emitted file ending in ${ suffix } — got ${ files . map ( ( x ) => x . path ) . join ( ", " ) } ` ) ;
34+ return f ;
35+ } ;
36+
3137describe ( "tanstackQuery() factory" , ( ) => {
3238 test ( "returns a Generator named 'tanstack-query'" , ( ) => {
3339 const gen = tanstackQuery ( ) ;
@@ -38,8 +44,7 @@ describe("tanstackQuery() factory", () => {
3844 test ( "emits Subscriber.hooks.ts with the query-key factory and 5 hooks" , async ( ) => {
3945 const ctx = await buildCtx ( ) ;
4046 const files = await tanstackQuery ( ) . generate ( ctx ) ;
41- expect ( files . length ) . toBe ( 1 ) ;
42- const file = files [ 0 ] ! ;
47+ const file = pick ( files , ".hooks.ts" ) ;
4348 expect ( file . path ) . toBe ( "Subscriber.hooks.ts" ) ;
4449 expect ( file . content ) . toContain ( "@generated by @metaobjectsdev/codegen-ts" ) ;
4550 // Query-key factory:
@@ -57,28 +62,28 @@ describe("tanstackQuery() factory", () => {
5762
5863 test ( "mutation hooks invalidate via subscriberKeys.all()" , async ( ) => {
5964 const ctx = await buildCtx ( ) ;
60- const file = ( await tanstackQuery ( ) . generate ( ctx ) ) [ 0 ] ! ;
65+ const file = pick ( await tanstackQuery ( ) . generate ( ctx ) , ".hooks.ts" ) ;
6166 // Each mutation should call invalidateQueries with subscriberKeys.all().
6267 const matches = file . content . match ( / i n v a l i d a t e Q u e r i e s \( \{ \s * q u e r y K e y : \s * s u b s c r i b e r K e y s \. a l l \( \) / g) ;
6368 expect ( matches ?. length ) . toBe ( 3 ) ; // create, update, delete
6469 } ) ;
6570
6671 test ( "each hook accepts an opts override parameter" , async ( ) => {
6772 const ctx = await buildCtx ( ) ;
68- const file = ( await tanstackQuery ( ) . generate ( ctx ) ) [ 0 ] ! ;
73+ const file = pick ( await tanstackQuery ( ) . generate ( ctx ) , ".hooks.ts" ) ;
6974 expect ( file . content ) . toContain ( "opts?: Omit<UseQueryOptions" ) ;
7075 expect ( file . content ) . toContain ( "opts?: Omit<UseMutationOptions" ) ;
7176 } ) ;
7277
7378 test ( "uses Subscriber.$path from entity constants for URLs" , async ( ) => {
7479 const ctx = await buildCtx ( ) ;
75- const file = ( await tanstackQuery ( ) . generate ( ctx ) ) [ 0 ] ! ;
80+ const file = pick ( await tanstackQuery ( ) . generate ( ctx ) , ".hooks.ts" ) ;
7681 expect ( file . content ) . toContain ( "Subscriber.$path" ) ;
7782 } ) ;
7883
7984 test ( "prefixes fetch URLs with Subscriber.$apiPrefix" , async ( ) => {
8085 const ctx = await buildCtx ( ) ;
81- const file = ( await tanstackQuery ( ) . generate ( ctx ) ) [ 0 ] ! ;
86+ const file = pick ( await tanstackQuery ( ) . generate ( ctx ) , ".hooks.ts" ) ;
8287 // Every fetch URL should have $apiPrefix before $path so users can set apiPrefix once.
8388 expect ( file . content ) . toContain ( "Subscriber.$apiPrefix" ) ;
8489 // Specifically the get-one pattern:
0 commit comments