share_modal_ctrl.jest.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. var ctx = <any>{
  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. };
  29. (<any>window).Intl.DateTimeFormat = () => {
  30. return {
  31. resolvedOptions: () => {
  32. return { timeZone: 'UTC' };
  33. },
  34. };
  35. };
  36. // function setTime(range) {
  37. // ctx.timeSrv.timeRange = () => range;
  38. // }
  39. beforeEach(() => {
  40. config.bootData = {
  41. user: {
  42. orgId: 1,
  43. },
  44. };
  45. });
  46. // setTime({ from: new Date(1000), to: new Date(2000) });
  47. // beforeEach(angularMocks.module('grafana.controllers'));
  48. // beforeEach(angularMocks.module('grafana.services'));
  49. // beforeEach(
  50. // angularMocks.module(function($compileProvider) {
  51. // $compileProvider.preAssignBindingsEnabled(true);
  52. // })
  53. // );
  54. beforeEach(() => {
  55. ctx.ctrl = new ShareModalCtrl(
  56. ctx.scope,
  57. {},
  58. ctx.$location,
  59. {},
  60. ctx.timeSrv,
  61. ctx.templateSrv,
  62. new LinkSrv({}, ctx.stimeSrv)
  63. );
  64. });
  65. describe('shareUrl with current time range and panel', () => {
  66. it('should generate share url absolute time', () => {
  67. // ctx.$location.path('/test');
  68. ctx.scope.panel = { id: 22 };
  69. ctx.scope.init();
  70. expect(ctx.scope.shareUrl).toBe('http://server/#!/test?from=1000&to=2000&orgId=1&panelId=22&fullscreen');
  71. });
  72. it('should generate render url', () => {
  73. ctx.$location.absUrl = () => 'http://dashboards.grafana.com/d/abcdefghi/my-dash';
  74. ctx.scope.panel = { id: 22 };
  75. ctx.scope.init();
  76. var base = 'http://dashboards.grafana.com/render/d-solo/abcdefghi/my-dash';
  77. var params = '?from=1000&to=2000&orgId=1&panelId=22&width=1000&height=500&tz=UTC';
  78. expect(ctx.scope.imageUrl).toContain(base + params);
  79. });
  80. it('should generate render url for scripted dashboard', () => {
  81. ctx.$location.absUrl = () => 'http://dashboards.grafana.com/dashboard/script/my-dash.js';
  82. ctx.scope.panel = { id: 22 };
  83. ctx.scope.init();
  84. var base = 'http://dashboards.grafana.com/render/dashboard-solo/script/my-dash.js';
  85. var params = '?from=1000&to=2000&orgId=1&panelId=22&width=1000&height=500&tz=UTC';
  86. expect(ctx.scope.imageUrl).toContain(base + params);
  87. });
  88. it('should remove panel id when no panel in scope', () => {
  89. // ctx.$location.path('/test');
  90. ctx.$location.absUrl = () => 'http://server/#!/test';
  91. ctx.scope.options.forCurrent = true;
  92. ctx.scope.panel = null;
  93. ctx.scope.init();
  94. expect(ctx.scope.shareUrl).toBe('http://server/#!/test?from=1000&to=2000&orgId=1');
  95. });
  96. it('should add theme when specified', () => {
  97. // ctx.$location.path('/test');
  98. ctx.scope.options.theme = 'light';
  99. ctx.scope.panel = null;
  100. ctx.scope.init();
  101. expect(ctx.scope.shareUrl).toBe('http://server/#!/test?from=1000&to=2000&orgId=1&theme=light');
  102. });
  103. it('should remove fullscreen from image url when is first param in querystring and modeSharePanel is true', () => {
  104. ctx.$location.search = () => {
  105. return { fullscreen: true, edit: true };
  106. };
  107. ctx.$location.absUrl = () => 'http://server/#!/test?fullscreen&edit';
  108. ctx.scope.modeSharePanel = true;
  109. ctx.scope.panel = { id: 1 };
  110. ctx.scope.buildUrl();
  111. expect(ctx.scope.shareUrl).toContain('?fullscreen&edit&from=1000&to=2000&orgId=1&panelId=1');
  112. expect(ctx.scope.imageUrl).toContain('?from=1000&to=2000&orgId=1&panelId=1&width=1000&height=500&tz=UTC');
  113. });
  114. it('should remove edit from image url when is first param in querystring and modeSharePanel is true', () => {
  115. ctx.$location.search = () => {
  116. return { edit: true, fullscreen: true };
  117. };
  118. ctx.$location.absUrl = () => 'http://server/#!/test?edit&fullscreen';
  119. ctx.scope.modeSharePanel = true;
  120. ctx.scope.panel = { id: 1 };
  121. ctx.scope.buildUrl();
  122. expect(ctx.scope.shareUrl).toContain('?edit&fullscreen&from=1000&to=2000&orgId=1&panelId=1');
  123. expect(ctx.scope.imageUrl).toContain('?from=1000&to=2000&orgId=1&panelId=1&width=1000&height=500&tz=UTC');
  124. });
  125. it('should include template variables in url', () => {
  126. ctx.$location.search = () => {
  127. return {};
  128. };
  129. ctx.$location.absUrl = () => 'http://server/#!/test';
  130. ctx.scope.options.includeTemplateVars = true;
  131. ctx.templateSrv.fillVariableValuesForUrl = function(params) {
  132. params['var-app'] = 'mupp';
  133. params['var-server'] = 'srv-01';
  134. };
  135. ctx.scope.buildUrl();
  136. expect(ctx.scope.shareUrl).toContain(
  137. 'http://server/#!/test?from=1000&to=2000&orgId=1&var-app=mupp&var-server=srv-01'
  138. );
  139. });
  140. });
  141. });