ShareEmbed.test.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { render, screen } from '@testing-library/react';
  2. import React from 'react';
  3. import config from 'app/core/config';
  4. import { DashboardModel, PanelModel } from '../../state';
  5. import { ShareEmbed } from './ShareEmbed';
  6. jest.mock('app/features/dashboard/services/TimeSrv', () => ({
  7. getTimeSrv: () => ({
  8. timeRange: () => {
  9. return { from: new Date(1000), to: new Date(2000) };
  10. },
  11. }),
  12. }));
  13. jest.mock('app/core/services/context_srv', () => ({
  14. contextSrv: {
  15. sidemenu: true,
  16. user: {},
  17. isSignedIn: false,
  18. isGrafanaAdmin: false,
  19. isEditor: false,
  20. hasEditPermissionFolders: false,
  21. },
  22. }));
  23. function mockLocationHref(href: string) {
  24. const location = window.location;
  25. let search = '';
  26. const searchPos = href.indexOf('?');
  27. if (searchPos >= 0) {
  28. search = href.substring(searchPos);
  29. }
  30. // @ts-ignore
  31. delete window.location;
  32. (window as any).location = {
  33. ...location,
  34. href,
  35. origin: new URL(href).origin,
  36. search,
  37. };
  38. }
  39. describe('ShareEmbed', () => {
  40. let originalBootData: any;
  41. beforeAll(() => {
  42. originalBootData = config.bootData;
  43. config.appUrl = 'http://dashboards.grafana.com/';
  44. config.bootData = {
  45. user: {
  46. orgId: 1,
  47. },
  48. } as any;
  49. });
  50. afterAll(() => {
  51. config.bootData = originalBootData;
  52. });
  53. it('generates the correct embed url for a dashboard', () => {
  54. const mockDashboard = new DashboardModel({
  55. uid: 'mockDashboardUid',
  56. });
  57. const mockPanel = new PanelModel({
  58. id: 'mockPanelId',
  59. });
  60. mockLocationHref(`http://dashboards.grafana.com/d/${mockDashboard.uid}?orgId=1`);
  61. render(<ShareEmbed dashboard={mockDashboard} panel={mockPanel} />);
  62. const embedUrl = screen.getByTestId('share-embed-html');
  63. expect(embedUrl).toBeInTheDocument();
  64. expect(embedUrl).toHaveTextContent(
  65. `http://dashboards.grafana.com/d-solo/${mockDashboard.uid}?orgId=1&from=1000&to=2000&panelId=${mockPanel.id}`
  66. );
  67. });
  68. it('generates the correct embed url for a dashboard set to the homepage in the grafana config', () => {
  69. mockLocationHref('http://dashboards.grafana.com/?orgId=1');
  70. const mockDashboard = new DashboardModel({
  71. uid: 'mockDashboardUid',
  72. });
  73. const mockPanel = new PanelModel({
  74. id: 'mockPanelId',
  75. });
  76. render(<ShareEmbed dashboard={mockDashboard} panel={mockPanel} />);
  77. const embedUrl = screen.getByTestId('share-embed-html');
  78. expect(embedUrl).toBeInTheDocument();
  79. expect(embedUrl).toHaveTextContent(
  80. `http://dashboards.grafana.com/d-solo/${mockDashboard.uid}?orgId=1&from=1000&to=2000&panelId=${mockPanel.id}`
  81. );
  82. });
  83. it('generates the correct embed url for a snapshot', () => {
  84. const mockSlug = 'mockSlug';
  85. mockLocationHref(`http://dashboards.grafana.com/dashboard/snapshot/${mockSlug}?orgId=1`);
  86. const mockDashboard = new DashboardModel({
  87. uid: 'mockDashboardUid',
  88. });
  89. const mockPanel = new PanelModel({
  90. id: 'mockPanelId',
  91. });
  92. render(<ShareEmbed dashboard={mockDashboard} panel={mockPanel} />);
  93. const embedUrl = screen.getByTestId('share-embed-html');
  94. expect(embedUrl).toBeInTheDocument();
  95. expect(embedUrl).toHaveTextContent(
  96. `http://dashboards.grafana.com/dashboard-solo/snapshot/${mockSlug}?orgId=1&from=1000&to=2000&panelId=${mockPanel.id}`
  97. );
  98. });
  99. it('generates the correct embed url for a scripted dashboard', () => {
  100. const mockSlug = 'scripted.js';
  101. mockLocationHref(`http://dashboards.grafana.com/dashboard/script/${mockSlug}?orgId=1`);
  102. const mockDashboard = new DashboardModel({
  103. uid: 'mockDashboardUid',
  104. });
  105. const mockPanel = new PanelModel({
  106. id: 'mockPanelId',
  107. });
  108. render(<ShareEmbed dashboard={mockDashboard} panel={mockPanel} />);
  109. const embedUrl = screen.getByTestId('share-embed-html');
  110. expect(embedUrl).toBeInTheDocument();
  111. expect(embedUrl).toHaveTextContent(
  112. `http://dashboards.grafana.com/dashboard-solo/script/${mockSlug}?orgId=1&from=1000&to=2000&panelId=${mockPanel.id}`
  113. );
  114. });
  115. });