DashboardLinksDashboard.test.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { describe, expect } from '../../../../../test/lib/common';
  2. import { DashboardSearchHit, DashboardSearchItemType } from '../../../search/types';
  3. import { DashboardLink } from '../../state/DashboardModel';
  4. import { resolveLinks, searchForTags } from './DashboardLinksDashboard';
  5. describe('searchForTags', () => {
  6. const setupTestContext = () => {
  7. const tags = ['A', 'B'];
  8. const link: DashboardLink = {
  9. targetBlank: false,
  10. keepTime: false,
  11. includeVars: false,
  12. asDropdown: false,
  13. icon: 'some icon',
  14. tags,
  15. title: 'some title',
  16. tooltip: 'some tooltip',
  17. type: 'dashboards',
  18. url: '/d/6ieouugGk/DashLinks',
  19. };
  20. const backendSrv: any = {
  21. search: jest.fn((args) => []),
  22. };
  23. return { link, backendSrv };
  24. };
  25. describe('when called', () => {
  26. it('then tags from link should be used in search and limit should be 100', async () => {
  27. const { link, backendSrv } = setupTestContext();
  28. const results = await searchForTags(link.tags, { getBackendSrv: () => backendSrv });
  29. expect(results.length).toEqual(0);
  30. expect(backendSrv.search).toHaveBeenCalledWith({ tag: ['A', 'B'], limit: 100 });
  31. expect(backendSrv.search).toHaveBeenCalledTimes(1);
  32. });
  33. });
  34. });
  35. describe('resolveLinks', () => {
  36. const setupTestContext = (dashboardId: number, searchHitId: number) => {
  37. const link: DashboardLink = {
  38. targetBlank: false,
  39. keepTime: false,
  40. includeVars: false,
  41. asDropdown: false,
  42. icon: 'some icon',
  43. tags: [],
  44. title: 'some title',
  45. tooltip: 'some tooltip',
  46. type: 'dashboards',
  47. url: '/d/6ieouugGk/DashLinks',
  48. };
  49. const searchHits: DashboardSearchHit[] = [
  50. {
  51. id: searchHitId,
  52. title: 'DashLinks',
  53. url: '/d/6ieouugGk/DashLinks',
  54. isStarred: false,
  55. items: [],
  56. tags: [],
  57. uri: 'db/DashLinks',
  58. type: DashboardSearchItemType.DashDB,
  59. },
  60. ];
  61. const linkSrv: any = {
  62. getLinkUrl: jest.fn((args) => args.url),
  63. };
  64. const sanitize = jest.fn((args) => args);
  65. const sanitizeUrl = jest.fn((args) => args);
  66. return { dashboardId, link, searchHits, linkSrv, sanitize, sanitizeUrl };
  67. };
  68. describe('when called', () => {
  69. it('should filter out the calling dashboardId', () => {
  70. const { dashboardId, link, searchHits, linkSrv, sanitize, sanitizeUrl } = setupTestContext(1, 1);
  71. const results = resolveLinks(dashboardId, link, searchHits, { getLinkSrv: () => linkSrv, sanitize, sanitizeUrl });
  72. expect(results.length).toEqual(0);
  73. expect(linkSrv.getLinkUrl).toHaveBeenCalledTimes(0);
  74. expect(sanitize).toHaveBeenCalledTimes(0);
  75. expect(sanitizeUrl).toHaveBeenCalledTimes(0);
  76. });
  77. it('should resolve link url', () => {
  78. const { dashboardId, link, searchHits, linkSrv, sanitize, sanitizeUrl } = setupTestContext(1, 2);
  79. const results = resolveLinks(dashboardId, link, searchHits, { getLinkSrv: () => linkSrv, sanitize, sanitizeUrl });
  80. expect(results.length).toEqual(1);
  81. expect(linkSrv.getLinkUrl).toHaveBeenCalledTimes(1);
  82. expect(linkSrv.getLinkUrl).toHaveBeenCalledWith({ ...link, url: searchHits[0].url });
  83. });
  84. it('should sanitize title', () => {
  85. const { dashboardId, link, searchHits, linkSrv, sanitize, sanitizeUrl } = setupTestContext(1, 2);
  86. const results = resolveLinks(dashboardId, link, searchHits, { getLinkSrv: () => linkSrv, sanitize, sanitizeUrl });
  87. expect(results.length).toEqual(1);
  88. expect(sanitize).toHaveBeenCalledTimes(1);
  89. expect(sanitize).toHaveBeenCalledWith(searchHits[0].title);
  90. });
  91. it('should sanitize url', () => {
  92. const { dashboardId, link, searchHits, linkSrv, sanitize, sanitizeUrl } = setupTestContext(1, 2);
  93. const results = resolveLinks(dashboardId, link, searchHits, { getLinkSrv: () => linkSrv, sanitize, sanitizeUrl });
  94. expect(results.length).toEqual(1);
  95. expect(sanitizeUrl).toHaveBeenCalledTimes(1);
  96. expect(sanitizeUrl).toHaveBeenCalledWith(searchHits[0].url);
  97. });
  98. });
  99. });