dashboard_import_ctrl.jest.ts 2.3 KB

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