search_srv.jest.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { SearchSrv } from 'app/core/services/search_srv';
  2. import { BackendSrvMock } from 'test/mocks/backend_srv';
  3. describe('SearchSrv', () => {
  4. let searchSrv, backendSrvMock;
  5. beforeEach(() => {
  6. backendSrvMock = new BackendSrvMock();
  7. searchSrv = new SearchSrv(backendSrvMock);
  8. });
  9. describe("with no query string and dashboards with folders returned", () => {
  10. let results;
  11. beforeEach(() => {
  12. backendSrvMock.search = jest.fn().mockReturnValue(Promise.resolve([
  13. {
  14. title: 'folder1',
  15. type: 'dash-folder',
  16. id: 1,
  17. },
  18. {
  19. title: 'dash with no folder',
  20. type: 'dash-db',
  21. id: 2,
  22. },
  23. {
  24. title: 'dash in folder1 1',
  25. type: 'dash-db',
  26. id: 3,
  27. folderId: 1
  28. },
  29. {
  30. title: 'dash in folder1 2',
  31. type: 'dash-db',
  32. id: 4,
  33. folderId: 1
  34. },
  35. ]));
  36. return searchSrv.search({query: ''}).then(res => {
  37. results = res;
  38. });
  39. });
  40. it("should create sections for each folder and root", () => {
  41. expect(results).toHaveLength(2);
  42. });
  43. it('should place folders first', () => {
  44. expect(results[0].title).toBe('folder1');
  45. });
  46. });
  47. describe("with query string and dashboards with folders returned", () => {
  48. let results;
  49. beforeEach(() => {
  50. backendSrvMock.search = jest.fn();
  51. backendSrvMock.search.mockReturnValue(Promise.resolve([
  52. {
  53. id: 2,
  54. title: 'dash with no folder',
  55. type: 'dash-db',
  56. },
  57. {
  58. id: 3,
  59. title: 'dash in folder1 1',
  60. type: 'dash-db',
  61. folderId: 1,
  62. folderTitle: 'folder1',
  63. },
  64. ]));
  65. return searchSrv.search({query: 'search'}).then(res => {
  66. results = res;
  67. });
  68. });
  69. it("should not specify folder ids", () => {
  70. expect(backendSrvMock.search.mock.calls[0][0].folderIds).toHaveLength(0);
  71. });
  72. it('should place all results in a single section', () => {
  73. expect(results).toHaveLength(1);
  74. expect(results[0].hideHeader).toBe(true);
  75. });
  76. });
  77. });