|
| 1 | +import { DataQuery, DataSourceInstanceSettings, PluginType, getDefaultTimeRange } from '@grafana/data'; |
| 2 | +import { DataSourceWithBackend } from '@grafana/runtime'; |
| 3 | +import { DatasourceWithAsyncBackend } from './DatasourceWithAsyncBackend'; |
| 4 | + |
| 5 | +const queryMock = jest.fn().mockImplementation(() => Promise.resolve({ data: [] })); |
| 6 | +jest.spyOn(DataSourceWithBackend.prototype, 'query').mockImplementation(queryMock); |
| 7 | + |
| 8 | +const defaultInstanceSettings: DataSourceInstanceSettings<{}> = { |
| 9 | + id: 1, |
| 10 | + uid: 'test', |
| 11 | + type: 'test', |
| 12 | + name: 'test', |
| 13 | + meta: { |
| 14 | + id: 'test', |
| 15 | + name: 'test', |
| 16 | + type: PluginType.datasource, |
| 17 | + info: { |
| 18 | + author: { |
| 19 | + name: 'test', |
| 20 | + }, |
| 21 | + description: 'test', |
| 22 | + links: [], |
| 23 | + logos: { |
| 24 | + large: '', |
| 25 | + small: '', |
| 26 | + }, |
| 27 | + screenshots: [], |
| 28 | + updated: '', |
| 29 | + version: '', |
| 30 | + }, |
| 31 | + module: '', |
| 32 | + baseUrl: '', |
| 33 | + }, |
| 34 | + access: 'direct', |
| 35 | + jsonData: {}, |
| 36 | +}; |
| 37 | +const defaultQuery = { refId: 'refId-1' }; |
| 38 | +const defaultQuery2 = { refId: 'refId-2' }; |
| 39 | +const defaultRequest = { |
| 40 | + requestId: 'requestId', |
| 41 | + interval: '1', |
| 42 | + intervalMs: 1, |
| 43 | + range: getDefaultTimeRange(), |
| 44 | + scopedVars: {}, |
| 45 | + targets: [defaultQuery, defaultQuery2], |
| 46 | + timezone: 'utc', |
| 47 | + app: 'test', |
| 48 | + startTime: 0, |
| 49 | +}; |
| 50 | + |
| 51 | +const setupDatasourceWithAsyncBackend = ({ |
| 52 | + settings = defaultInstanceSettings, |
| 53 | + asyncQueryDataSupport = true, |
| 54 | +}: { |
| 55 | + settings?: DataSourceInstanceSettings<{}>; |
| 56 | + asyncQueryDataSupport?: boolean; |
| 57 | +}) => new DatasourceWithAsyncBackend<DataQuery>(settings, asyncQueryDataSupport); |
| 58 | + |
| 59 | +describe('DatasourceWithAsyncBackend', () => { |
| 60 | + // beforeAll(() => { |
| 61 | + // queryMock.mockClear(); |
| 62 | + // }); |
| 63 | + |
| 64 | + it('can store running queries', () => { |
| 65 | + const ds = setupDatasourceWithAsyncBackend({}); |
| 66 | + |
| 67 | + ds.storeQuery(defaultQuery, { queryID: '123' }); |
| 68 | + expect(ds.getQuery(defaultQuery)).toEqual({ queryID: '123' }); |
| 69 | + }); |
| 70 | + |
| 71 | + it('can remove running queries', () => { |
| 72 | + const ds = setupDatasourceWithAsyncBackend({}); |
| 73 | + |
| 74 | + ds.storeQuery(defaultQuery, { queryID: '123' }); |
| 75 | + expect(ds.getQuery(defaultQuery)).toEqual({ queryID: '123' }); |
| 76 | + ds.removeQuery(defaultQuery); |
| 77 | + expect(ds.getQuery(defaultQuery)).toEqual({}); |
| 78 | + }); |
| 79 | + |
| 80 | + it('can cancel running queries', () => { |
| 81 | + const ds = setupDatasourceWithAsyncBackend({}); |
| 82 | + |
| 83 | + ds.storeQuery(defaultQuery, { queryID: '123' }); |
| 84 | + ds.cancel(defaultQuery); |
| 85 | + expect(ds.getQuery(defaultQuery)).toEqual({ queryID: '123', shouldCancel: true }); |
| 86 | + }); |
| 87 | + |
| 88 | + it('can queue individual queries to run asynchronously if feature toggle asyncQueryDataSupport is `true`', () => { |
| 89 | + const ds = setupDatasourceWithAsyncBackend({ asyncQueryDataSupport: true }); |
| 90 | + |
| 91 | + ds.doSingle = jest.fn().mockReturnValue(Promise.resolve({ data: [] })); |
| 92 | + expect(ds.doSingle).not.toHaveBeenCalled(); |
| 93 | + ds.query(defaultRequest); |
| 94 | + expect(ds.doSingle).toHaveBeenCalledTimes(2); |
| 95 | + expect(ds.doSingle).toHaveBeenCalledWith(defaultQuery, defaultRequest); |
| 96 | + expect(ds.doSingle).toHaveBeenCalledWith(defaultQuery2, defaultRequest); |
| 97 | + }); |
| 98 | + |
| 99 | + it('can run queries synchronously if feature toggle asyncQueryDataSupport is `false`', () => { |
| 100 | + const ds = setupDatasourceWithAsyncBackend({ asyncQueryDataSupport: false }); |
| 101 | + |
| 102 | + ds.doSingle = jest.fn(); |
| 103 | + expect(ds.doSingle).not.toHaveBeenCalled(); |
| 104 | + ds.query(defaultRequest); |
| 105 | + expect(ds.doSingle).not.toHaveBeenCalled(); |
| 106 | + expect(queryMock).toHaveBeenCalledTimes(1); |
| 107 | + expect(queryMock).toHaveBeenCalledWith(defaultRequest); |
| 108 | + }); |
| 109 | +}); |
0 commit comments