1- import { renderHook , waitFor } from '@testing-library/react' ;
1+ import { render , renderHook , screen , waitFor } from '@testing-library/react' ;
2+ import React from 'react' ;
23import { beforeEach , describe , expect , it , vi } from 'vitest' ;
34
45const mockUser : any = { id : 'user_1' } ;
@@ -7,7 +8,10 @@ const mockOrganization: any = { id: 'org_1' };
78const getPlansSpy = vi . fn ( ( args : any ) =>
89 Promise . resolve ( {
910 // pageSize maps to limit; default to 10 if missing
10- data : Array . from ( { length : args . limit ?? args . pageSize ?? 10 } , ( _ , i ) => ( { id : `plan_${ i + 1 } ` } ) ) ,
11+ data : Array . from < Partial < BillingPlanResource > , Partial < BillingPlanResource > > (
12+ { length : args . limit ?? args . pageSize ?? 10 } ,
13+ ( _ , i ) => ( { id : `plan_${ i + 1 } ` , forPayerType : args . for } ) ,
14+ ) ,
1115 total_count : 25 ,
1216 } ) ,
1317) ;
@@ -37,6 +41,8 @@ vi.mock('../../contexts', () => {
3741 } ;
3842} ) ;
3943
44+ import type { BillingPlanResource } from '@clerk/types' ;
45+
4046import { usePlans } from '../usePlans' ;
4147import { wrapper } from './wrapper' ;
4248
@@ -112,4 +118,69 @@ describe('usePlans', () => {
112118 expect ( getPlansSpy . mock . calls [ 0 ] [ 0 ] ) . toStrictEqual ( { for : 'organization' , pageSize : 3 , initialPage : 1 } ) ;
113119 expect ( result . current . data . length ) . toBe ( 3 ) ;
114120 } ) ;
121+
122+ it ( 'mounts user and organization hooks together and renders their respective data' , async ( ) => {
123+ const DualPlans = ( ) => {
124+ const userPlans = usePlans ( { initialPage : 1 , pageSize : 2 } ) ;
125+ const orgPlans = usePlans ( { initialPage : 1 , pageSize : 2 , for : 'organization' } as any ) ;
126+
127+ return (
128+ < >
129+ < div data-testid = 'user-count' > { userPlans . data . length } </ div >
130+ < div data-testid = 'org-count' > { orgPlans . data . length } </ div >
131+ </ >
132+ ) ;
133+ } ;
134+
135+ render ( < DualPlans /> , { wrapper } ) ;
136+
137+ await waitFor ( ( ) => expect ( screen . getByTestId ( 'user-count' ) . textContent ) . toBe ( '2' ) ) ;
138+ await waitFor ( ( ) => expect ( screen . getByTestId ( 'org-count' ) . textContent ) . toBe ( '2' ) ) ;
139+
140+ expect ( getPlansSpy ) . toHaveBeenCalledTimes ( 2 ) ;
141+ const calls = getPlansSpy . mock . calls . map ( c => c [ 0 ] ) ;
142+ expect ( calls ) . toEqual (
143+ expect . arrayContaining ( [
144+ { for : 'user' , initialPage : 1 , pageSize : 2 } ,
145+ { for : 'organization' , initialPage : 1 , pageSize : 2 } ,
146+ ] ) ,
147+ ) ;
148+
149+ // Ensure orgId does not leak into the fetcher params
150+ for ( const call of calls ) {
151+ expect ( call ) . not . toHaveProperty ( 'orgId' ) ;
152+ }
153+ } ) ;
154+
155+ it ( 'conditionally renders hooks based on prop passed to render' , async ( ) => {
156+ const UserPlansCount = ( ) => {
157+ const userPlans = usePlans ( { initialPage : 1 , pageSize : 2 } ) ;
158+ return < div data-testid = 'user-type' > { userPlans . data . map ( p => p . forPayerType ) [ 0 ] } </ div > ;
159+ } ;
160+
161+ const OrgPlansCount = ( ) => {
162+ const orgPlans = usePlans ( { initialPage : 1 , pageSize : 2 , for : 'organization' } as any ) ;
163+ return < div data-testid = 'org-type' > { orgPlans . data . map ( p => p . forPayerType ) [ 0 ] } </ div > ;
164+ } ;
165+
166+ const Conditional = ( { showOrg } : { showOrg : boolean } ) => ( showOrg ? < OrgPlansCount /> : < UserPlansCount /> ) ;
167+
168+ const { rerender } = render ( < Conditional showOrg = { false } /> , { wrapper } ) ;
169+
170+ await waitFor ( ( ) => expect ( screen . getByTestId ( 'user-type' ) . textContent ) . toBe ( 'user' ) ) ;
171+ expect ( getPlansSpy ) . toHaveBeenCalledTimes ( 1 ) ;
172+ expect ( getPlansSpy . mock . calls [ 0 ] [ 0 ] ) . toStrictEqual ( { for : 'user' , initialPage : 1 , pageSize : 2 } ) ;
173+
174+ rerender ( < Conditional showOrg /> ) ;
175+
176+ await waitFor ( ( ) => expect ( screen . getByTestId ( 'org-type' ) . textContent ) . toBe ( 'organization' ) ) ;
177+ expect ( getPlansSpy ) . toHaveBeenCalledTimes ( 2 ) ;
178+ const calls = getPlansSpy . mock . calls . map ( c => c [ 0 ] ) ;
179+ expect ( calls ) . toEqual (
180+ expect . arrayContaining ( [
181+ { for : 'user' , initialPage : 1 , pageSize : 2 } ,
182+ { for : 'organization' , initialPage : 1 , pageSize : 2 } ,
183+ ] ) ,
184+ ) ;
185+ } ) ;
115186} ) ;
0 commit comments