utils.spec.tsx 3.0 KB

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