dashboard_import_ctrl.jest.ts 2.4 KB

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