dashboard_import_ctrl.test.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { DashboardImportCtrl } from '../dashboard_import_ctrl';
  2. import config from '../../../core/config';
  3. describe('DashboardImportCtrl', function() {
  4. const ctx: any = {};
  5. let navModelSrv;
  6. let backendSrv;
  7. let validationSrv;
  8. beforeEach(() => {
  9. navModelSrv = {
  10. getNav: () => {},
  11. };
  12. backendSrv = {
  13. search: jest.fn().mockReturnValue(Promise.resolve([])),
  14. getDashboardByUid: jest.fn().mockReturnValue(Promise.resolve([])),
  15. get: jest.fn(),
  16. };
  17. validationSrv = {
  18. validateNewDashboardName: jest.fn().mockReturnValue(Promise.resolve()),
  19. };
  20. ctx.ctrl = new DashboardImportCtrl(backendSrv, validationSrv, navModelSrv, {}, {});
  21. });
  22. describe('when uploading json', function() {
  23. beforeEach(function() {
  24. config.datasources = {
  25. ds: {
  26. type: 'test-db',
  27. },
  28. };
  29. ctx.ctrl.onUpload({
  30. __inputs: [
  31. {
  32. name: 'ds',
  33. pluginId: 'test-db',
  34. type: 'datasource',
  35. pluginName: 'Test DB',
  36. },
  37. ],
  38. });
  39. });
  40. it('should build input model', function() {
  41. expect(ctx.ctrl.inputs.length).toBe(1);
  42. expect(ctx.ctrl.inputs[0].name).toBe('ds');
  43. expect(ctx.ctrl.inputs[0].info).toBe('Select a Test DB data source');
  44. });
  45. it('should set inputValid to false', function() {
  46. expect(ctx.ctrl.inputsValid).toBe(false);
  47. });
  48. });
  49. describe('when specifying grafana.com url', function() {
  50. beforeEach(function() {
  51. ctx.ctrl.gnetUrl = 'http://grafana.com/dashboards/123';
  52. // setup api mock
  53. backendSrv.get = jest.fn(() => {
  54. return Promise.resolve({
  55. json: {},
  56. });
  57. });
  58. return ctx.ctrl.checkGnetDashboard();
  59. });
  60. it('should call gnet api with correct dashboard id', function() {
  61. expect(backendSrv.get.mock.calls[0][0]).toBe('api/gnet/dashboards/123');
  62. });
  63. });
  64. describe('when specifying dashboard id', function() {
  65. beforeEach(function() {
  66. ctx.ctrl.gnetUrl = '2342';
  67. // setup api mock
  68. backendSrv.get = jest.fn(() => {
  69. return Promise.resolve({
  70. json: {},
  71. });
  72. });
  73. return ctx.ctrl.checkGnetDashboard();
  74. });
  75. it('should call gnet api with correct dashboard id', function() {
  76. expect(backendSrv.get.mock.calls[0][0]).toBe('api/gnet/dashboards/2342');
  77. });
  78. });
  79. });