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