LogRowContextProvider.test.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { DataFrameHelper, FieldType, LogRowModel } from '@grafana/data';
  2. import { getRowContexts } from './LogRowContextProvider';
  3. describe('getRowContexts', () => {
  4. describe('when called with a DataFrame and results are returned', () => {
  5. it('then the result should be in correct format', async () => {
  6. const firstResult = new DataFrameHelper({
  7. refId: 'B',
  8. labels: {},
  9. fields: [
  10. { name: 'ts', type: FieldType.time, values: [3, 2, 1] },
  11. { name: 'line', type: FieldType.string, values: ['3', '2', '1'] },
  12. ],
  13. });
  14. const secondResult = new DataFrameHelper({
  15. refId: 'B',
  16. labels: {},
  17. fields: [
  18. { name: 'ts', type: FieldType.time, values: [6, 5, 4] },
  19. { name: 'line', type: FieldType.string, values: ['6', '5', '4'] },
  20. ],
  21. });
  22. const row: LogRowModel = {
  23. entry: '4',
  24. labels: null,
  25. hasAnsi: false,
  26. raw: '4',
  27. logLevel: null,
  28. timeEpochMs: 4,
  29. timeFromNow: '',
  30. timeLocal: '',
  31. timeUtc: '',
  32. timestamp: '4',
  33. };
  34. const getRowContext = jest
  35. .fn()
  36. .mockResolvedValueOnce({ data: [firstResult] })
  37. .mockResolvedValueOnce({ data: [secondResult] });
  38. const result = await getRowContexts(getRowContext, row, 10);
  39. expect(result).toEqual({ data: [[['3', '2', '1']], [['6', '5', '4']]], errors: [null, null] });
  40. });
  41. });
  42. describe('when called with a DataFrame and errors occur', () => {
  43. it('then the result should be in correct format', async () => {
  44. const firstError = new Error('Error 1');
  45. const secondError = new Error('Error 2');
  46. const row: LogRowModel = {
  47. entry: '4',
  48. labels: null,
  49. hasAnsi: false,
  50. raw: '4',
  51. logLevel: null,
  52. timeEpochMs: 4,
  53. timeFromNow: '',
  54. timeLocal: '',
  55. timeUtc: '',
  56. timestamp: '4',
  57. };
  58. const getRowContext = jest
  59. .fn()
  60. .mockRejectedValueOnce(firstError)
  61. .mockRejectedValueOnce(secondError);
  62. const result = await getRowContexts(getRowContext, row, 10);
  63. expect(result).toEqual({ data: [[], []], errors: ['Error 1', 'Error 2'] });
  64. });
  65. });
  66. });