savedSearchesStore.spec.jsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. import {Searches} from 'fixtures/js-stubs/searches';
  2. import {
  3. deleteSavedSearch,
  4. fetchSavedSearches,
  5. pinSearch,
  6. unpinSearch,
  7. } from 'sentry/actionCreators/savedSearches';
  8. import {Client} from 'sentry/api';
  9. import SavedSearchesStore from 'sentry/stores/savedSearchesStore';
  10. describe('SavedSearchesStore', function () {
  11. let api;
  12. beforeAll(async function () {
  13. api = new Client();
  14. await SavedSearchesStore.reset();
  15. });
  16. beforeEach(function () {
  17. Client.addMockResponse({
  18. url: '/organizations/org-1/searches/',
  19. body: Searches(),
  20. });
  21. Client.addMockResponse({
  22. url: '/organizations/org-1/pinned-searches/',
  23. method: 'PUT',
  24. body: {},
  25. });
  26. Client.addMockResponse({
  27. url: '/organizations/org-1/pinned-searches/',
  28. method: 'DELETE',
  29. });
  30. });
  31. afterEach(async function () {
  32. Client.clearMockResponses();
  33. SavedSearchesStore.reset();
  34. await tick();
  35. });
  36. it('get', function () {
  37. expect(SavedSearchesStore.get()).toEqual({
  38. hasError: false,
  39. isLoading: true,
  40. savedSearches: [],
  41. });
  42. });
  43. it('fetching saved searches updates store', async function () {
  44. await fetchSavedSearches(api, 'org-1', {});
  45. await tick();
  46. expect(SavedSearchesStore.get().savedSearches).toHaveLength(2);
  47. expect(SavedSearchesStore.get().isLoading).toBe(false);
  48. });
  49. it('failed fetches do not corrupt store', async function () {
  50. Client.addMockResponse({
  51. url: '/organizations/org-1/searches/',
  52. body: '',
  53. });
  54. await fetchSavedSearches(api, 'org-1', {});
  55. await tick();
  56. expect(SavedSearchesStore.get().savedSearches).toHaveLength(0);
  57. expect(SavedSearchesStore.get().isLoading).toBe(false);
  58. });
  59. it('creates a new pin search', async function () {
  60. await fetchSavedSearches(api, 'org-1', {});
  61. await tick();
  62. Client.addMockResponse({
  63. url: '/organizations/org-1/pinned-searches/',
  64. method: 'PUT',
  65. body: {
  66. id: '123',
  67. query: 'level:info',
  68. sort: 'freq',
  69. isPinned: true,
  70. },
  71. });
  72. pinSearch(api, 'org-1', 0, 'level:info', 'freq');
  73. await tick();
  74. await tick();
  75. expect(SavedSearchesStore.get().savedSearches).toHaveLength(3);
  76. expect(SavedSearchesStore.get().savedSearches[0]).toEqual(
  77. expect.objectContaining({
  78. id: '123',
  79. isPinned: true,
  80. type: 0,
  81. query: 'level:info',
  82. name: 'My Pinned Search',
  83. sort: 'freq',
  84. })
  85. );
  86. });
  87. it('changes pinned search from a custom search to an existing search', async function () {
  88. const searches = Searches();
  89. Client.addMockResponse({
  90. url: '/organizations/org-1/searches/',
  91. body: [
  92. {
  93. id: null,
  94. isPinned: true,
  95. type: 0,
  96. query: 'assigned:me',
  97. sort: 'date',
  98. },
  99. ...searches,
  100. ],
  101. });
  102. Client.addMockResponse({
  103. url: '/organizations/org-1/pinned-searches/',
  104. method: 'PUT',
  105. body: {
  106. id: '1',
  107. isDefault: false,
  108. isGlobal: true,
  109. isOrgCustom: false,
  110. isPinned: true,
  111. query: 'is:unresolved',
  112. sort: 'date',
  113. name: 'Unresolved Issues',
  114. type: 0,
  115. },
  116. });
  117. await fetchSavedSearches(api, 'org-1', {});
  118. await tick();
  119. pinSearch(api, 'org-1', 0, searches[1].query, 'date');
  120. await tick();
  121. await tick();
  122. // Order should remain the same
  123. expect(SavedSearchesStore.get().savedSearches[1]).toEqual(
  124. expect.objectContaining({
  125. id: '1',
  126. isDefault: false,
  127. isGlobal: true,
  128. isOrgCustom: false,
  129. isPinned: true,
  130. type: 0,
  131. name: 'Unresolved Issues',
  132. query: 'is:unresolved',
  133. sort: 'date',
  134. })
  135. );
  136. // Saved custom search should be removed
  137. expect(SavedSearchesStore.get().savedSearches).toHaveLength(2);
  138. });
  139. it('changes pinned search from an existing search to another existing search', async function () {
  140. const searches = Searches();
  141. Client.addMockResponse({
  142. url: '/organizations/org-1/searches/',
  143. body: [{...searches[0], isPinned: true}, searches[1]],
  144. });
  145. Client.addMockResponse({
  146. url: '/organizations/org-1/pinned-searches/',
  147. method: 'PUT',
  148. body: {
  149. id: '1',
  150. isDefault: false,
  151. isGlobal: true,
  152. isOrgCustom: false,
  153. isPinned: true,
  154. query: 'is:unresolved',
  155. sort: 'date',
  156. name: 'Unresolved Issues',
  157. type: 0,
  158. },
  159. });
  160. await fetchSavedSearches(api, 'org-1', {});
  161. await tick();
  162. pinSearch(api, 'org-1', 0, searches[1].query, 'date');
  163. await tick();
  164. await tick();
  165. expect(SavedSearchesStore.get().savedSearches).toHaveLength(2);
  166. expect(SavedSearchesStore.get().savedSearches[0]).toEqual(
  167. expect.objectContaining({
  168. id: '2',
  169. isPinned: false,
  170. type: 0,
  171. name: 'Needs Triage',
  172. query: 'is:unresolved is:unassigned',
  173. sort: 'date',
  174. })
  175. );
  176. expect(SavedSearchesStore.get().savedSearches[1]).toEqual(
  177. expect.objectContaining({
  178. id: '1',
  179. isDefault: false,
  180. isGlobal: true,
  181. isOrgCustom: false,
  182. isPinned: true,
  183. type: 0,
  184. name: 'Unresolved Issues',
  185. query: 'is:unresolved',
  186. sort: 'date',
  187. })
  188. );
  189. });
  190. it('unpins a user custom search (not global, and not org custom)', async function () {
  191. const searches = Searches();
  192. Client.addMockResponse({
  193. url: '/organizations/org-1/searches/',
  194. body: [
  195. {
  196. id: null,
  197. isPinned: true,
  198. type: 0,
  199. query: 'assigned:me',
  200. sort: 'date',
  201. },
  202. ...searches,
  203. ],
  204. });
  205. await fetchSavedSearches(api, 'org-1', {});
  206. await tick();
  207. unpinSearch(api, 'org-1', 0, searches[0]);
  208. await tick();
  209. await tick();
  210. // Saved custom search should be removed
  211. expect(SavedSearchesStore.get().savedSearches).toHaveLength(2);
  212. expect(SavedSearchesStore.get().savedSearches[0]).toEqual(
  213. expect.objectContaining({
  214. id: '2',
  215. isPinned: false,
  216. type: 0,
  217. name: 'Needs Triage',
  218. query: 'is:unresolved is:unassigned',
  219. sort: 'date',
  220. })
  221. );
  222. expect(SavedSearchesStore.get().savedSearches[1]).toEqual(
  223. expect.objectContaining({
  224. id: '1',
  225. isPinned: false,
  226. type: 0,
  227. name: 'Unresolved Issues',
  228. query: 'is:unresolved',
  229. sort: 'date',
  230. })
  231. );
  232. });
  233. it('unpins an existing global saved search', async function () {
  234. const searches = Searches();
  235. Client.addMockResponse({
  236. url: '/organizations/org-1/searches/',
  237. body: [{...searches[0], isPinned: true}, searches[1]],
  238. });
  239. await fetchSavedSearches(api, 'org-1', {});
  240. await tick();
  241. unpinSearch(api, 'org-1', 0, searches[0]);
  242. await tick();
  243. await tick();
  244. expect(SavedSearchesStore.get().savedSearches).toHaveLength(2);
  245. expect(SavedSearchesStore.get().savedSearches[0]).toEqual(
  246. expect.objectContaining({
  247. id: '2',
  248. isPinned: false,
  249. type: 0,
  250. name: 'Needs Triage',
  251. query: 'is:unresolved is:unassigned',
  252. sort: 'date',
  253. })
  254. );
  255. expect(SavedSearchesStore.get().savedSearches[1]).toEqual(
  256. expect.objectContaining({
  257. id: '1',
  258. isPinned: false,
  259. type: 0,
  260. name: 'Unresolved Issues',
  261. query: 'is:unresolved',
  262. sort: 'date',
  263. })
  264. );
  265. });
  266. it('unpins an existing org saved search', async function () {
  267. const searches = Searches();
  268. Client.addMockResponse({
  269. url: '/organizations/org-1/searches/',
  270. body: [
  271. {...searches[0], isOrgCustom: true, isGlobal: false, isPinned: true},
  272. searches[1],
  273. ],
  274. });
  275. await fetchSavedSearches(api, 'org-1', {});
  276. await tick();
  277. unpinSearch(api, 'org-1', 0, searches[0]);
  278. await tick();
  279. await tick();
  280. expect(SavedSearchesStore.get().savedSearches).toHaveLength(2);
  281. expect(SavedSearchesStore.get().savedSearches[0]).toEqual(
  282. expect.objectContaining({
  283. id: '2',
  284. isPinned: false,
  285. type: 0,
  286. name: 'Needs Triage',
  287. query: 'is:unresolved is:unassigned',
  288. sort: 'date',
  289. })
  290. );
  291. expect(SavedSearchesStore.get().savedSearches[1]).toEqual(
  292. expect.objectContaining({
  293. id: '1',
  294. isPinned: false,
  295. type: 0,
  296. name: 'Unresolved Issues',
  297. query: 'is:unresolved',
  298. sort: 'date',
  299. })
  300. );
  301. });
  302. it('removes deleted saved searches', async function () {
  303. await fetchSavedSearches(api, 'org-1', {});
  304. await tick();
  305. const searches = SavedSearchesStore.get().savedSearches;
  306. Client.addMockResponse({
  307. url: `/organizations/org-1/searches/${searches[0].id}/`,
  308. method: 'DELETE',
  309. body: {},
  310. });
  311. await deleteSavedSearch(api, 'org-1', searches[0]);
  312. await tick();
  313. const newSearches = SavedSearchesStore.get().savedSearches;
  314. expect(newSearches.length).toBeLessThan(searches.length);
  315. expect(newSearches[0].id).not.toBe(searches[0].id);
  316. });
  317. });