|
| 1 | +// @vitest-environment jsdom |
| 2 | +import { buildSchema, type GraphQLObjectType } from 'graphql'; |
| 3 | +import { fireEvent, render, screen } from '@testing-library/react'; |
| 4 | +import { BuilderObjectField, BuilderScalarField } from './builder'; |
| 5 | + |
| 6 | +const laboratory = vi.hoisted(() => ({ current: {} as Record<string, unknown> })); |
| 7 | + |
| 8 | +vi.mock('./context', () => ({ |
| 9 | + useLaboratory: () => laboratory.current, |
| 10 | +})); |
| 11 | + |
| 12 | +const schema = buildSchema(` |
| 13 | + type Profile { city: String } |
| 14 | + type User { id: ID!, profile: Profile } |
| 15 | + type Query { me: User } |
| 16 | +`); |
| 17 | + |
| 18 | +const idField = (schema.getType('User') as GraphQLObjectType).getFields().id; |
| 19 | +const meField = schema.getQueryType()!.getFields().me; |
| 20 | + |
| 21 | +const openDocs = vi.fn(); |
| 22 | + |
| 23 | +const mount = (node: React.ReactElement, enableDocs: boolean) => { |
| 24 | + laboratory.current = { |
| 25 | + schema, |
| 26 | + enableDocs, |
| 27 | + openDocs, |
| 28 | + activeOperation: null, |
| 29 | + activeTab: { type: 'operation' }, |
| 30 | + addPathToActiveOperation: vi.fn(), |
| 31 | + deletePathFromActiveOperation: vi.fn(), |
| 32 | + addArgToActiveOperation: vi.fn(), |
| 33 | + deleteArgFromActiveOperation: vi.fn(), |
| 34 | + }; |
| 35 | + |
| 36 | + return render(node); |
| 37 | +}; |
| 38 | + |
| 39 | +const scalarRow = (disableChildren: boolean) => ( |
| 40 | + <BuilderScalarField |
| 41 | + field={idField} |
| 42 | + path={['query', 'me', 'id']} |
| 43 | + openPaths={[]} |
| 44 | + setOpenPaths={vi.fn()} |
| 45 | + disableChildren={disableChildren} |
| 46 | + /> |
| 47 | +); |
| 48 | + |
| 49 | +describe('Builder row docs context menu', () => { |
| 50 | + beforeEach(() => { |
| 51 | + openDocs.mockReset(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('offers Open in Docs on a plain row', () => { |
| 55 | + const { container } = mount(scalarRow(false), true); |
| 56 | + |
| 57 | + fireEvent.contextMenu(container.firstElementChild!); |
| 58 | + |
| 59 | + expect(screen.getByText('Open in Docs')).toBeDefined(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('opens the field the row represents, not its return type', () => { |
| 63 | + const { container } = mount(scalarRow(false), true); |
| 64 | + |
| 65 | + fireEvent.contextMenu(container.firstElementChild!); |
| 66 | + fireEvent.click(screen.getByText('Open in Docs')); |
| 67 | + |
| 68 | + expect(openDocs).toHaveBeenCalledWith({ kind: 'field', typeName: 'User', fieldName: 'id' }); |
| 69 | + }); |
| 70 | + |
| 71 | + it('offers nothing when docs are disabled', () => { |
| 72 | + const { container } = mount(scalarRow(false), false); |
| 73 | + |
| 74 | + fireEvent.contextMenu(container.firstElementChild!); |
| 75 | + |
| 76 | + expect(screen.queryByText('Open in Docs')).toBeNull(); |
| 77 | + }); |
| 78 | + |
| 79 | + it('still renders the sticky row variant', () => { |
| 80 | + const { container } = mount(scalarRow(true), true); |
| 81 | + |
| 82 | + expect(container.firstElementChild).not.toBeNull(); |
| 83 | + }); |
| 84 | + |
| 85 | + // The collapsible variant nests ContextMenuTrigger asChild around |
| 86 | + // CollapsibleTrigger asChild, so both slots have to resolve onto the Button. |
| 87 | + it('renders the collapsible row without breaking the asChild chain', () => { |
| 88 | + const { container } = mount( |
| 89 | + <BuilderObjectField |
| 90 | + field={meField} |
| 91 | + path={['query', 'me']} |
| 92 | + openPaths={[]} |
| 93 | + setOpenPaths={vi.fn()} |
| 94 | + />, |
| 95 | + true, |
| 96 | + ); |
| 97 | + |
| 98 | + const button = container.querySelector('button'); |
| 99 | + expect(button).not.toBeNull(); |
| 100 | + |
| 101 | + fireEvent.contextMenu(button!); |
| 102 | + |
| 103 | + expect(screen.getByText('Open in Docs')).toBeDefined(); |
| 104 | + }); |
| 105 | +}); |
0 commit comments