ShareModalCtrl.test.ts 4.6 KB

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