getDerivedFields.test.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { MutableDataFrame } from '@grafana/data';
  2. import { getDerivedFields } from './getDerivedFields';
  3. jest.mock('@grafana/runtime', () => ({
  4. // @ts-ignore
  5. ...jest.requireActual('@grafana/runtime'),
  6. getDataSourceSrv: () => {
  7. return {
  8. getInstanceSettings: () => {
  9. return { name: 'Loki1' };
  10. },
  11. };
  12. },
  13. }));
  14. describe('getDerivedFields', () => {
  15. it('adds links to fields', () => {
  16. const df = new MutableDataFrame({ fields: [{ name: 'line', values: ['nothing', 'trace1=1234', 'trace2=foo'] }] });
  17. const newFields = getDerivedFields(df, [
  18. {
  19. matcherRegex: 'trace1=(\\w+)',
  20. name: 'trace1',
  21. url: 'http://localhost/${__value.raw}',
  22. },
  23. {
  24. matcherRegex: 'trace2=(\\w+)',
  25. name: 'trace2',
  26. url: 'test',
  27. datasourceUid: 'uid',
  28. },
  29. {
  30. matcherRegex: 'trace2=(\\w+)',
  31. name: 'trace2',
  32. url: 'test',
  33. datasourceUid: 'uid2',
  34. urlDisplayLabel: 'Custom Label',
  35. },
  36. ]);
  37. expect(newFields.length).toBe(2);
  38. const trace1 = newFields.find((f) => f.name === 'trace1');
  39. expect(trace1!.values.toArray()).toEqual([null, '1234', null]);
  40. expect(trace1!.config.links![0]).toEqual({
  41. url: 'http://localhost/${__value.raw}',
  42. title: '',
  43. });
  44. const trace2 = newFields.find((f) => f.name === 'trace2');
  45. expect(trace2!.values.toArray()).toEqual([null, null, 'foo']);
  46. expect(trace2!.config.links!.length).toBe(2);
  47. expect(trace2!.config.links![0]).toEqual({
  48. title: '',
  49. internal: { datasourceName: 'Loki1', datasourceUid: 'uid', query: { query: 'test' } },
  50. url: '',
  51. });
  52. expect(trace2!.config.links![1]).toEqual({
  53. title: 'Custom Label',
  54. internal: { datasourceName: 'Loki1', datasourceUid: 'uid2', query: { query: 'test' } },
  55. url: '',
  56. });
  57. });
  58. });