utils.spec.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import EventView from 'sentry/utils/discover/eventView';
  2. import {ALL_VIEWS} from 'sentry/views/discover/data';
  3. import {
  4. handleCreateQuery,
  5. handleDeleteQuery,
  6. handleUpdateQuery,
  7. handleUpdateQueryName,
  8. } from 'sentry/views/discover/savedQuery/utils';
  9. describe('SavedQueries API helpers', () => {
  10. const api = new MockApiClient();
  11. const organization = TestStubs.Organization();
  12. const errorsQuery = ALL_VIEWS.find(view => view.name === 'Errors by Title')!;
  13. const errorsView = EventView.fromSavedQuery(errorsQuery);
  14. errorsView.id = '1'; // set id manually as errorsView.id is undefined
  15. const yAxis = ['count()', 'failure_count()'];
  16. let mockCall;
  17. afterEach(() => {
  18. MockApiClient.clearMockResponses();
  19. });
  20. describe('handleCreateQuery', () => {
  21. beforeEach(() => {
  22. mockCall = MockApiClient.addMockResponse({
  23. method: 'POST',
  24. url: `/organizations/${organization.slug}/discover/saved/`,
  25. body: {data: {}, fromBody: {}},
  26. });
  27. });
  28. it('calls the correct API endpoint', async () => {
  29. const response = await handleCreateQuery(api, organization, errorsView, yAxis);
  30. expect(mockCall).toHaveBeenCalledWith(
  31. expect.anything(),
  32. expect.objectContaining({
  33. data: expect.objectContaining({yAxis}),
  34. })
  35. );
  36. expect(response).toEqual({data: {}, fromBody: {}});
  37. });
  38. });
  39. describe('handleUpdateQuery', () => {
  40. beforeEach(() => {
  41. mockCall = MockApiClient.addMockResponse({
  42. method: 'PUT',
  43. url: `/organizations/${organization.slug}/discover/saved/${errorsView.id}/`,
  44. body: {data: {}, fromBody: {}},
  45. });
  46. });
  47. it('calls the correct API endpoint', async () => {
  48. const response = await handleUpdateQuery(api, organization, errorsView, yAxis);
  49. expect(mockCall).toHaveBeenCalledWith(
  50. expect.anything(),
  51. expect.objectContaining({
  52. data: expect.objectContaining({yAxis}),
  53. })
  54. );
  55. expect(response).toEqual({data: {}, fromBody: {}});
  56. });
  57. });
  58. describe('handleUpdateQueryName', () => {
  59. beforeEach(() => {
  60. MockApiClient.addMockResponse({
  61. method: 'PUT',
  62. url: `/organizations/${organization.slug}/discover/saved/${errorsView.id}/`,
  63. body: {data: {}, fromBody: {}},
  64. });
  65. });
  66. it('calls the correct API endpoint', async () => {
  67. const response = await handleUpdateQueryName(api, organization, errorsView);
  68. expect(response).toEqual({data: {}, fromBody: {}});
  69. });
  70. });
  71. describe('handleDeleteQuery', () => {
  72. beforeEach(() => {
  73. MockApiClient.addMockResponse({
  74. method: 'DELETE',
  75. url: `/organizations/${organization.slug}/discover/saved/${errorsView.id}/`,
  76. body: {data: {}, fromBody: {}},
  77. });
  78. });
  79. it('calls the correct API endpoint', async () => {
  80. const response = await handleDeleteQuery(api, organization, errorsView);
  81. expect(response).toEqual({data: {}, fromBody: {}});
  82. });
  83. });
  84. });