Skip to content

Commit 02c4157

Browse files
committed
Add unit tests
1 parent db991c2 commit 02c4157

File tree

3 files changed

+172
-1
lines changed

3 files changed

+172
-1
lines changed

packages/clerk-js/src/ui/components/SessionTasks/tasks/TaskSelectOrganization/SelectOrganizationScreen.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { OrganizationPreview } from '@/ui/elements/OrganizationPreview';
2020
import { Add } from '@/ui/icons';
2121
import { handleError } from '@/ui/utils/errorHandler';
2222

23+
// TODO -> Do not use stuff from OrganizationList
2324
import { useOrganizationListInView } from '../../../OrganizationList/OrganizationListPage';
2425
import {
2526
OrganizationListPreviewButton,
@@ -44,7 +45,6 @@ export const SelectOrganizationScreen = withCardStateProvider(
4445
const userInvitationsData = userInvitations.data?.filter(a => !!a);
4546
const userSuggestionsData = userSuggestions.data?.filter(a => !!a);
4647

47-
// TODO -> Set organization as active
4848
return (
4949
<Col elementDescriptor={descriptors.main}>
5050
<OrganizationPreviewListItems elementDescriptor={descriptors.taskSelectOrganizationPreviewItems}>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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+
});

packages/clerk-js/src/ui/utils/test/fixtureHelpers.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
PublicUserDataJSON,
1111
SamlAccountJSON,
1212
SessionJSON,
13+
SessionTask,
1314
SignInJSON,
1415
SignUpJSON,
1516
UserJSON,
@@ -49,6 +50,7 @@ const createUserFixtureHelpers = (baseClient: ClientJSON) => {
4950
external_accounts?: Array<OAuthProvider | Partial<ExternalAccountJSON>>;
5051
saml_accounts?: Array<Partial<SamlAccountJSON>>;
5152
organization_memberships?: Array<string | OrgParams>;
53+
tasks?: SessionTask[];
5254
};
5355

5456
const createPublicUserData = (params: WithUserParams) => {
@@ -87,6 +89,7 @@ const createUserFixtureHelpers = (baseClient: ClientJSON) => {
8789
last_active_token: {
8890
jwt: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NzU4NzY3OTAsImRhdGEiOiJmb29iYXIiLCJpYXQiOjE2NzU4NzY3MzB9.Z1BC47lImYvaAtluJlY-kBo0qOoAk42Xb-gNrB2SxJg',
8991
},
92+
tasks: params.tasks,
9093
} as SessionJSON;
9194
baseClient.sessions.push(session);
9295
};

0 commit comments

Comments
 (0)