datasource.test.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import StackdriverDataSource from '../datasource';
  2. import { metricDescriptors } from './testData';
  3. describe('StackdriverDataSource', () => {
  4. describe('when performing testDataSource', () => {
  5. describe('and call to stackdriver api succeeds', () => {
  6. let ds;
  7. let result;
  8. beforeEach(async () => {
  9. const backendSrv = {
  10. async datasourceRequest() {
  11. return Promise.resolve({ status: 200 });
  12. },
  13. };
  14. ds = new StackdriverDataSource({}, backendSrv);
  15. result = await ds.testDatasource();
  16. });
  17. it('should return successfully', () => {
  18. expect(result.status).toBe('success');
  19. });
  20. });
  21. describe('and a list of metricDescriptors are returned', () => {
  22. let ds;
  23. let result;
  24. beforeEach(async () => {
  25. console.log('erik', metricDescriptors);
  26. const backendSrv = {
  27. datasourceRequest: async () => Promise.resolve({ status: 200, data: metricDescriptors }),
  28. };
  29. ds = new StackdriverDataSource({}, backendSrv);
  30. result = await ds.testDatasource();
  31. });
  32. it('should return status success', () => {
  33. expect(result.status).toBe('success');
  34. });
  35. });
  36. describe('and call to stackdriver api fails with 400 error', () => {
  37. let ds;
  38. let result;
  39. beforeEach(async () => {
  40. const backendSrv = {
  41. datasourceRequest: async () =>
  42. Promise.reject({
  43. statusText: 'Bad Request',
  44. data: { error: { code: 400, message: 'Field interval.endTime had an invalid value' } },
  45. }),
  46. };
  47. ds = new StackdriverDataSource({}, backendSrv);
  48. result = await ds.testDatasource();
  49. });
  50. it('should return error status and a detailed error message', () => {
  51. expect(result.status).toEqual('error');
  52. expect(result.message).toBe('Stackdriver: Bad Request: 400. Field interval.endTime had an invalid value');
  53. });
  54. });
  55. });
  56. });