|
| 1 | +/** @vitest-environment jsdom */ |
| 2 | +import { act } from 'react' |
| 3 | +import { toast } from '@sim/emcn' |
| 4 | +import { NuqsTestingAdapter } from 'nuqs/adapters/testing' |
| 5 | +import { createRoot, type Root } from 'react-dom/client' |
| 6 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | + |
| 8 | +const mocks = vi.hoisted(() => ({ |
| 9 | + context: vi.fn(), |
| 10 | + accounts: vi.fn(), |
| 11 | + people: vi.fn(), |
| 12 | + invite: vi.fn(), |
| 13 | + refetch: vi.fn(), |
| 14 | +})) |
| 15 | +vi.mock('@/app/o/[organizationId]/providers/organization-provider', () => ({ |
| 16 | + useOrganizationContext: mocks.context, |
| 17 | +})) |
| 18 | +vi.mock( |
| 19 | + '@/app/o/[organizationId]/settings/components/integrations/organization-integrations-setup', |
| 20 | + () => ({ OrganizationIntegrationsSetup: () => <div>Provider setup</div> }) |
| 21 | +) |
| 22 | +vi.mock('@/hooks/queries/organization-accounts', () => ({ |
| 23 | + useOrganizationAccounts: mocks.accounts, |
| 24 | + useOrganizationAccountPeople: mocks.people, |
| 25 | + useInviteOrganizationAccountPeople: () => ({ mutateAsync: mocks.invite, reset: vi.fn() }), |
| 26 | + useResendOrganizationAccountInvitation: () => ({}), |
| 27 | + useRevokeOrganizationAccountEnrollment: () => ({}), |
| 28 | +})) |
| 29 | + |
| 30 | +import { OrganizationIntegrationsSettings } from '@/app/o/[organizationId]/settings/components/integrations/organization-integrations-settings' |
| 31 | + |
| 32 | +describe('organization integration invitations', () => { |
| 33 | + let root: Root |
| 34 | + let container: HTMLDivElement |
| 35 | + |
| 36 | + beforeEach(() => { |
| 37 | + vi.clearAllMocks() |
| 38 | + vi.spyOn(toast, 'success').mockReturnValue('toast-id') |
| 39 | + vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true) |
| 40 | + mocks.context.mockReturnValue({ organization: { id: 'org-a' }, viewer: { isAdmin: true } }) |
| 41 | + mocks.accounts.mockReturnValue({ |
| 42 | + data: { credentialGroup: { id: 'group-a' } }, |
| 43 | + error: null, |
| 44 | + refetch: mocks.refetch, |
| 45 | + }) |
| 46 | + mocks.people.mockReturnValue({ data: { pages: [{ enrollments: [] }] } }) |
| 47 | + mocks.invite.mockResolvedValue({ |
| 48 | + sentCount: 2, |
| 49 | + results: [ |
| 50 | + { email: 'one@example.com', success: true }, |
| 51 | + { email: 'two@example.com', success: true }, |
| 52 | + ], |
| 53 | + }) |
| 54 | + container = document.createElement('div') |
| 55 | + document.body.appendChild(container) |
| 56 | + root = createRoot(container) |
| 57 | + }) |
| 58 | + |
| 59 | + afterEach(async () => { |
| 60 | + await act(async () => root.unmount()) |
| 61 | + container.remove() |
| 62 | + vi.restoreAllMocks() |
| 63 | + vi.unstubAllGlobals() |
| 64 | + }) |
| 65 | + |
| 66 | + async function render(searchParams = '') { |
| 67 | + await act(async () => |
| 68 | + root.render( |
| 69 | + <NuqsTestingAdapter hasMemory searchParams={searchParams}> |
| 70 | + <OrganizationIntegrationsSettings /> |
| 71 | + </NuqsTestingAdapter> |
| 72 | + ) |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | + async function click(label: string) { |
| 77 | + const button = Array.from(document.querySelectorAll('button')).find( |
| 78 | + (element) => element.textContent === label |
| 79 | + ) |
| 80 | + if (!button) throw new Error(`Missing ${label} button`) |
| 81 | + await act(async () => button.click()) |
| 82 | + } |
| 83 | + |
| 84 | + it('keeps provider setup as the default and sends manual invitations from People to this org', async () => { |
| 85 | + await render() |
| 86 | + expect(container.textContent).toContain('Provider setup') |
| 87 | + expect(mocks.accounts).toHaveBeenLastCalledWith(undefined) |
| 88 | + expect(mocks.people).not.toHaveBeenCalled() |
| 89 | + |
| 90 | + await click('People') |
| 91 | + expect(container.textContent).not.toContain('Provider setup') |
| 92 | + expect(mocks.accounts).toHaveBeenLastCalledWith('org-a') |
| 93 | + expect(mocks.people).toHaveBeenLastCalledWith('org-a') |
| 94 | + expect(container.querySelector('[aria-label="Search people"]')).not.toBeNull() |
| 95 | + expect(mocks.invite).not.toHaveBeenCalled() |
| 96 | + |
| 97 | + await click('Request connections') |
| 98 | + const input = document.querySelector<HTMLInputElement>('input[placeholder="Enter emails"]') |
| 99 | + if (!input) throw new Error('Missing invitation email input') |
| 100 | + const paste = new Event('paste', { bubbles: true, cancelable: true }) |
| 101 | + Object.defineProperty(paste, 'clipboardData', { |
| 102 | + value: { getData: () => 'one@example.com two@example.com' }, |
| 103 | + }) |
| 104 | + await act(async () => input.dispatchEvent(paste)) |
| 105 | + await click('Send requests') |
| 106 | + expect(mocks.invite).toHaveBeenCalledExactlyOnceWith({ |
| 107 | + organizationId: 'org-a', |
| 108 | + emails: ['one@example.com', 'two@example.com'], |
| 109 | + }) |
| 110 | + expect(document.querySelector('[role="dialog"]')).toBeNull() |
| 111 | + }) |
| 112 | + |
| 113 | + it('opens People directly from the saved URL', async () => { |
| 114 | + await render('?tab=people') |
| 115 | + expect(container.textContent).toContain('Request connections') |
| 116 | + expect(container.textContent).not.toContain('Provider setup') |
| 117 | + expect(mocks.people).toHaveBeenLastCalledWith('org-a') |
| 118 | + }) |
| 119 | + |
| 120 | + it('sends an org without a credential group back to provider setup before invitations', async () => { |
| 121 | + mocks.accounts.mockReturnValue({ data: { credentialGroup: null }, error: null }) |
| 122 | + await render('?tab=people') |
| 123 | + expect(container.textContent).toContain('before inviting people') |
| 124 | + expect(mocks.people).not.toHaveBeenCalled() |
| 125 | + expect(container.textContent).not.toContain('Request connections') |
| 126 | + await click('Set up providers') |
| 127 | + expect(container.textContent).toContain('Provider setup') |
| 128 | + expect(mocks.invite).not.toHaveBeenCalled() |
| 129 | + }) |
| 130 | + |
| 131 | + it('surfaces account lookup errors instead of treating them as missing setup', async () => { |
| 132 | + mocks.accounts.mockReturnValue({ |
| 133 | + error: new Error('Account access denied'), |
| 134 | + refetch: mocks.refetch, |
| 135 | + }) |
| 136 | + await render('?tab=people') |
| 137 | + expect(container.textContent).toContain('Account access denied') |
| 138 | + expect(container.textContent).not.toContain('Set up providers') |
| 139 | + expect(mocks.people).not.toHaveBeenCalled() |
| 140 | + await click('Try again') |
| 141 | + expect(mocks.refetch).toHaveBeenCalledOnce() |
| 142 | + }) |
| 143 | + |
| 144 | + it('does not load admin account data or expose invitations to an ordinary member', async () => { |
| 145 | + mocks.context.mockReturnValue({ organization: { id: 'org-a' }, viewer: { isAdmin: false } }) |
| 146 | + await render('?tab=people') |
| 147 | + expect(container.textContent).toBe('') |
| 148 | + expect(mocks.accounts).toHaveBeenLastCalledWith(undefined) |
| 149 | + expect(mocks.people).not.toHaveBeenCalled() |
| 150 | + expect(mocks.invite).not.toHaveBeenCalled() |
| 151 | + }) |
| 152 | +}) |
0 commit comments