datasource_integration.test.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { of } from 'rxjs';
  2. import { setBackendSrv } from '@grafana/runtime';
  3. import { BackendSrv } from 'app/core/services/backend_srv';
  4. import { ContextSrv, User } from '../../../core/services/context_srv';
  5. import { GraphiteDatasource } from './datasource';
  6. interface Context {
  7. ds: GraphiteDatasource;
  8. }
  9. describe('graphiteDatasource integration with backendSrv and fetch', () => {
  10. let ctx = {} as Context;
  11. beforeEach(() => {
  12. jest.clearAllMocks();
  13. const instanceSettings = {
  14. url: '/api/datasources/proxy/1',
  15. name: 'graphiteProd',
  16. jsonData: {
  17. rollupIndicatorEnabled: true,
  18. },
  19. };
  20. const ds = new GraphiteDatasource(instanceSettings);
  21. ctx = { ds };
  22. });
  23. describe('returns a list of functions', () => {
  24. it('should return a list of functions with invalid JSON', async () => {
  25. const INVALID_JSON =
  26. '{"testFunction":{"name":"function","description":"description","module":"graphite.render.functions","group":"Transform","params":[{"name":"param","type":"intOrInf","required":true,"default":Infinity}]}}';
  27. mockBackendSrv(INVALID_JSON);
  28. const funcDefs = await ctx.ds.getFuncDefs();
  29. expect(funcDefs).toEqual({
  30. testFunction: {
  31. category: 'Transform',
  32. defaultParams: ['inf'],
  33. description: 'description',
  34. fake: true,
  35. name: 'function',
  36. params: [
  37. {
  38. multiple: false,
  39. name: 'param',
  40. optional: false,
  41. options: undefined,
  42. type: 'int_or_infinity',
  43. },
  44. ],
  45. },
  46. });
  47. });
  48. it('should return a list of functions with valid JSON', async () => {
  49. const VALID_JSON =
  50. '{"testFunction":{"name":"function","description":"description","module":"graphite.render.functions","group":"Transform","params":[{"name":"param","type":"intOrInf","required":true,"default":1e9999}]}}';
  51. mockBackendSrv(VALID_JSON);
  52. const funcDefs = await ctx.ds.getFuncDefs();
  53. expect(funcDefs).toEqual({
  54. testFunction: {
  55. category: 'Transform',
  56. defaultParams: ['inf'],
  57. description: 'description',
  58. fake: true,
  59. name: 'function',
  60. params: [
  61. {
  62. multiple: false,
  63. name: 'param',
  64. optional: false,
  65. options: undefined,
  66. type: 'int_or_infinity',
  67. },
  68. ],
  69. },
  70. });
  71. });
  72. });
  73. });
  74. function mockBackendSrv(data: string) {
  75. const defaults = {
  76. data: '',
  77. ok: true,
  78. status: 200,
  79. statusText: 'Ok',
  80. isSignedIn: true,
  81. orgId: 1337,
  82. redirected: false,
  83. type: 'basic',
  84. url: 'http://localhost:3000/api/some-mock',
  85. };
  86. const props = { ...defaults };
  87. props.data = data;
  88. const textMock = jest.fn().mockResolvedValue(props.data);
  89. const fromFetchMock = jest.fn().mockImplementation(() => {
  90. const mockedResponse = {
  91. ok: props.ok,
  92. status: props.status,
  93. statusText: props.statusText,
  94. text: textMock,
  95. redirected: false,
  96. type: 'basic',
  97. url: 'http://localhost:3000/api/some-mock',
  98. headers: {
  99. method: 'GET',
  100. url: '/functions',
  101. // to work around Graphite returning invalid JSON
  102. responseType: 'text',
  103. },
  104. };
  105. return of(mockedResponse);
  106. });
  107. const appEventsMock = {} as any;
  108. const user: User = {
  109. isSignedIn: props.isSignedIn,
  110. orgId: props.orgId,
  111. } as any as User;
  112. const contextSrvMock: ContextSrv = {
  113. user,
  114. } as any as ContextSrv;
  115. const logoutMock = jest.fn();
  116. const mockedBackendSrv = new BackendSrv({
  117. fromFetch: fromFetchMock,
  118. appEvents: appEventsMock,
  119. contextSrv: contextSrvMock,
  120. logout: logoutMock,
  121. });
  122. setBackendSrv(mockedBackendSrv);
  123. }