|
| 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