Skip to content

Commit c8d880f

Browse files
feat(mosaic): open the Mosaic profiles from the switchers only behind experimental.newProfiles
1 parent 9a1bc45 commit c8d880f

12 files changed

Lines changed: 837 additions & 138 deletions

File tree

‎packages/mosaic/src/__tests__/feature/render.tsx‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { act, render } from '@testing-library/react';
44
import type { ReactElement } from 'react';
55
import { vi } from 'vitest';
66

7+
import type { MosaicExperimental } from '../../hooks/use-mosaic-experimental';
78
import type { MosaicLocalization } from '../../localization';
89
import { MosaicProvider } from '../../mosaic-provider';
910
import { publishableKey } from './fake-fapi';
@@ -12,6 +13,7 @@ export async function renderWithClerk(
1213
ui: ReactElement,
1314
options?: Parameters<Clerk['load']>[0],
1415
localization?: MosaicLocalization,
16+
experimental?: MosaicExperimental,
1517
) {
1618
const clerk = new Clerk(publishableKey());
1719
const navigate = vi.fn((_to: string) => Promise.resolve());
@@ -21,7 +23,12 @@ export async function renderWithClerk(
2123
clerk={clerk}
2224
clerkStatus={clerk.status}
2325
>
24-
<MosaicProvider localization={localization}>{element}</MosaicProvider>
26+
<MosaicProvider
27+
localization={localization}
28+
experimental={experimental}
29+
>
30+
{element}
31+
</MosaicProvider>
2532
</ClerkContextProvider>
2633
);
2734
let current = ui;

‎packages/mosaic/src/features/organization-switcher/__tests__/organization-switcher.feature.test.tsx‎

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@ const aliceSession = fapiSession({ id: 'sess_1', user: alice, last_active_organi
4747
const bobSession = fapiSession({ id: 'sess_2', user: bob });
4848
const personalSession = fapiSession({ id: 'sess_1', user: alice });
4949

50-
async function wideViewport() {
51-
await page.viewport(1280, 800);
52-
onTestFinished(() => page.viewport(414, 896));
53-
}
54-
5550
function signedIn(overrides: FakeFapiSeed = {}): FakeFapiSeed {
5651
return {
5752
client: fapiClient([aliceSession, bobSession]),
@@ -135,8 +130,8 @@ describe('OrganizationSwitcher', () => {
135130
});
136131

137132
it('heads the surface with the active organization, and manages it from the rows beneath', async () => {
138-
await wideViewport();
139-
await renderSwitcher({}, noOffers());
133+
const { clerk } = await renderSwitcher({}, noOffers());
134+
const openOrganizationProfile = vi.spyOn(clerk, 'openOrganizationProfile').mockImplementation(() => {});
140135
const user = await openWithList();
141136

142137
const header = requiredPopup().querySelector('.cl-organization-switcher-header') ?? requiredPopup();
@@ -151,8 +146,7 @@ describe('OrganizationSwitcher', () => {
151146

152147
await user.click(screen.getByRole('button', { name: 'Settings' }));
153148
expect(screen.queryByRole('menu')).toBeNull();
154-
expect(await screen.findByRole('tab', { name: 'General' })).toHaveAttribute('aria-selected', 'true');
155-
expect(screen.queryByRole('tab', { name: 'Account' })).toBeNull();
149+
expect(openOrganizationProfile).toHaveBeenCalled();
156150
});
157151

158152
it('falls back to the account, with nothing to manage, where no organization is active', async () => {
@@ -223,19 +217,16 @@ describe('OrganizationSwitcher', () => {
223217
await waitFor(() => expect(navigate).toHaveBeenCalledWith('/orgs/Other'));
224218
});
225219

226-
it('lands on afterLeaveOrganizationUrl after leaving from the opened profile', async () => {
227-
await wideViewport();
228-
const { navigate } = await renderSwitcher({ afterLeaveOrganizationUrl: '/left' }, noOffers());
220+
it('tells the organization profile where to land after leaving', async () => {
221+
const { clerk } = await renderSwitcher({ afterLeaveOrganizationUrl: '/left' }, noOffers());
222+
const openOrganizationProfile = vi.spyOn(clerk, 'openOrganizationProfile').mockImplementation(() => {});
229223
const user = await openWithList();
230224

231225
await user.click(screen.getByRole('button', { name: 'Settings' }));
232-
const dialog = await openDialog(user, await screen.findByRole('button', { name: 'Leave organization' }), {
233-
role: 'alertdialog',
234-
});
235-
await user.type(within(dialog).getByRole('textbox'), 'Acme');
236-
await user.click(within(dialog).getByRole('button', { name: 'Leave organization' }));
237226

238-
await waitFor(() => expect(navigate).toHaveBeenCalledWith('/left'));
227+
expect(openOrganizationProfile).toHaveBeenCalledWith(
228+
expect.objectContaining({ afterLeaveOrganizationUrl: '/left' }),
229+
);
239230
});
240231

241232
it('tells create-organization where to land and whether to skip inviting members', async () => {
@@ -384,3 +375,38 @@ describe('OrganizationSwitcher custom trigger badges', () => {
384375
expect(trigger()).toHaveAccessibleName('Open organization menu for Other');
385376
});
386377
});
378+
379+
describe('OrganizationSwitcher with experimental.newProfiles', () => {
380+
async function renderWithNewProfiles(props: OrganizationSwitcherProps = {}) {
381+
await page.viewport(1280, 800);
382+
onTestFinished(() => page.viewport(414, 896));
383+
serveFapi(noOffers());
384+
return renderWithClerk(tree(props), undefined, undefined, { newProfiles: true });
385+
}
386+
387+
it('opens the OrganizationProfile, without the account pages, from Settings', async () => {
388+
const { clerk } = await renderWithNewProfiles();
389+
const openOrganizationProfile = vi.spyOn(clerk, 'openOrganizationProfile');
390+
const user = await openWithList();
391+
392+
await user.click(screen.getByRole('button', { name: 'Settings' }));
393+
394+
expect(await screen.findByRole('tab', { name: 'General' })).toHaveAttribute('aria-selected', 'true');
395+
expect(screen.queryByRole('tab', { name: 'Account' })).toBeNull();
396+
expect(openOrganizationProfile).not.toHaveBeenCalled();
397+
});
398+
399+
it('lands on afterLeaveOrganizationUrl after leaving from the opened profile', async () => {
400+
const { navigate } = await renderWithNewProfiles({ afterLeaveOrganizationUrl: '/left' });
401+
const user = await openWithList();
402+
403+
await user.click(screen.getByRole('button', { name: 'Settings' }));
404+
const dialog = await openDialog(user, await screen.findByRole('button', { name: 'Leave organization' }), {
405+
role: 'alertdialog',
406+
});
407+
await user.type(within(dialog).getByRole('textbox'), 'Acme');
408+
await user.click(within(dialog).getByRole('button', { name: 'Leave organization' }));
409+
410+
await waitFor(() => expect(navigate).toHaveBeenCalledWith('/left'));
411+
});
412+
});
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
import type { CustomPage } from '@clerk/shared/types';
2+
import { act, render, screen, within } from '@testing-library/react';
3+
import { beforeEach, describe, expect, it } from 'vitest';
4+
5+
import type { CustomProfileItem } from '../../user-profile/user-profile.types';
6+
import type { CustomPagesOptions } from '../switcher.pages';
7+
import { useCustomPages } from '../switcher.pages';
8+
9+
// The bridge's other half lives in clerk-js: `ExternalElementMounter` renders a `div` and hands it to
10+
// `mount`, then hands it back to `unmount` when the profile goes away. These stand in for it, so the
11+
// tests exercise the same handshake the real modal performs.
12+
function mountInto(callback: ((el: HTMLDivElement) => void) | undefined): HTMLDivElement {
13+
const el = document.createElement('div');
14+
document.body.appendChild(el);
15+
act(() => callback?.(el));
16+
return el;
17+
}
18+
19+
function unmountFrom(callback: ((el?: HTMLDivElement) => void) | undefined, el: HTMLDivElement) {
20+
act(() => callback?.(el));
21+
el.remove();
22+
}
23+
24+
let emitted: CustomPage[] | undefined;
25+
26+
function Harness({ items, order, builtInPages = ['account', 'security'] }: Partial<CustomPagesOptions>) {
27+
const { customPages, portals } = useCustomPages({ items, order, builtInPages });
28+
emitted = customPages;
29+
return <div data-testid='host'>{portals}</div>;
30+
}
31+
32+
const terms: CustomProfileItem = {
33+
label: 'Terms',
34+
path: 'terms',
35+
icon: <span>terms icon</span>,
36+
content: <p>Terms body</p>,
37+
};
38+
39+
const docs: CustomProfileItem = {
40+
label: 'Docs',
41+
path: 'docs',
42+
href: 'https://clerk.com/docs',
43+
icon: <span>docs icon</span>,
44+
};
45+
46+
beforeEach(() => {
47+
emitted = undefined;
48+
});
49+
50+
describe('useCustomPages', () => {
51+
it('sends nothing when there are no custom pages', () => {
52+
render(<Harness />);
53+
54+
expect(emitted).toBeUndefined();
55+
expect(screen.getByTestId('host')).toBeEmptyDOMElement();
56+
});
57+
58+
it('sends a page as its path and a link as its href', () => {
59+
render(<Harness items={[terms, docs]} />);
60+
61+
expect(emitted?.map(page => page.url)).toEqual(['terms', 'https://clerk.com/docs']);
62+
expect(emitted?.map(page => page.label)).toEqual(['Terms', 'Docs']);
63+
});
64+
65+
// clerk-js tells a page from a link by which callbacks are present, so content callbacks are what
66+
// make an item a page. A link carrying them would be routed to instead of followed.
67+
it('sends content callbacks for a page and none for a link', () => {
68+
render(<Harness items={[terms, docs]} />);
69+
70+
const [page, link] = emitted ?? [];
71+
expect(page.mount).toBeTypeOf('function');
72+
expect(page.unmount).toBeTypeOf('function');
73+
expect(link.mount).toBeUndefined();
74+
expect(link.unmount).toBeUndefined();
75+
});
76+
77+
// Without them clerk-js drops the page as invalid, so `icon` could not be optional.
78+
it('sends the icon callbacks even for an item with no icon', () => {
79+
render(<Harness items={[{ label: 'Terms', path: 'terms', content: <p>Terms body</p> }]} />);
80+
81+
const [page] = emitted ?? [];
82+
expect(page.mountIcon).toBeTypeOf('function');
83+
expect(page.unmountIcon).toBeTypeOf('function');
84+
85+
const el = mountInto(page.mountIcon);
86+
expect(el).toBeEmptyDOMElement();
87+
});
88+
89+
it('renders an icon into its own element, apart from the content', () => {
90+
render(<Harness items={[terms]} />);
91+
92+
const content = mountInto(emitted?.[0].mount);
93+
const icon = mountInto(emitted?.[0].mountIcon);
94+
95+
expect(within(icon).getByText('terms icon')).toBeInTheDocument();
96+
expect(within(content).queryByText('terms icon')).toBeNull();
97+
});
98+
99+
it('keeps each page in the element that asked for it', () => {
100+
const help: CustomProfileItem = { label: 'Help', path: 'help', content: <p>Help body</p> };
101+
render(<Harness items={[terms, help]} />);
102+
103+
const first = mountInto(emitted?.[0].mount);
104+
const second = mountInto(emitted?.[1].mount);
105+
106+
expect(within(first).getByText('Terms body')).toBeInTheDocument();
107+
expect(within(second).getByText('Help body')).toBeInTheDocument();
108+
});
109+
110+
it('stops rendering content once clerk-js gives the element back', () => {
111+
render(<Harness items={[terms]} />);
112+
113+
const el = mountInto(emitted?.[0].mount);
114+
expect(within(el).getByText('Terms body')).toBeInTheDocument();
115+
116+
unmountFrom(emitted?.[0].unmount, el);
117+
118+
expect(screen.queryByText('Terms body')).toBeNull();
119+
});
120+
121+
// The profile is opened once with the callbacks from that render, and never handed a later set.
122+
// They have to keep working against the current content, or a page re-rendered while the profile
123+
// is open goes stale.
124+
it('renders updated content through the callbacks the profile was opened with', () => {
125+
const { rerender } = render(<Harness items={[terms]} />);
126+
const el = mountInto(emitted?.[0].mount);
127+
128+
rerender(<Harness items={[{ ...terms, content: <p>Revised terms</p> }]} />);
129+
130+
expect(within(el).getByText('Revised terms')).toBeInTheDocument();
131+
});
132+
133+
describe('order', () => {
134+
it('leaves the built-in pages alone when no order is given', () => {
135+
render(<Harness items={[terms, docs]} />);
136+
137+
expect(emitted?.map(page => page.label)).toEqual(['Terms', 'Docs']);
138+
});
139+
140+
// Anything more than the id and clerk-js reads it as a custom page.
141+
it('sends a built-in page as its id alone', () => {
142+
render(<Harness order={['security', 'account']} />);
143+
144+
expect(emitted).toEqual([{ label: 'security' }, { label: 'account' }]);
145+
});
146+
147+
// Unsent built-ins jump to the front, so one left out of the order would not stay put.
148+
it('sends the pages left out of the order after the ones in it', () => {
149+
render(
150+
<Harness
151+
items={[terms, docs]}
152+
builtInPages={['account', 'security', 'billing']}
153+
order={['terms']}
154+
/>,
155+
);
156+
157+
expect(emitted?.map(page => page.label)).toEqual(['Terms', 'account', 'security', 'billing', 'Docs']);
158+
});
159+
160+
it('drops an id that belongs to no page', () => {
161+
render(
162+
<Harness
163+
items={[terms]}
164+
order={['billing', 'terms']}
165+
/>,
166+
);
167+
168+
expect(emitted?.map(page => page.label)).toEqual(['Terms', 'account', 'security']);
169+
});
170+
171+
it('sends a page once even when the order names it twice', () => {
172+
render(<Harness order={['security', 'account', 'security']} />);
173+
174+
expect(emitted?.map(page => page.label)).toEqual(['security', 'account']);
175+
});
176+
177+
it('renders a reordered page into the element clerk-js hands back', () => {
178+
render(
179+
<Harness
180+
items={[terms]}
181+
order={['account', 'terms']}
182+
/>,
183+
);
184+
185+
const el = mountInto(emitted?.[1].mount);
186+
187+
expect(within(el).getByText('Terms body')).toBeInTheDocument();
188+
});
189+
});
190+
});

0 commit comments

Comments
 (0)