search_results.jest.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { SearchResultsCtrl } from '../components/search/search_results';
  2. import { beforeEach, afterEach } from 'test/lib/common';
  3. import appEvents from 'app/core/app_events';
  4. jest.mock('app/core/app_events', () => {
  5. return {
  6. emit: jest.fn<any>(),
  7. };
  8. });
  9. describe('SearchResultsCtrl', () => {
  10. let ctrl;
  11. describe('when checking an item that is not checked', () => {
  12. let item = { checked: false };
  13. let selectionChanged = false;
  14. beforeEach(() => {
  15. ctrl = new SearchResultsCtrl({});
  16. ctrl.onSelectionChanged = () => (selectionChanged = true);
  17. ctrl.toggleSelection(item);
  18. });
  19. it('should set checked to true', () => {
  20. expect(item.checked).toBeTruthy();
  21. });
  22. it('should trigger selection changed callback', () => {
  23. expect(selectionChanged).toBeTruthy();
  24. });
  25. });
  26. describe('when checking an item that is checked', () => {
  27. let item = { checked: true };
  28. let selectionChanged = false;
  29. beforeEach(() => {
  30. ctrl = new SearchResultsCtrl({});
  31. ctrl.onSelectionChanged = () => (selectionChanged = true);
  32. ctrl.toggleSelection(item);
  33. });
  34. it('should set checked to false', () => {
  35. expect(item.checked).toBeFalsy();
  36. });
  37. it('should trigger selection changed callback', () => {
  38. expect(selectionChanged).toBeTruthy();
  39. });
  40. });
  41. describe('when selecting a tag', () => {
  42. let selectedTag = null;
  43. beforeEach(() => {
  44. ctrl = new SearchResultsCtrl({});
  45. ctrl.onTagSelected = tag => (selectedTag = tag);
  46. ctrl.selectTag('tag-test');
  47. });
  48. it('should trigger tag selected callback', () => {
  49. expect(selectedTag['$tag']).toBe('tag-test');
  50. });
  51. });
  52. describe('when toggle a collapsed folder', () => {
  53. let folderExpanded = false;
  54. beforeEach(() => {
  55. ctrl = new SearchResultsCtrl({});
  56. ctrl.onFolderExpanding = () => {
  57. folderExpanded = true;
  58. };
  59. let folder = {
  60. expanded: false,
  61. toggle: () => Promise.resolve(folder),
  62. };
  63. ctrl.toggleFolderExpand(folder);
  64. });
  65. it('should trigger folder expanding callback', () => {
  66. expect(folderExpanded).toBeTruthy();
  67. });
  68. });
  69. describe('when toggle an expanded folder', () => {
  70. let folderExpanded = false;
  71. beforeEach(() => {
  72. ctrl = new SearchResultsCtrl({});
  73. ctrl.onFolderExpanding = () => {
  74. folderExpanded = true;
  75. };
  76. let folder = {
  77. expanded: true,
  78. toggle: () => Promise.resolve(folder),
  79. };
  80. ctrl.toggleFolderExpand(folder);
  81. });
  82. it('should not trigger folder expanding callback', () => {
  83. expect(folderExpanded).toBeFalsy();
  84. });
  85. });
  86. describe('when clicking on a link in search result', () => {
  87. const dashPath = 'dashboard/path';
  88. const $location = { path: () => dashPath };
  89. const appEventsMock = appEvents as any;
  90. describe('with the same url as current path', () => {
  91. beforeEach(() => {
  92. ctrl = new SearchResultsCtrl($location);
  93. const item = { url: dashPath };
  94. ctrl.onItemClick(item);
  95. });
  96. it('should close the search', () => {
  97. expect(appEventsMock.emit.mock.calls.length).toBe(1);
  98. expect(appEventsMock.emit.mock.calls[0][0]).toBe('hide-dash-search');
  99. });
  100. });
  101. describe('with a different url than current path', () => {
  102. beforeEach(() => {
  103. ctrl = new SearchResultsCtrl($location);
  104. const item = { url: 'another/path' };
  105. ctrl.onItemClick(item);
  106. });
  107. it('should do nothing', () => {
  108. expect(appEventsMock.emit.mock.calls.length).toBe(0);
  109. });
  110. });
  111. afterEach(() => {
  112. appEventsMock.emit.mockClear();
  113. });
  114. });
  115. });