11import { useOrganization } from '@clerk/shared/react' ;
2+ import { createDeferredPromise } from '@clerk/shared/utils/index' ;
23import { describe , expect , it } from 'vitest' ;
34
45import { bindCreateFixtures } from '@/test/create-fixtures' ;
@@ -42,7 +43,7 @@ const undefinedPaginatedResource = {
4243
4344describe ( 'useOrganization' , ( ) => {
4445 it ( 'returns default values' , async ( ) => {
45- const { wrapper } = await createFixtures ( f => {
46+ const { wrapper, fixtures } = await createFixtures ( f => {
4647 f . withOrganizations ( ) ;
4748 f . withUser ( {
4849 email_addresses :
[ '[email protected] ' ] , @@ -65,10 +66,15 @@ describe('useOrganization', () => {
6566 expect ( result . current . memberships ) . toEqual ( expect . objectContaining ( undefinedPaginatedResource ) ) ;
6667 expect ( result . current . domains ) . toEqual ( expect . objectContaining ( undefinedPaginatedResource ) ) ;
6768 expect ( result . current . membershipRequests ) . toEqual ( expect . objectContaining ( undefinedPaginatedResource ) ) ;
69+
70+ expect ( fixtures . clerk . organization ?. getMemberships ) . not . toHaveBeenCalled ( ) ;
71+ expect ( fixtures . clerk . organization ?. getDomains ) . not . toHaveBeenCalled ( ) ;
72+ expect ( fixtures . clerk . organization ?. getMembershipRequests ) . not . toHaveBeenCalled ( ) ;
73+ expect ( fixtures . clerk . organization ?. getInvitations ) . not . toHaveBeenCalled ( ) ;
6874 } ) ;
6975
70- it ( 'returns null when a organization is not active ' , async ( ) => {
71- const { wrapper } = await createFixtures ( f => {
76+ it ( 'returns null when a organization is not active' , async ( ) => {
77+ const { wrapper, fixtures } = await createFixtures ( f => {
7278 f . withOrganizations ( ) ;
7379 f . withUser ( {
7480 email_addresses :
[ '[email protected] ' ] , @@ -83,6 +89,8 @@ describe('useOrganization', () => {
8389 expect ( result . current . memberships ) . toBeNull ( ) ;
8490 expect ( result . current . domains ) . toBeNull ( ) ;
8591 expect ( result . current . membershipRequests ) . toBeNull ( ) ;
92+
93+ expect ( fixtures . clerk . organization ) . toBeNull ( ) ;
8694 } ) ;
8795
8896 describe ( 'memberships' , ( ) => {
@@ -543,52 +551,34 @@ describe('useOrganization', () => {
543551 await waitFor ( ( ) => expect ( result . current . invitations ?. isLoading ) . toBe ( false ) ) ;
544552 expect ( result . current . invitations ?. isFetching ) . toBe ( false ) ;
545553
546- fixtures . clerk . organization ?. getInvitations . mockReturnValueOnce (
547- Promise . resolve ( {
548- data : [
549- createFakeOrganizationInvitation ( {
550- id : '1' ,
551- emailAddress :
'[email protected] ' , 552- organizationId : '1' ,
553- createdAt : new Date ( '2022-01-01' ) ,
554- } ) ,
555- createFakeOrganizationInvitation ( {
556- id : '2' ,
557- emailAddress :
'[email protected] ' , 558- organizationId : '1' ,
559- createdAt : new Date ( '2022-01-01' ) ,
560- } ) ,
561- ] ,
562- total_count : 4 ,
563- } ) ,
564- ) ;
565-
566- fixtures . clerk . organization ?. getInvitations . mockReturnValueOnce (
567- Promise . resolve ( {
568- data : [
569- createFakeOrganizationInvitation ( {
570- id : '3' ,
571- emailAddress :
'[email protected] ' , 572- organizationId : '1' ,
573- createdAt : new Date ( '2022-01-01' ) ,
574- } ) ,
575- createFakeOrganizationInvitation ( {
576- id : '4' ,
577- emailAddress :
'[email protected] ' , 578- organizationId : '1' ,
579- createdAt : new Date ( '2022-01-01' ) ,
580- } ) ,
581- ] ,
582- total_count : 4 ,
583- } ) ,
584- ) ;
554+ const deferred = createDeferredPromise ( ) ;
585555
556+ fixtures . clerk . organization ?. getInvitations . mockReturnValueOnce ( deferred . promise ) ;
586557 act ( ( ) => result . current . invitations ?. fetchNext ?.( ) ) ;
587558
588559 await waitFor ( ( ) => expect ( result . current . invitations ?. isFetching ) . toBe ( true ) ) ;
589560 expect ( result . current . invitations ?. isLoading ) . toBe ( false ) ;
590561
562+ deferred . resolve ( {
563+ data : [
564+ createFakeOrganizationInvitation ( {
565+ id : '3' ,
566+ emailAddress :
'[email protected] ' , 567+ organizationId : '1' ,
568+ createdAt : new Date ( '2022-01-01' ) ,
569+ } ) ,
570+ createFakeOrganizationInvitation ( {
571+ id : '4' ,
572+ emailAddress :
'[email protected] ' , 573+ organizationId : '1' ,
574+ createdAt : new Date ( '2022-01-01' ) ,
575+ } ) ,
576+ ] ,
577+ total_count : 4 ,
578+ } ) ;
579+
591580 await waitFor ( ( ) => expect ( result . current . invitations ?. isFetching ) . toBe ( false ) ) ;
581+
592582 expect ( result . current . invitations ?. data ) . toEqual (
593583 expect . arrayContaining ( [
594584 expect . objectContaining ( {
0 commit comments