ShareModalCtrl.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import config from 'app/core/config';
  2. import { LinkSrv } from 'app/features/panel/panellinks/link_srv';
  3. import { ShareModalCtrl } from './ShareModalCtrl';
  4. import { TemplateSrv } from 'app/features/templating/template_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. // @ts-ignore
  43. ctx.ctrl = new ShareModalCtrl(
  44. ctx.scope,
  45. {},
  46. ctx.$location,
  47. {},
  48. ctx.timeSrv,
  49. ctx.templateSrv,
  50. new LinkSrv({} as TemplateSrv, ctx.stimeSrv)
  51. );
  52. });
  53. describe('shareUrl with current time range and panel', () => {
  54. it('should generate share url absolute time', () => {
  55. ctx.scope.panel = { id: 22 };
  56. ctx.scope.init();
  57. expect(ctx.scope.shareUrl).toBe('http://server/#!/test?from=1000&to=2000&orgId=1&panelId=22&fullscreen');
  58. });
  59. it('should generate render url', () => {
  60. ctx.$location.absUrl = () => 'http://dashboards.grafana.com/d/abcdefghi/my-dash';
  61. ctx.scope.panel = { id: 22 };
  62. ctx.scope.init();
  63. const base = 'http://dashboards.grafana.com/render/d-solo/abcdefghi/my-dash';
  64. const params = '?from=1000&to=2000&orgId=1&panelId=22&width=1000&height=500&tz=UTC';
  65. expect(ctx.scope.imageUrl).toContain(base + params);
  66. });
  67. it('should generate render url for scripted dashboard', () => {
  68. ctx.$location.absUrl = () => 'http://dashboards.grafana.com/dashboard/script/my-dash.js';
  69. ctx.scope.panel = { id: 22 };
  70. ctx.scope.init();
  71. const base = 'http://dashboards.grafana.com/render/dashboard-solo/script/my-dash.js';
  72. const params = '?from=1000&to=2000&orgId=1&panelId=22&width=1000&height=500&tz=UTC';
  73. expect(ctx.scope.imageUrl).toContain(base + params);
  74. });
  75. it('should remove panel id when no panel in scope', () => {
  76. ctx.$location.absUrl = () => 'http://server/#!/test';
  77. ctx.scope.options.forCurrent = true;
  78. ctx.scope.panel = null;
  79. ctx.scope.init();
  80. expect(ctx.scope.shareUrl).toBe('http://server/#!/test?from=1000&to=2000&orgId=1');
  81. });
  82. it('should add theme when specified', () => {
  83. ctx.scope.options.theme = 'light';
  84. ctx.scope.panel = null;
  85. ctx.scope.init();
  86. expect(ctx.scope.shareUrl).toBe('http://server/#!/test?from=1000&to=2000&orgId=1&theme=light');
  87. });
  88. it('should remove fullscreen from image url when is first param in querystring and modeSharePanel is true', () => {
  89. ctx.$location.search = () => {
  90. return { fullscreen: true, edit: true };
  91. };
  92. ctx.$location.absUrl = () => 'http://server/#!/test?fullscreen&edit';
  93. ctx.scope.modeSharePanel = true;
  94. ctx.scope.panel = { id: 1 };
  95. ctx.scope.buildUrl();
  96. expect(ctx.scope.shareUrl).toContain('?fullscreen&edit&from=1000&to=2000&orgId=1&panelId=1');
  97. expect(ctx.scope.imageUrl).toContain('?from=1000&to=2000&orgId=1&panelId=1&width=1000&height=500&tz=UTC');
  98. });
  99. it('should remove edit from image url when is first param in querystring and modeSharePanel is true', () => {
  100. ctx.$location.search = () => {
  101. return { edit: true, fullscreen: true };
  102. };
  103. ctx.$location.absUrl = () => 'http://server/#!/test?edit&fullscreen';
  104. ctx.scope.modeSharePanel = true;
  105. ctx.scope.panel = { id: 1 };
  106. ctx.scope.buildUrl();
  107. expect(ctx.scope.shareUrl).toContain('?edit&fullscreen&from=1000&to=2000&orgId=1&panelId=1');
  108. expect(ctx.scope.imageUrl).toContain('?from=1000&to=2000&orgId=1&panelId=1&width=1000&height=500&tz=UTC');
  109. });
  110. it('should include template variables in url', () => {
  111. ctx.$location.search = () => {
  112. return {};
  113. };
  114. ctx.$location.absUrl = () => 'http://server/#!/test';
  115. ctx.scope.options.includeTemplateVars = true;
  116. ctx.templateSrv.fillVariableValuesForUrl = (params: any) => {
  117. params['var-app'] = 'mupp';
  118. params['var-server'] = 'srv-01';
  119. };
  120. ctx.scope.buildUrl();
  121. expect(ctx.scope.shareUrl).toContain(
  122. 'http://server/#!/test?from=1000&to=2000&orgId=1&var-app=mupp&var-server=srv-01'
  123. );
  124. });
  125. });
  126. });