|
1 |
| -import { DataQueryResponse, DataSourceInstanceSettings, toDataFrame } from '@grafana/data'; |
2 |
| -import { of } from 'rxjs'; |
| 1 | +import { DataQueryRequest, DataQueryResponse, DataSourceInstanceSettings, toDataFrame } from '@grafana/data'; |
| 2 | +import { lastValueFrom, of } from 'rxjs'; |
| 3 | +import { GithubVariableSupport } from 'variables'; |
3 | 4 | import { GitHubDataSource } from 'DataSource';
|
4 | 5 | import type { GitHubVariableQuery } from 'types/query';
|
5 | 6 |
|
6 | 7 | describe('DataSource', () => {
|
7 |
| - describe('metricFindQuery', () => { |
| 8 | + describe('GithubVariableSupport', () => { |
| 9 | + const SAMPLE_RESPONSE_WITH_MULTIPLE_FIELDS = [ |
| 10 | + toDataFrame({ |
| 11 | + fields: [ |
| 12 | + { name: 'test', values: ['value1', 'value2'] }, |
| 13 | + { name: 'foo', values: ['foo1', 'foo2'] }, |
| 14 | + { name: 'bar', values: ['bar1', 'bar2'] }, |
| 15 | + ], |
| 16 | + }), |
| 17 | + ]; |
8 | 18 | it('should return empty array if data in response is empty array', async () => {
|
9 | 19 | const ds = new GitHubDataSource({} as DataSourceInstanceSettings);
|
| 20 | + const vs = new GithubVariableSupport(ds); |
10 | 21 | const query = {} as GitHubVariableQuery;
|
11 |
| - |
12 | 22 | jest.spyOn(ds, 'query').mockReturnValue(of({ data: [] }));
|
13 |
| - const res = await ds.metricFindQuery(query, {}); |
14 |
| - expect(res).toEqual([]); |
| 23 | + const res = await lastValueFrom(vs.query({ targets: [query] } as DataQueryRequest)); |
| 24 | + expect(res?.data.map((d) => d.value)).toEqual([]); |
| 25 | + expect(res?.data.map((d) => d.text)).toEqual([]); |
15 | 26 | });
|
16 |
| - |
17 | 27 | it('should return empty array if no data in response', async () => {
|
18 | 28 | const ds = new GitHubDataSource({} as DataSourceInstanceSettings);
|
| 29 | + const vs = new GithubVariableSupport(ds); |
19 | 30 | const query = {} as GitHubVariableQuery;
|
20 |
| - |
21 | 31 | jest.spyOn(ds, 'query').mockReturnValue(of({} as DataQueryResponse));
|
22 |
| - const res = await ds.metricFindQuery(query, {}); |
23 |
| - expect(res).toEqual([]); |
| 32 | + const res = await lastValueFrom(vs.query({ targets: [query] } as DataQueryRequest)); |
| 33 | + expect(res?.data.map((d) => d.value)).toEqual([]); |
| 34 | + expect(res?.data.map((d) => d.text)).toEqual([]); |
24 | 35 | });
|
25 |
| - |
26 | 36 | it('should return array with values if response has data', async () => {
|
27 | 37 | const ds = new GitHubDataSource({} as DataSourceInstanceSettings);
|
| 38 | + const vs = new GithubVariableSupport(ds); |
28 | 39 | const query = { key: 'test', field: 'test' } as GitHubVariableQuery;
|
29 |
| - |
30 |
| - jest.spyOn(ds, 'query').mockReturnValue( |
31 |
| - of({ |
32 |
| - data: [ |
33 |
| - toDataFrame({ |
34 |
| - fields: [{ name: 'test', values: ['value1', 'value2'] }], |
35 |
| - }), |
36 |
| - ], |
37 |
| - } as DataQueryResponse) |
38 |
| - ); |
39 |
| - const res = await ds.metricFindQuery(query, {}); |
40 |
| - expect(res).toEqual([ |
41 |
| - { value: 'value1', text: 'value1' }, |
42 |
| - { value: 'value2', text: 'value2' }, |
43 |
| - ]); |
| 40 | + const data = [toDataFrame({ fields: [{ name: 'test', values: ['value1', 'value2'] }] })]; |
| 41 | + jest.spyOn(ds, 'query').mockReturnValue(of({ data })); |
| 42 | + const res = await lastValueFrom(vs.query({ targets: [query] } as DataQueryRequest)); |
| 43 | + expect(res?.data.map((d) => d.value)).toEqual(['value1', 'value2']); |
| 44 | + expect(res?.data.map((d) => d.text)).toEqual(['value1', 'value2']); |
| 45 | + }); |
| 46 | + it('mapping of key', async () => { |
| 47 | + const ds = new GitHubDataSource({} as DataSourceInstanceSettings); |
| 48 | + const vs = new GithubVariableSupport(ds); |
| 49 | + const query = { key: 'foo' } as GitHubVariableQuery; |
| 50 | + const data = SAMPLE_RESPONSE_WITH_MULTIPLE_FIELDS; |
| 51 | + jest.spyOn(ds, 'query').mockReturnValue(of({ data })); |
| 52 | + const res = await lastValueFrom(vs.query({ targets: [query] } as DataQueryRequest)); |
| 53 | + expect(res?.data.map((d) => d.value)).toEqual(['foo1', 'foo2']); |
| 54 | + expect(res?.data.map((d) => d.text)).toEqual(['foo1', 'foo2']); |
| 55 | + }); |
| 56 | + it('mapping of key and field', async () => { |
| 57 | + const ds = new GitHubDataSource({} as DataSourceInstanceSettings); |
| 58 | + const vs = new GithubVariableSupport(ds); |
| 59 | + const query = { key: 'bar', field: 'foo' } as GitHubVariableQuery; |
| 60 | + const data = SAMPLE_RESPONSE_WITH_MULTIPLE_FIELDS; |
| 61 | + jest.spyOn(ds, 'query').mockReturnValue(of({ data })); |
| 62 | + const res = await lastValueFrom(vs.query({ targets: [query] } as DataQueryRequest)); |
| 63 | + expect(res?.data.map((d) => d.value)).toEqual(['bar1', 'bar2']); |
| 64 | + expect(res?.data.map((d) => d.text)).toEqual(['foo1', 'foo2']); |
| 65 | + }); |
| 66 | + it('mapping of field', async () => { |
| 67 | + const ds = new GitHubDataSource({} as DataSourceInstanceSettings); |
| 68 | + const vs = new GithubVariableSupport(ds); |
| 69 | + const query = { field: 'foo' } as GitHubVariableQuery; |
| 70 | + const data = SAMPLE_RESPONSE_WITH_MULTIPLE_FIELDS; |
| 71 | + jest.spyOn(ds, 'query').mockReturnValue(of({ data })); |
| 72 | + const res = await lastValueFrom(vs.query({ targets: [query] } as DataQueryRequest)); |
| 73 | + expect(res?.data.map((d) => d.value)).toEqual(['foo1', 'foo2']); |
| 74 | + expect(res?.data.map((d) => d.text)).toEqual(['foo1', 'foo2']); |
44 | 75 | });
|
45 | 76 | });
|
46 | 77 | });
|
0 commit comments