Skip to content

Commit dca6a65

Browse files
feat(ui): prompt for reverification before deleting an organization (#10871)
1 parent c71b7d3 commit dca6a65

3 files changed

Lines changed: 66 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/ui': patch
3+
---
4+
5+
`<OrganizationProfile />` now prompts for reverification before deleting an organization, when the instance requires it.

‎packages/ui/src/components/OrganizationProfile/ActionConfirmationPage.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useOrganization, useOrganizationList, useUser } from '@clerk/shared/react';
1+
import { useOrganization, useOrganizationList, useReverification, useUser } from '@clerk/shared/react';
22

33
import { useCardState, withCardStateProvider } from '@/ui/elements/contexts';
44
import { Form } from '@/ui/elements/Form';
@@ -72,7 +72,8 @@ type DeleteOrganizationFormProps = FormProps;
7272
export const DeleteOrganizationForm = (props: DeleteOrganizationFormProps) => {
7373
const { organization, membership } = useOrganization();
7474

75-
const deleteOrg = useLeaveWithRevalidations(organization?.destroy);
75+
const destroyOrgWithReverification = useReverification(() => organization?.destroy());
76+
const deleteOrg = useLeaveWithRevalidations(destroyOrgWithReverification);
7677

7778
if (!organization || !membership) {
7879
return null;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { ClerkAPIResponseError } from '@clerk/shared/error';
2+
import type { DeletedObjectResource } from '@clerk/shared/types';
3+
import { act, waitFor } from '@testing-library/react';
4+
import { describe, expect, it, vi } from 'vitest';
5+
6+
import { bindCreateFixtures } from '@/test/create-fixtures';
7+
import { render } from '@/test/utils';
8+
import { CardStateProvider } from '@/ui/elements/contexts';
9+
10+
import { DeleteOrganizationForm } from '../ActionConfirmationPage';
11+
12+
const { createFixtures } = bindCreateFixtures('OrganizationProfile');
13+
14+
describe('DeleteOrganizationPage', () => {
15+
it('deletes the organization after reverification when it is required', async () => {
16+
const { wrapper, fixtures } = await createFixtures(f => {
17+
f.withOrganizations();
18+
f.withUser({
19+
email_addresses: ['test@clerk.com'],
20+
organization_memberships: [{ name: 'Org1', admin_delete_enabled: true }],
21+
});
22+
});
23+
24+
const destroy = fixtures.clerk.organization!.destroy;
25+
destroy
26+
.mockRejectedValueOnce(
27+
new ClerkAPIResponseError('Reverification required', {
28+
status: 403,
29+
data: [{ code: 'session_reverification_required', message: 'Reverification required' }],
30+
}),
31+
)
32+
.mockResolvedValueOnce({} as DeletedObjectResource);
33+
const openReverification = vi.spyOn(fixtures.clerk, '__internal_openReverification').mockImplementation(() => {});
34+
35+
const { getByRole, userEvent, getByLabelText, findByRole } = render(
36+
<CardStateProvider>
37+
<DeleteOrganizationForm
38+
onSuccess={vi.fn()}
39+
onReset={vi.fn()}
40+
/>
41+
</CardStateProvider>,
42+
{ wrapper },
43+
);
44+
45+
await userEvent.type(getByLabelText(/below to continue/i), 'Org1');
46+
await waitFor(async () => expect(await findByRole('button', { name: 'Delete organization' })).not.toBeDisabled());
47+
await userEvent.click(getByRole('button', { name: 'Delete organization' }));
48+
49+
await waitFor(() => expect(openReverification).toHaveBeenCalledTimes(1));
50+
expect(destroy).toHaveBeenCalledTimes(1);
51+
52+
await act(() => {
53+
openReverification.mock.calls[0][0].afterVerification();
54+
});
55+
56+
await waitFor(() => expect(destroy).toHaveBeenCalledTimes(2));
57+
});
58+
});

0 commit comments

Comments
 (0)