|
| 1 | +import { screen, waitFor } from '@testing-library/react'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | + |
| 4 | +import DashboardFiltersModal from '@/components/DashboardFiltersModal'; |
| 5 | +import { FilterFormControl } from '@/components/DashboardFiltersModal/filterFormState'; |
| 6 | + |
| 7 | +jest.mock('@/config', () => ({ |
| 8 | + ...jest.requireActual('@/config'), |
| 9 | + IS_PROMQL_ENABLED: true, |
| 10 | +})); |
| 11 | +// ConfirmProvider pulls in next/router, which jsdom has no answer for. |
| 12 | +jest.mock('@/useConfirm', () => ({ useConfirm: () => jest.fn() })); |
| 13 | +jest.mock('@/source', () => ({ useSources: () => ({ data: [] }) })); |
| 14 | +jest.mock('@/connection', () => ({ useConnections: () => ({ data: [] }) })); |
| 15 | + |
| 16 | +// The real editors would drag CodeMirror and source metadata into jsdom. These |
| 17 | +// register the shared source field with the same rules as the real forms. |
| 18 | +jest.mock( |
| 19 | + '@/components/DashboardFiltersModal/QueryExpressionFilterEditForm', |
| 20 | + () => ({ |
| 21 | + QueryExpressionFilterEditForm: ({ |
| 22 | + control, |
| 23 | + }: { |
| 24 | + control: FilterFormControl; |
| 25 | + }) => <input {...control.register('source', { required: true })} />, |
| 26 | + }), |
| 27 | +); |
| 28 | +jest.mock( |
| 29 | + '@/components/DashboardFiltersModal/PromqlLabelFilterEditForm', |
| 30 | + () => ({ |
| 31 | + PromqlLabelFilterEditForm: ({ |
| 32 | + control, |
| 33 | + }: { |
| 34 | + control: FilterFormControl; |
| 35 | + }) => ( |
| 36 | + <input |
| 37 | + data-testid="promql-source-input" |
| 38 | + {...control.register('source', { |
| 39 | + required: 'Select a PromQL source', |
| 40 | + validate: value => value !== 'logs' || 'Select a PromQL source', |
| 41 | + })} |
| 42 | + /> |
| 43 | + ), |
| 44 | + }), |
| 45 | +); |
| 46 | +jest.mock('@/components/DashboardFiltersModal/SqlQueryFilterEditForm', () => ({ |
| 47 | + SqlQueryFilterEditForm: ({ control }: { control: FilterFormControl }) => ( |
| 48 | + <div> |
| 49 | + <input |
| 50 | + data-testid="connection-input" |
| 51 | + {...control.register('connection', { required: true })} |
| 52 | + /> |
| 53 | + <input data-testid="sql-source-input" {...control.register('source')} /> |
| 54 | + <textarea |
| 55 | + data-testid="sql-input" |
| 56 | + {...control.register('sqlTemplate', { required: true })} |
| 57 | + /> |
| 58 | + </div> |
| 59 | + ), |
| 60 | +})); |
| 61 | + |
| 62 | +// Mantine's Combobox calls scrollIntoView when its dropdown opens; jsdom lacks it. |
| 63 | +window.HTMLElement.prototype.scrollIntoView = jest.fn(); |
| 64 | + |
| 65 | +describe('DashboardFiltersModal with PromQL enabled', () => { |
| 66 | + it.each([ |
| 67 | + ['without a source', ''], |
| 68 | + ['with a non-PromQL source', 'logs'], |
| 69 | + ])( |
| 70 | + 'saves a SQL query filter %s after visiting the PromQL editor', |
| 71 | + async (_, source) => { |
| 72 | + const onSaveFilter = jest.fn(); |
| 73 | + renderWithMantine( |
| 74 | + <DashboardFiltersModal |
| 75 | + opened |
| 76 | + filters={[]} |
| 77 | + showVariableOptions |
| 78 | + showRequiredFilterOptions |
| 79 | + onClose={jest.fn()} |
| 80 | + onSaveFilter={onSaveFilter} |
| 81 | + onRemoveFilter={jest.fn()} |
| 82 | + />, |
| 83 | + ); |
| 84 | + const user = userEvent.setup(); |
| 85 | + const selectFilterType = async (label: string) => { |
| 86 | + await user.click(screen.getByTestId('filter-type-picker')); |
| 87 | + await user.click(await screen.findByText(label)); |
| 88 | + }; |
| 89 | + |
| 90 | + await user.click(screen.getByTestId('add-filter-button')); |
| 91 | + await selectFilterType('PromQL label values'); |
| 92 | + expect(screen.getByTestId('promql-source-input')).toBeInTheDocument(); |
| 93 | + await selectFilterType('SQL query values'); |
| 94 | + await user.type(screen.getByTestId('filter-name-input'), 'Service'); |
| 95 | + await user.type(screen.getByTestId('connection-input'), 'conn1'); |
| 96 | + if (source) { |
| 97 | + await user.type(screen.getByTestId('sql-source-input'), source); |
| 98 | + } |
| 99 | + await user.type(screen.getByTestId('sql-input'), 'SELECT 1'); |
| 100 | + await user.click(screen.getByTestId('save-filter-button')); |
| 101 | + |
| 102 | + await waitFor(() => expect(onSaveFilter).toHaveBeenCalledTimes(1)); |
| 103 | + const saved = onSaveFilter.mock.calls[0][0]; |
| 104 | + expect(saved.type).toBe('SQL_QUERY'); |
| 105 | + expect(saved.source).toBe(source || undefined); |
| 106 | + }, |
| 107 | + ); |
| 108 | +}); |
0 commit comments