|
| 1 | +import { render, waitFor, fireEvent, act } from '@testing-library/react'; |
| 2 | +import React from 'react'; |
| 3 | +import { getOrganizations } from '../services/organizations'; |
| 4 | +import { getStreams } from '../services/streams'; |
| 5 | +import { QueryEditor } from './QueryEditor'; |
| 6 | +import { MyDataSourceOptions } from 'types'; |
| 7 | +import { DataSource } from 'datasource'; |
| 8 | + |
| 9 | +import { DataSourceInstanceSettings, PluginSignatureStatus, PluginType } from '@grafana/data'; |
| 10 | +const instanceSettings: DataSourceInstanceSettings<MyDataSourceOptions> = { |
| 11 | + id: 2, |
| 12 | + uid: 'fd886f75-fdd9-444b-8868-be92687ff464', |
| 13 | + type: 'zinc-grafanatest-datasource', |
| 14 | + name: 'ZincObserve', |
| 15 | + meta: { |
| 16 | + id: 'zinc-grafanatest-datasource', |
| 17 | + type: 'datasource' as PluginType, |
| 18 | + name: 'ZincObserve', |
| 19 | + info: { |
| 20 | + author: { |
| 21 | + name: 'Zinc', |
| 22 | + url: '', |
| 23 | + }, |
| 24 | + description: 'Zinc observe', |
| 25 | + links: [], |
| 26 | + logos: { |
| 27 | + small: 'public/plugins/zinc-grafanatest-datasource/img/logo.svg', |
| 28 | + large: 'public/plugins/zinc-grafanatest-datasource/img/logo.svg', |
| 29 | + }, |
| 30 | + build: {}, |
| 31 | + screenshots: [], |
| 32 | + version: '1.0.0', |
| 33 | + updated: '2023-05-15', |
| 34 | + }, |
| 35 | + dependencies: { |
| 36 | + grafanaDependency: '^9.3.8', |
| 37 | + grafanaVersion: '*', |
| 38 | + plugins: [], |
| 39 | + }, |
| 40 | + includes: undefined, |
| 41 | + category: '', |
| 42 | + backend: false, |
| 43 | + annotations: false, |
| 44 | + metrics: true, |
| 45 | + alerting: false, |
| 46 | + logs: true, |
| 47 | + tracing: false, |
| 48 | + streaming: false, |
| 49 | + signature: 'unsigned' as PluginSignatureStatus, |
| 50 | + module: 'plugins/zinc-grafanatest-datasource/module', |
| 51 | + baseUrl: 'public/plugins/zinc-grafanatest-datasource', |
| 52 | + }, |
| 53 | + url: '/api/datasources/proxy/uid/fd886f75-fdd9-444b-8868-be92687ff464', |
| 54 | + isDefault: false, |
| 55 | + access: 'proxy', |
| 56 | + jsonData: { |
| 57 | + url: '/api/datasources/proxy/uid/fd886f75-fdd9-444b-8868-be92687ff464', |
| 58 | + }, |
| 59 | + readOnly: false, |
| 60 | +}; |
| 61 | + |
| 62 | +const mockOrgs = [ |
| 63 | + { |
| 64 | + id: 1, |
| 65 | + identifier: 'default', |
| 66 | + name: 'default', |
| 67 | + |
| 68 | + ingest_threshold: 9383939382, |
| 69 | + search_threshold: 9383939382, |
| 70 | + type: 'default', |
| 71 | + UserObj: { |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + }, |
| 76 | + }, |
| 77 | + { |
| 78 | + id: 3, |
| 79 | + identifier: 'neworg', |
| 80 | + name: 'neworg', |
| 81 | + |
| 82 | + ingest_threshold: 9383939382, |
| 83 | + search_threshold: 9383939382, |
| 84 | + type: 'custom', |
| 85 | + UserObj: { |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + }, |
| 90 | + }, |
| 91 | + { |
| 92 | + id: 4, |
| 93 | + identifier: 'production_n230k19AUNT56m0', |
| 94 | + name: 'production_n230k19AUNT56m0', |
| 95 | + |
| 96 | + ingest_threshold: 9383939382, |
| 97 | + search_threshold: 9383939382, |
| 98 | + type: 'custom', |
| 99 | + UserObj: { |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + }, |
| 104 | + }, |
| 105 | +]; |
| 106 | +const mockStreams = [ |
| 107 | + { |
| 108 | + name: 'gke-fluentbit', |
| 109 | + storage_type: 's3', |
| 110 | + stream_type: 'logs', |
| 111 | + stats: { |
| 112 | + doc_time_min: 1680273248592184, |
| 113 | + doc_time_max: 1684231835601605, |
| 114 | + doc_num: 509047692, |
| 115 | + file_num: 31771, |
| 116 | + storage_size: 577585.3, |
| 117 | + compressed_size: 13723.22, |
| 118 | + }, |
| 119 | + schema: [ |
| 120 | + { |
| 121 | + name: '_p', |
| 122 | + type: 'Utf8', |
| 123 | + }, |
| 124 | + { |
| 125 | + name: '_timestamp', |
| 126 | + type: 'Int64', |
| 127 | + }, |
| 128 | + { |
| 129 | + name: 'kubernetes_annotations_checksum_config', |
| 130 | + type: 'Utf8', |
| 131 | + }, |
| 132 | + { |
| 133 | + name: 'kubernetes_annotations_checksum_luascripts', |
| 134 | + type: 'Utf8', |
| 135 | + }, |
| 136 | + { |
| 137 | + name: 'kubernetes_container_image', |
| 138 | + type: 'Utf8', |
| 139 | + }, |
| 140 | + { |
| 141 | + name: 'kubernetes_container_name', |
| 142 | + type: 'Utf8', |
| 143 | + }, |
| 144 | + { |
| 145 | + name: 'kubernetes_docker_id', |
| 146 | + type: 'Utf8', |
| 147 | + }, |
| 148 | + { |
| 149 | + name: 'kubernetes_host', |
| 150 | + type: 'Utf8', |
| 151 | + }, |
| 152 | + { |
| 153 | + name: 'kubernetes_labels_app_kubernetes_io_instance', |
| 154 | + type: 'Utf8', |
| 155 | + }, |
| 156 | + { |
| 157 | + name: 'kubernetes_labels_app_kubernetes_io_name', |
| 158 | + type: 'Utf8', |
| 159 | + }, |
| 160 | + { |
| 161 | + name: 'kubernetes_labels_controller_revision_hash', |
| 162 | + type: 'Utf8', |
| 163 | + }, |
| 164 | + { |
| 165 | + name: 'kubernetes_labels_pod_template_generation', |
| 166 | + type: 'Utf8', |
| 167 | + }, |
| 168 | + { |
| 169 | + name: 'kubernetes_namespace_name', |
| 170 | + type: 'Utf8', |
| 171 | + }, |
| 172 | + { |
| 173 | + name: 'kubernetes_pod_id', |
| 174 | + type: 'Utf8', |
| 175 | + }, |
| 176 | + { |
| 177 | + name: 'kubernetes_pod_name', |
| 178 | + type: 'Utf8', |
| 179 | + }, |
| 180 | + { |
| 181 | + name: 'log', |
| 182 | + type: 'Utf8', |
| 183 | + }, |
| 184 | + { |
| 185 | + name: 'stream', |
| 186 | + type: 'Utf8', |
| 187 | + }, |
| 188 | + { |
| 189 | + name: 'time', |
| 190 | + type: 'Utf8', |
| 191 | + }, |
| 192 | + ], |
| 193 | + settings: { |
| 194 | + partition_keys: {}, |
| 195 | + full_text_search_keys: ['log', 'message', 'msg'], |
| 196 | + schema_validation: false, |
| 197 | + }, |
| 198 | + }, |
| 199 | +]; |
| 200 | + |
| 201 | +jest.mock('../services/organizations', () => ({ |
| 202 | + getOrganizations: jest.fn(), |
| 203 | +})); |
| 204 | + |
| 205 | +jest.mock('../services/streams', () => ({ |
| 206 | + getStreams: jest.fn(), |
| 207 | +})); |
| 208 | +let wrapper: any; |
| 209 | +describe('QueryEdsitor', () => { |
| 210 | + const mockProps = { |
| 211 | + query: { |
| 212 | + refId: 'A', |
| 213 | + constant: 5, |
| 214 | + datasource: { |
| 215 | + type: 'zinc-grafanatest-datasource', |
| 216 | + uid: 'fd886f75-fdd9-444b-8868-be92687ff464', |
| 217 | + }, |
| 218 | + stream: 'gke-fluentbit', |
| 219 | + organization: 'default', |
| 220 | + streamFields: [ |
| 221 | + { |
| 222 | + name: '_p', |
| 223 | + type: 'Utf8', |
| 224 | + }, |
| 225 | + { |
| 226 | + name: '_timestamp', |
| 227 | + type: 'Int64', |
| 228 | + }, |
| 229 | + { |
| 230 | + name: 'kubernetes_annotations_checksum_config', |
| 231 | + type: 'Utf8', |
| 232 | + }, |
| 233 | + { |
| 234 | + name: 'kubernetes_annotations_checksum_luascripts', |
| 235 | + type: 'Utf8', |
| 236 | + }, |
| 237 | + { |
| 238 | + name: 'kubernetes_container_image', |
| 239 | + type: 'Utf8', |
| 240 | + }, |
| 241 | + ], |
| 242 | + sqlMode: false, |
| 243 | + query: '', |
| 244 | + key: 'Q-a981d2e0-bfc9-4fe5-a062-16bf7848d19f-0', |
| 245 | + }, |
| 246 | + onChange: jest.fn(), |
| 247 | + onRunQuery: jest.fn(), |
| 248 | + datasource: new DataSource(instanceSettings), |
| 249 | + }; |
| 250 | + |
| 251 | + function renderQueryEditor() { |
| 252 | + return render(<QueryEditor {...mockProps} />); |
| 253 | + } |
| 254 | + |
| 255 | + beforeEach(() => { |
| 256 | + jest.resetAllMocks(); |
| 257 | + (getOrganizations as jest.Mock).mockResolvedValue({ data: mockOrgs }); |
| 258 | + (getStreams as jest.Mock).mockResolvedValue({ list: mockStreams }); |
| 259 | + }); |
| 260 | + |
| 261 | + it('renders select organization', async () => { |
| 262 | + await act(async () => { |
| 263 | + wrapper = renderQueryEditor(); |
| 264 | + }); |
| 265 | + expect(await wrapper.getByTestId('query-editor-select-organization-label').textContent).toBe('Select Organization'); |
| 266 | + expect(await wrapper.container.querySelector('[id="query-editor-select-organization-input"]')).toBeVisible(); |
| 267 | + }); |
| 268 | + |
| 269 | + it('renders select stream', async () => { |
| 270 | + await act(async () => { |
| 271 | + wrapper = renderQueryEditor(); |
| 272 | + }); |
| 273 | + expect(await wrapper.getByTestId('query-editor-select-stream-label').textContent).toBe('Select Stream'); |
| 274 | + expect(await wrapper.container.querySelector('[id="query-editor-select-stream-input"]')).toBeVisible(); |
| 275 | + }); |
| 276 | + |
| 277 | + it('fetches and displays organizations and streams on load', async () => { |
| 278 | + const { findByText } = renderQueryEditor(); |
| 279 | + await waitFor(() => expect(getOrganizations).toHaveBeenCalled()); |
| 280 | + await waitFor(() => expect(getStreams).toHaveBeenCalled()); |
| 281 | + expect(await findByText('Select Stream')).toBeInTheDocument(); |
| 282 | + }); |
| 283 | + |
| 284 | + it('triggers onChange when SQL Mode is toggled', async () => { |
| 285 | + await act(async () => { |
| 286 | + wrapper = renderQueryEditor(); |
| 287 | + }); |
| 288 | + expect(wrapper.getByTestId('query-editor-sql-mode-label').textContent).toBe('SQL Mode'); |
| 289 | + fireEvent.click(wrapper.getByTestId('query-editor-sql-mode-switch')); |
| 290 | + expect(mockProps.onChange).toHaveBeenCalled(); |
| 291 | + }); |
| 292 | +}); |
0 commit comments