|
| 1 | +import { describe } from '@jest/globals'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | + |
| 4 | +import { bindCreateFixtures, render } from '@/testUtils'; |
| 5 | + |
| 6 | +import { TaskSelectOrganization } from '../'; |
| 7 | + |
| 8 | +const { createFixtures } = bindCreateFixtures('TaskSelectOrganization'); |
| 9 | + |
| 10 | +describe('TaskSelectOrganization', () => { |
| 11 | + it('does not render component without existing session task', async () => { |
| 12 | + const { wrapper } = await createFixtures(f => { |
| 13 | + f.withOrganizations(); |
| 14 | + f.withForceOrganizationSelection(); |
| 15 | + f.withUser({ |
| 16 | + email_addresses: ['test@clerk.com'], |
| 17 | + create_organization_enabled: true, |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + const { queryByText, queryByRole } = render(<TaskSelectOrganization />, { wrapper }); |
| 22 | + |
| 23 | + expect(queryByText('Setup your account')).not.toBeInTheDocument(); |
| 24 | + expect(queryByText('Tell us a bit about your organization')).not.toBeInTheDocument(); |
| 25 | + expect(queryByRole('button', { name: /sign out/i })).not.toBeInTheDocument(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('renders component when session task exists', async () => { |
| 29 | + const { wrapper } = await createFixtures(f => { |
| 30 | + f.withOrganizations(); |
| 31 | + f.withForceOrganizationSelection(); |
| 32 | + f.withUser({ |
| 33 | + email_addresses: ['test@clerk.com'], |
| 34 | + create_organization_enabled: true, |
| 35 | + tasks: [{ key: 'select-organization' }], |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + const { getByText, getByRole } = render(<TaskSelectOrganization />, { wrapper }); |
| 40 | + |
| 41 | + expect(getByText('Setup your account')).toBeInTheDocument(); |
| 42 | + expect(getByText('Tell us a bit about your organization')).toBeInTheDocument(); |
| 43 | + expect(getByRole('button', { name: /sign out/i })).toBeInTheDocument(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('shows create organization screen when user has no existing resources', async () => { |
| 47 | + const { wrapper } = await createFixtures(f => { |
| 48 | + f.withOrganizations(); |
| 49 | + f.withForceOrganizationSelection(); |
| 50 | + f.withUser({ |
| 51 | + email_addresses: ['test@clerk.com'], |
| 52 | + create_organization_enabled: true, |
| 53 | + tasks: [{ key: 'select-organization' }], |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + const { getByRole, getByText } = render(<TaskSelectOrganization />, { wrapper }); |
| 58 | + |
| 59 | + // Should show create organization form by default when no existing resources |
| 60 | + expect(getByRole('textbox', { name: /name/i })).toBeInTheDocument(); |
| 61 | + expect(getByText('Create organization')).toBeInTheDocument(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('shows select organization screen when user has existing organizations', async () => { |
| 65 | + const { wrapper } = await createFixtures(f => { |
| 66 | + f.withOrganizations(); |
| 67 | + f.withForceOrganizationSelection(); |
| 68 | + f.withUser({ |
| 69 | + email_addresses: ['test@clerk.com'], |
| 70 | + create_organization_enabled: true, |
| 71 | + organization_memberships: [{ name: 'Test Org', slug: 'test-org' }], |
| 72 | + tasks: [{ key: 'select-organization' }], |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + const { getByText, queryByRole } = render(<TaskSelectOrganization />, { wrapper }); |
| 77 | + |
| 78 | + expect(getByText('Test Org')).toBeInTheDocument(); |
| 79 | + expect(getByText('Create organization')).toBeInTheDocument(); |
| 80 | + expect(queryByRole('textbox', { name: /name/i })).not.toBeInTheDocument(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('allows switching between select and create organization screens', async () => { |
| 84 | + const { wrapper } = await createFixtures(f => { |
| 85 | + f.withOrganizations(); |
| 86 | + f.withForceOrganizationSelection(); |
| 87 | + f.withUser({ |
| 88 | + email_addresses: ['test@clerk.com'], |
| 89 | + create_organization_enabled: true, |
| 90 | + organization_memberships: [{ name: 'Existing Org', slug: 'existing-org' }], |
| 91 | + tasks: [{ key: 'select-organization' }], |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + const { getByText, getByRole, queryByRole, queryByText } = render(<TaskSelectOrganization />, { wrapper }); |
| 96 | + |
| 97 | + expect(getByText('Existing Org')).toBeInTheDocument(); |
| 98 | + expect(getByText('Create organization')).toBeInTheDocument(); |
| 99 | + expect(queryByRole('textbox', { name: /name/i })).not.toBeInTheDocument(); |
| 100 | + |
| 101 | + const createButton = getByText('Create organization'); |
| 102 | + await userEvent.click(createButton); |
| 103 | + |
| 104 | + expect(getByRole('textbox', { name: /name/i })).toBeInTheDocument(); |
| 105 | + expect(getByRole('button', { name: /create organization/i })).toBeInTheDocument(); |
| 106 | + expect(getByRole('button', { name: /cancel/i })).toBeInTheDocument(); |
| 107 | + expect(queryByText('Existing Org')).not.toBeInTheDocument(); |
| 108 | + |
| 109 | + const cancelButton = getByRole('button', { name: /cancel/i }); |
| 110 | + await userEvent.click(cancelButton); |
| 111 | + |
| 112 | + expect(getByText('Existing Org')).toBeInTheDocument(); |
| 113 | + expect(getByText('Create organization')).toBeInTheDocument(); |
| 114 | + expect(queryByRole('textbox', { name: /name/i })).not.toBeInTheDocument(); |
| 115 | + }); |
| 116 | + |
| 117 | + it('displays user identifier in sign out section', async () => { |
| 118 | + const { wrapper } = await createFixtures(f => { |
| 119 | + f.withOrganizations(); |
| 120 | + f.withForceOrganizationSelection(); |
| 121 | + f.withUser({ |
| 122 | + email_addresses: ['user@test.com'], |
| 123 | + create_organization_enabled: true, |
| 124 | + tasks: [{ key: 'select-organization' }], |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + const { getByText } = render(<TaskSelectOrganization />, { wrapper }); |
| 129 | + |
| 130 | + expect(getByText(/user@test\.com/)).toBeInTheDocument(); |
| 131 | + expect(getByText('Sign out')).toBeInTheDocument(); |
| 132 | + }); |
| 133 | + |
| 134 | + it('handles sign out correctly', async () => { |
| 135 | + const { wrapper, fixtures } = await createFixtures(f => { |
| 136 | + f.withOrganizations(); |
| 137 | + f.withForceOrganizationSelection(); |
| 138 | + f.withUser({ |
| 139 | + email_addresses: ['test@clerk.com'], |
| 140 | + create_organization_enabled: true, |
| 141 | + tasks: [{ key: 'select-organization' }], |
| 142 | + }); |
| 143 | + }); |
| 144 | + |
| 145 | + const { getByRole } = render(<TaskSelectOrganization />, { wrapper }); |
| 146 | + const signOutButton = getByRole('button', { name: /sign out/i }); |
| 147 | + |
| 148 | + await userEvent.click(signOutButton); |
| 149 | + |
| 150 | + expect(fixtures.clerk.signOut).toHaveBeenCalled(); |
| 151 | + }); |
| 152 | + |
| 153 | + it('renders with username when email is not available', async () => { |
| 154 | + const { wrapper } = await createFixtures(f => { |
| 155 | + f.withOrganizations(); |
| 156 | + f.withForceOrganizationSelection(); |
| 157 | + f.withUser({ |
| 158 | + username: 'testuser', |
| 159 | + create_organization_enabled: true, |
| 160 | + tasks: [{ key: 'select-organization' }], |
| 161 | + }); |
| 162 | + }); |
| 163 | + |
| 164 | + const { getByText } = render(<TaskSelectOrganization />, { wrapper }); |
| 165 | + |
| 166 | + expect(getByText(/testuser/)).toBeInTheDocument(); |
| 167 | + }); |
| 168 | +}); |
0 commit comments