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