share_modal_ctrl.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import '../shareModalCtrl';
  2. import { ShareModalCtrl } from '../shareModalCtrl';
  3. import config from 'app/core/config';
  4. import { LinkSrv } from 'app/features/panellinks/link_srv';
  5. describe('ShareModalCtrl', () => {
  6. const ctx = {
  7. timeSrv: {
  8. timeRange: () => {
  9. return { from: new Date(1000), to: new Date(2000) };
  10. },
  11. },
  12. $location: {
  13. absUrl: () => 'http://server/#!/test',
  14. search: () => {
  15. return { from: '', to: '' };
  16. },
  17. },
  18. scope: {
  19. dashboard: {
  20. meta: {
  21. isSnapshot: true,
  22. },
  23. },
  24. },
  25. templateSrv: {
  26. fillVariableValuesForUrl: () => {},
  27. },
  28. } as any;
  29. (window as any).Intl.DateTimeFormat = () => {
  30. return {
  31. resolvedOptions: () => {
  32. return { timeZone: 'UTC' };
  33. },
  34. };
  35. };
  36. beforeEach(() => {
  37. config.bootData = {
  38. user: {
  39. orgId: 1,
  40. },
  41. };
  42. ctx.ctrl = new ShareModalCtrl(
  43. ctx.scope,
  44. {},
  45. ctx.$location,
  46. {},
  47. ctx.timeSrv,
  48. ctx.templateSrv,
  49. new LinkSrv({}, ctx.stimeSrv)
  50. );
  51. });
  52. describe('shareUrl with current time range and panel', () => {
  53. it('should generate share url absolute time', () => {
  54. ctx.scope.panel = { id: 22 };
  55. ctx.scope.init();
  56. expect(ctx.scope.shareUrl).toBe('http://server/#!/test?from=1000&to=2000&orgId=1&panelId=22&fullscreen');
  57. });
  58. it('should generate render url', () => {
  59. ctx.$location.absUrl = () => 'http://dashboards.grafana.com/d/abcdefghi/my-dash';
  60. ctx.scope.panel = { id: 22 };
  61. ctx.scope.init();
  62. const base = 'http://dashboards.grafana.com/render/d-solo/abcdefghi/my-dash';
  63. const params = '?from=1000&to=2000&orgId=1&panelId=22&width=1000&height=500&tz=UTC';
  64. expect(ctx.scope.imageUrl).toContain(base + params);
  65. });
  66. it('should generate render url for scripted dashboard', () => {
  67. ctx.$location.absUrl = () => 'http://dashboards.grafana.com/dashboard/script/my-dash.js';
  68. ctx.scope.panel = { id: 22 };
  69. ctx.scope.init();
  70. const base = 'http://dashboards.grafana.com/render/dashboard-solo/script/my-dash.js';
  71. const params = '?from=1000&to=2000&orgId=1&panelId=22&width=1000&height=500&tz=UTC';
  72. expect(ctx.scope.imageUrl).toContain(base + params);
  73. });
  74. it('should remove panel id when no panel in scope', () => {
  75. ctx.$location.absUrl = () => 'http://server/#!/test';
  76. ctx.scope.options.forCurrent = true;
  77. ctx.scope.panel = null;
  78. ctx.scope.init();
  79. expect(ctx.scope.shareUrl).toBe('http://server/#!/test?from=1000&to=2000&orgId=1');
  80. });
  81. it('should add theme when specified', () => {
  82. ctx.scope.options.theme = 'light';
  83. ctx.scope.panel = null;
  84. ctx.scope.init();
  85. expect(ctx.scope.shareUrl).toBe('http://server/#!/test?from=1000&to=2000&orgId=1&theme=light');
  86. });
  87. it('should remove fullscreen from image url when is first param in querystring and modeSharePanel is true', () => {
  88. ctx.$location.search = () => {
  89. return { fullscreen: true, edit: true };
  90. };
  91. ctx.$location.absUrl = () => 'http://server/#!/test?fullscreen&edit';
  92. ctx.scope.modeSharePanel = true;
  93. ctx.scope.panel = { id: 1 };
  94. ctx.scope.buildUrl();
  95. expect(ctx.scope.shareUrl).toContain('?fullscreen&edit&from=1000&to=2000&orgId=1&panelId=1');
  96. expect(ctx.scope.imageUrl).toContain('?from=1000&to=2000&orgId=1&panelId=1&width=1000&height=500&tz=UTC');
  97. });
  98. it('should remove edit from image url when is first param in querystring and modeSharePanel is true', () => {
  99. ctx.$location.search = () => {
  100. return { edit: true, fullscreen: true };
  101. };
  102. ctx.$location.absUrl = () => 'http://server/#!/test?edit&fullscreen';
  103. ctx.scope.modeSharePanel = true;
  104. ctx.scope.panel = { id: 1 };
  105. ctx.scope.buildUrl();
  106. expect(ctx.scope.shareUrl).toContain('?edit&fullscreen&from=1000&to=2000&orgId=1&panelId=1');
  107. expect(ctx.scope.imageUrl).toContain('?from=1000&to=2000&orgId=1&panelId=1&width=1000&height=500&tz=UTC');
  108. });
  109. it('should include template variables in url', () => {
  110. ctx.$location.search = () => {
  111. return {};
  112. };
  113. ctx.$location.absUrl = () => 'http://server/#!/test';
  114. ctx.scope.options.includeTemplateVars = true;
  115. ctx.templateSrv.fillVariableValuesForUrl = function(params) {
  116. params['var-app'] = 'mupp';
  117. params['var-server'] = 'srv-01';
  118. };
  119. ctx.scope.buildUrl();
  120. expect(ctx.scope.shareUrl).toContain(
  121. 'http://server/#!/test?from=1000&to=2000&orgId=1&var-app=mupp&var-server=srv-01'
  122. );
  123. });
  124. });
  125. });