1- import type { OrganizationDomainResource , OrganizationMembershipResource } from '@clerk/types' ;
1+ import type { ClerkPaginatedResponse , OrganizationDomainResource , OrganizationMembershipResource } from '@clerk/types' ;
22import { describe , it } from '@jest/globals' ;
33import userEvent from '@testing-library/user-event' ;
44
@@ -30,12 +30,14 @@ describe('OrganizationSettings', () => {
3030 ) ;
3131
3232 const { getByText } = render ( < OrganizationSettings /> , { wrapper } ) ;
33+
3334 await waitFor ( ( ) => {
3435 expect ( fixtures . clerk . organization ?. getMemberships ) . toHaveBeenCalled ( ) ;
35- expect ( getByText ( 'Settings' ) ) . toBeDefined ( ) ;
36- expect ( getByText ( 'Org1' , { exact : false } ) . closest ( 'button' ) ) . not . toBeNull ( ) ;
37- expect ( getByText ( / l e a v e o r g a n i z a t i o n / i, { exact : false } ) . closest ( 'button' ) ) . toHaveAttribute ( 'disabled' ) ;
3836 } ) ;
37+
38+ expect ( getByText ( 'Settings' ) ) . toBeDefined ( ) ;
39+ expect ( getByText ( 'Org1' , { exact : false } ) . closest ( 'button' ) ) . not . toBeNull ( ) ;
40+ expect ( getByText ( / l e a v e o r g a n i z a t i o n / i, { exact : false } ) . closest ( 'button' ) ) . toHaveAttribute ( 'disabled' ) ;
3941 } ) ;
4042
4143 it ( 'enables organization profile button and enables leave when user is admin and there is more' , async ( ) => {
@@ -63,7 +65,10 @@ describe('OrganizationSettings', () => {
6365 } ) ;
6466
6567 it . skip ( 'disables organization profile button and enables leave when user is not admin' , async ( ) => {
66- const adminsList : OrganizationMembershipResource [ ] = [ createFakeMember ( { id : '1' , orgId : '1' , role : 'admin' } ) ] ;
68+ const adminsList : ClerkPaginatedResponse < OrganizationMembershipResource > = {
69+ data : [ createFakeMember ( { id : '1' , orgId : '1' , role : 'admin' } ) ] ,
70+ total_count : 1 ,
71+ } ;
6772
6873 const { wrapper, fixtures } = await createFixtures ( f => {
6974 f . withOrganizations ( ) ;
@@ -98,7 +103,7 @@ describe('OrganizationSettings', () => {
98103 expect ( fixtures . clerk . organization ?. getDomains ) . not . toBeCalled ( ) ;
99104 } ) ;
100105
101- it ( 'shows domains when `read` permission exists' , async ( ) => {
106+ it ( 'shows domains when `read` permission exists but hides the Add domain button ' , async ( ) => {
102107 const { wrapper, fixtures } = await createFixtures ( f => {
103108 f . withOrganizations ( ) ;
104109 f . withOrganizationDomains ( ) ;
@@ -117,6 +122,30 @@ describe('OrganizationSettings', () => {
117122
118123 await new Promise ( r => setTimeout ( r , 100 ) ) ;
119124 expect ( queryByText ( 'Verified domains' ) ) . toBeInTheDocument ( ) ;
125+ expect ( queryByText ( 'Add domain' ) ) . not . toBeInTheDocument ( ) ;
126+ expect ( fixtures . clerk . organization ?. getDomains ) . toBeCalled ( ) ;
127+ } ) ;
128+
129+ it ( 'shows domains and shows the Add domain button when `org:sys_domains:manage` exists' , async ( ) => {
130+ const { wrapper, fixtures } = await createFixtures ( f => {
131+ f . withOrganizations ( ) ;
132+ f . withOrganizationDomains ( ) ;
133+ f . withUser ( {
134+ email_addresses :
[ '[email protected] ' ] , 135+ organization_memberships : [ { name : 'Org1' , permissions : [ 'org:sys_domains:read' , 'org:sys_domains:manage' ] } ] ,
136+ } ) ;
137+ } ) ;
138+ fixtures . clerk . organization ?. getDomains . mockReturnValue (
139+ Promise . resolve ( {
140+ data : [ ] ,
141+ total_count : 0 ,
142+ } ) ,
143+ ) ;
144+ const { queryByText } = await act ( ( ) => render ( < OrganizationSettings /> , { wrapper } ) ) ;
145+
146+ await new Promise ( r => setTimeout ( r , 100 ) ) ;
147+ expect ( queryByText ( 'Verified domains' ) ) . toBeInTheDocument ( ) ;
148+ expect ( queryByText ( 'Add domain' ) ) . toBeInTheDocument ( ) ;
120149 expect ( fixtures . clerk . organization ?. getDomains ) . toBeCalled ( ) ;
121150 } ) ;
122151
@@ -156,18 +185,21 @@ describe('OrganizationSettings', () => {
156185 } ) ;
157186
158187 it . skip ( 'disabled leave organization button with delete organization button' , async ( ) => {
159- const adminsList : OrganizationMembershipResource [ ] = [
160- createFakeMember ( {
161- id : '1' ,
162- orgId : '1' ,
163- role : 'admin' ,
164- } ) ,
165- createFakeMember ( {
166- id : '2' ,
167- orgId : '1' ,
168- role : 'admin' ,
169- } ) ,
170- ] ;
188+ const adminsList : ClerkPaginatedResponse < OrganizationMembershipResource > = {
189+ data : [
190+ createFakeMember ( {
191+ id : '1' ,
192+ orgId : '1' ,
193+ role : 'admin' ,
194+ } ) ,
195+ createFakeMember ( {
196+ id : '2' ,
197+ orgId : '1' ,
198+ role : 'admin' ,
199+ } ) ,
200+ ] ,
201+ total_count : 2 ,
202+ } ;
171203
172204 const { wrapper, fixtures } = await createFixtures ( f => {
173205 f . withOrganizations ( ) ;
@@ -212,21 +244,18 @@ describe('OrganizationSettings', () => {
212244 expect ( fixtures . router . navigate ) . toHaveBeenCalledWith ( 'profile' ) ;
213245 } ) ;
214246
215- it ( 'navigates to Leave Organization page when clicking on the respective button and user is not admin' , async ( ) => {
216- const adminsList : OrganizationMembershipResource [ ] = [ createFakeMember ( { id : '1' , orgId : '1' , role : 'admin' } ) ] ;
217-
247+ // TODO(@panteliselef): Update this test to allow user to leave an org, only if there will be at least one person left with the minimum set of permissions
248+ it ( 'navigates to Leave Organization page when clicking on the respective button' , async ( ) => {
218249 const { wrapper, fixtures } = await createFixtures ( f => {
219250 f . withOrganizations ( ) ;
220251 f . withUser ( {
221252 email_addresses :
[ '[email protected] ' ] , 222- organization_memberships : [ { name : 'Org1' , role : 'basic_member' } ] ,
253+ organization_memberships : [ { name : 'Org1' , permissions : [ ] } ] ,
223254 } ) ;
224255 } ) ;
225256
226- fixtures . clerk . organization ?. getMemberships . mockReturnValue ( Promise . resolve ( adminsList ) ) ;
227257 const { findByText } = render ( < OrganizationSettings /> , { wrapper } ) ;
228258 await waitFor ( async ( ) => {
229- // expect(fixtures.clerk.organization?.getMemberships).toHaveBeenCalled();
230259 await userEvent . click ( await findByText ( / l e a v e o r g a n i z a t i o n / i, { exact : false } ) ) ;
231260 } ) ;
232261 expect ( fixtures . router . navigate ) . toHaveBeenCalledWith ( 'leave' ) ;
0 commit comments