|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { act } from 'react' |
| 5 | +import { flushMacrotask } from '@sim/testing/helpers/async' |
| 6 | +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' |
| 7 | +import { createRoot, type Root } from 'react-dom/client' |
| 8 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 9 | +import { SimLinkOrganizationProvider } from '@/app/o/[organizationId]/links/sim-link-organization-provider' |
| 10 | +import { MessageSources } from '@/app/workspace/[workspaceId]/home/components/message-content/components/message-sources/message-sources' |
| 11 | + |
| 12 | +const source = { url: 'https://docs.example.test/guide', title: 'Setup guide' } |
| 13 | + |
| 14 | +/** Radix menus open on a primary-button pointer press, which jsdom's click() does not send. */ |
| 15 | +function press(element: Element | null) { |
| 16 | + if (!element) throw new Error('Expected an element to press') |
| 17 | + element.dispatchEvent(new MouseEvent('pointerdown', { bubbles: true, button: 0 })) |
| 18 | +} |
| 19 | + |
| 20 | +async function settle() { |
| 21 | + await act(flushMacrotask) |
| 22 | +} |
| 23 | + |
| 24 | +describe('creating a Sim link from the sources popover', () => { |
| 25 | + let container: HTMLDivElement |
| 26 | + let root: Root |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true) |
| 30 | + vi.stubGlobal( |
| 31 | + 'fetch', |
| 32 | + vi.fn(async () => Response.json({ links: [], nextCursor: null })) |
| 33 | + ) |
| 34 | + container = document.createElement('div') |
| 35 | + document.body.append(container) |
| 36 | + root = createRoot(container) |
| 37 | + act(() => |
| 38 | + root.render( |
| 39 | + <QueryClientProvider client={new QueryClient()}> |
| 40 | + <SimLinkOrganizationProvider organizationId='org-1'> |
| 41 | + <MessageSources sources={[source]} /> |
| 42 | + </SimLinkOrganizationProvider> |
| 43 | + </QueryClientProvider> |
| 44 | + ) |
| 45 | + ) |
| 46 | + }) |
| 47 | + |
| 48 | + afterEach(() => { |
| 49 | + act(() => root.unmount()) |
| 50 | + container.remove() |
| 51 | + }) |
| 52 | + |
| 53 | + it('opens the form outside the popover and dismisses the popover, so the form is never beneath it', async () => { |
| 54 | + act(() => document.querySelector<HTMLButtonElement>('button[aria-label="1 source"]')?.click()) |
| 55 | + const row = () => document.querySelector(`a[href="${source.url}"]`) |
| 56 | + expect(row()).not.toBeNull() |
| 57 | + |
| 58 | + act(() => press(document.querySelector(`[aria-label="Actions for ${source.title}"]`))) |
| 59 | + await settle() |
| 60 | + const create = [...document.querySelectorAll('[role="menuitem"]')].find( |
| 61 | + (item) => item.textContent === 'Create Sim link' |
| 62 | + ) |
| 63 | + if (!(create instanceof HTMLElement)) throw new Error('Expected a Create Sim link item') |
| 64 | + expect(create.getAttribute('aria-disabled')).not.toBe('true') |
| 65 | + act(() => create.click()) |
| 66 | + await settle() |
| 67 | + |
| 68 | + const form = [...document.querySelectorAll('[role="dialog"]')].find((dialog) => |
| 69 | + dialog.textContent?.includes('New Sim link') |
| 70 | + ) |
| 71 | + expect(form).toBeDefined() |
| 72 | + expect(document.querySelector<HTMLInputElement>('input[type="url"]')?.value).toBe(source.url) |
| 73 | + expect(row()).toBeNull() |
| 74 | + }) |
| 75 | +}) |
0 commit comments